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   */
55  public class EditArchivedSetupsAction extends EditConfigurationAction {
56  
57      public void processAction(
58              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
59              ActionRequest actionRequest, ActionResponse actionResponse)
60          throws Exception {
61  
62          Portlet portlet = null;
63  
64          try {
65              portlet = getPortlet(actionRequest);
66          }
67          catch (PrincipalException pe) {
68              SessionErrors.add(
69                  actionRequest, PrincipalException.class.getName());
70  
71              setForward(actionRequest, "portlet.portlet_configuration.error");
72          }
73  
74          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
75  
76          try {
77              if (cmd.equals(Constants.SAVE)) {
78                  updateSetup(actionRequest, portlet);
79  
80                  sendRedirect(actionRequest, actionResponse);
81              }
82              else if (cmd.equals(Constants.RESTORE)) {
83                  restoreSetup(actionRequest, portlet);
84  
85                  sendRedirect(actionRequest, actionResponse);
86              }
87              else if (cmd.equals(Constants.DELETE)) {
88                  deleteSetup(actionRequest);
89  
90                  sendRedirect(actionRequest, actionResponse);
91              }
92          }
93          catch (Exception e) {
94              if (e instanceof NoSuchPortletItemException ||
95                  e instanceof PortletItemNameException) {
96  
97                  SessionErrors.add(actionRequest, e.getClass().getName());
98  
99                  setForward(
100                     actionRequest,
101                     "portlet.portlet_configuration.edit_archived_setups");
102             }
103             else if (e instanceof PrincipalException) {
104                 SessionErrors.add(actionRequest, e.getClass().getName());
105 
106                 setForward(
107                     actionRequest, "portlet.portlet_configuration.error");
108             }
109             else {
110                 throw e;
111             }
112         }
113     }
114 
115     public ActionForward render(
116             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
117             RenderRequest renderRequest, RenderResponse renderResponse)
118         throws Exception {
119 
120         Portlet portlet = null;
121 
122         try {
123             portlet = getPortlet(renderRequest);
124         }
125         catch (PrincipalException pe) {
126             SessionErrors.add(
127                 renderRequest, PrincipalException.class.getName());
128 
129             return mapping.findForward("portlet.portlet_configuration.error");
130         }
131 
132         renderResponse.setTitle(getTitle(portlet, renderRequest));
133 
134         return mapping.findForward(getForward(
135             renderRequest,
136             "portlet.portlet_configuration.edit_archived_setups"));
137     }
138 
139     private void deleteSetup(ActionRequest actionRequest) throws Exception {
140         long portletItemId = ParamUtil.getLong(actionRequest, "portletItemId");
141 
142         PortletPreferencesServiceUtil.deleteArchivedPreferences(portletItemId);
143     }
144 
145     private void restoreSetup(ActionRequest actionRequest, Portlet portlet)
146         throws Exception {
147 
148         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
149 
150         String name = ParamUtil.getString(actionRequest, "name");
151 
152         PortletPreferences setup =
153             PortletPreferencesFactoryUtil.getPortletSetup(
154                 actionRequest, portlet.getPortletId());
155 
156         PortletPreferencesServiceUtil.restoreArchivedPreferences(
157             layout.getGroupId(), name, portlet.getRootPortletId(), setup);
158     }
159 
160     protected void updateSetup(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         PortletPreferences setup =
169             PortletPreferencesFactoryUtil.getPortletSetup(
170                 actionRequest, portlet.getPortletId());
171 
172         PortletPreferencesServiceUtil.updateArchivePreferences(
173             themeDisplay.getUserId(), themeDisplay.getLayout().getGroupId(),
174             name, portlet.getRootPortletId(), setup);
175     }
176 
177 }