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