001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.action;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.LocalizationUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portlet.PortletPreferencesFactoryUtil;
029    
030    import java.util.ArrayList;
031    import java.util.Iterator;
032    import java.util.List;
033    import java.util.Locale;
034    import java.util.Map;
035    import java.util.TreeMap;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.ActionResponse;
039    import javax.portlet.PortletConfig;
040    import javax.portlet.PortletPreferences;
041    import javax.portlet.RenderRequest;
042    import javax.portlet.RenderResponse;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     */
047    public class ConfigurationActionImpl extends BaseConfigurationAction {
048    
049            public void processAction(
050                            PortletConfig portletConfig, ActionRequest actionRequest,
051                            ActionResponse actionResponse)
052                    throws Exception {
053    
054                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
055    
056                    if (!cmd.equals(Constants.UPDATE)) {
057                            return;
058                    }
059    
060                    String portletResource = ParamUtil.getString(
061                            actionRequest, "portletResource");
062    
063                    PortletPreferences preferences =
064                            PortletPreferencesFactoryUtil.getPortletSetup(
065                                    actionRequest, portletResource);
066    
067                    String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
068    
069                    if (tabs2.equals("email-from")) {
070                            updateEmailFrom(actionRequest, preferences);
071                    }
072                    else if (tabs2.equals("general")) {
073                            updateGeneral(actionRequest, preferences);
074                    }
075                    else if (tabs2.equals("message-added-email")) {
076                            updateEmailMessageAdded(actionRequest, preferences);
077                    }
078                    else if (tabs2.equals("message-updated-email")) {
079                            updateEmailMessageUpdated(actionRequest, preferences);
080                    }
081                    else if (tabs2.equals("rss")) {
082                            updateRSS(actionRequest, preferences);
083                    }
084                    else if (tabs2.equals("thread-priorities")) {
085                            updateThreadPriorities(actionRequest, preferences);
086                    }
087                    else if (tabs2.equals("user-ranks")) {
088                            updateUserRanks(actionRequest, preferences);
089                    }
090    
091                    if (SessionErrors.isEmpty(actionRequest)) {
092                            preferences.store();
093    
094                            SessionMessages.add(
095                                    actionRequest, portletConfig.getPortletName() + ".doConfigure");
096                    }
097            }
098    
099            public String render(
100                            PortletConfig portletConfig, RenderRequest renderRequest,
101                            RenderResponse renderResponse)
102                    throws Exception {
103    
104                    return "/html/portlet/message_boards/configuration.jsp";
105            }
106    
107            protected void updateEmailFrom(
108                            ActionRequest actionRequest, PortletPreferences preferences)
109                    throws Exception {
110    
111                    String emailFromName = ParamUtil.getString(
112                            actionRequest, "emailFromName");
113                    String emailFromAddress = ParamUtil.getString(
114                            actionRequest, "emailFromAddress");
115                    boolean emailHtmlFormat = ParamUtil.getBoolean(
116                            actionRequest, "emailHtmlFormat");
117    
118                    if (Validator.isNull(emailFromName)) {
119                            SessionErrors.add(actionRequest, "emailFromName");
120                    }
121                    else if (!Validator.isEmailAddress(emailFromAddress) &&
122                                     !Validator.isVariableTerm(emailFromAddress)) {
123    
124                            SessionErrors.add(actionRequest, "emailFromAddress");
125                    }
126                    else {
127                            preferences.setValue("email-from-name", emailFromName);
128                            preferences.setValue("email-from-address", emailFromAddress);
129                            preferences.setValue(
130                                    "email-html-format", String.valueOf(emailHtmlFormat));
131                    }
132            }
133    
134            protected void updateEmailMessageAdded(
135                            ActionRequest actionRequest, PortletPreferences preferences)
136                    throws Exception {
137    
138                    boolean emailMessageAddedEnabled = ParamUtil.getBoolean(
139                            actionRequest, "emailMessageAddedEnabled");
140                    String emailMessageAddedSubjectPrefix = ParamUtil.getString(
141                            actionRequest, "emailMessageAddedSubjectPrefix");
142                    String emailMessageAddedBody = ParamUtil.getString(
143                            actionRequest, "emailMessageAddedBody");
144                    String emailMessageAddedSignature = ParamUtil.getString(
145                            actionRequest, "emailMessageAddedSignature");
146    
147                    if (Validator.isNull(emailMessageAddedSubjectPrefix)) {
148                            SessionErrors.add(actionRequest, "emailMessageAddedSubjectPrefix");
149                    }
150                    else if (Validator.isNull(emailMessageAddedBody)) {
151                            SessionErrors.add(actionRequest, "emailMessageAddedBody");
152                    }
153                    else {
154                            preferences.setValue(
155                                    "email-message-added-enabled",
156                                    String.valueOf(emailMessageAddedEnabled));
157                            preferences.setValue(
158                                    "email-message-added-subject-prefix",
159                                    emailMessageAddedSubjectPrefix);
160                            preferences.setValue(
161                                    "email-message-added-body", emailMessageAddedBody);
162                            preferences.setValue(
163                                    "email-message-added-signature", emailMessageAddedSignature);
164                    }
165            }
166    
167            protected void updateEmailMessageUpdated(
168                            ActionRequest actionRequest, PortletPreferences preferences)
169                    throws Exception {
170    
171                    boolean emailMessageUpdatedEnabled = ParamUtil.getBoolean(
172                            actionRequest, "emailMessageUpdatedEnabled");
173                    String emailMessageUpdatedSubjectPrefix = ParamUtil.getString(
174                            actionRequest, "emailMessageUpdatedSubjectPrefix");
175                    String emailMessageUpdatedBody = ParamUtil.getString(
176                            actionRequest, "emailMessageUpdatedBody");
177                    String emailMessageUpdatedSignature = ParamUtil.getString(
178                            actionRequest, "emailMessageUpdatedSignature");
179    
180                    if (Validator.isNull(emailMessageUpdatedSubjectPrefix)) {
181                            SessionErrors.add(
182                                    actionRequest, "emailMessageUpdatedSubjectPrefix");
183                    }
184                    else if (Validator.isNull(emailMessageUpdatedBody)) {
185                            SessionErrors.add(actionRequest, "emailMessageUpdatedBody");
186                    }
187                    else {
188                            preferences.setValue(
189                                    "email-message-updated-enabled",
190                                    String.valueOf(emailMessageUpdatedEnabled));
191                            preferences.setValue(
192                                    "email-message-updated-subject-prefix",
193                                    emailMessageUpdatedSubjectPrefix);
194                            preferences.setValue(
195                                    "email-message-updated-body", emailMessageUpdatedBody);
196                            preferences.setValue(
197                                    "email-message-updated-signature",
198                                    emailMessageUpdatedSignature);
199                    }
200            }
201    
202            protected void updateGeneral(
203                            ActionRequest actionRequest, PortletPreferences preferences)
204                    throws Exception {
205    
206                    String allowAnonymousPosting = ParamUtil.getString(
207                            actionRequest, "allowAnonymousPosting");
208                    String enableFlags = ParamUtil.getString(actionRequest, "enableFlags");
209                    boolean enableRatings = ParamUtil.getBoolean(
210                            actionRequest, "enableRatings");
211    
212                    preferences.setValue("allow-anonymous-posting", allowAnonymousPosting);
213                    preferences.setValue("enable-flags", enableFlags);
214                    preferences.setValue(
215                            "enable-message-ratings", String.valueOf(enableRatings));
216            }
217    
218            protected void updateRSS(
219                            ActionRequest actionRequest, PortletPreferences preferences)
220                    throws Exception {
221    
222                    int rssDelta = ParamUtil.getInteger(actionRequest, "rssDelta");
223                    String rssDisplayStyle = ParamUtil.getString(
224                            actionRequest, "rssDisplayStyle");
225                    String rssFormat = ParamUtil.getString(actionRequest, "rssFormat");
226    
227                    preferences.setValue("rss-delta", String.valueOf(rssDelta));
228                    preferences.setValue("rss-display-style", rssDisplayStyle);
229                    preferences.setValue("rss-format", rssFormat);
230            }
231    
232            protected void updateThreadPriorities(
233                            ActionRequest actionRequest, PortletPreferences preferences)
234                    throws Exception {
235    
236                    Locale[] locales = LanguageUtil.getAvailableLocales();
237    
238                    for (int i = 0; i < locales.length; i++) {
239                            String languageId = LocaleUtil.toLanguageId(locales[i]);
240    
241                            List<String> priorities = new ArrayList<String>();
242    
243                            for (int j = 0; j < 10; j++) {
244                                    String name = ParamUtil.getString(
245                                            actionRequest, "priorityName" + j + "_" + languageId);
246                                    String image = ParamUtil.getString(
247                                            actionRequest, "priorityImage" + j + "_" + languageId);
248                                    double value = ParamUtil.getDouble(
249                                            actionRequest, "priorityValue" + j + "_" + languageId);
250    
251                                    if (Validator.isNotNull(name) || Validator.isNotNull(image) ||
252                                            (value != 0.0)) {
253    
254                                            priorities.add(
255                                                    name + StringPool.COMMA + image + StringPool.COMMA +
256                                                            value);
257                                    }
258                            }
259    
260                            LocalizationUtil.setPreferencesValues(
261                                    preferences, "priorities", languageId,
262                                    priorities.toArray(new String[priorities.size()]));
263                    }
264            }
265    
266            protected void updateUserRanks(
267                            ActionRequest actionRequest, PortletPreferences preferences)
268                    throws Exception {
269    
270                    Locale[] locales = LanguageUtil.getAvailableLocales();
271    
272                    for (int i = 0; i < locales.length; i++) {
273                            String languageId = LocaleUtil.toLanguageId(locales[i]);
274    
275                            String[] ranks = StringUtil.split(
276                                    ParamUtil.getString(actionRequest, "ranks_" + languageId),
277                                    StringPool.NEW_LINE);
278    
279                            Map<String, String> map = new TreeMap<String, String>();
280    
281                            for (int j = 0; j < ranks.length; j++) {
282                                    String[] kvp = StringUtil.split(ranks[j], StringPool.EQUAL);
283    
284                                    String kvpName = kvp[0];
285                                    String kvpValue = kvp[1];
286    
287                                    map.put(kvpValue, kvpName);
288                            }
289    
290                            ranks = new String[map.size()];
291    
292                            int count = 0;
293    
294                            Iterator<Map.Entry<String, String>> itr =
295                                    map.entrySet().iterator();
296    
297                            while (itr.hasNext()) {
298                                    Map.Entry<String, String> entry = itr.next();
299    
300                                    String kvpValue = entry.getKey();
301                                    String kvpName = entry.getValue();
302    
303                                    ranks[count++] = kvpName + StringPool.EQUAL + kvpValue;
304                            }
305    
306                            LocalizationUtil.setPreferencesValues(
307                                    preferences, "ranks", languageId, ranks);
308                    }
309            }
310    
311    }