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.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 preferences =
75              PortletPreferencesFactoryUtil.getPortletSetup(
76                  actionRequest, portletResource);
77  
78          String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
79  
80          if (tabs2.equals("anonymous-posting")) {
81              updateAnonymousPosting(actionRequest, preferences);
82          }
83          else if (tabs2.equals("email-from")) {
84              updateEmailFrom(actionRequest, preferences);
85          }
86          else if (tabs2.equals("message-added-email")) {
87              updateEmailMessageAdded(actionRequest, preferences);
88          }
89          else if (tabs2.equals("message-updated-email")) {
90              updateEmailMessageUpdated(actionRequest, preferences);
91          }
92          else if (tabs2.equals("ratings")) {
93              updateRatings(actionRequest, preferences);
94          }
95          else if (tabs2.equals("rss")) {
96              updateRSS(actionRequest, preferences);
97          }
98          else if (tabs2.equals("thread-priorities")) {
99              updateThreadPriorities(actionRequest, preferences);
100         }
101         else if (tabs2.equals("user-ranks")) {
102             updateUserRanks(actionRequest, preferences);
103         }
104 
105         if (SessionErrors.isEmpty(actionRequest)) {
106             preferences.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 preferences)
123         throws Exception {
124 
125         String allowAnonymousPosting = ParamUtil.getString(
126             actionRequest, "allowAnonymousPosting");
127 
128         preferences.setValue("allow-anonymous-posting", allowAnonymousPosting);
129     }
130 
131     protected void updateEmailFrom(
132             ActionRequest actionRequest, PortletPreferences preferences)
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             preferences.setValue("email-from-name", emailFromName);
152             preferences.setValue("email-from-address", emailFromAddress);
153             preferences.setValue(
154                 "email-html-format", String.valueOf(emailHtmlFormat));
155         }
156     }
157 
158     protected void updateEmailMessageAdded(
159             ActionRequest actionRequest, PortletPreferences preferences)
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             preferences.setValue(
179                 "email-message-added-enabled",
180                 String.valueOf(emailMessageAddedEnabled));
181             preferences.setValue(
182                 "email-message-added-subject-prefix",
183                 emailMessageAddedSubjectPrefix);
184             preferences.setValue(
185                 "email-message-added-body", emailMessageAddedBody);
186             preferences.setValue(
187                 "email-message-added-signature", emailMessageAddedSignature);
188         }
189     }
190 
191     protected void updateEmailMessageUpdated(
192             ActionRequest actionRequest, PortletPreferences preferences)
193         throws Exception {
194 
195         boolean emailMessageUpdatedEnabled = ParamUtil.getBoolean(
196             actionRequest, "emailMessageUpdatedEnabled");
197         String emailMessageUpdatedSubjectPrefix = ParamUtil.getString(
198             actionRequest, "emailMessageUpdatedSubjectPrefix");
199         String emailMessageUpdatedBody = ParamUtil.getString(
200             actionRequest, "emailMessageUpdatedBody");
201         String emailMessageUpdatedSignature = ParamUtil.getString(
202             actionRequest, "emailMessageUpdatedSignature");
203 
204         if (Validator.isNull(emailMessageUpdatedSubjectPrefix)) {
205             SessionErrors.add(
206                 actionRequest, "emailMessageUpdatedSubjectPrefix");
207         }
208         else if (Validator.isNull(emailMessageUpdatedBody)) {
209             SessionErrors.add(actionRequest, "emailMessageUpdatedBody");
210         }
211         else {
212             preferences.setValue(
213                 "email-message-updated-enabled",
214                 String.valueOf(emailMessageUpdatedEnabled));
215             preferences.setValue(
216                 "email-message-updated-subject-prefix",
217                 emailMessageUpdatedSubjectPrefix);
218             preferences.setValue(
219                 "email-message-updated-body", emailMessageUpdatedBody);
220             preferences.setValue(
221                 "email-message-updated-signature",
222                 emailMessageUpdatedSignature);
223         }
224     }
225 
226     protected void updateRatings(
227             ActionRequest actionRequest, PortletPreferences preferences)
228         throws Exception {
229 
230         boolean enableMessageRatings = ParamUtil.getBoolean(
231             actionRequest, "enableMessageRatings");
232 
233         preferences.setValue(
234             "enable-message-ratings", String.valueOf(enableMessageRatings));
235     }
236 
237     protected void updateRSS(
238             ActionRequest actionRequest, PortletPreferences preferences)
239         throws Exception {
240 
241         int rssDelta = ParamUtil.getInteger(actionRequest, "rssDelta");
242         String rssDisplayStyle = ParamUtil.getString(
243             actionRequest, "rssDisplayStyle");
244         String rssFormat = ParamUtil.getString(actionRequest, "rssFormat");
245 
246         preferences.setValue("rss-delta", String.valueOf(rssDelta));
247         preferences.setValue("rss-display-style", rssDisplayStyle);
248         preferences.setValue("rss-format", rssFormat);
249     }
250 
251     protected void updateThreadPriorities(
252             ActionRequest actionRequest, PortletPreferences preferences)
253         throws Exception {
254 
255         Locale[] locales = LanguageUtil.getAvailableLocales();
256 
257         for (int i = 0; i < locales.length; i++) {
258             String languageId = LocaleUtil.toLanguageId(locales[i]);
259 
260             List<String> priorities = new ArrayList<String>();
261 
262             for (int j = 0; j < 10; j++) {
263                 String name = ParamUtil.getString(
264                     actionRequest, "priorityName" + j + "_" + languageId);
265                 String image = ParamUtil.getString(
266                     actionRequest, "priorityImage" + j + "_" + languageId);
267                 double value = ParamUtil.getDouble(
268                     actionRequest, "priorityValue" + j + "_" + languageId);
269 
270                 if (Validator.isNotNull(name) || Validator.isNotNull(image) ||
271                     (value != 0.0)) {
272 
273                     priorities.add(
274                         name + StringPool.COMMA + image + StringPool.COMMA +
275                             value);
276                 }
277             }
278 
279             LocalizationUtil.setPreferencesValues(
280                 preferences, "priorities", languageId,
281                 priorities.toArray(new String[priorities.size()]));
282         }
283     }
284 
285     protected void updateUserRanks(
286             ActionRequest actionRequest, PortletPreferences preferences)
287         throws Exception {
288 
289         Locale[] locales = LanguageUtil.getAvailableLocales();
290 
291         for (int i = 0; i < locales.length; i++) {
292             String languageId = LocaleUtil.toLanguageId(locales[i]);
293 
294             String[] ranks = StringUtil.split(
295                 ParamUtil.getString(actionRequest, "ranks_" + languageId),
296                 StringPool.NEW_LINE);
297 
298             Map<String, String> map = new TreeMap<String, String>();
299 
300             for (int j = 0; j < ranks.length; j++) {
301                 String[] kvp = StringUtil.split(ranks[j], StringPool.EQUAL);
302 
303                 String kvpName = kvp[0];
304                 String kvpValue = kvp[1];
305 
306                 map.put(kvpValue, kvpName);
307             }
308 
309             ranks = new String[map.size()];
310 
311             int count = 0;
312 
313             Iterator<Map.Entry<String, String>> itr =
314                 map.entrySet().iterator();
315 
316             while (itr.hasNext()) {
317                 Map.Entry<String, String> entry = itr.next();
318 
319                 String kvpValue = entry.getKey();
320                 String kvpName = entry.getValue();
321 
322                 ranks[count++] = kvpName + StringPool.EQUAL + kvpValue;
323             }
324 
325             LocalizationUtil.setPreferencesValues(
326                 preferences, "ranks", languageId, ranks);
327         }
328     }
329 
330 }