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.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.util.PortalUtil;
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                    SessionMessages.add(
089                            actionRequest,
090                            PortalUtil.getPortletId(actionRequest) +
091                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
092                            portletResource);
093    
094                    SessionMessages.add(
095                            actionRequest,
096                            PortalUtil.getPortletId(actionRequest) +
097                                    SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
098            }
099    
100            protected void removeFooterArticle(
101                            ActionRequest actionRequest, PortletPreferences preferences)
102                    throws Exception {
103    
104                    preferences.setValues("footerArticleValues", new String[] {"0", ""});
105            }
106    
107            protected void removeHeaderArticle(
108                            ActionRequest actionRequest, PortletPreferences preferences)
109                    throws Exception {
110    
111                    preferences.setValues("headerArticleValues", new String[] {"0", ""});
112            }
113    
114            protected void setFooterArticle(
115                            ActionRequest actionRequest, PortletPreferences preferences)
116                    throws Exception {
117    
118                    long articleGroupId = ParamUtil.getLong(
119                            actionRequest, "articleGroupId");
120                    String articleId = ParamUtil.getString(actionRequest, "articleId");
121    
122                    preferences.setValues(
123                            "footerArticleValues",
124                            new String[] {String.valueOf(articleGroupId), articleId});
125            }
126    
127            protected void setHeaderArticle(
128                            ActionRequest actionRequest, PortletPreferences preferences)
129                    throws Exception {
130    
131                    long articleGroupId = ParamUtil.getLong(
132                            actionRequest, "articleGroupId");
133                    String articleId = ParamUtil.getString(actionRequest, "articleId");
134    
135                    preferences.setValues(
136                            "headerArticleValues",
137                            new String[] {String.valueOf(articleGroupId), articleId});
138            }
139    
140            protected void updateSubscriptions(ActionRequest actionRequest)
141                    throws Exception {
142    
143                    int[] subscriptionIndexes = StringUtil.split(
144                            ParamUtil.getString(actionRequest, "subscriptionIndexes"), 0);
145    
146                    Map<String, String> subscriptions = new LinkedHashMap<String, String>();
147    
148                    for (int subscriptionIndex : subscriptionIndexes) {
149                            String url = ParamUtil.getString(
150                                    actionRequest, "url" + subscriptionIndex);
151                            String title = ParamUtil.getString(
152                                    actionRequest, "title" + subscriptionIndex);
153    
154                            if (Validator.isNull(url)) {
155                                    continue;
156                            }
157    
158                            subscriptions.put(url, title);
159                    }
160    
161                    String[] urls = new String[subscriptions.size()];
162                    String[] titles = new String[subscriptions.size()];
163    
164                    int i = 0;
165    
166                    for (Map.Entry<String, String> entry : subscriptions.entrySet()) {
167                            urls[i] = entry.getKey();
168                            titles[i] = entry.getValue();
169    
170                            i++;
171                    }
172    
173                    setPreference(actionRequest, "urls", urls);
174                    setPreference(actionRequest, "titles", titles);
175            }
176    
177    }