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