1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.Portlet;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.service.PortletPreferencesServiceUtil;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.WebKeys;
27  import com.liferay.portlet.PortletPreferencesFactoryUtil;
28  
29  import javax.portlet.ActionRequest;
30  import javax.portlet.ActionResponse;
31  import javax.portlet.PortletConfig;
32  import javax.portlet.PortletPreferences;
33  import javax.portlet.RenderRequest;
34  import javax.portlet.RenderResponse;
35  
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  
40  /**
41   * <a href="EditArchivedSetupsAction.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Jorge Ferrer
44   */
45  public class EditArchivedSetupsAction extends EditConfigurationAction {
46  
47      public void processAction(
48              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
49              ActionRequest actionRequest, ActionResponse actionResponse)
50          throws Exception {
51  
52          Portlet portlet = null;
53  
54          try {
55              portlet = getPortlet(actionRequest);
56          }
57          catch (PrincipalException pe) {
58              SessionErrors.add(
59                  actionRequest, PrincipalException.class.getName());
60  
61              setForward(actionRequest, "portlet.portlet_configuration.error");
62          }
63  
64          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
65  
66          try {
67              if (cmd.equals(Constants.SAVE)) {
68                  updateSetup(actionRequest, portlet);
69  
70                  sendRedirect(actionRequest, actionResponse);
71              }
72              else if (cmd.equals(Constants.RESTORE)) {
73                  restoreSetup(actionRequest, portlet);
74  
75                  sendRedirect(actionRequest, actionResponse);
76              }
77              else if (cmd.equals(Constants.DELETE)) {
78                  deleteSetup(actionRequest);
79  
80                  sendRedirect(actionRequest, actionResponse);
81              }
82          }
83          catch (Exception e) {
84              if (e instanceof NoSuchPortletItemException ||
85                  e instanceof PortletItemNameException) {
86  
87                  SessionErrors.add(actionRequest, e.getClass().getName());
88  
89                  sendRedirect(actionRequest, actionResponse);
90              }
91              else if (e instanceof PrincipalException) {
92                  SessionErrors.add(actionRequest, e.getClass().getName());
93  
94                  setForward(
95                      actionRequest, "portlet.portlet_configuration.error");
96              }
97              else {
98                  throw e;
99              }
100         }
101     }
102 
103     public ActionForward render(
104             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
105             RenderRequest renderRequest, RenderResponse renderResponse)
106         throws Exception {
107 
108         Portlet portlet = null;
109 
110         try {
111             portlet = getPortlet(renderRequest);
112         }
113         catch (PrincipalException pe) {
114             SessionErrors.add(
115                 renderRequest, PrincipalException.class.getName());
116 
117             return mapping.findForward("portlet.portlet_configuration.error");
118         }
119 
120         renderResponse.setTitle(getTitle(portlet, renderRequest));
121 
122         return mapping.findForward(getForward(
123             renderRequest,
124             "portlet.portlet_configuration.edit_archived_setups"));
125     }
126 
127     private void deleteSetup(ActionRequest actionRequest) throws Exception {
128         long portletItemId = ParamUtil.getLong(actionRequest, "portletItemId");
129 
130         PortletPreferencesServiceUtil.deleteArchivedPreferences(portletItemId);
131     }
132 
133     private void restoreSetup(ActionRequest actionRequest, Portlet portlet)
134         throws Exception {
135 
136         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
137             WebKeys.THEME_DISPLAY);
138 
139         String name = ParamUtil.getString(actionRequest, "name");
140 
141         PortletPreferences setup =
142             PortletPreferencesFactoryUtil.getPortletSetup(
143                 actionRequest, portlet.getPortletId());
144 
145         PortletPreferencesServiceUtil.restoreArchivedPreferences(
146             themeDisplay.getScopeGroupId(), name, portlet.getRootPortletId(),
147             setup);
148     }
149 
150     protected void updateSetup(ActionRequest actionRequest, Portlet portlet)
151         throws Exception {
152 
153         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
154             WebKeys.THEME_DISPLAY);
155 
156         String name = ParamUtil.getString(actionRequest, "name");
157 
158         PortletPreferences setup =
159             PortletPreferencesFactoryUtil.getPortletSetup(
160                 actionRequest, portlet.getPortletId());
161 
162         PortletPreferencesServiceUtil.updateArchivePreferences(
163             themeDisplay.getUserId(), themeDisplay.getScopeGroupId(),
164             name, portlet.getRootPortletId(), setup);
165     }
166 
167 }