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.BaseJSPSettingsConfigurationAction;
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
045            extends BaseJSPSettingsConfigurationAction {
046    
047            @Override
048            public void processAction(
049                            PortletConfig portletConfig, ActionRequest actionRequest,
050                            ActionResponse actionResponse)
051                    throws Exception {
052    
053                    validateEmail(actionRequest, "emailMessageAdded");
054                    validateEmail(actionRequest, "emailMessageUpdated");
055                    validateEmailFrom(actionRequest);
056    
057                    super.processAction(portletConfig, actionRequest, actionResponse);
058            }
059    
060            protected boolean isValidUserRank(String rank) {
061                    if ((StringUtil.count(rank, StringPool.EQUAL) != 1) ||
062                            rank.startsWith(StringPool.EQUAL) ||
063                            rank.endsWith(StringPool.EQUAL)) {
064    
065                            return false;
066                    }
067    
068                    return true;
069            }
070    
071            @Override
072            protected void updateMultiValuedKeys(ActionRequest actionRequest) {
073                    super.updateMultiValuedKeys(actionRequest);
074    
075                    updateThreadPriorities(actionRequest);
076                    updateUserRanks(actionRequest);
077            }
078    
079            protected void updateThreadPriorities(ActionRequest actionRequest) {
080                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
081                            WebKeys.THEME_DISPLAY);
082    
083                    for (Locale locale : LanguageUtil.getAvailableLocales(
084                                    themeDisplay.getSiteGroupId())) {
085    
086                            String languageId = LocaleUtil.toLanguageId(locale);
087    
088                            List<String> priorities = new ArrayList<>();
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                    for (Locale locale : LanguageUtil.getAvailableLocales(
121                                    themeDisplay.getSiteGroupId())) {
122    
123                            String languageId = LocaleUtil.toLanguageId(locale);
124    
125                            String[] ranks = StringUtil.splitLines(
126                                    ParamUtil.getString(actionRequest, "ranks_" + languageId));
127    
128                            Map<String, String> map = new TreeMap<>(
129                                    new NaturalOrderStringComparator());
130    
131                            for (String rank : ranks) {
132                                    if (!isValidUserRank(rank)) {
133                                            SessionErrors.add(actionRequest, "userRank");
134    
135                                            return;
136                                    }
137    
138                                    String[] kvp = StringUtil.split(rank, CharPool.EQUAL);
139    
140                                    String kvpName = kvp[0];
141                                    String kvpValue = kvp[1];
142    
143                                    map.put(kvpValue, kvpName);
144                            }
145    
146                            ranks = new String[map.size()];
147    
148                            int count = 0;
149    
150                            for (Map.Entry<String, String> entry : map.entrySet()) {
151                                    String kvpValue = entry.getKey();
152                                    String kvpName = entry.getValue();
153    
154                                    ranks[count++] = kvpName + StringPool.EQUAL + kvpValue;
155                            }
156    
157                            String preferenceName = LocalizationUtil.getLocalizedName(
158                                    "ranks", languageId);
159    
160                            setPreference(actionRequest, preferenceName, ranks);
161                    }
162            }
163    
164    }