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