001    /**
002     * Copyright (c) 2000-2012 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.DefaultConfigurationAction;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.util.CharPool;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.LocalizationUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    
028    import java.util.ArrayList;
029    import java.util.Iterator;
030    import java.util.List;
031    import java.util.Locale;
032    import java.util.Map;
033    import java.util.TreeMap;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.PortletConfig;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class ConfigurationActionImpl extends DefaultConfigurationAction {
043    
044            @Override
045            public void processAction(
046                            PortletConfig portletConfig, ActionRequest actionRequest,
047                            ActionResponse actionResponse)
048                    throws Exception {
049    
050                    String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
051    
052                    if (tabs2.equals("email-from")) {
053                            validateEmailFrom(actionRequest);
054                    }
055                    else if (tabs2.equals("message-added-email")) {
056                            validateEmailMessageAdded(actionRequest);
057                    }
058                    else if (tabs2.equals("message-updated-email")) {
059                            validateEmailMessageUpdated(actionRequest);
060                    }
061                    else if (tabs2.equals("thread-priorities")) {
062                            updateThreadPriorities(actionRequest);
063                    }
064                    else if (tabs2.equals("user-ranks")) {
065                            updateUserRanks(actionRequest);
066                    }
067    
068                    super.processAction(portletConfig, actionRequest, actionResponse);
069            }
070    
071            protected void validateEmailFrom(ActionRequest actionRequest)
072                    throws Exception {
073    
074                    String emailFromName = getParameter(actionRequest, "emailFromName");
075                    String emailFromAddress = getParameter(
076                            actionRequest, "emailFromAddress");
077    
078                    if (Validator.isNull(emailFromName)) {
079                            SessionErrors.add(actionRequest, "emailFromName");
080                    }
081                    else if (!Validator.isEmailAddress(emailFromAddress) &&
082                                     !Validator.isVariableTerm(emailFromAddress)) {
083    
084                            SessionErrors.add(actionRequest, "emailFromAddress");
085                    }
086            }
087    
088            protected void validateEmailMessageAdded(ActionRequest actionRequest)
089                    throws Exception {
090    
091                    String emailMessageAddedSubjectPrefix = getParameter(
092                            actionRequest, "emailMessageAddedSubjectPrefix");
093                    String emailMessageAddedBody = getParameter(
094                            actionRequest, "emailMessageAddedBody");
095    
096                    if (Validator.isNull(emailMessageAddedSubjectPrefix)) {
097                            SessionErrors.add(actionRequest, "emailMessageAddedSubjectPrefix");
098                    }
099                    else if (Validator.isNull(emailMessageAddedBody)) {
100                            SessionErrors.add(actionRequest, "emailMessageAddedBody");
101                    }
102            }
103    
104            protected void validateEmailMessageUpdated(ActionRequest actionRequest)
105                    throws Exception {
106    
107                    String emailMessageUpdatedSubjectPrefix = getParameter(
108                            actionRequest, "emailMessageUpdatedSubjectPrefix");
109                    String emailMessageUpdatedBody = getParameter(
110                            actionRequest, "emailMessageUpdatedBody");
111    
112                    if (Validator.isNull(emailMessageUpdatedSubjectPrefix)) {
113                            SessionErrors.add(
114                                    actionRequest, "emailMessageUpdatedSubjectPrefix");
115                    }
116                    else if (Validator.isNull(emailMessageUpdatedBody)) {
117                            SessionErrors.add(actionRequest, "emailMessageUpdatedBody");
118                    }
119            }
120    
121            protected void updateThreadPriorities(ActionRequest actionRequest)
122                    throws Exception {
123    
124                    Locale[] locales = LanguageUtil.getAvailableLocales();
125    
126                    for (int i = 0; i < locales.length; i++) {
127                            String languageId = LocaleUtil.toLanguageId(locales[i]);
128    
129                            List<String> priorities = new ArrayList<String>();
130    
131                            for (int j = 0; j < 10; j++) {
132                                    String name = ParamUtil.getString(
133                                            actionRequest, "priorityName" + j + "_" + languageId);
134                                    String image = ParamUtil.getString(
135                                            actionRequest, "priorityImage" + j + "_" + languageId);
136                                    double value = ParamUtil.getDouble(
137                                            actionRequest, "priorityValue" + j + "_" + languageId);
138    
139                                    if (Validator.isNotNull(name) || Validator.isNotNull(image) ||
140                                            (value != 0.0)) {
141    
142                                            priorities.add(
143                                                    name + StringPool.COMMA + image + StringPool.COMMA +
144                                                            value);
145                                    }
146                            }
147    
148                            String preferenceName = LocalizationUtil.getPreferencesKey(
149                                    "priorities", languageId);
150    
151                            setPreference(
152                                    actionRequest, preferenceName,
153                                    priorities.toArray(new String[priorities.size()]));
154                    }
155            }
156    
157            protected void updateUserRanks(ActionRequest actionRequest)
158                    throws Exception {
159    
160                    Locale[] locales = LanguageUtil.getAvailableLocales();
161    
162                    for (int i = 0; i < locales.length; i++) {
163                            String languageId = LocaleUtil.toLanguageId(locales[i]);
164    
165                            String[] ranks = StringUtil.splitLines(
166                                    ParamUtil.getString(actionRequest, "ranks_" + languageId));
167    
168                            Map<String, String> map = new TreeMap<String, String>();
169    
170                            for (int j = 0; j < ranks.length; j++) {
171                                    String[] kvp = StringUtil.split(ranks[j], CharPool.EQUAL);
172    
173                                    String kvpName = kvp[0];
174                                    String kvpValue = kvp[1];
175    
176                                    map.put(kvpValue, kvpName);
177                            }
178    
179                            ranks = new String[map.size()];
180    
181                            int count = 0;
182    
183                            Iterator<Map.Entry<String, String>> itr = map.entrySet().iterator();
184    
185                            while (itr.hasNext()) {
186                                    Map.Entry<String, String> entry = itr.next();
187    
188                                    String kvpValue = entry.getKey();
189                                    String kvpName = entry.getValue();
190    
191                                    ranks[count++] = kvpName + StringPool.EQUAL + kvpValue;
192                            }
193    
194                            String preferenceName = LocalizationUtil.getPreferencesKey(
195                                    "ranks", languageId);
196    
197                            setPreference(actionRequest, preferenceName, ranks);
198                    }
199            }
200    
201    }