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