001    /**
002     * Copyright (c) 2000-2010 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.amazonrankings.action;
016    
017    import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
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.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portlet.PortletPreferencesFactoryUtil;
025    
026    import java.util.Arrays;
027    
028    import javax.portlet.ActionRequest;
029    import javax.portlet.ActionResponse;
030    import javax.portlet.PortletConfig;
031    import javax.portlet.PortletPreferences;
032    import javax.portlet.RenderRequest;
033    import javax.portlet.RenderResponse;
034    import javax.portlet.ValidatorException;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class ConfigurationActionImpl extends BaseConfigurationAction {
040    
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                            return;
050                    }
051    
052                    String[] isbns = StringUtil.split(
053                            ParamUtil.getString(actionRequest, "isbns").toUpperCase(),
054                            StringPool.SPACE);
055    
056                    Arrays.sort(isbns);
057    
058                    String portletResource = ParamUtil.getString(
059                            actionRequest, "portletResource");
060    
061                    PortletPreferences preferences =
062                            PortletPreferencesFactoryUtil.getPortletSetup(
063                                    actionRequest, portletResource);
064    
065                    preferences.setValues("isbns", isbns);
066    
067                    try {
068                            preferences.store();
069                    }
070                    catch (ValidatorException ve) {
071                            SessionErrors.add(
072                                    actionRequest, ValidatorException.class.getName(), ve);
073    
074                            return;
075                    }
076    
077                    SessionMessages.add(
078                            actionRequest, portletConfig.getPortletName() + ".doConfigure");
079            }
080    
081            public String render(
082                            PortletConfig portletConfig, RenderRequest renderRequest,
083                            RenderResponse renderResponse)
084                    throws Exception {
085    
086                    return "/html/portlet/amazon_rankings/configuration.jsp";
087            }
088    
089    }