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