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.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 Bruno Farache
44   *
45   */
46  public class ConfigurationActionImpl implements ConfigurationAction {
47  
48      public void processAction(
49              PortletConfig portletConfig, ActionRequest actionRequest,
50              ActionResponse actionResponse)
51          throws Exception {
52  
53          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
54  
55          if (!cmd.equals(Constants.UPDATE)) {
56              return;
57          }
58  
59          String portletResource = ParamUtil.getString(
60              actionRequest, "portletResource");
61  
62          PortletPreferences prefs =
63              PortletPreferencesFactoryUtil.getPortletSetup(
64                  actionRequest, portletResource);
65  
66          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
67  
68          if (tabs2.equals("display-settings")) {
69              updateDisplaySettings(actionRequest, prefs);
70          }
71          else if (tabs2.equals("email-from")) {
72              updateEmailFrom(actionRequest, prefs);
73          }
74          else if (tabs2.equals("page-added-email")) {
75              updateEmailPageAdded(actionRequest, prefs);
76          }
77          else if (tabs2.equals("page-updated-email")) {
78              updateEmailPageUpdated(actionRequest, prefs);
79          }
80          else if (tabs2.equals("rss")) {
81              updateRSS(actionRequest, prefs);
82          }
83  
84          if (SessionErrors.isEmpty(actionRequest)) {
85              prefs.store();
86  
87              SessionMessages.add(
88                  actionRequest, portletConfig.getPortletName() + ".doConfigure");
89          }
90      }
91  
92      public String render(
93              PortletConfig portletConfig, RenderRequest renderRequest,
94              RenderResponse renderResponse)
95          throws Exception {
96  
97          return "/html/portlet/wiki/configuration.jsp";
98      }
99  
100     protected void updateDisplaySettings(
101             ActionRequest actionRequest, PortletPreferences prefs)
102         throws Exception {
103 
104         boolean enableComments = ParamUtil.getBoolean(
105             actionRequest, "enableComments");
106         boolean enableCommentRatings = ParamUtil.getBoolean(
107             actionRequest, "enableCommentRatings");
108         String visibleNodes = ParamUtil.getString(
109             actionRequest, "visibleNodes");
110         String hiddenNodes = ParamUtil.getString(actionRequest, "hiddenNodes");
111 
112         if (Validator.isNull(visibleNodes)) {
113             SessionErrors.add(actionRequest, "visibleNodesCount");
114         }
115         else {
116             prefs.setValue("enable-comments", String.valueOf(enableComments));
117             prefs.setValue(
118                 "enable-comment-ratings", String.valueOf(enableCommentRatings));
119             prefs.setValue("visible-nodes", visibleNodes);
120             prefs.setValue("hidden-nodes", hiddenNodes);
121         }
122     }
123 
124     protected void updateEmailFrom(
125             ActionRequest actionRequest, PortletPreferences prefs)
126         throws Exception {
127 
128         String emailFromName = ParamUtil.getString(
129             actionRequest, "emailFromName");
130         String emailFromAddress = ParamUtil.getString(
131             actionRequest, "emailFromAddress");
132 
133         if (Validator.isNull(emailFromName)) {
134             SessionErrors.add(actionRequest, "emailFromName");
135         }
136         else if (!Validator.isEmailAddress(emailFromAddress) &&
137                  !Validator.isVariableTerm(emailFromAddress)) {
138 
139             SessionErrors.add(actionRequest, "emailFromAddress");
140         }
141         else {
142             prefs.setValue("email-from-name", emailFromName);
143             prefs.setValue("email-from-address", emailFromAddress);
144         }
145     }
146 
147     protected void updateEmailPageAdded(
148             ActionRequest actionRequest, PortletPreferences prefs)
149         throws Exception {
150 
151         boolean emailPageAddedEnabled = ParamUtil.getBoolean(
152             actionRequest, "emailPageAddedEnabled");
153         String emailPageAddedSubjectPrefix = ParamUtil.getString(
154             actionRequest, "emailPageAddedSubjectPrefix");
155         String emailPageAddedBody = ParamUtil.getString(
156             actionRequest, "emailPageAddedBody");
157         String emailPageAddedSignature = ParamUtil.getString(
158             actionRequest, "emailPageAddedSignature");
159 
160         if (Validator.isNull(emailPageAddedSubjectPrefix)) {
161             SessionErrors.add(actionRequest, "emailPageAddedSubjectPrefix");
162         }
163         else if (Validator.isNull(emailPageAddedBody)) {
164             SessionErrors.add(actionRequest, "emailPageAddedBody");
165         }
166         else {
167             prefs.setValue(
168                 "email-page-added-enabled",
169                 String.valueOf(emailPageAddedEnabled));
170             prefs.setValue(
171                 "email-page-added-subject-prefix", emailPageAddedSubjectPrefix);
172             prefs.setValue("email-page-added-body", emailPageAddedBody);
173             prefs.setValue(
174                 "email-page-added-signature", emailPageAddedSignature);
175         }
176     }
177 
178     protected void updateEmailPageUpdated(
179             ActionRequest actionRequest, PortletPreferences prefs)
180         throws Exception {
181 
182         boolean emailPageUpdatedEnabled = ParamUtil.getBoolean(
183             actionRequest, "emailPageUpdatedEnabled");
184         String emailPageUpdatedSubjectPrefix = ParamUtil.getString(
185             actionRequest, "emailPageUpdatedSubjectPrefix");
186         String emailPageUpdatedBody = ParamUtil.getString(
187             actionRequest, "emailPageUpdatedBody");
188         String emailPageUpdatedSignature = ParamUtil.getString(
189             actionRequest, "emailPageUpdatedSignature");
190 
191         if (Validator.isNull(emailPageUpdatedSubjectPrefix)) {
192             SessionErrors.add(actionRequest, "emailPageUpdatedSubjectPrefix");
193         }
194         else if (Validator.isNull(emailPageUpdatedBody)) {
195             SessionErrors.add(actionRequest, "emailPageUpdatedBody");
196         }
197         else {
198             prefs.setValue(
199                 "email-page-updated-enabled",
200                 String.valueOf(emailPageUpdatedEnabled));
201             prefs.setValue(
202                 "email-page-updated-subject-prefix",
203                 emailPageUpdatedSubjectPrefix);
204             prefs.setValue("email-page-updated-body", emailPageUpdatedBody);
205             prefs.setValue(
206                 "email-page-updated-signature", emailPageUpdatedSignature);
207         }
208     }
209 
210     protected void updateRSS(
211             ActionRequest actionRequest, PortletPreferences prefs)
212         throws Exception {
213 
214         int rssDelta = ParamUtil.getInteger(actionRequest, "rssDelta");
215         String rssDisplayStyle = ParamUtil.getString(
216             actionRequest, "rssDisplayStyle");
217 
218         prefs.setValue("rss-delta", String.valueOf(rssDelta));
219         prefs.setValue("rss-display-style", rssDisplayStyle);
220     }
221 
222 }