1   /**
2    * Copyright (c) 2000-2009 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.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 Bruno Farache
44   *
45   */
46  public class ConfigurationActionImpl extends BaseConfigurationAction {
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 preferences =
63              PortletPreferencesFactoryUtil.getPortletSetup(
64                  actionRequest, portletResource);
65  
66          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
67  
68          if (tabs2.equals("display-settings")) {
69              updateDisplaySettings(actionRequest, preferences);
70          }
71          else if (tabs2.equals("email-from")) {
72              updateEmailFrom(actionRequest, preferences);
73          }
74          else if (tabs2.equals("page-added-email")) {
75              updateEmailPageAdded(actionRequest, preferences);
76          }
77          else if (tabs2.equals("page-updated-email")) {
78              updateEmailPageUpdated(actionRequest, preferences);
79          }
80          else if (tabs2.equals("rss")) {
81              updateRSS(actionRequest, preferences);
82          }
83  
84          if (SessionErrors.isEmpty(actionRequest)) {
85              preferences.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 preferences)
102         throws Exception {
103 
104         boolean enablePageRatings = ParamUtil.getBoolean(
105             actionRequest, "enablePageRatings");
106         boolean enableComments = ParamUtil.getBoolean(
107             actionRequest, "enableComments");
108         boolean enableCommentRatings = ParamUtil.getBoolean(
109             actionRequest, "enableCommentRatings");
110         String visibleNodes = ParamUtil.getString(
111             actionRequest, "visibleNodes");
112         String hiddenNodes = ParamUtil.getString(actionRequest, "hiddenNodes");
113 
114         if (Validator.isNull(visibleNodes)) {
115             SessionErrors.add(actionRequest, "visibleNodesCount");
116         }
117         else {
118             preferences.setValue(
119                 "enable-page-ratings", String.valueOf(enablePageRatings));
120             preferences.setValue(
121                 "enable-comments", String.valueOf(enableComments));
122             preferences.setValue(
123                 "enable-comment-ratings", String.valueOf(enableCommentRatings));
124             preferences.setValue("visible-nodes", visibleNodes);
125             preferences.setValue("hidden-nodes", hiddenNodes);
126         }
127     }
128 
129     protected void updateEmailFrom(
130             ActionRequest actionRequest, PortletPreferences preferences)
131         throws Exception {
132 
133         String emailFromName = ParamUtil.getString(
134             actionRequest, "emailFromName");
135         String emailFromAddress = ParamUtil.getString(
136             actionRequest, "emailFromAddress");
137 
138         if (Validator.isNull(emailFromName)) {
139             SessionErrors.add(actionRequest, "emailFromName");
140         }
141         else if (!Validator.isEmailAddress(emailFromAddress) &&
142                  !Validator.isVariableTerm(emailFromAddress)) {
143 
144             SessionErrors.add(actionRequest, "emailFromAddress");
145         }
146         else {
147             preferences.setValue("email-from-name", emailFromName);
148             preferences.setValue("email-from-address", emailFromAddress);
149         }
150     }
151 
152     protected void updateEmailPageAdded(
153             ActionRequest actionRequest, PortletPreferences preferences)
154         throws Exception {
155 
156         boolean emailPageAddedEnabled = ParamUtil.getBoolean(
157             actionRequest, "emailPageAddedEnabled");
158         String emailPageAddedSubjectPrefix = ParamUtil.getString(
159             actionRequest, "emailPageAddedSubjectPrefix");
160         String emailPageAddedBody = ParamUtil.getString(
161             actionRequest, "emailPageAddedBody");
162         String emailPageAddedSignature = ParamUtil.getString(
163             actionRequest, "emailPageAddedSignature");
164 
165         if (Validator.isNull(emailPageAddedSubjectPrefix)) {
166             SessionErrors.add(actionRequest, "emailPageAddedSubjectPrefix");
167         }
168         else if (Validator.isNull(emailPageAddedBody)) {
169             SessionErrors.add(actionRequest, "emailPageAddedBody");
170         }
171         else {
172             preferences.setValue(
173                 "email-page-added-enabled",
174                 String.valueOf(emailPageAddedEnabled));
175             preferences.setValue(
176                 "email-page-added-subject-prefix", emailPageAddedSubjectPrefix);
177             preferences.setValue("email-page-added-body", emailPageAddedBody);
178             preferences.setValue(
179                 "email-page-added-signature", emailPageAddedSignature);
180         }
181     }
182 
183     protected void updateEmailPageUpdated(
184             ActionRequest actionRequest, PortletPreferences preferences)
185         throws Exception {
186 
187         boolean emailPageUpdatedEnabled = ParamUtil.getBoolean(
188             actionRequest, "emailPageUpdatedEnabled");
189         String emailPageUpdatedSubjectPrefix = ParamUtil.getString(
190             actionRequest, "emailPageUpdatedSubjectPrefix");
191         String emailPageUpdatedBody = ParamUtil.getString(
192             actionRequest, "emailPageUpdatedBody");
193         String emailPageUpdatedSignature = ParamUtil.getString(
194             actionRequest, "emailPageUpdatedSignature");
195 
196         if (Validator.isNull(emailPageUpdatedSubjectPrefix)) {
197             SessionErrors.add(actionRequest, "emailPageUpdatedSubjectPrefix");
198         }
199         else if (Validator.isNull(emailPageUpdatedBody)) {
200             SessionErrors.add(actionRequest, "emailPageUpdatedBody");
201         }
202         else {
203             preferences.setValue(
204                 "email-page-updated-enabled",
205                 String.valueOf(emailPageUpdatedEnabled));
206             preferences.setValue(
207                 "email-page-updated-subject-prefix",
208                 emailPageUpdatedSubjectPrefix);
209             preferences.setValue(
210                 "email-page-updated-body", emailPageUpdatedBody);
211             preferences.setValue(
212                 "email-page-updated-signature", emailPageUpdatedSignature);
213         }
214     }
215 
216     protected void updateRSS(
217             ActionRequest actionRequest, PortletPreferences preferences)
218         throws Exception {
219 
220         int rssDelta = ParamUtil.getInteger(actionRequest, "rssDelta");
221         String rssDisplayStyle = ParamUtil.getString(
222             actionRequest, "rssDisplayStyle");
223 
224         preferences.setValue("rss-delta", String.valueOf(rssDelta));
225         preferences.setValue("rss-display-style", rssDisplayStyle);
226     }
227 
228 }