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