001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(getForward(
148                            renderRequest,
149                            "portlet.portlet_configuration.edit_archived_setups"));
150            }
151    
152            protected void deleteSetup(ActionRequest actionRequest) throws Exception {
153                    long portletItemId = ParamUtil.getLong(actionRequest, "portletItemId");
154    
155                    PortletPreferencesServiceUtil.deleteArchivedPreferences(portletItemId);
156            }
157    
158            protected void restoreSetup(ActionRequest actionRequest, Portlet portlet)
159                    throws Exception {
160    
161                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
162                            WebKeys.THEME_DISPLAY);
163    
164                    String name = ParamUtil.getString(actionRequest, "name");
165    
166                    PortletPreferences setup =
167                            PortletPreferencesFactoryUtil.getPortletSetup(
168                                    actionRequest, portlet.getPortletId());
169    
170                    PortletPreferencesServiceUtil.restoreArchivedPreferences(
171                            themeDisplay.getScopeGroupId(), name, themeDisplay.getLayout(),
172                            portlet.getRootPortletId(), setup);
173            }
174    
175            protected void updateSetup(ActionRequest actionRequest, Portlet portlet)
176                    throws Exception {
177    
178                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
179                            WebKeys.THEME_DISPLAY);
180    
181                    String name = ParamUtil.getString(actionRequest, "name");
182    
183                    PortletPreferences setup =
184                            PortletPreferencesFactoryUtil.getPortletSetup(
185                                    actionRequest, portlet.getPortletId());
186    
187                    PortletPreferencesServiceUtil.updateArchivePreferences(
188                            themeDisplay.getUserId(), themeDisplay.getScopeGroupId(), name,
189                            portlet.getRootPortletId(), setup);
190            }
191    
192    }