001    /**
002     * Copyright (c) 2000-present 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.settings.ArchivedSettings;
022    import com.liferay.portal.kernel.settings.ModifiableSettings;
023    import com.liferay.portal.kernel.settings.Settings;
024    import com.liferay.portal.kernel.settings.SettingsFactoryUtil;
025    import com.liferay.portal.kernel.util.Constants;
026    import com.liferay.portal.kernel.util.ParamUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.Portlet;
029    import com.liferay.portal.security.auth.PrincipalException;
030    import com.liferay.portal.struts.PortletAction;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portal.util.WebKeys;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.PortletConfig;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Jorge Ferrer
047     * @author Raymond Aug??
048     */
049    public class EditArchivedSetupsAction extends PortletAction {
050    
051            @Override
052            public void processAction(
053                            ActionMapping actionMapping, ActionForm actionForm,
054                            PortletConfig portletConfig, ActionRequest actionRequest,
055                            ActionResponse actionResponse)
056                    throws Exception {
057    
058                    Portlet portlet = null;
059    
060                    try {
061                            portlet = ActionUtil.getPortlet(actionRequest);
062                    }
063                    catch (PrincipalException pe) {
064                            SessionErrors.add(
065                                    actionRequest, PrincipalException.class.getName());
066    
067                            setForward(actionRequest, "portlet.portlet_configuration.error");
068                    }
069    
070                    actionRequest = ActionUtil.getWrappedActionRequest(actionRequest, null);
071    
072                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
073    
074                    try {
075                            if (cmd.equals(Constants.SAVE)) {
076                                    updateSetup(actionRequest, portlet);
077                            }
078                            else if (cmd.equals(Constants.RESTORE)) {
079                                    restoreSetup(actionRequest, portlet);
080                            }
081                            else if (cmd.equals(Constants.DELETE)) {
082                                    deleteSetup(actionRequest, portlet);
083                            }
084                    }
085                    catch (Exception e) {
086                            if (e instanceof NoSuchPortletItemException ||
087                                    e instanceof PortletItemNameException) {
088    
089                                    SessionErrors.add(actionRequest, e.getClass());
090    
091                                    sendRedirect(actionRequest, actionResponse);
092                            }
093                            else if (e instanceof PrincipalException) {
094                                    SessionErrors.add(actionRequest, e.getClass());
095    
096                                    setForward(
097                                            actionRequest, "portlet.portlet_configuration.error");
098                            }
099                            else {
100                                    throw e;
101                            }
102                    }
103    
104                    if (!SessionErrors.isEmpty(actionRequest)) {
105                            return;
106                    }
107    
108                    String portletResource = ParamUtil.getString(
109                            actionRequest, "portletResource");
110    
111                    SessionMessages.add(
112                            actionRequest,
113                            PortalUtil.getPortletId(actionRequest) +
114                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
115                            portletResource);
116    
117                    SessionMessages.add(
118                            actionRequest,
119                            PortalUtil.getPortletId(actionRequest) +
120                                    SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
121    
122                    String redirect = PortalUtil.escapeRedirect(
123                            ParamUtil.getString(actionRequest, "redirect"));
124    
125                    if (Validator.isNotNull(redirect)) {
126                            actionResponse.sendRedirect(redirect);
127                    }
128            }
129    
130            @Override
131            public ActionForward render(
132                            ActionMapping actionMapping, ActionForm actionForm,
133                            PortletConfig portletConfig, RenderRequest renderRequest,
134                            RenderResponse renderResponse)
135                    throws Exception {
136    
137                    Portlet portlet = null;
138    
139                    try {
140                            portlet = ActionUtil.getPortlet(renderRequest);
141                    }
142                    catch (PrincipalException pe) {
143                            SessionErrors.add(
144                                    renderRequest, PrincipalException.class.getName());
145    
146                            return actionMapping.findForward(
147                                    "portlet.portlet_configuration.error");
148                    }
149    
150                    renderRequest = ActionUtil.getWrappedRenderRequest(renderRequest, null);
151    
152                    renderResponse.setTitle(ActionUtil.getTitle(portlet, renderRequest));
153    
154                    return actionMapping.findForward(
155                            getForward(
156                                    renderRequest,
157                                    "portlet.portlet_configuration.edit_archived_setups"));
158            }
159    
160            protected void deleteSetup(ActionRequest actionRequest, Portlet portlet)
161                    throws Exception {
162    
163                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
164                            WebKeys.THEME_DISPLAY);
165    
166                    String name = ParamUtil.getString(actionRequest, "name");
167    
168                    ArchivedSettings archivedSettings =
169                            SettingsFactoryUtil.getPortletInstanceArchivedSettings(
170                                    themeDisplay.getSiteGroupId(), portlet.getRootPortletId(),
171                                    name);
172    
173                    archivedSettings.delete();
174            }
175    
176            protected void restoreSetup(ActionRequest actionRequest, Portlet portlet)
177                    throws Exception {
178    
179                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
180                            WebKeys.THEME_DISPLAY);
181    
182                    Settings portletInstanceSettings =
183                            SettingsFactoryUtil.getPortletInstanceSettings(
184                                    themeDisplay.getLayout(), portlet.getPortletId());
185    
186                    ModifiableSettings portletInstanceModifiableSettings =
187                            portletInstanceSettings.getModifiableSettings();
188    
189                    String name = ParamUtil.getString(actionRequest, "name");
190    
191                    ArchivedSettings archivedSettings =
192                            SettingsFactoryUtil.getPortletInstanceArchivedSettings(
193                                    themeDisplay.getSiteGroupId(), portlet.getRootPortletId(),
194                                    name);
195    
196                    portletInstanceModifiableSettings.setValues(archivedSettings);
197    
198                    portletInstanceModifiableSettings.store();
199            }
200    
201            protected void updateSetup(ActionRequest actionRequest, Portlet portlet)
202                    throws Exception {
203    
204                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
205                            WebKeys.THEME_DISPLAY);
206    
207                    String name = ParamUtil.getString(actionRequest, "name");
208    
209                    ArchivedSettings archivedSettings =
210                            SettingsFactoryUtil.getPortletInstanceArchivedSettings(
211                                    themeDisplay.getSiteGroupId(), portlet.getRootPortletId(),
212                                    name);
213    
214                    Settings portletInstanceSettings =
215                            SettingsFactoryUtil.getPortletInstanceSettings(
216                                    themeDisplay.getLayout(), portlet.getPortletId());
217    
218                    ModifiableSettings portletInstanceModifiableSettings =
219                            portletInstanceSettings.getModifiableSettings();
220    
221                    archivedSettings.setValues(portletInstanceModifiableSettings);
222    
223                    archivedSettings.store();
224            }
225    
226    }