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                  sendRedirect(actionRequest, actionResponse);
99              }
100             else if (e instanceof PrincipalException) {
101                 SessionErrors.add(actionRequest, e.getClass().getName());
102 
103                 setForward(
104                     actionRequest, "portlet.portlet_configuration.error");
105             }
106             else {
107                 throw e;
108             }
109         }
110     }
111 
112     public ActionForward render(
113             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
114             RenderRequest renderRequest, RenderResponse renderResponse)
115         throws Exception {
116 
117         Portlet portlet = null;
118 
119         try {
120             portlet = getPortlet(renderRequest);
121         }
122         catch (PrincipalException pe) {
123             SessionErrors.add(
124                 renderRequest, PrincipalException.class.getName());
125 
126             return mapping.findForward("portlet.portlet_configuration.error");
127         }
128 
129         renderResponse.setTitle(getTitle(portlet, renderRequest));
130 
131         return mapping.findForward(getForward(
132             renderRequest,
133             "portlet.portlet_configuration.edit_archived_setups"));
134     }
135 
136     private void deleteSetup(ActionRequest actionRequest) throws Exception {
137         long portletItemId = ParamUtil.getLong(actionRequest, "portletItemId");
138 
139         PortletPreferencesServiceUtil.deleteArchivedPreferences(portletItemId);
140     }
141 
142     private void restoreSetup(ActionRequest actionRequest, Portlet portlet)
143         throws Exception {
144 
145         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
146 
147         String name = ParamUtil.getString(actionRequest, "name");
148 
149         PortletPreferences setup =
150             PortletPreferencesFactoryUtil.getPortletSetup(
151                 actionRequest, portlet.getPortletId());
152 
153         PortletPreferencesServiceUtil.restoreArchivedPreferences(
154             layout.getGroupId(), name, portlet.getRootPortletId(), setup);
155     }
156 
157     protected void updateSetup(ActionRequest actionRequest, Portlet portlet)
158         throws Exception {
159 
160         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
161             WebKeys.THEME_DISPLAY);
162 
163         String name = ParamUtil.getString(actionRequest, "name");
164 
165         PortletPreferences setup =
166             PortletPreferencesFactoryUtil.getPortletSetup(
167                 actionRequest, portlet.getPortletId());
168 
169         PortletPreferencesServiceUtil.updateArchivePreferences(
170             themeDisplay.getUserId(), themeDisplay.getLayout().getGroupId(),
171             name, portlet.getRootPortletId(), setup);
172     }
173 
174 }