1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.action;
16  
17  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.servlet.SessionMessages;
20  import com.liferay.portal.kernel.util.Constants;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portlet.PortletPreferencesFactoryUtil;
24  
25  import javax.portlet.ActionRequest;
26  import javax.portlet.ActionResponse;
27  import javax.portlet.PortletConfig;
28  import javax.portlet.PortletPreferences;
29  import javax.portlet.RenderRequest;
30  import javax.portlet.RenderResponse;
31  
32  /**
33   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class ConfigurationActionImpl extends BaseConfigurationAction {
38  
39      public void processAction(
40              PortletConfig portletConfig, ActionRequest actionRequest,
41              ActionResponse actionResponse)
42          throws Exception {
43  
44          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
45  
46          if (!cmd.equals(Constants.UPDATE)) {
47              return;
48          }
49  
50          String portletResource = ParamUtil.getString(
51              actionRequest, "portletResource");
52  
53          PortletPreferences preferences =
54              PortletPreferencesFactoryUtil.getPortletSetup(
55                  actionRequest, portletResource);
56  
57          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
58  
59          if (tabs2.equals("email-from")) {
60              updateEmailFrom(actionRequest, preferences);
61          }
62          else if (tabs2.equals("web-content-added-email")) {
63              updateEmailArticleAdded(actionRequest, preferences);
64          }
65          else if (tabs2.equals("web-content-approval-denied-email")) {
66              updateEmailArticleApprovalDenied(actionRequest, preferences);
67          }
68          else if (tabs2.equals("web-content-approval-granted-email")) {
69              updateEmailArticleApprovalGranted(actionRequest, preferences);
70          }
71          else if (tabs2.equals("web-content-approval-requested-email")) {
72              updateEmailArticleApprovalRequested(actionRequest, preferences);
73          }
74          else if (tabs2.equals("web-content-review-email")) {
75              updateEmailArticleReview(actionRequest, preferences);
76          }
77          else if (tabs2.equals("web-content-updated-email")) {
78              updateEmailArticleUpdated(actionRequest, preferences);
79          }
80  
81          if (SessionErrors.isEmpty(actionRequest)) {
82              preferences.store();
83  
84              SessionMessages.add(
85                  actionRequest, portletConfig.getPortletName() + ".doConfigure");
86          }
87      }
88  
89      public String render(
90              PortletConfig portletConfig, RenderRequest renderRequest,
91              RenderResponse renderResponse)
92          throws Exception {
93  
94          return "/html/portlet/journal/configuration.jsp";
95      }
96  
97      protected void updateEmailArticleAdded(
98              ActionRequest actionRequest, PortletPreferences preferences)
99          throws Exception {
100 
101         boolean emailArticleAddedEnabled = ParamUtil.getBoolean(
102             actionRequest, "emailArticleAddedEnabled");
103         String emailArticleAddedSubject = ParamUtil.getString(
104             actionRequest, "emailArticleAddedSubject");
105         String emailArticleAddedBody = ParamUtil.getString(
106             actionRequest, "emailArticleAddedBody");
107 
108         if (Validator.isNull(emailArticleAddedSubject)) {
109             SessionErrors.add(actionRequest, "emailArticleAddedSubject");
110         }
111         else if (Validator.isNull(emailArticleAddedBody)) {
112             SessionErrors.add(actionRequest, "emailArticleAddedBody");
113         }
114         else {
115             preferences.setValue(
116                 "email-article-added-enabled",
117                 String.valueOf(emailArticleAddedEnabled));
118             preferences.setValue(
119                 "email-article-added-subject", emailArticleAddedSubject);
120             preferences.setValue(
121                 "email-article-added-body", emailArticleAddedBody);
122         }
123     }
124 
125     protected void updateEmailArticleApprovalDenied(
126             ActionRequest actionRequest, PortletPreferences preferences)
127         throws Exception {
128 
129         boolean emailArticleApprovalDeniedEnabled = ParamUtil.getBoolean(
130             actionRequest, "emailArticleApprovalDeniedEnabled");
131         String emailArticleApprovalDeniedSubject = ParamUtil.getString(
132             actionRequest, "emailArticleApprovalDeniedSubject");
133         String emailArticleApprovalDeniedBody = ParamUtil.getString(
134             actionRequest, "emailArticleApprovalDeniedBody");
135 
136         if (Validator.isNull(emailArticleApprovalDeniedSubject)) {
137             SessionErrors.add(
138                 actionRequest, "emailArticleApprovalDeniedSubject");
139         }
140         else if (Validator.isNull(emailArticleApprovalDeniedBody)) {
141             SessionErrors.add(actionRequest, "emailArticleApprovalDeniedBody");
142         }
143         else {
144             preferences.setValue(
145                 "email-article-approval-denied-enabled",
146                 String.valueOf(emailArticleApprovalDeniedEnabled));
147             preferences.setValue(
148                 "email-article-approval-denied-subject",
149                 emailArticleApprovalDeniedSubject);
150             preferences.setValue(
151                 "email-article-approval-denied-body",
152                 emailArticleApprovalDeniedBody);
153         }
154     }
155 
156     protected void updateEmailArticleApprovalGranted(
157             ActionRequest actionRequest, PortletPreferences preferences)
158         throws Exception {
159 
160         boolean emailArticleApprovalGrantedEnabled = ParamUtil.getBoolean(
161             actionRequest, "emailArticleApprovalGrantedEnabled");
162         String emailArticleApprovalGrantedSubject = ParamUtil.getString(
163             actionRequest, "emailArticleApprovalGrantedSubject");
164         String emailArticleApprovalGrantedBody = ParamUtil.getString(
165             actionRequest, "emailArticleApprovalGrantedBody");
166 
167         if (Validator.isNull(emailArticleApprovalGrantedSubject)) {
168             SessionErrors.add(
169                 actionRequest, "emailArticleApprovalGrantedSubject");
170         }
171         else if (Validator.isNull(emailArticleApprovalGrantedBody)) {
172             SessionErrors.add(actionRequest, "emailArticleApprovalGrantedBody");
173         }
174         else {
175             preferences.setValue(
176                 "email-article-approval-granted-enabled",
177                 String.valueOf(emailArticleApprovalGrantedEnabled));
178             preferences.setValue(
179                 "email-article-approval-granted-subject",
180                 emailArticleApprovalGrantedSubject);
181             preferences.setValue(
182                 "email-article-approval-granted-body",
183                 emailArticleApprovalGrantedBody);
184         }
185     }
186 
187     protected void updateEmailArticleApprovalRequested(
188             ActionRequest actionRequest, PortletPreferences preferences)
189         throws Exception {
190 
191         boolean emailArticleApprovalRequestedEnabled = ParamUtil.getBoolean(
192             actionRequest, "emailArticleApprovalRequestedEnabled");
193         String emailArticleApprovalRequestedSubject = ParamUtil.getString(
194             actionRequest, "emailArticleApprovalRequestedSubject");
195         String emailArticleApprovalRequestedBody = ParamUtil.getString(
196             actionRequest, "emailArticleApprovalRequestedBody");
197 
198         if (Validator.isNull(emailArticleApprovalRequestedSubject)) {
199             SessionErrors.add(
200                 actionRequest, "emailArticleApprovalRequestedSubject");
201         }
202         else if (Validator.isNull(emailArticleApprovalRequestedBody)) {
203             SessionErrors.add(
204                 actionRequest, "emailArticleApprovalRequestedBody");
205         }
206         else {
207             preferences.setValue(
208                 "email-article-approval-requested-enabled",
209                 String.valueOf(emailArticleApprovalRequestedEnabled));
210             preferences.setValue(
211                 "email-article-approval-requested-subject",
212                 emailArticleApprovalRequestedSubject);
213             preferences.setValue(
214                 "email-article-approval-requested-body",
215                 emailArticleApprovalRequestedBody);
216         }
217     }
218 
219     protected void updateEmailArticleReview(
220             ActionRequest actionRequest, PortletPreferences preferences)
221         throws Exception {
222 
223         boolean emailArticleReviewEnabled = ParamUtil.getBoolean(
224             actionRequest, "emailArticleReviewEnabled");
225         String emailArticleReviewSubject = ParamUtil.getString(
226             actionRequest, "emailArticleReviewSubject");
227         String emailArticleReviewBody = ParamUtil.getString(
228             actionRequest, "emailArticleReviewBody");
229 
230         if (Validator.isNull(emailArticleReviewSubject)) {
231             SessionErrors.add(actionRequest, "emailArticleReviewSubject");
232         }
233         else if (Validator.isNull(emailArticleReviewBody)) {
234             SessionErrors.add(actionRequest, "emailArticleReviewBody");
235         }
236         else {
237             preferences.setValue(
238                 "email-article-review-enabled",
239                 String.valueOf(emailArticleReviewEnabled));
240             preferences.setValue(
241                 "email-article-review-subject", emailArticleReviewSubject);
242             preferences.setValue(
243                 "email-article-review-body", emailArticleReviewBody);
244         }
245     }
246 
247     protected void updateEmailArticleUpdated(
248             ActionRequest actionRequest, PortletPreferences preferences)
249         throws Exception {
250 
251         boolean emailArticleUpdatedEnabled = ParamUtil.getBoolean(
252             actionRequest, "emailArticleUpdatedEnabled");
253         String emailArticleUpdatedSubject = ParamUtil.getString(
254             actionRequest, "emailArticleUpdatedSubject");
255         String emailArticleUpdatedBody = ParamUtil.getString(
256             actionRequest, "emailArticleUpdatedBody");
257 
258         if (Validator.isNull(emailArticleUpdatedSubject)) {
259             SessionErrors.add(actionRequest, "emailArticleUpdatedSubject");
260         }
261         else if (Validator.isNull(emailArticleUpdatedBody)) {
262             SessionErrors.add(actionRequest, "emailArticleUpdatedBody");
263         }
264         else {
265             preferences.setValue(
266                 "email-article-updated-enabled",
267                 String.valueOf(emailArticleUpdatedEnabled));
268             preferences.setValue(
269                 "email-article-updated-subject", emailArticleUpdatedSubject);
270             preferences.setValue(
271                 "email-article-updated-body", emailArticleUpdatedBody);
272         }
273     }
274 
275     protected void updateEmailFrom(
276             ActionRequest actionRequest, PortletPreferences preferences)
277         throws Exception {
278 
279         String emailFromName = ParamUtil.getString(
280             actionRequest, "emailFromName");
281         String emailFromAddress = ParamUtil.getString(
282             actionRequest, "emailFromAddress");
283 
284         if (Validator.isNull(emailFromName)) {
285             SessionErrors.add(actionRequest, "emailFromName");
286         }
287         else if (!Validator.isEmailAddress(emailFromAddress)) {
288             SessionErrors.add(actionRequest, "emailFromAddress");
289         }
290         else {
291             preferences.setValue("email-from-name", emailFromName);
292             preferences.setValue("email-from-address", emailFromAddress);
293         }
294     }
295 
296 }