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