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