1   /**
2    * Copyright (c) 2000-2008 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.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.model.Portlet;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.service.PortletPreferencesServiceUtil;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.PortletPreferencesFactoryUtil;
36  import com.liferay.util.servlet.SessionErrors;
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 config,
59              ActionRequest req, ActionResponse res)
60          throws Exception {
61  
62          Portlet portlet = null;
63  
64          try {
65              portlet = getPortlet(req);
66          }
67          catch (PrincipalException pe) {
68              SessionErrors.add(req, PrincipalException.class.getName());
69  
70              setForward(req, "portlet.portlet_configuration.error");
71          }
72  
73          String cmd = ParamUtil.getString(req, Constants.CMD);
74  
75          try {
76              if (cmd.equals(Constants.SAVE)) {
77                  updateSetup(req, portlet);
78  
79                  sendRedirect(req, res);
80              }
81              else if (cmd.equals(Constants.RESTORE)) {
82                  restoreSetup(req, portlet);
83  
84                  sendRedirect(req, res);
85              }
86              else if (cmd.equals(Constants.DELETE)) {
87                  deleteSetup(req);
88  
89                  sendRedirect(req, res);
90              }
91          }
92          catch (Exception e) {
93              if (e instanceof NoSuchPortletItemException ||
94                  e instanceof PortletItemNameException) {
95  
96                  SessionErrors.add(req, e.getClass().getName());
97  
98                  setForward(
99                      req, "portlet.portlet_configuration.edit_archived_setups");
100             }
101             else if (e instanceof PrincipalException) {
102                 SessionErrors.add(req, e.getClass().getName());
103 
104                 setForward(req, "portlet.portlet_configuration.error");
105             }
106             else {
107                 throw e;
108             }
109         }
110     }
111 
112     public ActionForward render(
113             ActionMapping mapping, ActionForm form, PortletConfig config,
114             RenderRequest req, RenderResponse res)
115         throws Exception {
116 
117         Portlet portlet = null;
118 
119         try {
120             portlet = getPortlet(req);
121         }
122         catch (PrincipalException pe) {
123             SessionErrors.add(req, PrincipalException.class.getName());
124 
125             return mapping.findForward("portlet.portlet_configuration.error");
126         }
127 
128         res.setTitle(getTitle(portlet, req));
129 
130         return mapping.findForward(getForward(
131             req, "portlet.portlet_configuration.edit_archived_setups"));
132     }
133 
134     private void deleteSetup(ActionRequest req) throws Exception {
135         long portletItemId = ParamUtil.getLong(req, "portletItemId");
136 
137         PortletPreferencesServiceUtil.deleteArchivedPreferences(portletItemId);
138     }
139 
140     private void restoreSetup(ActionRequest req, Portlet portlet)
141         throws Exception {
142 
143         Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);
144 
145         String name = ParamUtil.getString(req, "name");
146 
147         PortletPreferences setup =
148             PortletPreferencesFactoryUtil.getPortletSetup(
149                 req, portlet.getPortletId());
150 
151         PortletPreferencesServiceUtil.restoreArchivedPreferences(
152             layout.getGroupId(), name, portlet.getRootPortletId(), setup);
153     }
154 
155     protected void updateSetup(ActionRequest req, Portlet portlet)
156         throws Exception {
157 
158         ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
159             WebKeys.THEME_DISPLAY);
160 
161         String name = ParamUtil.getString(req, "name");
162 
163         PortletPreferences setup =
164             PortletPreferencesFactoryUtil.getPortletSetup(
165                 req, portlet.getPortletId());
166 
167         PortletPreferencesServiceUtil.updateArchivePreferences(
168             themeDisplay.getUserId(), themeDisplay.getLayout().getGroupId(),
169             name, portlet.getRootPortletId(), setup);
170     }
171 
172 }