1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ConfigurationActionImpl extends BaseConfigurationAction {
46  
47      public void processAction(
48              PortletConfig portletConfig, ActionRequest actionRequest,
49              ActionResponse actionResponse)
50          throws Exception {
51  
52          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
53  
54          if (!cmd.equals(Constants.UPDATE)) {
55              return;
56          }
57  
58          String portletResource = ParamUtil.getString(
59              actionRequest, "portletResource");
60  
61          PortletPreferences prefs =
62              PortletPreferencesFactoryUtil.getPortletSetup(
63                  actionRequest, portletResource);
64  
65          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
66  
67          if (tabs2.equals("email-from")) {
68              updateEmailFrom(actionRequest, prefs);
69          }
70          else if (tabs2.equals("article-approval-denied-email")) {
71              updateEmailArticleApprovalDenied(actionRequest, prefs);
72          }
73          else if (tabs2.equals("article-approval-granted-email")) {
74              updateEmailArticleApprovalGranted(actionRequest, prefs);
75          }
76          else if (tabs2.equals("article-approval-requested-email")) {
77              updateEmailArticleApprovalRequested(actionRequest, prefs);
78          }
79          else if (tabs2.equals("article-review-email")) {
80              updateEmailArticleReview(actionRequest, prefs);
81          }
82  
83          if (SessionErrors.isEmpty(actionRequest)) {
84              prefs.store();
85  
86              SessionMessages.add(
87                  actionRequest, portletConfig.getPortletName() + ".doConfigure");
88          }
89      }
90  
91      public String render(
92              PortletConfig portletConfig, RenderRequest renderRequest,
93              RenderResponse renderResponse)
94          throws Exception {
95  
96          return "/html/portlet/journal/configuration.jsp";
97      }
98  
99      protected void updateEmailFrom(
100             ActionRequest actionRequest, PortletPreferences prefs)
101         throws Exception {
102 
103         String emailFromName = ParamUtil.getString(
104             actionRequest, "emailFromName");
105         String emailFromAddress = ParamUtil.getString(
106             actionRequest, "emailFromAddress");
107 
108         if (Validator.isNull(emailFromName)) {
109             SessionErrors.add(actionRequest, "emailFromName");
110         }
111         else if (!Validator.isEmailAddress(emailFromAddress)) {
112             SessionErrors.add(actionRequest, "emailFromAddress");
113         }
114         else {
115             prefs.setValue("email-from-name", emailFromName);
116             prefs.setValue("email-from-address", emailFromAddress);
117         }
118     }
119 
120     protected void updateEmailArticleApprovalDenied(
121             ActionRequest actionRequest, PortletPreferences prefs)
122         throws Exception {
123 
124         boolean emailArticleApprovalDeniedEnabled = ParamUtil.getBoolean(
125             actionRequest, "emailArticleApprovalDeniedEnabled");
126         String emailArticleApprovalDeniedSubject = ParamUtil.getString(
127             actionRequest, "emailArticleApprovalDeniedSubject");
128         String emailArticleApprovalDeniedBody = ParamUtil.getString(
129             actionRequest, "emailArticleApprovalDeniedBody");
130 
131         if (Validator.isNull(emailArticleApprovalDeniedSubject)) {
132             SessionErrors.add(
133                 actionRequest, "emailArticleApprovalDeniedSubject");
134         }
135         else if (Validator.isNull(emailArticleApprovalDeniedBody)) {
136             SessionErrors.add(actionRequest, "emailArticleApprovalDeniedBody");
137         }
138         else {
139             prefs.setValue(
140                 "email-article-approval-denied-enabled",
141                 String.valueOf(emailArticleApprovalDeniedEnabled));
142             prefs.setValue(
143                 "email-article-approval-denied-subject",
144                 emailArticleApprovalDeniedSubject);
145             prefs.setValue(
146                 "email-article-approval-denied-body",
147                 emailArticleApprovalDeniedBody);
148         }
149     }
150 
151     protected void updateEmailArticleApprovalGranted(
152             ActionRequest actionRequest, PortletPreferences prefs)
153         throws Exception {
154 
155         boolean emailArticleApprovalGrantedEnabled = ParamUtil.getBoolean(
156             actionRequest, "emailArticleApprovalGrantedEnabled");
157         String emailArticleApprovalGrantedSubject = ParamUtil.getString(
158             actionRequest, "emailArticleApprovalGrantedSubject");
159         String emailArticleApprovalGrantedBody = ParamUtil.getString(
160             actionRequest, "emailArticleApprovalGrantedBody");
161 
162         if (Validator.isNull(emailArticleApprovalGrantedSubject)) {
163             SessionErrors.add(
164                 actionRequest, "emailArticleApprovalGrantedSubject");
165         }
166         else if (Validator.isNull(emailArticleApprovalGrantedBody)) {
167             SessionErrors.add(actionRequest, "emailArticleApprovalGrantedBody");
168         }
169         else {
170             prefs.setValue(
171                 "email-article-approval-granted-enabled",
172                 String.valueOf(emailArticleApprovalGrantedEnabled));
173             prefs.setValue(
174                 "email-article-approval-granted-subject",
175                 emailArticleApprovalGrantedSubject);
176             prefs.setValue(
177                 "email-article-approval-granted-body",
178                 emailArticleApprovalGrantedBody);
179         }
180     }
181 
182     protected void updateEmailArticleApprovalRequested(
183             ActionRequest actionRequest, PortletPreferences prefs)
184         throws Exception {
185 
186         boolean emailArticleApprovalRequestedEnabled = ParamUtil.getBoolean(
187             actionRequest, "emailArticleApprovalRequestedEnabled");
188         String emailArticleApprovalRequestedSubject = ParamUtil.getString(
189             actionRequest, "emailArticleApprovalRequestedSubject");
190         String emailArticleApprovalRequestedBody = ParamUtil.getString(
191             actionRequest, "emailArticleApprovalRequestedBody");
192 
193         if (Validator.isNull(emailArticleApprovalRequestedSubject)) {
194             SessionErrors.add(
195                 actionRequest, "emailArticleApprovalRequestedSubject");
196         }
197         else if (Validator.isNull(emailArticleApprovalRequestedBody)) {
198             SessionErrors.add(
199                 actionRequest, "emailArticleApprovalRequestedBody");
200         }
201         else {
202             prefs.setValue(
203                 "email-article-approval-requested-enabled",
204                 String.valueOf(emailArticleApprovalRequestedEnabled));
205             prefs.setValue(
206                 "email-article-approval-requested-subject",
207                 emailArticleApprovalRequestedSubject);
208             prefs.setValue(
209                 "email-article-approval-requested-body",
210                 emailArticleApprovalRequestedBody);
211         }
212     }
213 
214     protected void updateEmailArticleReview(
215             ActionRequest actionRequest, PortletPreferences prefs)
216         throws Exception {
217 
218         boolean emailArticleReviewEnabled = ParamUtil.getBoolean(
219             actionRequest, "emailArticleReviewEnabled");
220         String emailArticleReviewSubject = ParamUtil.getString(
221             actionRequest, "emailArticleReviewSubject");
222         String emailArticleReviewBody = ParamUtil.getString(
223             actionRequest, "emailArticleReviewBody");
224 
225         if (Validator.isNull(emailArticleReviewSubject)) {
226             SessionErrors.add(actionRequest, "emailArticleReviewSubject");
227         }
228         else if (Validator.isNull(emailArticleReviewBody)) {
229             SessionErrors.add(actionRequest, "emailArticleReviewBody");
230         }
231         else {
232             prefs.setValue(
233                 "email-article-review-enabled",
234                 String.valueOf(emailArticleReviewEnabled));
235             prefs.setValue(
236                 "email-article-review-subject", emailArticleReviewSubject);
237             prefs.setValue("email-article-review-body", emailArticleReviewBody);
238         }
239     }
240 
241 }