001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.portlet.PortletPreferencesFactoryUtil;
025    
026    import java.util.HashMap;
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                    else {
054                            String portletResource = ParamUtil.getString(
055                                    actionRequest, "portletResource");
056    
057                            PortletPreferences preferences =
058                                    PortletPreferencesFactoryUtil.getPortletSetup(
059                                            actionRequest, portletResource);
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                                    try {
076                                            preferences.store();
077                                    }
078                                    catch (ValidatorException ve) {
079                                            SessionErrors.add(
080                                                    actionRequest, ValidatorException.class.getName(), ve);
081    
082                                            return;
083                                    }
084    
085                                    SessionMessages.add(
086                                            actionRequest,
087                                            portletConfig.getPortletName() + ".doConfigure");
088    
089                                    SessionMessages.add(
090                                            actionRequest,
091                                            portletConfig.getPortletName() + ".doRefresh",
092                                            portletResource);
093                            }
094                    }
095            }
096    
097            protected void removeFooterArticle(
098                            ActionRequest actionRequest, PortletPreferences preferences)
099                    throws Exception {
100    
101                    preferences.setValues("footerArticleValues", new String[] {"0", ""});
102            }
103    
104            protected void removeHeaderArticle(
105                            ActionRequest actionRequest, PortletPreferences preferences)
106                    throws Exception {
107    
108                    preferences.setValues("headerArticleValues", new String[] {"0", ""});
109            }
110    
111            protected void setFooterArticle(
112                            ActionRequest actionRequest, PortletPreferences preferences)
113                    throws Exception {
114    
115                    long articleGroupId = ParamUtil.getLong(
116                            actionRequest, "articleGroupId");
117                    String articleId = ParamUtil.getString(actionRequest, "articleId");
118    
119                    preferences.setValues(
120                            "footerArticleValues",
121                            new String[] {String.valueOf(articleGroupId), articleId});
122            }
123    
124            protected void setHeaderArticle(
125                            ActionRequest actionRequest, PortletPreferences preferences)
126                    throws Exception {
127    
128                    long articleGroupId = ParamUtil.getLong(
129                            actionRequest, "articleGroupId");
130                    String articleId = ParamUtil.getString(actionRequest, "articleId");
131    
132                    preferences.setValues(
133                            "headerArticleValues",
134                            new String[] {String.valueOf(articleGroupId), articleId});
135            }
136    
137            protected void updateSubscriptions(ActionRequest actionRequest)
138                    throws Exception {
139    
140                    int[] subscriptionIndexes = StringUtil.split(
141                            ParamUtil.getString(actionRequest, "subscriptionIndexes"), 0);
142    
143                    Map<String, String> subscriptions = new HashMap<String, String>();
144    
145                    for (int subscriptionIndex : subscriptionIndexes) {
146                            String url = ParamUtil.getString(
147                                    actionRequest, "url" + subscriptionIndex);
148                            String title = ParamUtil.getString(
149                                    actionRequest, "title" + subscriptionIndex);
150    
151                            if (Validator.isNull(url)) {
152                                    continue;
153                            }
154    
155                            subscriptions.put(url, title);
156                    }
157    
158                    String[] urls = new String[subscriptions.size()];
159                    String[] titles = new String[subscriptions.size()];
160    
161                    int i = 0;
162    
163                    for (Map.Entry<String, String> entry : subscriptions.entrySet()) {
164                            urls[i] = entry.getKey();
165                            titles[i] = entry.getValue();
166    
167                            i++;
168                    }
169    
170                    setPreference(actionRequest, "urls", urls);
171                    setPreference(actionRequest, "titles", titles);
172            }
173    
174    }