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.struts.PortletAction;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.WebKeys;
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 PortletAction {
049    
050            @Override
051            public void processAction(
052                            ActionMapping actionMapping, ActionForm actionForm,
053                            PortletConfig portletConfig, ActionRequest actionRequest,
054                            ActionResponse actionResponse)
055                    throws Exception {
056    
057                    actionRequest = ActionUtil.getWrappedActionRequest(actionRequest, null);
058    
059                    Portlet portlet = null;
060    
061                    try {
062                            portlet = ActionUtil.getPortlet(actionRequest);
063                    }
064                    catch (PrincipalException pe) {
065                            SessionErrors.add(
066                                    actionRequest, PrincipalException.class.getName());
067    
068                            setForward(actionRequest, "portlet.portlet_configuration.error");
069                    }
070    
071                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
072    
073                    try {
074                            if (cmd.equals(Constants.SAVE)) {
075                                    updateSetup(actionRequest, portlet);
076                            }
077                            else if (cmd.equals(Constants.RESTORE)) {
078                                    restoreSetup(actionRequest, portlet);
079                            }
080                            else if (cmd.equals(Constants.DELETE)) {
081                                    deleteSetup(actionRequest);
082                            }
083                    }
084                    catch (Exception e) {
085                            if (e instanceof NoSuchPortletItemException ||
086                                    e instanceof PortletItemNameException) {
087    
088                                    SessionErrors.add(actionRequest, e.getClass());
089    
090                                    sendRedirect(actionRequest, actionResponse);
091                            }
092                            else if (e instanceof PrincipalException) {
093                                    SessionErrors.add(actionRequest, e.getClass());
094    
095                                    setForward(
096                                            actionRequest, "portlet.portlet_configuration.error");
097                            }
098                            else {
099                                    throw e;
100                            }
101                    }
102    
103                    if (!SessionErrors.isEmpty(actionRequest)) {
104                            return;
105                    }
106    
107                    LiferayPortletConfig liferayPortletConfig =
108                            (LiferayPortletConfig)portletConfig;
109    
110                    String portletResource = ParamUtil.getString(
111                            actionRequest, "portletResource");
112    
113                    SessionMessages.add(
114                            actionRequest,
115                            liferayPortletConfig.getPortletId() +
116                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
117                            portletResource);
118    
119                    SessionMessages.add(
120                            actionRequest,
121                            liferayPortletConfig.getPortletId() +
122                                    SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
123    
124                    String redirect = PortalUtil.escapeRedirect(
125                            ParamUtil.getString(actionRequest, "redirect"));
126    
127                    if (Validator.isNotNull(redirect)) {
128                            actionResponse.sendRedirect(redirect);
129                    }
130            }
131    
132            @Override
133            public ActionForward render(
134                            ActionMapping actionMapping, ActionForm actionForm,
135                            PortletConfig portletConfig, RenderRequest renderRequest,
136                            RenderResponse renderResponse)
137                    throws Exception {
138    
139                    renderRequest = ActionUtil.getWrappedRenderRequest(renderRequest, null);
140    
141                    Portlet portlet = null;
142    
143                    try {
144                            portlet = ActionUtil.getPortlet(renderRequest);
145                    }
146                    catch (PrincipalException pe) {
147                            SessionErrors.add(
148                                    renderRequest, PrincipalException.class.getName());
149    
150                            return actionMapping.findForward(
151                                    "portlet.portlet_configuration.error");
152                    }
153    
154                    renderResponse.setTitle(ActionUtil.getTitle(portlet, renderRequest));
155    
156                    return actionMapping.findForward(
157                            getForward(
158                                    renderRequest,
159                                    "portlet.portlet_configuration.edit_archived_setups"));
160            }
161    
162            protected void deleteSetup(ActionRequest actionRequest) throws Exception {
163                    long portletItemId = ParamUtil.getLong(actionRequest, "portletItemId");
164    
165                    PortletPreferencesServiceUtil.deleteArchivedPreferences(portletItemId);
166            }
167    
168            protected void restoreSetup(ActionRequest actionRequest, Portlet portlet)
169                    throws Exception {
170    
171                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
172                            WebKeys.THEME_DISPLAY);
173    
174                    String name = ParamUtil.getString(actionRequest, "name");
175    
176                    PortletPreferences portletPreferences = actionRequest.getPreferences();
177    
178                    PortletPreferencesServiceUtil.restoreArchivedPreferences(
179                            themeDisplay.getScopeGroupId(), name, themeDisplay.getLayout(),
180                            portlet.getRootPortletId(), portletPreferences);
181            }
182    
183            protected void updateSetup(ActionRequest actionRequest, Portlet portlet)
184                    throws Exception {
185    
186                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
187                            WebKeys.THEME_DISPLAY);
188    
189                    String name = ParamUtil.getString(actionRequest, "name");
190    
191                    PortletPreferences portletPreferences = actionRequest.getPreferences();
192    
193                    PortletPreferencesServiceUtil.updateArchivePreferences(
194                            themeDisplay.getUserId(), themeDisplay.getScopeGroupId(), name,
195                            portlet.getRootPortletId(), portletPreferences);
196            }
197    
198    }