001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
075                            actionRequest, "emailFromName");
076                    String emailFromAddress = getParameter(
077                            actionRequest, "emailFromAddress");
078    
079                    if (Validator.isNull(emailFromName)) {
080                            SessionErrors.add(actionRequest, "emailFromName");
081                    }
082                    else if (!Validator.isEmailAddress(emailFromAddress) &&
083                                     !Validator.isVariableTerm(emailFromAddress)) {
084    
085                            SessionErrors.add(actionRequest, "emailFromAddress");
086                    }
087            }
088    
089            protected void validateEmailMessageAdded(ActionRequest actionRequest)
090                    throws Exception {
091    
092                    String emailMessageAddedSubjectPrefix = getParameter(
093                            actionRequest, "emailMessageAddedSubjectPrefix");
094                    String emailMessageAddedBody = getParameter(
095                            actionRequest, "emailMessageAddedBody");
096    
097                    if (Validator.isNull(emailMessageAddedSubjectPrefix)) {
098                            SessionErrors.add(actionRequest, "emailMessageAddedSubjectPrefix");
099                    }
100                    else if (Validator.isNull(emailMessageAddedBody)) {
101                            SessionErrors.add(actionRequest, "emailMessageAddedBody");
102                    }
103            }
104    
105            protected void validateEmailMessageUpdated(ActionRequest actionRequest)
106                    throws Exception {
107    
108                    String emailMessageUpdatedSubjectPrefix = getParameter(
109                            actionRequest, "emailMessageUpdatedSubjectPrefix");
110                    String emailMessageUpdatedBody = getParameter(
111                            actionRequest, "emailMessageUpdatedBody");
112    
113                    if (Validator.isNull(emailMessageUpdatedSubjectPrefix)) {
114                            SessionErrors.add(
115                                    actionRequest, "emailMessageUpdatedSubjectPrefix");
116                    }
117                    else if (Validator.isNull(emailMessageUpdatedBody)) {
118                            SessionErrors.add(actionRequest, "emailMessageUpdatedBody");
119                    }
120            }
121    
122            protected void updateThreadPriorities(ActionRequest actionRequest)
123                    throws Exception {
124    
125                    Locale[] locales = LanguageUtil.getAvailableLocales();
126    
127                    for (int i = 0; i < locales.length; i++) {
128                            String languageId = LocaleUtil.toLanguageId(locales[i]);
129    
130                            List<String> priorities = new ArrayList<String>();
131    
132                            for (int j = 0; j < 10; j++) {
133                                    String name = ParamUtil.getString(
134                                            actionRequest, "priorityName" + j + "_" + languageId);
135                                    String image = ParamUtil.getString(
136                                            actionRequest, "priorityImage" + j + "_" + languageId);
137                                    double value = ParamUtil.getDouble(
138                                            actionRequest, "priorityValue" + j + "_" + languageId);
139    
140                                    if (Validator.isNotNull(name) || Validator.isNotNull(image) ||
141                                            (value != 0.0)) {
142    
143                                            priorities.add(
144                                                    name + StringPool.COMMA + image + StringPool.COMMA +
145                                                            value);
146                                    }
147                            }
148    
149                            String preferenceName = LocalizationUtil.getPreferencesKey(
150                                    "priorities", languageId);
151    
152                            setPreference(
153                                    actionRequest, preferenceName,
154                                    priorities.toArray(new String[priorities.size()]));
155                    }
156            }
157    
158            protected void updateUserRanks(ActionRequest actionRequest)
159                    throws Exception {
160    
161                    Locale[] locales = LanguageUtil.getAvailableLocales();
162    
163                    for (int i = 0; i < locales.length; i++) {
164                            String languageId = LocaleUtil.toLanguageId(locales[i]);
165    
166                            String[] ranks = StringUtil.splitLines(
167                                    ParamUtil.getString(actionRequest, "ranks_" + languageId));
168    
169                            Map<String, String> map = new TreeMap<String, String>();
170    
171                            for (int j = 0; j < ranks.length; j++) {
172                                    String[] kvp = StringUtil.split(ranks[j], CharPool.EQUAL);
173    
174                                    String kvpName = kvp[0];
175                                    String kvpValue = kvp[1];
176    
177                                    map.put(kvpValue, kvpName);
178                            }
179    
180                            ranks = new String[map.size()];
181    
182                            int count = 0;
183    
184                            Iterator<Map.Entry<String, String>> itr =
185                                    map.entrySet().iterator();
186    
187                            while (itr.hasNext()) {
188                                    Map.Entry<String, String> entry = itr.next();
189    
190                                    String kvpValue = entry.getKey();
191                                    String kvpName = entry.getValue();
192    
193                                    ranks[count++] = kvpName + StringPool.EQUAL + kvpValue;
194                            }
195    
196                            String preferenceName = LocalizationUtil.getPreferencesKey(
197                                    "ranks", languageId);
198    
199                            setPreference(actionRequest, preferenceName, ranks);
200                    }
201            }
202    
203    }