001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.action;
016    
017    import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portlet.PortletPreferencesFactoryUtil;
024    
025    import javax.portlet.ActionRequest;
026    import javax.portlet.ActionResponse;
027    import javax.portlet.PortletConfig;
028    import javax.portlet.PortletPreferences;
029    import javax.portlet.RenderRequest;
030    import javax.portlet.RenderResponse;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class ConfigurationActionImpl extends BaseConfigurationAction {
036    
037            public void processAction(
038                            PortletConfig portletConfig, ActionRequest actionRequest,
039                            ActionResponse actionResponse)
040                    throws Exception {
041    
042                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
043    
044                    if (!cmd.equals(Constants.UPDATE)) {
045                            return;
046                    }
047    
048                    String portletResource = ParamUtil.getString(
049                            actionRequest, "portletResource");
050    
051                    PortletPreferences preferences =
052                            PortletPreferencesFactoryUtil.getPortletSetup(
053                                    actionRequest, portletResource);
054    
055                    String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
056    
057                    if (tabs2.equals("email-from")) {
058                            updateEmailFrom(actionRequest, preferences);
059                    }
060                    else if (tabs2.equals("web-content-added-email")) {
061                            updateEmailArticleAdded(actionRequest, preferences);
062                    }
063                    else if (tabs2.equals("web-content-approval-denied-email")) {
064                            updateEmailArticleApprovalDenied(actionRequest, preferences);
065                    }
066                    else if (tabs2.equals("web-content-approval-granted-email")) {
067                            updateEmailArticleApprovalGranted(actionRequest, preferences);
068                    }
069                    else if (tabs2.equals("web-content-approval-requested-email")) {
070                            updateEmailArticleApprovalRequested(actionRequest, preferences);
071                    }
072                    else if (tabs2.equals("web-content-review-email")) {
073                            updateEmailArticleReview(actionRequest, preferences);
074                    }
075                    else if (tabs2.equals("web-content-updated-email")) {
076                            updateEmailArticleUpdated(actionRequest, preferences);
077                    }
078    
079                    if (SessionErrors.isEmpty(actionRequest)) {
080                            preferences.store();
081    
082                            SessionMessages.add(
083                                    actionRequest, portletConfig.getPortletName() + ".doConfigure");
084                    }
085            }
086    
087            public String render(
088                            PortletConfig portletConfig, RenderRequest renderRequest,
089                            RenderResponse renderResponse)
090                    throws Exception {
091    
092                    return "/html/portlet/journal/configuration.jsp";
093            }
094    
095            protected void updateEmailArticleAdded(
096                            ActionRequest actionRequest, PortletPreferences preferences)
097                    throws Exception {
098    
099                    boolean emailArticleAddedEnabled = ParamUtil.getBoolean(
100                            actionRequest, "emailArticleAddedEnabled");
101                    String emailArticleAddedSubject = ParamUtil.getString(
102                            actionRequest, "emailArticleAddedSubject");
103                    String emailArticleAddedBody = ParamUtil.getString(
104                            actionRequest, "emailArticleAddedBody");
105    
106                    if (Validator.isNull(emailArticleAddedSubject)) {
107                            SessionErrors.add(actionRequest, "emailArticleAddedSubject");
108                    }
109                    else if (Validator.isNull(emailArticleAddedBody)) {
110                            SessionErrors.add(actionRequest, "emailArticleAddedBody");
111                    }
112                    else {
113                            preferences.setValue(
114                                    "email-article-added-enabled",
115                                    String.valueOf(emailArticleAddedEnabled));
116                            preferences.setValue(
117                                    "email-article-added-subject", emailArticleAddedSubject);
118                            preferences.setValue(
119                                    "email-article-added-body", emailArticleAddedBody);
120                    }
121            }
122    
123            protected void updateEmailArticleApprovalDenied(
124                            ActionRequest actionRequest, PortletPreferences preferences)
125                    throws Exception {
126    
127                    boolean emailArticleApprovalDeniedEnabled = ParamUtil.getBoolean(
128                            actionRequest, "emailArticleApprovalDeniedEnabled");
129                    String emailArticleApprovalDeniedSubject = ParamUtil.getString(
130                            actionRequest, "emailArticleApprovalDeniedSubject");
131                    String emailArticleApprovalDeniedBody = ParamUtil.getString(
132                            actionRequest, "emailArticleApprovalDeniedBody");
133    
134                    if (Validator.isNull(emailArticleApprovalDeniedSubject)) {
135                            SessionErrors.add(
136                                    actionRequest, "emailArticleApprovalDeniedSubject");
137                    }
138                    else if (Validator.isNull(emailArticleApprovalDeniedBody)) {
139                            SessionErrors.add(actionRequest, "emailArticleApprovalDeniedBody");
140                    }
141                    else {
142                            preferences.setValue(
143                                    "email-article-approval-denied-enabled",
144                                    String.valueOf(emailArticleApprovalDeniedEnabled));
145                            preferences.setValue(
146                                    "email-article-approval-denied-subject",
147                                    emailArticleApprovalDeniedSubject);
148                            preferences.setValue(
149                                    "email-article-approval-denied-body",
150                                    emailArticleApprovalDeniedBody);
151                    }
152            }
153    
154            protected void updateEmailArticleApprovalGranted(
155                            ActionRequest actionRequest, PortletPreferences preferences)
156                    throws Exception {
157    
158                    boolean emailArticleApprovalGrantedEnabled = ParamUtil.getBoolean(
159                            actionRequest, "emailArticleApprovalGrantedEnabled");
160                    String emailArticleApprovalGrantedSubject = ParamUtil.getString(
161                            actionRequest, "emailArticleApprovalGrantedSubject");
162                    String emailArticleApprovalGrantedBody = ParamUtil.getString(
163                            actionRequest, "emailArticleApprovalGrantedBody");
164    
165                    if (Validator.isNull(emailArticleApprovalGrantedSubject)) {
166                            SessionErrors.add(
167                                    actionRequest, "emailArticleApprovalGrantedSubject");
168                    }
169                    else if (Validator.isNull(emailArticleApprovalGrantedBody)) {
170                            SessionErrors.add(actionRequest, "emailArticleApprovalGrantedBody");
171                    }
172                    else {
173                            preferences.setValue(
174                                    "email-article-approval-granted-enabled",
175                                    String.valueOf(emailArticleApprovalGrantedEnabled));
176                            preferences.setValue(
177                                    "email-article-approval-granted-subject",
178                                    emailArticleApprovalGrantedSubject);
179                            preferences.setValue(
180                                    "email-article-approval-granted-body",
181                                    emailArticleApprovalGrantedBody);
182                    }
183            }
184    
185            protected void updateEmailArticleApprovalRequested(
186                            ActionRequest actionRequest, PortletPreferences preferences)
187                    throws Exception {
188    
189                    boolean emailArticleApprovalRequestedEnabled = ParamUtil.getBoolean(
190                            actionRequest, "emailArticleApprovalRequestedEnabled");
191                    String emailArticleApprovalRequestedSubject = ParamUtil.getString(
192                            actionRequest, "emailArticleApprovalRequestedSubject");
193                    String emailArticleApprovalRequestedBody = ParamUtil.getString(
194                            actionRequest, "emailArticleApprovalRequestedBody");
195    
196                    if (Validator.isNull(emailArticleApprovalRequestedSubject)) {
197                            SessionErrors.add(
198                                    actionRequest, "emailArticleApprovalRequestedSubject");
199                    }
200                    else if (Validator.isNull(emailArticleApprovalRequestedBody)) {
201                            SessionErrors.add(
202                                    actionRequest, "emailArticleApprovalRequestedBody");
203                    }
204                    else {
205                            preferences.setValue(
206                                    "email-article-approval-requested-enabled",
207                                    String.valueOf(emailArticleApprovalRequestedEnabled));
208                            preferences.setValue(
209                                    "email-article-approval-requested-subject",
210                                    emailArticleApprovalRequestedSubject);
211                            preferences.setValue(
212                                    "email-article-approval-requested-body",
213                                    emailArticleApprovalRequestedBody);
214                    }
215            }
216    
217            protected void updateEmailArticleReview(
218                            ActionRequest actionRequest, PortletPreferences preferences)
219                    throws Exception {
220    
221                    boolean emailArticleReviewEnabled = ParamUtil.getBoolean(
222                            actionRequest, "emailArticleReviewEnabled");
223                    String emailArticleReviewSubject = ParamUtil.getString(
224                            actionRequest, "emailArticleReviewSubject");
225                    String emailArticleReviewBody = ParamUtil.getString(
226                            actionRequest, "emailArticleReviewBody");
227    
228                    if (Validator.isNull(emailArticleReviewSubject)) {
229                            SessionErrors.add(actionRequest, "emailArticleReviewSubject");
230                    }
231                    else if (Validator.isNull(emailArticleReviewBody)) {
232                            SessionErrors.add(actionRequest, "emailArticleReviewBody");
233                    }
234                    else {
235                            preferences.setValue(
236                                    "email-article-review-enabled",
237                                    String.valueOf(emailArticleReviewEnabled));
238                            preferences.setValue(
239                                    "email-article-review-subject", emailArticleReviewSubject);
240                            preferences.setValue(
241                                    "email-article-review-body", emailArticleReviewBody);
242                    }
243            }
244    
245            protected void updateEmailArticleUpdated(
246                            ActionRequest actionRequest, PortletPreferences preferences)
247                    throws Exception {
248    
249                    boolean emailArticleUpdatedEnabled = ParamUtil.getBoolean(
250                            actionRequest, "emailArticleUpdatedEnabled");
251                    String emailArticleUpdatedSubject = ParamUtil.getString(
252                            actionRequest, "emailArticleUpdatedSubject");
253                    String emailArticleUpdatedBody = ParamUtil.getString(
254                            actionRequest, "emailArticleUpdatedBody");
255    
256                    if (Validator.isNull(emailArticleUpdatedSubject)) {
257                            SessionErrors.add(actionRequest, "emailArticleUpdatedSubject");
258                    }
259                    else if (Validator.isNull(emailArticleUpdatedBody)) {
260                            SessionErrors.add(actionRequest, "emailArticleUpdatedBody");
261                    }
262                    else {
263                            preferences.setValue(
264                                    "email-article-updated-enabled",
265                                    String.valueOf(emailArticleUpdatedEnabled));
266                            preferences.setValue(
267                                    "email-article-updated-subject", emailArticleUpdatedSubject);
268                            preferences.setValue(
269                                    "email-article-updated-body", emailArticleUpdatedBody);
270                    }
271            }
272    
273            protected void updateEmailFrom(
274                            ActionRequest actionRequest, PortletPreferences preferences)
275                    throws Exception {
276    
277                    String emailFromName = ParamUtil.getString(
278                            actionRequest, "emailFromName");
279                    String emailFromAddress = ParamUtil.getString(
280                            actionRequest, "emailFromAddress");
281    
282                    if (Validator.isNull(emailFromName)) {
283                            SessionErrors.add(actionRequest, "emailFromName");
284                    }
285                    else if (!Validator.isEmailAddress(emailFromAddress)) {
286                            SessionErrors.add(actionRequest, "emailFromAddress");
287                    }
288                    else {
289                            preferences.setValue("email-from-name", emailFromName);
290                            preferences.setValue("email-from-address", emailFromAddress);
291                    }
292            }
293    
294    }