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.currencyconverter.action;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
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.struts.PortletAction;
024    
025    import javax.portlet.ActionRequest;
026    import javax.portlet.ActionResponse;
027    import javax.portlet.PortletConfig;
028    import javax.portlet.PortletPreferences;
029    import javax.portlet.RenderRequest;
030    import javax.portlet.RenderResponse;
031    import javax.portlet.ValidatorException;
032    
033    import org.apache.struts.action.ActionForm;
034    import org.apache.struts.action.ActionForward;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class EditPreferencesAction extends PortletAction {
041    
042            @Override
043            public void processAction(
044                            ActionMapping actionMapping, ActionForm actionForm,
045                            PortletConfig portletConfig, ActionRequest actionRequest,
046                            ActionResponse actionResponse)
047                    throws Exception {
048    
049                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
050    
051                    if (!cmd.equals(Constants.UPDATE)) {
052                            return;
053                    }
054    
055                    PortletPreferences preferences = actionRequest.getPreferences();
056    
057                    String[] symbols = StringUtil.split(
058                            ParamUtil.getString(actionRequest, "symbols").toUpperCase());
059    
060                    preferences.setValues("symbols", symbols);
061    
062                    try {
063                            preferences.store();
064                    }
065                    catch (ValidatorException ve) {
066                            SessionErrors.add(
067                                    actionRequest, ValidatorException.class.getName(), ve);
068    
069                            return;
070                    }
071    
072                    LiferayPortletConfig liferayPortletConfig =
073                            (LiferayPortletConfig)portletConfig;
074    
075                    SessionMessages.add(
076                            actionRequest,
077                            liferayPortletConfig.getPortletId() +
078                                    SessionMessages.KEY_SUFFIX_UPDATED_PREFERENCES);
079            }
080    
081            @Override
082            public ActionForward render(
083                            ActionMapping actionMapping, ActionForm actionForm,
084                            PortletConfig portletConfig, RenderRequest renderRequest,
085                            RenderResponse renderResponse)
086                    throws Exception {
087    
088                    return actionMapping.findForward("portlet.currency_converter.edit");
089            }
090    
091    }