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.rss.action;
016    
017    import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
018    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    
026    import java.util.LinkedHashMap;
027    import java.util.Map;
028    
029    import javax.portlet.ActionRequest;
030    import javax.portlet.ActionResponse;
031    import javax.portlet.PortletConfig;
032    import javax.portlet.PortletPreferences;
033    import javax.portlet.ValidatorException;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class ConfigurationActionImpl extends DefaultConfigurationAction {
039    
040            @Override
041            public void processAction(
042                            PortletConfig portletConfig, ActionRequest actionRequest,
043                            ActionResponse actionResponse)
044                    throws Exception {
045    
046                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
047    
048                    if (cmd.equals(Constants.UPDATE)) {
049                            updateSubscriptions(actionRequest);
050    
051                            super.processAction(portletConfig, actionRequest, actionResponse);
052    
053                            return;
054                    }
055    
056                    String portletResource = ParamUtil.getString(
057                            actionRequest, "portletResource");
058    
059                    PortletPreferences preferences = actionRequest.getPreferences();
060    
061                    if (cmd.equals("remove-footer-article")) {
062                            removeFooterArticle(actionRequest, preferences);
063                    }
064                    else if (cmd.equals("remove-header-article")) {
065                            removeHeaderArticle(actionRequest, preferences);
066                    }
067                    else if (cmd.equals("set-footer-article")) {
068                            setFooterArticle(actionRequest, preferences);
069                    }
070                    else if (cmd.equals("set-header-article")) {
071                            setHeaderArticle(actionRequest, preferences);
072                    }
073    
074                    if (!SessionErrors.isEmpty(actionRequest)) {
075                            return;
076                    }
077    
078                    try {
079                            preferences.store();
080                    }
081                    catch (ValidatorException ve) {
082                            SessionErrors.add(
083                                    actionRequest, ValidatorException.class.getName(), ve);
084    
085                            return;
086                    }
087    
088                    LiferayPortletConfig liferayPortletConfig =
089                            (LiferayPortletConfig)portletConfig;
090    
091                    SessionMessages.add(
092                            actionRequest,
093                            liferayPortletConfig.getPortletId() +
094                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
095                            portletResource);
096    
097                    SessionMessages.add(
098                            actionRequest,
099                            liferayPortletConfig.getPortletId() +
100                                    SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
101            }
102    
103            protected void removeFooterArticle(
104                            ActionRequest actionRequest, PortletPreferences preferences)
105                    throws Exception {
106    
107                    preferences.setValues("footerArticleValues", new String[] {"0", ""});
108            }
109    
110            protected void removeHeaderArticle(
111                            ActionRequest actionRequest, PortletPreferences preferences)
112                    throws Exception {
113    
114                    preferences.setValues("headerArticleValues", new String[] {"0", ""});
115            }
116    
117            protected void setFooterArticle(
118                            ActionRequest actionRequest, PortletPreferences preferences)
119                    throws Exception {
120    
121                    long articleGroupId = ParamUtil.getLong(
122                            actionRequest, "articleGroupId");
123                    String articleId = ParamUtil.getString(actionRequest, "articleId");
124    
125                    preferences.setValues(
126                            "footerArticleValues",
127                            new String[] {String.valueOf(articleGroupId), articleId});
128            }
129    
130            protected void setHeaderArticle(
131                            ActionRequest actionRequest, PortletPreferences preferences)
132                    throws Exception {
133    
134                    long articleGroupId = ParamUtil.getLong(
135                            actionRequest, "articleGroupId");
136                    String articleId = ParamUtil.getString(actionRequest, "articleId");
137    
138                    preferences.setValues(
139                            "headerArticleValues",
140                            new String[] {String.valueOf(articleGroupId), articleId});
141            }
142    
143            protected void updateSubscriptions(ActionRequest actionRequest)
144                    throws Exception {
145    
146                    int[] subscriptionIndexes = StringUtil.split(
147                            ParamUtil.getString(actionRequest, "subscriptionIndexes"), 0);
148    
149                    Map<String, String> subscriptions = new LinkedHashMap<String, String>();
150    
151                    for (int subscriptionIndex : subscriptionIndexes) {
152                            String url = ParamUtil.getString(
153                                    actionRequest, "url" + subscriptionIndex);
154                            String title = ParamUtil.getString(
155                                    actionRequest, "title" + subscriptionIndex);
156    
157                            if (Validator.isNull(url)) {
158                                    continue;
159                            }
160    
161                            subscriptions.put(url, title);
162                    }
163    
164                    String[] urls = new String[subscriptions.size()];
165                    String[] titles = new String[subscriptions.size()];
166    
167                    int i = 0;
168    
169                    for (Map.Entry<String, String> entry : subscriptions.entrySet()) {
170                            urls[i] = entry.getKey();
171                            titles[i] = entry.getValue();
172    
173                            i++;
174                    }
175    
176                    setPreference(actionRequest, "urls", urls);
177                    setPreference(actionRequest, "titles", titles);
178            }
179    
180    }