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