1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journal.action;
21  
22  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
23  import com.liferay.portal.kernel.servlet.SessionErrors;
24  import com.liferay.portal.kernel.servlet.SessionMessages;
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portlet.PortletPreferencesFactoryUtil;
29  
30  import javax.portlet.ActionRequest;
31  import javax.portlet.ActionResponse;
32  import javax.portlet.PortletConfig;
33  import javax.portlet.PortletPreferences;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  /**
38   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class ConfigurationActionImpl extends BaseConfigurationAction {
44  
45      public void processAction(
46              PortletConfig portletConfig, ActionRequest actionRequest,
47              ActionResponse actionResponse)
48          throws Exception {
49  
50          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
51  
52          if (!cmd.equals(Constants.UPDATE)) {
53              return;
54          }
55  
56          String portletResource = ParamUtil.getString(
57              actionRequest, "portletResource");
58  
59          PortletPreferences preferences =
60              PortletPreferencesFactoryUtil.getPortletSetup(
61                  actionRequest, portletResource);
62  
63          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
64  
65          if (tabs2.equals("email-from")) {
66              updateEmailFrom(actionRequest, preferences);
67          }
68          else if (tabs2.equals("web-content-approval-denied-email")) {
69              updateEmailArticleApprovalDenied(actionRequest, preferences);
70          }
71          else if (tabs2.equals("web-content-approval-granted-email")) {
72              updateEmailArticleApprovalGranted(actionRequest, preferences);
73          }
74          else if (tabs2.equals("web-content-approval-requested-email")) {
75              updateEmailArticleApprovalRequested(actionRequest, preferences);
76          }
77          else if (tabs2.equals("web-content-review-email")) {
78              updateEmailArticleReview(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 updateEmailFrom(
98              ActionRequest actionRequest, PortletPreferences preferences)
99          throws Exception {
100 
101         String emailFromName = ParamUtil.getString(
102             actionRequest, "emailFromName");
103         String emailFromAddress = ParamUtil.getString(
104             actionRequest, "emailFromAddress");
105 
106         if (Validator.isNull(emailFromName)) {
107             SessionErrors.add(actionRequest, "emailFromName");
108         }
109         else if (!Validator.isEmailAddress(emailFromAddress)) {
110             SessionErrors.add(actionRequest, "emailFromAddress");
111         }
112         else {
113             preferences.setValue("email-from-name", emailFromName);
114             preferences.setValue("email-from-address", emailFromAddress);
115         }
116     }
117 
118     protected void updateEmailArticleApprovalDenied(
119             ActionRequest actionRequest, PortletPreferences preferences)
120         throws Exception {
121 
122         boolean emailArticleApprovalDeniedEnabled = ParamUtil.getBoolean(
123             actionRequest, "emailArticleApprovalDeniedEnabled");
124         String emailArticleApprovalDeniedSubject = ParamUtil.getString(
125             actionRequest, "emailArticleApprovalDeniedSubject");
126         String emailArticleApprovalDeniedBody = ParamUtil.getString(
127             actionRequest, "emailArticleApprovalDeniedBody");
128 
129         if (Validator.isNull(emailArticleApprovalDeniedSubject)) {
130             SessionErrors.add(
131                 actionRequest, "emailArticleApprovalDeniedSubject");
132         }
133         else if (Validator.isNull(emailArticleApprovalDeniedBody)) {
134             SessionErrors.add(actionRequest, "emailArticleApprovalDeniedBody");
135         }
136         else {
137             preferences.setValue(
138                 "email-article-approval-denied-enabled",
139                 String.valueOf(emailArticleApprovalDeniedEnabled));
140             preferences.setValue(
141                 "email-article-approval-denied-subject",
142                 emailArticleApprovalDeniedSubject);
143             preferences.setValue(
144                 "email-article-approval-denied-body",
145                 emailArticleApprovalDeniedBody);
146         }
147     }
148 
149     protected void updateEmailArticleApprovalGranted(
150             ActionRequest actionRequest, PortletPreferences preferences)
151         throws Exception {
152 
153         boolean emailArticleApprovalGrantedEnabled = ParamUtil.getBoolean(
154             actionRequest, "emailArticleApprovalGrantedEnabled");
155         String emailArticleApprovalGrantedSubject = ParamUtil.getString(
156             actionRequest, "emailArticleApprovalGrantedSubject");
157         String emailArticleApprovalGrantedBody = ParamUtil.getString(
158             actionRequest, "emailArticleApprovalGrantedBody");
159 
160         if (Validator.isNull(emailArticleApprovalGrantedSubject)) {
161             SessionErrors.add(
162                 actionRequest, "emailArticleApprovalGrantedSubject");
163         }
164         else if (Validator.isNull(emailArticleApprovalGrantedBody)) {
165             SessionErrors.add(actionRequest, "emailArticleApprovalGrantedBody");
166         }
167         else {
168             preferences.setValue(
169                 "email-article-approval-granted-enabled",
170                 String.valueOf(emailArticleApprovalGrantedEnabled));
171             preferences.setValue(
172                 "email-article-approval-granted-subject",
173                 emailArticleApprovalGrantedSubject);
174             preferences.setValue(
175                 "email-article-approval-granted-body",
176                 emailArticleApprovalGrantedBody);
177         }
178     }
179 
180     protected void updateEmailArticleApprovalRequested(
181             ActionRequest actionRequest, PortletPreferences preferences)
182         throws Exception {
183 
184         boolean emailArticleApprovalRequestedEnabled = ParamUtil.getBoolean(
185             actionRequest, "emailArticleApprovalRequestedEnabled");
186         String emailArticleApprovalRequestedSubject = ParamUtil.getString(
187             actionRequest, "emailArticleApprovalRequestedSubject");
188         String emailArticleApprovalRequestedBody = ParamUtil.getString(
189             actionRequest, "emailArticleApprovalRequestedBody");
190 
191         if (Validator.isNull(emailArticleApprovalRequestedSubject)) {
192             SessionErrors.add(
193                 actionRequest, "emailArticleApprovalRequestedSubject");
194         }
195         else if (Validator.isNull(emailArticleApprovalRequestedBody)) {
196             SessionErrors.add(
197                 actionRequest, "emailArticleApprovalRequestedBody");
198         }
199         else {
200             preferences.setValue(
201                 "email-article-approval-requested-enabled",
202                 String.valueOf(emailArticleApprovalRequestedEnabled));
203             preferences.setValue(
204                 "email-article-approval-requested-subject",
205                 emailArticleApprovalRequestedSubject);
206             preferences.setValue(
207                 "email-article-approval-requested-body",
208                 emailArticleApprovalRequestedBody);
209         }
210     }
211 
212     protected void updateEmailArticleReview(
213             ActionRequest actionRequest, PortletPreferences preferences)
214         throws Exception {
215 
216         boolean emailArticleReviewEnabled = ParamUtil.getBoolean(
217             actionRequest, "emailArticleReviewEnabled");
218         String emailArticleReviewSubject = ParamUtil.getString(
219             actionRequest, "emailArticleReviewSubject");
220         String emailArticleReviewBody = ParamUtil.getString(
221             actionRequest, "emailArticleReviewBody");
222 
223         if (Validator.isNull(emailArticleReviewSubject)) {
224             SessionErrors.add(actionRequest, "emailArticleReviewSubject");
225         }
226         else if (Validator.isNull(emailArticleReviewBody)) {
227             SessionErrors.add(actionRequest, "emailArticleReviewBody");
228         }
229         else {
230             preferences.setValue(
231                 "email-article-review-enabled",
232                 String.valueOf(emailArticleReviewEnabled));
233             preferences.setValue(
234                 "email-article-review-subject", emailArticleReviewSubject);
235             preferences.setValue(
236                 "email-article-review-body", emailArticleReviewBody);
237         }
238     }
239 
240 }