001    /**
002     * Copyright (c) 2000-2012 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 mapping, ActionForm form, PortletConfig portletConfig,
045                            ActionRequest actionRequest, ActionResponse actionResponse)
046                    throws Exception {
047    
048                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
049    
050                    if (!cmd.equals(Constants.UPDATE)) {
051                            return;
052                    }
053    
054                    PortletPreferences preferences = actionRequest.getPreferences();
055    
056                    String[] symbols = StringUtil.split(
057                            ParamUtil.getString(actionRequest, "symbols").toUpperCase());
058    
059                    preferences.setValues("symbols", symbols);
060    
061                    try {
062                            preferences.store();
063                    }
064                    catch (ValidatorException ve) {
065                            SessionErrors.add(
066                                    actionRequest, ValidatorException.class.getName(), ve);
067    
068                            return;
069                    }
070    
071                    LiferayPortletConfig liferayPortletConfig =
072                            (LiferayPortletConfig)portletConfig;
073    
074                    SessionMessages.add(
075                            actionRequest,
076                            liferayPortletConfig.getPortletId() +
077                                    SessionMessages.KEY_SUFFIX_UPDATED_PREFERENCES);
078            }
079    
080            @Override
081            public ActionForward render(
082                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
083                            RenderRequest renderRequest, RenderResponse renderResponse)
084                    throws Exception {
085    
086                    return mapping.findForward("portlet.currency_converter.edit");
087            }
088    
089    }