1   /**
2    * Copyright (c) 2000-2008 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.wiki.action;
24  
25  import com.liferay.portal.kernel.portlet.ConfigurationAction;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portlet.PortletPreferencesFactoryUtil;
30  import com.liferay.util.servlet.SessionErrors;
31  import com.liferay.util.servlet.SessionMessages;
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 Bruno Farache
44   *
45   */
46  public class ConfigurationActionImpl implements ConfigurationAction {
47  
48      public void processAction(
49              PortletConfig config, ActionRequest req, ActionResponse res)
50          throws Exception {
51  
52          String cmd = ParamUtil.getString(req, Constants.CMD);
53  
54          if (!cmd.equals(Constants.UPDATE)) {
55              return;
56          }
57  
58          String portletResource = ParamUtil.getString(req, "portletResource");
59  
60          PortletPreferences prefs =
61              PortletPreferencesFactoryUtil.getPortletSetup(req, portletResource);
62  
63          String tabs2 = ParamUtil.getString(req, "tabs2");
64  
65          if (tabs2.equals("email-from")) {
66              updateEmailFrom(req, prefs);
67          }
68          else if (tabs2.equals("page-added-email")) {
69              updateEmailPageAdded(req, prefs);
70          }
71          else if (tabs2.equals("page-updated-email")) {
72              updateEmailPageUpdated(req, prefs);
73          }
74          else if (tabs2.equals("rss")) {
75              updateRSS(req, prefs);
76          }
77  
78          if (SessionErrors.isEmpty(req)) {
79              prefs.store();
80  
81              SessionMessages.add(req, config.getPortletName() + ".doConfigure");
82          }
83      }
84  
85      public String render(
86              PortletConfig config, RenderRequest req, RenderResponse res)
87          throws Exception {
88  
89          return "/html/portlet/wiki/configuration.jsp";
90      }
91  
92      protected void updateEmailFrom(ActionRequest req, PortletPreferences prefs)
93          throws Exception {
94  
95          String emailFromName = ParamUtil.getString(req, "emailFromName");
96          String emailFromAddress = ParamUtil.getString(req, "emailFromAddress");
97  
98          if (Validator.isNull(emailFromName)) {
99              SessionErrors.add(req, "emailFromName");
100         }
101         else if (!Validator.isEmailAddress(emailFromAddress) &&
102                  !Validator.isVariableTerm(emailFromAddress)) {
103 
104             SessionErrors.add(req, "emailFromAddress");
105         }
106         else {
107             prefs.setValue("email-from-name", emailFromName);
108             prefs.setValue("email-from-address", emailFromAddress);
109         }
110     }
111 
112     protected void updateEmailPageAdded(
113             ActionRequest req, PortletPreferences prefs)
114         throws Exception {
115 
116         boolean emailPageAddedEnabled = ParamUtil.getBoolean(
117             req, "emailPageAddedEnabled");
118         String emailPageAddedSubjectPrefix = ParamUtil.getString(
119             req, "emailPageAddedSubjectPrefix");
120         String emailPageAddedBody = ParamUtil.getString(
121             req, "emailPageAddedBody");
122         String emailPageAddedSignature = ParamUtil.getString(
123             req, "emailPageAddedSignature");
124 
125         if (Validator.isNull(emailPageAddedSubjectPrefix)) {
126             SessionErrors.add(req, "emailPageAddedSubjectPrefix");
127         }
128         else if (Validator.isNull(emailPageAddedBody)) {
129             SessionErrors.add(req, "emailPageAddedBody");
130         }
131         else {
132             prefs.setValue(
133                 "email-page-added-enabled",
134                 String.valueOf(emailPageAddedEnabled));
135             prefs.setValue(
136                 "email-page-added-subject-prefix", emailPageAddedSubjectPrefix);
137             prefs.setValue("email-page-added-body", emailPageAddedBody);
138             prefs.setValue(
139                 "email-page-added-signature", emailPageAddedSignature);
140         }
141     }
142 
143     protected void updateEmailPageUpdated(
144             ActionRequest req, PortletPreferences prefs)
145         throws Exception {
146 
147         boolean emailPageUpdatedEnabled = ParamUtil.getBoolean(
148             req, "emailPageUpdatedEnabled");
149         String emailPageUpdatedSubjectPrefix = ParamUtil.getString(
150             req, "emailPageUpdatedSubjectPrefix");
151         String emailPageUpdatedBody = ParamUtil.getString(
152             req, "emailPageUpdatedBody");
153         String emailPageUpdatedSignature = ParamUtil.getString(
154             req, "emailPageUpdatedSignature");
155 
156         if (Validator.isNull(emailPageUpdatedSubjectPrefix)) {
157             SessionErrors.add(req, "emailPageUpdatedSubjectPrefix");
158         }
159         else if (Validator.isNull(emailPageUpdatedBody)) {
160             SessionErrors.add(req, "emailPageUpdatedBody");
161         }
162         else {
163             prefs.setValue(
164                 "email-page-updated-enabled",
165                 String.valueOf(emailPageUpdatedEnabled));
166             prefs.setValue(
167                 "email-page-updated-subject-prefix",
168                 emailPageUpdatedSubjectPrefix);
169             prefs.setValue("email-page-updated-body", emailPageUpdatedBody);
170             prefs.setValue(
171                 "email-page-updated-signature", emailPageUpdatedSignature);
172         }
173     }
174 
175     protected void updateRSS(ActionRequest req, PortletPreferences prefs)
176         throws Exception {
177 
178         int rssDelta = ParamUtil.getInteger(req, "rssDelta");
179         String rssDisplayStyle = ParamUtil.getString(req, "rssDisplayStyle");
180 
181         prefs.setValue("rss-delta", String.valueOf(rssDelta));
182         prefs.setValue("rss-display-style", rssDisplayStyle);
183     }
184 
185 }