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 updateThreadPriorities(ActionRequest actionRequest)
072                    throws Exception {
073    
074                    Locale[] locales = LanguageUtil.getAvailableLocales();
075    
076                    for (int i = 0; i < locales.length; i++) {
077                            String languageId = LocaleUtil.toLanguageId(locales[i]);
078    
079                            List<String> priorities = new ArrayList<String>();
080    
081                            for (int j = 0; j < 10; j++) {
082                                    String name = ParamUtil.getString(
083                                            actionRequest, "priorityName" + j + "_" + languageId);
084                                    String image = ParamUtil.getString(
085                                            actionRequest, "priorityImage" + j + "_" + languageId);
086                                    double value = ParamUtil.getDouble(
087                                            actionRequest, "priorityValue" + j + "_" + languageId);
088    
089                                    if (Validator.isNotNull(name) || Validator.isNotNull(image) ||
090                                            (value != 0.0)) {
091    
092                                            priorities.add(
093                                                    name + StringPool.COMMA + image + StringPool.COMMA +
094                                                            value);
095                                    }
096                            }
097    
098                            String preferenceName = LocalizationUtil.getPreferencesKey(
099                                    "priorities", languageId);
100    
101                            setPreference(
102                                    actionRequest, preferenceName,
103                                    priorities.toArray(new String[priorities.size()]));
104                    }
105            }
106    
107            protected void updateUserRanks(ActionRequest actionRequest)
108                    throws Exception {
109    
110                    Locale[] locales = LanguageUtil.getAvailableLocales();
111    
112                    for (int i = 0; i < locales.length; i++) {
113                            String languageId = LocaleUtil.toLanguageId(locales[i]);
114    
115                            String[] ranks = StringUtil.splitLines(
116                                    ParamUtil.getString(actionRequest, "ranks_" + languageId));
117    
118                            Map<String, String> map = new TreeMap<String, String>();
119    
120                            for (int j = 0; j < ranks.length; j++) {
121                                    String[] kvp = StringUtil.split(ranks[j], CharPool.EQUAL);
122    
123                                    String kvpName = kvp[0];
124                                    String kvpValue = kvp[1];
125    
126                                    map.put(kvpValue, kvpName);
127                            }
128    
129                            ranks = new String[map.size()];
130    
131                            int count = 0;
132    
133                            Iterator<Map.Entry<String, String>> itr = map.entrySet().iterator();
134    
135                            while (itr.hasNext()) {
136                                    Map.Entry<String, String> entry = itr.next();
137    
138                                    String kvpValue = entry.getKey();
139                                    String kvpName = entry.getValue();
140    
141                                    ranks[count++] = kvpName + StringPool.EQUAL + kvpValue;
142                            }
143    
144                            String preferenceName = LocalizationUtil.getPreferencesKey(
145                                    "ranks", languageId);
146    
147                            setPreference(actionRequest, preferenceName, ranks);
148                    }
149            }
150    
151            protected void validateEmailFrom(ActionRequest actionRequest)
152                    throws Exception {
153    
154                    String emailFromName = getParameter(actionRequest, "emailFromName");
155                    String emailFromAddress = getParameter(
156                            actionRequest, "emailFromAddress");
157    
158                    if (Validator.isNull(emailFromName)) {
159                            SessionErrors.add(actionRequest, "emailFromName");
160                    }
161                    else if (!Validator.isEmailAddress(emailFromAddress) &&
162                                     !Validator.isVariableTerm(emailFromAddress)) {
163    
164                            SessionErrors.add(actionRequest, "emailFromAddress");
165                    }
166            }
167    
168            protected void validateEmailMessageAdded(ActionRequest actionRequest)
169                    throws Exception {
170    
171                    String emailMessageAddedSubjectPrefix = getParameter(
172                            actionRequest, "emailMessageAddedSubjectPrefix");
173                    String emailMessageAddedBody = getParameter(
174                            actionRequest, "emailMessageAddedBody");
175    
176                    if (Validator.isNull(emailMessageAddedSubjectPrefix)) {
177                            SessionErrors.add(actionRequest, "emailMessageAddedSubjectPrefix");
178                    }
179                    else if (Validator.isNull(emailMessageAddedBody)) {
180                            SessionErrors.add(actionRequest, "emailMessageAddedBody");
181                    }
182            }
183    
184            protected void validateEmailMessageUpdated(ActionRequest actionRequest)
185                    throws Exception {
186    
187                    String emailMessageUpdatedSubjectPrefix = getParameter(
188                            actionRequest, "emailMessageUpdatedSubjectPrefix");
189                    String emailMessageUpdatedBody = getParameter(
190                            actionRequest, "emailMessageUpdatedBody");
191    
192                    if (Validator.isNull(emailMessageUpdatedSubjectPrefix)) {
193                            SessionErrors.add(
194                                    actionRequest, "emailMessageUpdatedSubjectPrefix");
195                    }
196                    else if (Validator.isNull(emailMessageUpdatedBody)) {
197                            SessionErrors.add(actionRequest, "emailMessageUpdatedBody");
198                    }
199            }
200    
201    }