1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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-approval-denied-email")) {
63              updateEmailArticleApprovalDenied(actionRequest, preferences);
64          }
65          else if (tabs2.equals("web-content-approval-granted-email")) {
66              updateEmailArticleApprovalGranted(actionRequest, preferences);
67          }
68          else if (tabs2.equals("web-content-approval-requested-email")) {
69              updateEmailArticleApprovalRequested(actionRequest, preferences);
70          }
71          else if (tabs2.equals("web-content-review-email")) {
72              updateEmailArticleReview(actionRequest, preferences);
73          }
74  
75          if (SessionErrors.isEmpty(actionRequest)) {
76              preferences.store();
77  
78              SessionMessages.add(
79                  actionRequest, portletConfig.getPortletName() + ".doConfigure");
80          }
81      }
82  
83      public String render(
84              PortletConfig portletConfig, RenderRequest renderRequest,
85              RenderResponse renderResponse)
86          throws Exception {
87  
88          return "/html/portlet/journal/configuration.jsp";
89      }
90  
91      protected void updateEmailFrom(
92              ActionRequest actionRequest, PortletPreferences preferences)
93          throws Exception {
94  
95          String emailFromName = ParamUtil.getString(
96              actionRequest, "emailFromName");
97          String emailFromAddress = ParamUtil.getString(
98              actionRequest, "emailFromAddress");
99  
100         if (Validator.isNull(emailFromName)) {
101             SessionErrors.add(actionRequest, "emailFromName");
102         }
103         else if (!Validator.isEmailAddress(emailFromAddress)) {
104             SessionErrors.add(actionRequest, "emailFromAddress");
105         }
106         else {
107             preferences.setValue("email-from-name", emailFromName);
108             preferences.setValue("email-from-address", emailFromAddress);
109         }
110     }
111 
112     protected void updateEmailArticleApprovalDenied(
113             ActionRequest actionRequest, PortletPreferences preferences)
114         throws Exception {
115 
116         boolean emailArticleApprovalDeniedEnabled = ParamUtil.getBoolean(
117             actionRequest, "emailArticleApprovalDeniedEnabled");
118         String emailArticleApprovalDeniedSubject = ParamUtil.getString(
119             actionRequest, "emailArticleApprovalDeniedSubject");
120         String emailArticleApprovalDeniedBody = ParamUtil.getString(
121             actionRequest, "emailArticleApprovalDeniedBody");
122 
123         if (Validator.isNull(emailArticleApprovalDeniedSubject)) {
124             SessionErrors.add(
125                 actionRequest, "emailArticleApprovalDeniedSubject");
126         }
127         else if (Validator.isNull(emailArticleApprovalDeniedBody)) {
128             SessionErrors.add(actionRequest, "emailArticleApprovalDeniedBody");
129         }
130         else {
131             preferences.setValue(
132                 "email-article-approval-denied-enabled",
133                 String.valueOf(emailArticleApprovalDeniedEnabled));
134             preferences.setValue(
135                 "email-article-approval-denied-subject",
136                 emailArticleApprovalDeniedSubject);
137             preferences.setValue(
138                 "email-article-approval-denied-body",
139                 emailArticleApprovalDeniedBody);
140         }
141     }
142 
143     protected void updateEmailArticleApprovalGranted(
144             ActionRequest actionRequest, PortletPreferences preferences)
145         throws Exception {
146 
147         boolean emailArticleApprovalGrantedEnabled = ParamUtil.getBoolean(
148             actionRequest, "emailArticleApprovalGrantedEnabled");
149         String emailArticleApprovalGrantedSubject = ParamUtil.getString(
150             actionRequest, "emailArticleApprovalGrantedSubject");
151         String emailArticleApprovalGrantedBody = ParamUtil.getString(
152             actionRequest, "emailArticleApprovalGrantedBody");
153 
154         if (Validator.isNull(emailArticleApprovalGrantedSubject)) {
155             SessionErrors.add(
156                 actionRequest, "emailArticleApprovalGrantedSubject");
157         }
158         else if (Validator.isNull(emailArticleApprovalGrantedBody)) {
159             SessionErrors.add(actionRequest, "emailArticleApprovalGrantedBody");
160         }
161         else {
162             preferences.setValue(
163                 "email-article-approval-granted-enabled",
164                 String.valueOf(emailArticleApprovalGrantedEnabled));
165             preferences.setValue(
166                 "email-article-approval-granted-subject",
167                 emailArticleApprovalGrantedSubject);
168             preferences.setValue(
169                 "email-article-approval-granted-body",
170                 emailArticleApprovalGrantedBody);
171         }
172     }
173 
174     protected void updateEmailArticleApprovalRequested(
175             ActionRequest actionRequest, PortletPreferences preferences)
176         throws Exception {
177 
178         boolean emailArticleApprovalRequestedEnabled = ParamUtil.getBoolean(
179             actionRequest, "emailArticleApprovalRequestedEnabled");
180         String emailArticleApprovalRequestedSubject = ParamUtil.getString(
181             actionRequest, "emailArticleApprovalRequestedSubject");
182         String emailArticleApprovalRequestedBody = ParamUtil.getString(
183             actionRequest, "emailArticleApprovalRequestedBody");
184 
185         if (Validator.isNull(emailArticleApprovalRequestedSubject)) {
186             SessionErrors.add(
187                 actionRequest, "emailArticleApprovalRequestedSubject");
188         }
189         else if (Validator.isNull(emailArticleApprovalRequestedBody)) {
190             SessionErrors.add(
191                 actionRequest, "emailArticleApprovalRequestedBody");
192         }
193         else {
194             preferences.setValue(
195                 "email-article-approval-requested-enabled",
196                 String.valueOf(emailArticleApprovalRequestedEnabled));
197             preferences.setValue(
198                 "email-article-approval-requested-subject",
199                 emailArticleApprovalRequestedSubject);
200             preferences.setValue(
201                 "email-article-approval-requested-body",
202                 emailArticleApprovalRequestedBody);
203         }
204     }
205 
206     protected void updateEmailArticleReview(
207             ActionRequest actionRequest, PortletPreferences preferences)
208         throws Exception {
209 
210         boolean emailArticleReviewEnabled = ParamUtil.getBoolean(
211             actionRequest, "emailArticleReviewEnabled");
212         String emailArticleReviewSubject = ParamUtil.getString(
213             actionRequest, "emailArticleReviewSubject");
214         String emailArticleReviewBody = ParamUtil.getString(
215             actionRequest, "emailArticleReviewBody");
216 
217         if (Validator.isNull(emailArticleReviewSubject)) {
218             SessionErrors.add(actionRequest, "emailArticleReviewSubject");
219         }
220         else if (Validator.isNull(emailArticleReviewBody)) {
221             SessionErrors.add(actionRequest, "emailArticleReviewBody");
222         }
223         else {
224             preferences.setValue(
225                 "email-article-review-enabled",
226                 String.valueOf(emailArticleReviewEnabled));
227             preferences.setValue(
228                 "email-article-review-subject", emailArticleReviewSubject);
229             preferences.setValue(
230                 "email-article-review-body", emailArticleReviewBody);
231         }
232     }
233 
234 }