1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.action;
24  
25  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.servlet.SessionMessages;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portlet.PortletPreferencesFactoryUtil;
32  
33  import javax.portlet.ActionRequest;
34  import javax.portlet.ActionResponse;
35  import javax.portlet.PortletConfig;
36  import javax.portlet.PortletPreferences;
37  import javax.portlet.RenderRequest;
38  import javax.portlet.RenderResponse;
39  
40  /**
41   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class ConfigurationActionImpl extends BaseConfigurationAction {
47  
48      public void processAction(
49              PortletConfig portletConfig, ActionRequest actionRequest,
50              ActionResponse actionResponse)
51          throws Exception {
52  
53          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
54  
55          if (!cmd.equals(Constants.UPDATE)) {
56              return;
57          }
58  
59          String portletResource = ParamUtil.getString(
60              actionRequest, "portletResource");
61  
62          PortletPreferences prefs =
63              PortletPreferencesFactoryUtil.getPortletSetup(
64                  actionRequest, portletResource);
65  
66          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
67  
68          if (tabs2.equals("email-from")) {
69              updateEmailFrom(actionRequest, prefs);
70          }
71          else if (tabs2.equals("article-approval-denied-email")) {
72              updateEmailArticleApprovalDenied(actionRequest, prefs);
73          }
74          else if (tabs2.equals("article-approval-granted-email")) {
75              updateEmailArticleApprovalGranted(actionRequest, prefs);
76          }
77          else if (tabs2.equals("article-approval-requested-email")) {
78              updateEmailArticleApprovalRequested(actionRequest, prefs);
79          }
80          else if (tabs2.equals("article-review-email")) {
81              updateEmailArticleReview(actionRequest, prefs);
82          }
83  
84          if (SessionErrors.isEmpty(actionRequest)) {
85              prefs.store();
86  
87              SessionMessages.add(
88                  actionRequest, portletConfig.getPortletName() + ".doConfigure");
89          }
90      }
91  
92      public String render(
93              PortletConfig portletConfig, RenderRequest renderRequest,
94              RenderResponse renderResponse)
95          throws Exception {
96  
97          return "/html/portlet/journal/configuration.jsp";
98      }
99  
100     protected void updateEmailFrom(
101             ActionRequest actionRequest, PortletPreferences prefs)
102         throws Exception {
103 
104         String emailFromName = ParamUtil.getString(
105             actionRequest, "emailFromName");
106         String emailFromAddress = ParamUtil.getString(
107             actionRequest, "emailFromAddress");
108 
109         if (Validator.isNull(emailFromName)) {
110             SessionErrors.add(actionRequest, "emailFromName");
111         }
112         else if (!Validator.isEmailAddress(emailFromAddress)) {
113             SessionErrors.add(actionRequest, "emailFromAddress");
114         }
115         else {
116             prefs.setValue("email-from-name", emailFromName);
117             prefs.setValue("email-from-address", emailFromAddress);
118         }
119     }
120 
121     protected void updateEmailArticleApprovalDenied(
122             ActionRequest actionRequest, PortletPreferences prefs)
123         throws Exception {
124 
125         boolean emailArticleApprovalDeniedEnabled = ParamUtil.getBoolean(
126             actionRequest, "emailArticleApprovalDeniedEnabled");
127         String emailArticleApprovalDeniedSubject = ParamUtil.getString(
128             actionRequest, "emailArticleApprovalDeniedSubject");
129         String emailArticleApprovalDeniedBody = ParamUtil.getString(
130             actionRequest, "emailArticleApprovalDeniedBody");
131 
132         if (Validator.isNull(emailArticleApprovalDeniedSubject)) {
133             SessionErrors.add(
134                 actionRequest, "emailArticleApprovalDeniedSubject");
135         }
136         else if (Validator.isNull(emailArticleApprovalDeniedBody)) {
137             SessionErrors.add(actionRequest, "emailArticleApprovalDeniedBody");
138         }
139         else {
140             prefs.setValue(
141                 "email-article-approval-denied-enabled",
142                 String.valueOf(emailArticleApprovalDeniedEnabled));
143             prefs.setValue(
144                 "email-article-approval-denied-subject",
145                 emailArticleApprovalDeniedSubject);
146             prefs.setValue(
147                 "email-article-approval-denied-body",
148                 emailArticleApprovalDeniedBody);
149         }
150     }
151 
152     protected void updateEmailArticleApprovalGranted(
153             ActionRequest actionRequest, PortletPreferences prefs)
154         throws Exception {
155 
156         boolean emailArticleApprovalGrantedEnabled = ParamUtil.getBoolean(
157             actionRequest, "emailArticleApprovalGrantedEnabled");
158         String emailArticleApprovalGrantedSubject = ParamUtil.getString(
159             actionRequest, "emailArticleApprovalGrantedSubject");
160         String emailArticleApprovalGrantedBody = ParamUtil.getString(
161             actionRequest, "emailArticleApprovalGrantedBody");
162 
163         if (Validator.isNull(emailArticleApprovalGrantedSubject)) {
164             SessionErrors.add(
165                 actionRequest, "emailArticleApprovalGrantedSubject");
166         }
167         else if (Validator.isNull(emailArticleApprovalGrantedBody)) {
168             SessionErrors.add(actionRequest, "emailArticleApprovalGrantedBody");
169         }
170         else {
171             prefs.setValue(
172                 "email-article-approval-granted-enabled",
173                 String.valueOf(emailArticleApprovalGrantedEnabled));
174             prefs.setValue(
175                 "email-article-approval-granted-subject",
176                 emailArticleApprovalGrantedSubject);
177             prefs.setValue(
178                 "email-article-approval-granted-body",
179                 emailArticleApprovalGrantedBody);
180         }
181     }
182 
183     protected void updateEmailArticleApprovalRequested(
184             ActionRequest actionRequest, PortletPreferences prefs)
185         throws Exception {
186 
187         boolean emailArticleApprovalRequestedEnabled = ParamUtil.getBoolean(
188             actionRequest, "emailArticleApprovalRequestedEnabled");
189         String emailArticleApprovalRequestedSubject = ParamUtil.getString(
190             actionRequest, "emailArticleApprovalRequestedSubject");
191         String emailArticleApprovalRequestedBody = ParamUtil.getString(
192             actionRequest, "emailArticleApprovalRequestedBody");
193 
194         if (Validator.isNull(emailArticleApprovalRequestedSubject)) {
195             SessionErrors.add(
196                 actionRequest, "emailArticleApprovalRequestedSubject");
197         }
198         else if (Validator.isNull(emailArticleApprovalRequestedBody)) {
199             SessionErrors.add(
200                 actionRequest, "emailArticleApprovalRequestedBody");
201         }
202         else {
203             prefs.setValue(
204                 "email-article-approval-requested-enabled",
205                 String.valueOf(emailArticleApprovalRequestedEnabled));
206             prefs.setValue(
207                 "email-article-approval-requested-subject",
208                 emailArticleApprovalRequestedSubject);
209             prefs.setValue(
210                 "email-article-approval-requested-body",
211                 emailArticleApprovalRequestedBody);
212         }
213     }
214 
215     protected void updateEmailArticleReview(
216             ActionRequest actionRequest, PortletPreferences prefs)
217         throws Exception {
218 
219         boolean emailArticleReviewEnabled = ParamUtil.getBoolean(
220             actionRequest, "emailArticleReviewEnabled");
221         String emailArticleReviewSubject = ParamUtil.getString(
222             actionRequest, "emailArticleReviewSubject");
223         String emailArticleReviewBody = ParamUtil.getString(
224             actionRequest, "emailArticleReviewBody");
225 
226         if (Validator.isNull(emailArticleReviewSubject)) {
227             SessionErrors.add(actionRequest, "emailArticleReviewSubject");
228         }
229         else if (Validator.isNull(emailArticleReviewBody)) {
230             SessionErrors.add(actionRequest, "emailArticleReviewBody");
231         }
232         else {
233             prefs.setValue(
234                 "email-article-review-enabled",
235                 String.valueOf(emailArticleReviewEnabled));
236             prefs.setValue(
237                 "email-article-review-subject", emailArticleReviewSubject);
238             prefs.setValue("email-article-review-body", emailArticleReviewBody);
239         }
240     }
241 
242 }