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.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 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 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             preferences.setValue(
117                 "enable-comments", String.valueOf(enableComments));
118             preferences.setValue(
119                 "enable-comment-ratings", String.valueOf(enableCommentRatings));
120             preferences.setValue("visible-nodes", visibleNodes);
121             preferences.setValue("hidden-nodes", hiddenNodes);
122         }
123     }
124 
125     protected void updateEmailFrom(
126             ActionRequest actionRequest, PortletPreferences preferences)
127         throws Exception {
128 
129         String emailFromName = ParamUtil.getString(
130             actionRequest, "emailFromName");
131         String emailFromAddress = ParamUtil.getString(
132             actionRequest, "emailFromAddress");
133 
134         if (Validator.isNull(emailFromName)) {
135             SessionErrors.add(actionRequest, "emailFromName");
136         }
137         else if (!Validator.isEmailAddress(emailFromAddress) &&
138                  !Validator.isVariableTerm(emailFromAddress)) {
139 
140             SessionErrors.add(actionRequest, "emailFromAddress");
141         }
142         else {
143             preferences.setValue("email-from-name", emailFromName);
144             preferences.setValue("email-from-address", emailFromAddress);
145         }
146     }
147 
148     protected void updateEmailPageAdded(
149             ActionRequest actionRequest, PortletPreferences preferences)
150         throws Exception {
151 
152         boolean emailPageAddedEnabled = ParamUtil.getBoolean(
153             actionRequest, "emailPageAddedEnabled");
154         String emailPageAddedSubjectPrefix = ParamUtil.getString(
155             actionRequest, "emailPageAddedSubjectPrefix");
156         String emailPageAddedBody = ParamUtil.getString(
157             actionRequest, "emailPageAddedBody");
158         String emailPageAddedSignature = ParamUtil.getString(
159             actionRequest, "emailPageAddedSignature");
160 
161         if (Validator.isNull(emailPageAddedSubjectPrefix)) {
162             SessionErrors.add(actionRequest, "emailPageAddedSubjectPrefix");
163         }
164         else if (Validator.isNull(emailPageAddedBody)) {
165             SessionErrors.add(actionRequest, "emailPageAddedBody");
166         }
167         else {
168             preferences.setValue(
169                 "email-page-added-enabled",
170                 String.valueOf(emailPageAddedEnabled));
171             preferences.setValue(
172                 "email-page-added-subject-prefix", emailPageAddedSubjectPrefix);
173             preferences.setValue("email-page-added-body", emailPageAddedBody);
174             preferences.setValue(
175                 "email-page-added-signature", emailPageAddedSignature);
176         }
177     }
178 
179     protected void updateEmailPageUpdated(
180             ActionRequest actionRequest, PortletPreferences preferences)
181         throws Exception {
182 
183         boolean emailPageUpdatedEnabled = ParamUtil.getBoolean(
184             actionRequest, "emailPageUpdatedEnabled");
185         String emailPageUpdatedSubjectPrefix = ParamUtil.getString(
186             actionRequest, "emailPageUpdatedSubjectPrefix");
187         String emailPageUpdatedBody = ParamUtil.getString(
188             actionRequest, "emailPageUpdatedBody");
189         String emailPageUpdatedSignature = ParamUtil.getString(
190             actionRequest, "emailPageUpdatedSignature");
191 
192         if (Validator.isNull(emailPageUpdatedSubjectPrefix)) {
193             SessionErrors.add(actionRequest, "emailPageUpdatedSubjectPrefix");
194         }
195         else if (Validator.isNull(emailPageUpdatedBody)) {
196             SessionErrors.add(actionRequest, "emailPageUpdatedBody");
197         }
198         else {
199             preferences.setValue(
200                 "email-page-updated-enabled",
201                 String.valueOf(emailPageUpdatedEnabled));
202             preferences.setValue(
203                 "email-page-updated-subject-prefix",
204                 emailPageUpdatedSubjectPrefix);
205             preferences.setValue(
206                 "email-page-updated-body", emailPageUpdatedBody);
207             preferences.setValue(
208                 "email-page-updated-signature", emailPageUpdatedSignature);
209         }
210     }
211 
212     protected void updateRSS(
213             ActionRequest actionRequest, PortletPreferences preferences)
214         throws Exception {
215 
216         int rssDelta = ParamUtil.getInteger(actionRequest, "rssDelta");
217         String rssDisplayStyle = ParamUtil.getString(
218             actionRequest, "rssDisplayStyle");
219 
220         preferences.setValue("rss-delta", String.valueOf(rssDelta));
221         preferences.setValue("rss-display-style", rssDisplayStyle);
222     }
223 
224 }