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