1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.portletconfiguration.action;
16  
17  import com.liferay.portal.NoSuchPortletItemException;
18  import com.liferay.portal.PortletItemNameException;
19  import com.liferay.portal.kernel.servlet.SessionErrors;
20  import com.liferay.portal.kernel.util.Constants;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.model.Layout;
23  import com.liferay.portal.model.Portlet;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.service.PortletPreferencesServiceUtil;
26  import com.liferay.portal.theme.ThemeDisplay;
27  import com.liferay.portal.util.WebKeys;
28  import com.liferay.portlet.PortletPreferencesFactoryUtil;
29  
30  import javax.portlet.ActionRequest;
31  import javax.portlet.ActionResponse;
32  import javax.portlet.PortletConfig;
33  import javax.portlet.PortletPreferences;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  import org.apache.struts.action.ActionForm;
38  import org.apache.struts.action.ActionForward;
39  import org.apache.struts.action.ActionMapping;
40  
41  /**
42   * <a href="EditArchivedSetupsAction.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Jorge Ferrer
45   */
46  public class EditArchivedSetupsAction extends EditConfigurationAction {
47  
48      public void processAction(
49              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
50              ActionRequest actionRequest, ActionResponse actionResponse)
51          throws Exception {
52  
53          Portlet portlet = null;
54  
55          try {
56              portlet = getPortlet(actionRequest);
57          }
58          catch (PrincipalException pe) {
59              SessionErrors.add(
60                  actionRequest, PrincipalException.class.getName());
61  
62              setForward(actionRequest, "portlet.portlet_configuration.error");
63          }
64  
65          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
66  
67          try {
68              if (cmd.equals(Constants.SAVE)) {
69                  updateSetup(actionRequest, portlet);
70  
71                  sendRedirect(actionRequest, actionResponse);
72              }
73              else if (cmd.equals(Constants.RESTORE)) {
74                  restoreSetup(actionRequest, portlet);
75  
76                  sendRedirect(actionRequest, actionResponse);
77              }
78              else if (cmd.equals(Constants.DELETE)) {
79                  deleteSetup(actionRequest);
80  
81                  sendRedirect(actionRequest, actionResponse);
82              }
83          }
84          catch (Exception e) {
85              if (e instanceof NoSuchPortletItemException ||
86                  e instanceof PortletItemNameException) {
87  
88                  SessionErrors.add(actionRequest, e.getClass().getName());
89  
90                  sendRedirect(actionRequest, actionResponse);
91              }
92              else if (e instanceof PrincipalException) {
93                  SessionErrors.add(actionRequest, e.getClass().getName());
94  
95                  setForward(
96                      actionRequest, "portlet.portlet_configuration.error");
97              }
98              else {
99                  throw e;
100             }
101         }
102     }
103 
104     public ActionForward render(
105             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
106             RenderRequest renderRequest, RenderResponse renderResponse)
107         throws Exception {
108 
109         Portlet portlet = null;
110 
111         try {
112             portlet = getPortlet(renderRequest);
113         }
114         catch (PrincipalException pe) {
115             SessionErrors.add(
116                 renderRequest, PrincipalException.class.getName());
117 
118             return mapping.findForward("portlet.portlet_configuration.error");
119         }
120 
121         renderResponse.setTitle(getTitle(portlet, renderRequest));
122 
123         return mapping.findForward(getForward(
124             renderRequest,
125             "portlet.portlet_configuration.edit_archived_setups"));
126     }
127 
128     private void deleteSetup(ActionRequest actionRequest) throws Exception {
129         long portletItemId = ParamUtil.getLong(actionRequest, "portletItemId");
130 
131         PortletPreferencesServiceUtil.deleteArchivedPreferences(portletItemId);
132     }
133 
134     private void restoreSetup(ActionRequest actionRequest, Portlet portlet)
135         throws Exception {
136 
137         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
138 
139         String name = ParamUtil.getString(actionRequest, "name");
140 
141         PortletPreferences setup =
142             PortletPreferencesFactoryUtil.getPortletSetup(
143                 actionRequest, portlet.getPortletId());
144 
145         PortletPreferencesServiceUtil.restoreArchivedPreferences(
146             layout.getGroupId(), name, portlet.getRootPortletId(), setup);
147     }
148 
149     protected void updateSetup(ActionRequest actionRequest, Portlet portlet)
150         throws Exception {
151 
152         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
153             WebKeys.THEME_DISPLAY);
154 
155         String name = ParamUtil.getString(actionRequest, "name");
156 
157         PortletPreferences setup =
158             PortletPreferencesFactoryUtil.getPortletSetup(
159                 actionRequest, portlet.getPortletId());
160 
161         PortletPreferencesServiceUtil.updateArchivePreferences(
162             themeDisplay.getUserId(), themeDisplay.getLayout().getGroupId(),
163             name, portlet.getRootPortletId(), setup);
164     }
165 
166 }