1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.portletconfiguration.action;
24  
25  import com.liferay.portal.NoSuchPortletItemException;
26  import com.liferay.portal.PortletItemNameException;
27  import com.liferay.portal.kernel.servlet.SessionErrors;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.service.PortletPreferencesServiceUtil;
34  import com.liferay.portal.theme.ThemeDisplay;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.PortletPreferencesFactoryUtil;
37  
38  import javax.portlet.ActionRequest;
39  import javax.portlet.ActionResponse;
40  import javax.portlet.PortletConfig;
41  import javax.portlet.PortletPreferences;
42  import javax.portlet.RenderRequest;
43  import javax.portlet.RenderResponse;
44  
45  import org.apache.struts.action.ActionForm;
46  import org.apache.struts.action.ActionForward;
47  import org.apache.struts.action.ActionMapping;
48  
49  /**
50   * <a href="EditArchivedSetupsAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Jorge Ferrer
53   */
54  public class EditArchivedSetupsAction extends EditConfigurationAction {
55  
56      public void processAction(
57              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
58              ActionRequest actionRequest, ActionResponse actionResponse)
59          throws Exception {
60  
61          Portlet portlet = null;
62  
63          try {
64              portlet = getPortlet(actionRequest);
65          }
66          catch (PrincipalException pe) {
67              SessionErrors.add(
68                  actionRequest, PrincipalException.class.getName());
69  
70              setForward(actionRequest, "portlet.portlet_configuration.error");
71          }
72  
73          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
74  
75          try {
76              if (cmd.equals(Constants.SAVE)) {
77                  updateSetup(actionRequest, portlet);
78  
79                  sendRedirect(actionRequest, actionResponse);
80              }
81              else if (cmd.equals(Constants.RESTORE)) {
82                  restoreSetup(actionRequest, portlet);
83  
84                  sendRedirect(actionRequest, actionResponse);
85              }
86              else if (cmd.equals(Constants.DELETE)) {
87                  deleteSetup(actionRequest);
88  
89                  sendRedirect(actionRequest, actionResponse);
90              }
91          }
92          catch (Exception e) {
93              if (e instanceof NoSuchPortletItemException ||
94                  e instanceof PortletItemNameException) {
95  
96                  SessionErrors.add(actionRequest, e.getClass().getName());
97  
98                  setForward(
99                      actionRequest,
100                     "portlet.portlet_configuration.edit_archived_setups");
101             }
102             else if (e instanceof PrincipalException) {
103                 SessionErrors.add(actionRequest, e.getClass().getName());
104 
105                 setForward(
106                     actionRequest, "portlet.portlet_configuration.error");
107             }
108             else {
109                 throw e;
110             }
111         }
112     }
113 
114     public ActionForward render(
115             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
116             RenderRequest renderRequest, RenderResponse renderResponse)
117         throws Exception {
118 
119         Portlet portlet = null;
120 
121         try {
122             portlet = getPortlet(renderRequest);
123         }
124         catch (PrincipalException pe) {
125             SessionErrors.add(
126                 renderRequest, PrincipalException.class.getName());
127 
128             return mapping.findForward("portlet.portlet_configuration.error");
129         }
130 
131         renderResponse.setTitle(getTitle(portlet, renderRequest));
132 
133         return mapping.findForward(getForward(
134             renderRequest,
135             "portlet.portlet_configuration.edit_archived_setups"));
136     }
137 
138     private void deleteSetup(ActionRequest actionRequest) throws Exception {
139         long portletItemId = ParamUtil.getLong(actionRequest, "portletItemId");
140 
141         PortletPreferencesServiceUtil.deleteArchivedPreferences(portletItemId);
142     }
143 
144     private void restoreSetup(ActionRequest actionRequest, Portlet portlet)
145         throws Exception {
146 
147         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
148 
149         String name = ParamUtil.getString(actionRequest, "name");
150 
151         PortletPreferences setup =
152             PortletPreferencesFactoryUtil.getPortletSetup(
153                 actionRequest, portlet.getPortletId());
154 
155         PortletPreferencesServiceUtil.restoreArchivedPreferences(
156             layout.getGroupId(), name, portlet.getRootPortletId(), setup);
157     }
158 
159     protected void updateSetup(ActionRequest actionRequest, Portlet portlet)
160         throws Exception {
161 
162         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
163             WebKeys.THEME_DISPLAY);
164 
165         String name = ParamUtil.getString(actionRequest, "name");
166 
167         PortletPreferences setup =
168             PortletPreferencesFactoryUtil.getPortletSetup(
169                 actionRequest, portlet.getPortletId());
170 
171         PortletPreferencesServiceUtil.updateArchivePreferences(
172             themeDisplay.getUserId(), themeDisplay.getLayout().getGroupId(),
173             name, portlet.getRootPortletId(), setup);
174     }
175 
176 }