1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.calendar.action;
21  
22  import com.liferay.portal.kernel.portlet.ConfigurationAction;
23  import com.liferay.portal.kernel.servlet.SessionErrors;
24  import com.liferay.portal.kernel.servlet.SessionMessages;
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portlet.PortletPreferencesFactoryUtil;
29  
30  import javax.portlet.ActionRequest;
31  import javax.portlet.ActionResponse;
32  import javax.portlet.PortletConfig;
33  import javax.portlet.PortletPreferences;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  /**
38   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Arcko Yongming Duan
42   *
43   */
44  public class ConfigurationActionImpl implements ConfigurationAction {
45  
46      public void processAction(
47              PortletConfig portletConfig, ActionRequest actionRequest,
48              ActionResponse actionResponse)
49          throws Exception {
50  
51          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
52  
53          if (!cmd.equals(Constants.UPDATE)) {
54              return;
55          }
56  
57          String portletResource = ParamUtil.getString(
58              actionRequest, "portletResource");
59  
60          PortletPreferences prefs =
61              PortletPreferencesFactoryUtil.getPortletSetup(
62                  actionRequest, portletResource);
63  
64          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
65  
66          if (tabs2.equals("display-settings")) {
67              updateDisplaySettings(actionRequest, prefs);
68          }
69          else if (tabs2.equals("email-from")) {
70              updateEmailFrom(actionRequest, prefs);
71          }
72          else if (tabs2.equals("event-reminder-email")) {
73              updateEmailEventReminder(actionRequest, prefs);
74          }
75  
76          if (SessionErrors.isEmpty(actionRequest)) {
77              prefs.store();
78  
79              SessionMessages.add(
80                  actionRequest, portletConfig.getPortletName() + ".doConfigure");
81          }
82      }
83  
84      public String render(
85              PortletConfig portletConfig, RenderRequest renderRequest,
86              RenderResponse renderResponse)
87          throws Exception {
88  
89          return "/html/portlet/calendar/configuration.jsp";
90      }
91  
92      protected void updateDisplaySettings(
93              ActionRequest actionRequest, PortletPreferences prefs)
94          throws Exception {
95  
96          String tabs1Default = ParamUtil.getString(
97              actionRequest, "tabs1Default");
98          String summaryTabOrientation = ParamUtil.getString(
99              actionRequest, "summaryTabOrientation");
100         String summaryTabShowMiniMonth = ParamUtil.getString(
101             actionRequest, "summaryTabShowMiniMonth");
102         String summaryTabShowTodaysEvents = ParamUtil.getString(
103             actionRequest, "summaryTabShowTodaysEvents");
104 
105         prefs.setValue("tabs1-default", tabs1Default);
106         prefs.setValue("summary-tab-orientation", summaryTabOrientation);
107         prefs.setValue("summary-tab-show-mini-month", summaryTabShowMiniMonth);
108         prefs.setValue(
109             "summary-tab-show-todays-events", summaryTabShowTodaysEvents);
110     }
111 
112     protected void updateEmailFrom(
113             ActionRequest actionRequest, PortletPreferences prefs)
114         throws Exception {
115 
116         String emailFromName = ParamUtil.getString(
117             actionRequest, "emailFromName");
118         String emailFromAddress = ParamUtil.getString(
119             actionRequest, "emailFromAddress");
120 
121         if (Validator.isNull(emailFromName)) {
122             SessionErrors.add(actionRequest, "emailFromName");
123         }
124         else if (!Validator.isEmailAddress(emailFromAddress)) {
125             SessionErrors.add(actionRequest, "emailFromAddress");
126         }
127         else {
128             prefs.setValue("email-from-name", emailFromName);
129             prefs.setValue("email-from-address", emailFromAddress);
130         }
131     }
132 
133     protected void updateEmailEventReminder(
134             ActionRequest actionRequest, PortletPreferences prefs)
135         throws Exception {
136 
137         boolean emailEventReminderEnabled = ParamUtil.getBoolean(
138             actionRequest, "emailEventReminderEnabled");
139         String emailEventReminderSubject = ParamUtil.getString(
140             actionRequest, "emailEventReminderSubject");
141         String emailEventReminderBody = ParamUtil.getString(
142             actionRequest, "emailEventReminderBody");
143 
144         if (Validator.isNull(emailEventReminderSubject)) {
145             SessionErrors.add(actionRequest, "emailEventReminderSubject");
146         }
147         else if (Validator.isNull(emailEventReminderBody)) {
148             SessionErrors.add(actionRequest, "emailEventReminderBody");
149         }
150         else {
151             prefs.setValue(
152                 "email-event-reminder-enabled",
153                 String.valueOf(emailEventReminderEnabled));
154             prefs.setValue(
155                 "email-event-reminder-subject", emailEventReminderSubject);
156             prefs.setValue("email-event-reminder-body", emailEventReminderBody);
157         }
158     }
159 
160 }