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.BaseConfigurationAction;
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 extends BaseConfigurationAction {
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 preferences =
61              PortletPreferencesFactoryUtil.getPortletSetup(
62                  actionRequest, portletResource);
63  
64          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
65  
66          if (tabs2.equals("display-settings")) {
67              updateDisplaySettings(actionRequest, preferences);
68          }
69          else if (tabs2.equals("email-from")) {
70              updateEmailFrom(actionRequest, preferences);
71          }
72          else if (tabs2.equals("event-reminder-email")) {
73              updateEmailEventReminder(actionRequest, preferences);
74          }
75  
76          if (SessionErrors.isEmpty(actionRequest)) {
77              preferences.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 preferences)
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         preferences.setValue("tabs1-default", tabs1Default);
106         preferences.setValue("summary-tab-orientation", summaryTabOrientation);
107         preferences.setValue(
108             "summary-tab-show-mini-month", summaryTabShowMiniMonth);
109         preferences.setValue(
110             "summary-tab-show-todays-events", summaryTabShowTodaysEvents);
111     }
112 
113     protected void updateEmailFrom(
114             ActionRequest actionRequest, PortletPreferences preferences)
115         throws Exception {
116 
117         String emailFromName = ParamUtil.getString(
118             actionRequest, "emailFromName");
119         String emailFromAddress = ParamUtil.getString(
120             actionRequest, "emailFromAddress");
121 
122         if (Validator.isNull(emailFromName)) {
123             SessionErrors.add(actionRequest, "emailFromName");
124         }
125         else if (!Validator.isEmailAddress(emailFromAddress)) {
126             SessionErrors.add(actionRequest, "emailFromAddress");
127         }
128         else {
129             preferences.setValue("email-from-name", emailFromName);
130             preferences.setValue("email-from-address", emailFromAddress);
131         }
132     }
133 
134     protected void updateEmailEventReminder(
135             ActionRequest actionRequest, PortletPreferences preferences)
136         throws Exception {
137 
138         boolean emailEventReminderEnabled = ParamUtil.getBoolean(
139             actionRequest, "emailEventReminderEnabled");
140         String emailEventReminderSubject = ParamUtil.getString(
141             actionRequest, "emailEventReminderSubject");
142         String emailEventReminderBody = ParamUtil.getString(
143             actionRequest, "emailEventReminderBody");
144 
145         if (Validator.isNull(emailEventReminderSubject)) {
146             SessionErrors.add(actionRequest, "emailEventReminderSubject");
147         }
148         else if (Validator.isNull(emailEventReminderBody)) {
149             SessionErrors.add(actionRequest, "emailEventReminderBody");
150         }
151         else {
152             preferences.setValue(
153                 "email-event-reminder-enabled",
154                 String.valueOf(emailEventReminderEnabled));
155             preferences.setValue(
156                 "email-event-reminder-subject", emailEventReminderSubject);
157             preferences.setValue(
158                 "email-event-reminder-body", emailEventReminderBody);
159         }
160     }
161 
162 }