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.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    }