1   /**
2    * Copyright (c) 2000-2008 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.messageboards.action;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.portlet.ConfigurationAction;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.LocaleUtil;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portlet.PortletPreferencesFactoryUtil;
34  import com.liferay.util.LocalizationUtil;
35  import com.liferay.util.servlet.SessionErrors;
36  import com.liferay.util.servlet.SessionMessages;
37  
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  import java.util.List;
41  import java.util.Locale;
42  import java.util.Map;
43  import java.util.TreeMap;
44  
45  import javax.portlet.ActionRequest;
46  import javax.portlet.ActionResponse;
47  import javax.portlet.PortletConfig;
48  import javax.portlet.PortletPreferences;
49  import javax.portlet.RenderRequest;
50  import javax.portlet.RenderResponse;
51  
52  /**
53   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class ConfigurationActionImpl implements ConfigurationAction {
59  
60      public void processAction(
61              PortletConfig config, ActionRequest req, ActionResponse res)
62          throws Exception {
63  
64          String cmd = ParamUtil.getString(req, Constants.CMD);
65  
66          if (!cmd.equals(Constants.UPDATE)) {
67              return;
68          }
69  
70          String portletResource = ParamUtil.getString(
71              req, "portletResource");
72  
73          PortletPreferences prefs =
74              PortletPreferencesFactoryUtil.getPortletSetup(
75                  req, portletResource, false, true);
76  
77          String tabs2 = ParamUtil.getString(req, "tabs2");
78  
79          if (tabs2.equals("anonymous-posting")) {
80              updateAnonymousPosting(req, prefs);
81          }
82          else if (tabs2.equals("email-from")) {
83              updateEmailFrom(req, prefs);
84          }
85          else if (tabs2.equals("message-added-email")) {
86              updateEmailMessageAdded(req, prefs);
87          }
88          else if (tabs2.equals("message-updated-email")) {
89              updateEmailMessageUpdated(req, prefs);
90          }
91          else if (tabs2.equals("rss")) {
92              updateRSS(req, prefs);
93          }
94          else if (tabs2.equals("thread-priorities")) {
95              updateThreadPriorities(req, prefs);
96          }
97          else if (tabs2.equals("user-ranks")) {
98              updateUserRanks(req, prefs);
99          }
100 
101         if (SessionErrors.isEmpty(req)) {
102             prefs.store();
103 
104             SessionMessages.add(req, config.getPortletName() + ".doConfigure");
105         }
106     }
107 
108     public String render(
109             PortletConfig config, RenderRequest req, RenderResponse res)
110         throws Exception {
111 
112         return "/html/portlet/message_boards/configuration.jsp";
113     }
114 
115     protected void updateAnonymousPosting(
116             ActionRequest req, PortletPreferences prefs)
117         throws Exception {
118 
119         String allowAnonymousPosting = ParamUtil.getString(
120             req, "allowAnonymousPosting");
121 
122         prefs.setValue("allow-anonymous-posting", allowAnonymousPosting);
123     }
124 
125     protected void updateEmailFrom(ActionRequest req, PortletPreferences prefs)
126         throws Exception {
127 
128         String emailFromName = ParamUtil.getString(req, "emailFromName");
129         String emailFromAddress = ParamUtil.getString(req, "emailFromAddress");
130 
131         if (Validator.isNull(emailFromName)) {
132             SessionErrors.add(req, "emailFromName");
133         }
134         else if (!Validator.isEmailAddress(emailFromAddress) &&
135                  !Validator.isVariableTerm(emailFromAddress)) {
136 
137             SessionErrors.add(req, "emailFromAddress");
138         }
139         else {
140             prefs.setValue("email-from-name", emailFromName);
141             prefs.setValue("email-from-address", emailFromAddress);
142         }
143     }
144 
145     protected void updateEmailMessageAdded(
146             ActionRequest req, PortletPreferences prefs)
147         throws Exception {
148 
149         String emailMessageAddedEnabled = ParamUtil.getString(
150             req, "emailMessageAddedEnabled");
151         String emailMessageAddedSubjectPrefix = ParamUtil.getString(
152             req, "emailMessageAddedSubjectPrefix");
153         String emailMessageAddedBody = ParamUtil.getString(
154             req, "emailMessageAddedBody");
155         String emailMessageAddedSignature = ParamUtil.getString(
156             req, "emailMessageAddedSignature");
157 
158         if (Validator.isNull(emailMessageAddedSubjectPrefix)) {
159             SessionErrors.add(req, "emailMessageAddedSubjectPrefix");
160         }
161         else if (Validator.isNull(emailMessageAddedBody)) {
162             SessionErrors.add(req, "emailMessageAddedBody");
163         }
164         else {
165             prefs.setValue(
166                 "email-message-added-enabled", emailMessageAddedEnabled);
167             prefs.setValue(
168                 "email-message-added-subject-prefix",
169                 emailMessageAddedSubjectPrefix);
170             prefs.setValue("email-message-added-body", emailMessageAddedBody);
171             prefs.setValue(
172                 "email-message-added-signature", emailMessageAddedSignature);
173         }
174     }
175 
176     protected void updateEmailMessageUpdated(
177             ActionRequest req, PortletPreferences prefs)
178         throws Exception {
179 
180         String emailMessageUpdatedEnabled = ParamUtil.getString(
181             req, "emailMessageUpdatedEnabled");
182         String emailMessageUpdatedSubjectPrefix = ParamUtil.getString(
183             req, "emailMessageUpdatedSubjectPrefix");
184         String emailMessageUpdatedBody = ParamUtil.getString(
185             req, "emailMessageUpdatedBody");
186         String emailMessageUpdatedSignature = ParamUtil.getString(
187             req, "emailMessageUpdatedSignature");
188 
189         if (Validator.isNull(emailMessageUpdatedSubjectPrefix)) {
190             SessionErrors.add(req, "emailMessageUpdatedSubjectPrefix");
191         }
192         else if (Validator.isNull(emailMessageUpdatedBody)) {
193             SessionErrors.add(req, "emailMessageUpdatedBody");
194         }
195         else {
196             prefs.setValue(
197                 "email-message-updated-enabled", emailMessageUpdatedEnabled);
198             prefs.setValue(
199                 "email-message-updated-subject-prefix",
200                 emailMessageUpdatedSubjectPrefix);
201             prefs.setValue(
202                 "email-message-updated-body", emailMessageUpdatedBody);
203             prefs.setValue(
204                 "email-message-updated-signature",
205                 emailMessageUpdatedSignature);
206         }
207     }
208 
209     protected void updateRSS(ActionRequest req, PortletPreferences prefs)
210         throws Exception {
211 
212         int rssDelta = ParamUtil.getInteger(req, "rssDelta");
213         String rssDisplayStyle = ParamUtil.getString(req, "rssDisplayStyle");
214         String rssFormat = ParamUtil.getString(req, "rssFormat");
215 
216         prefs.setValue("rss-delta", String.valueOf(rssDelta));
217         prefs.setValue("rss-display-style", rssDisplayStyle);
218         prefs.setValue("rss-format", rssFormat);
219     }
220 
221     protected void updateThreadPriorities(
222             ActionRequest req, PortletPreferences prefs)
223         throws Exception {
224 
225         Locale[] locales = LanguageUtil.getAvailableLocales();
226 
227         for (int i = 0; i < locales.length; i++) {
228             String languageId = LocaleUtil.toLanguageId(locales[i]);
229 
230             List priorities = new ArrayList();
231 
232             for (int j = 0; j < 10; j++) {
233                 String name = ParamUtil.getString(
234                     req, "priorityName" + j + "_" + languageId);
235                 String image = ParamUtil.getString(
236                     req, "priorityImage" + j + "_" + languageId);
237                 double value = ParamUtil.getDouble(
238                     req, "priorityValue" + j + "_" + languageId);
239 
240                 if (Validator.isNotNull(name) || Validator.isNotNull(image) ||
241                     (value != 0.0)) {
242 
243                     priorities.add(
244                         name + StringPool.COMMA + image + StringPool.COMMA +
245                             value);
246                 }
247             }
248 
249             LocalizationUtil.setPrefsValues(
250                 prefs, "priorities", languageId,
251                 (String[])priorities.toArray(new String[0]));
252         }
253     }
254 
255     protected void updateUserRanks(ActionRequest req, PortletPreferences prefs)
256         throws Exception {
257 
258         Locale[] locales = LanguageUtil.getAvailableLocales();
259 
260         for (int i = 0; i < locales.length; i++) {
261             String languageId = LocaleUtil.toLanguageId(locales[i]);
262 
263             String[] ranks = StringUtil.split(
264                 ParamUtil.getString(req, "ranks_" + languageId),
265                 StringPool.NEW_LINE);
266 
267             Map map = new TreeMap();
268 
269             for (int j = 0; j < ranks.length; j++) {
270                 String[] kvp = StringUtil.split(ranks[j], StringPool.EQUAL);
271 
272                 String kvpName = kvp[0];
273                 String kvpValue = kvp[1];
274 
275                 map.put(kvpValue, kvpName);
276             }
277 
278             ranks = new String[map.size()];
279 
280             int count = 0;
281 
282             Iterator itr = map.entrySet().iterator();
283 
284             while (itr.hasNext()) {
285                 Map.Entry entry = (Map.Entry)itr.next();
286 
287                 String kvpValue = (String)entry.getKey();
288                 String kvpName = (String)entry.getValue();
289 
290                 ranks[count++] = kvpName + StringPool.EQUAL + kvpValue;
291             }
292 
293             LocalizationUtil.setPrefsValues(prefs, "ranks", languageId, ranks);
294         }
295     }
296 
297 }