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