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.portletconfiguration.action;
016    
017    import com.liferay.portal.NoSuchPortletItemException;
018    import com.liferay.portal.PortletItemNameException;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Portlet;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.PortletPreferencesServiceUtil;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.PortletPreferencesFactoryUtil;
031    
032    import javax.portlet.ActionRequest;
033    import javax.portlet.ActionResponse;
034    import javax.portlet.PortletConfig;
035    import javax.portlet.PortletPreferences;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Jorge Ferrer
045     * @author Raymond Augé
046     */
047    public class EditArchivedSetupsAction extends EditConfigurationAction {
048    
049            @Override
050            public void processAction(
051                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
052                            ActionRequest actionRequest, ActionResponse actionResponse)
053                    throws Exception {
054    
055                    Portlet portlet = null;
056    
057                    try {
058                            portlet = getPortlet(actionRequest);
059                    }
060                    catch (PrincipalException pe) {
061                            SessionErrors.add(
062                                    actionRequest, PrincipalException.class.getName());
063    
064                            setForward(actionRequest, "portlet.portlet_configuration.error");
065                    }
066    
067                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
068    
069                    try {
070                            if (cmd.equals(Constants.SAVE)) {
071                                    updateSetup(actionRequest, portlet);
072                            }
073                            else if (cmd.equals(Constants.RESTORE)) {
074                                    restoreSetup(actionRequest, portlet);
075                            }
076                            else if (cmd.equals(Constants.DELETE)) {
077                                    deleteSetup(actionRequest);
078                            }
079                    }
080                    catch (Exception e) {
081                            if (e instanceof NoSuchPortletItemException ||
082                                    e instanceof PortletItemNameException) {
083    
084                                    SessionErrors.add(actionRequest, e.getClass().getName());
085    
086                                    sendRedirect(actionRequest, actionResponse);
087                            }
088                            else if (e instanceof PrincipalException) {
089                                    SessionErrors.add(actionRequest, e.getClass().getName());
090    
091                                    setForward(
092                                            actionRequest, "portlet.portlet_configuration.error");
093                            }
094                            else {
095                                    throw e;
096                            }
097                    }
098    
099                    if (SessionErrors.isEmpty(actionRequest)) {
100                            String portletResource = ParamUtil.getString(
101                                    actionRequest, "portletResource");
102    
103                            SessionMessages.add(
104                                    actionRequest,
105                                    portletConfig.getPortletName() +
106                                            SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
107                                    portletResource);
108    
109                            SessionMessages.add(
110                                    actionRequest,
111                                    portletConfig.getPortletName() +
112                                            SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
113    
114                            String redirect = PortalUtil.escapeRedirect(
115                                    ParamUtil.getString(actionRequest, "redirect"));
116    
117                            if (Validator.isNotNull(redirect)) {
118                                    actionResponse.sendRedirect(redirect);
119                            }
120                    }
121            }
122    
123            @Override
124            public ActionForward render(
125                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
126                            RenderRequest renderRequest, RenderResponse renderResponse)
127                    throws Exception {
128    
129                    Portlet portlet = null;
130    
131                    try {
132                            portlet = getPortlet(renderRequest);
133                    }
134                    catch (PrincipalException pe) {
135                            SessionErrors.add(
136                                    renderRequest, PrincipalException.class.getName());
137    
138                            return mapping.findForward("portlet.portlet_configuration.error");
139                    }
140    
141                    renderResponse.setTitle(getTitle(portlet, renderRequest));
142    
143                    return mapping.findForward(getForward(
144                            renderRequest,
145                            "portlet.portlet_configuration.edit_archived_setups"));
146            }
147    
148            protected void deleteSetup(ActionRequest actionRequest) throws Exception {
149                    long portletItemId = ParamUtil.getLong(actionRequest, "portletItemId");
150    
151                    PortletPreferencesServiceUtil.deleteArchivedPreferences(portletItemId);
152            }
153    
154            protected void restoreSetup(ActionRequest actionRequest, Portlet portlet)
155                    throws Exception {
156    
157                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
158                            WebKeys.THEME_DISPLAY);
159    
160                    String name = ParamUtil.getString(actionRequest, "name");
161    
162                    PortletPreferences setup =
163                            PortletPreferencesFactoryUtil.getPortletSetup(
164                                    actionRequest, portlet.getPortletId());
165    
166                    PortletPreferencesServiceUtil.restoreArchivedPreferences(
167                            themeDisplay.getScopeGroupId(), name, themeDisplay.getLayout(),
168                            portlet.getRootPortletId(), setup);
169            }
170    
171            protected void updateSetup(ActionRequest actionRequest, Portlet portlet)
172                    throws Exception {
173    
174                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
175                            WebKeys.THEME_DISPLAY);
176    
177                    String name = ParamUtil.getString(actionRequest, "name");
178    
179                    PortletPreferences setup =
180                            PortletPreferencesFactoryUtil.getPortletSetup(
181                                    actionRequest, portlet.getPortletId());
182    
183                    PortletPreferencesServiceUtil.updateArchivePreferences(
184                            themeDisplay.getUserId(), themeDisplay.getScopeGroupId(), name,
185                            portlet.getRootPortletId(), setup);
186            }
187    
188    }