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