001    /**
002     * Copyright (c) 2000-present 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.SettingsConfigurationAction;
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.NaturalOrderStringComparator;
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.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.WebKeys;
030    
031    import java.util.ArrayList;
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    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class ConfigurationActionImpl extends SettingsConfigurationAction {
045    
046            @Override
047            public void processAction(
048                            PortletConfig portletConfig, ActionRequest actionRequest,
049                            ActionResponse actionResponse)
050                    throws Exception {
051    
052                    validateEmail(actionRequest, "emailMessageAdded");
053                    validateEmail(actionRequest, "emailMessageUpdated");
054                    validateEmailFrom(actionRequest);
055    
056                    super.processAction(portletConfig, actionRequest, actionResponse);
057            }
058    
059            protected boolean isValidUserRank(String rank) {
060                    if ((StringUtil.count(rank, StringPool.EQUAL) != 1) ||
061                            rank.startsWith(StringPool.EQUAL) ||
062                            rank.endsWith(StringPool.EQUAL)) {
063    
064                            return false;
065                    }
066    
067                    return true;
068            }
069    
070            @Override
071            protected void updateMultiValuedKeys(ActionRequest actionRequest) {
072                    super.updateMultiValuedKeys(actionRequest);
073    
074                    updateThreadPriorities(actionRequest);
075                    updateUserRanks(actionRequest);
076            }
077    
078            protected void updateThreadPriorities(ActionRequest actionRequest) {
079                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
080                            WebKeys.THEME_DISPLAY);
081    
082                    Locale[] locales = LanguageUtil.getAvailableLocales(
083                            themeDisplay.getSiteGroupId());
084    
085                    for (int i = 0; i < locales.length; i++) {
086                            String languageId = LocaleUtil.toLanguageId(locales[i]);
087    
088                            List<String> priorities = new ArrayList<String>();
089    
090                            for (int j = 0; j < 10; j++) {
091                                    String name = ParamUtil.getString(
092                                            actionRequest, "priorityName" + j + "_" + languageId);
093                                    String image = ParamUtil.getString(
094                                            actionRequest, "priorityImage" + j + "_" + languageId);
095                                    double value = ParamUtil.getDouble(
096                                            actionRequest, "priorityValue" + j + "_" + languageId);
097    
098                                    if (Validator.isNotNull(name) || Validator.isNotNull(image) ||
099                                            (value != 0.0)) {
100    
101                                            priorities.add(
102                                                    name + StringPool.PIPE + image + StringPool.PIPE +
103                                                            value);
104                                    }
105                            }
106    
107                            String preferenceName = LocalizationUtil.getLocalizedName(
108                                    "priorities", languageId);
109    
110                            setPreference(
111                                    actionRequest, preferenceName,
112                                    priorities.toArray(new String[priorities.size()]));
113                    }
114            }
115    
116            protected void updateUserRanks(ActionRequest actionRequest) {
117                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
118                            WebKeys.THEME_DISPLAY);
119    
120                    Locale[] locales = LanguageUtil.getAvailableLocales(
121                            themeDisplay.getSiteGroupId());
122    
123                    for (Locale locale : locales) {
124                            String languageId = LocaleUtil.toLanguageId(locale);
125    
126                            String[] ranks = StringUtil.splitLines(
127                                    ParamUtil.getString(actionRequest, "ranks_" + languageId));
128    
129                            Map<String, String> map = new TreeMap<String, String>(
130                                    new NaturalOrderStringComparator());
131    
132                            for (String rank : ranks) {
133                                    if (!isValidUserRank(rank)) {
134                                            SessionErrors.add(actionRequest, "userRank");
135    
136                                            return;
137                                    }
138    
139                                    String[] kvp = StringUtil.split(rank, CharPool.EQUAL);
140    
141                                    String kvpName = kvp[0];
142                                    String kvpValue = kvp[1];
143    
144                                    map.put(kvpValue, kvpName);
145                            }
146    
147                            ranks = new String[map.size()];
148    
149                            int count = 0;
150    
151                            for (Map.Entry<String, String> entry : map.entrySet()) {
152                                    String kvpValue = entry.getKey();
153                                    String kvpName = entry.getValue();
154    
155                                    ranks[count++] = kvpName + StringPool.EQUAL + kvpValue;
156                            }
157    
158                            String preferenceName = LocalizationUtil.getLocalizedName(
159                                    "ranks", languageId);
160    
161                            setPreference(actionRequest, preferenceName, ranks);
162                    }
163            }
164    
165    }