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