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