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.softwarecatalog.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.softwarecatalog.FrameworkVersionNameException;
34  import com.liferay.portlet.softwarecatalog.NoSuchFrameworkVersionException;
35  import com.liferay.portlet.softwarecatalog.service.SCFrameworkVersionServiceUtil;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="EditFrameworkVersionAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Jorge Ferrer
51   */
52  public class EditFrameworkVersionAction extends PortletAction {
53  
54      public void processAction(
55              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
56              ActionRequest actionRequest, ActionResponse actionResponse)
57          throws Exception {
58  
59          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
60  
61          try {
62              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
63                  updateFrameworkVersion(actionRequest);
64              }
65              else if (cmd.equals(Constants.DELETE)) {
66                  deleteFrameworkVersion(actionRequest);
67              }
68  
69              sendRedirect(actionRequest, actionResponse);
70          }
71          catch (Exception e) {
72              if (e instanceof NoSuchFrameworkVersionException ||
73                  e instanceof PrincipalException) {
74  
75                  SessionErrors.add(actionRequest, e.getClass().getName());
76  
77                  setForward(actionRequest, "portlet.software_catalog.error");
78              }
79              else if (e instanceof FrameworkVersionNameException) {
80  
81                  SessionErrors.add(actionRequest, e.getClass().getName());
82              }
83              else {
84                  throw e;
85              }
86          }
87      }
88  
89      public ActionForward render(
90              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
91              RenderRequest renderRequest, RenderResponse renderResponse)
92          throws Exception {
93  
94          try {
95              ActionUtil.getFrameworkVersion(renderRequest);
96          }
97          catch (Exception e) {
98              if (e instanceof NoSuchFrameworkVersionException ||
99                  e instanceof PrincipalException) {
100 
101                 SessionErrors.add(renderRequest, e.getClass().getName());
102 
103                 return mapping.findForward("portlet.software_catalog.error");
104             }
105             else {
106                 throw e;
107             }
108         }
109 
110         return mapping.findForward(getForward(
111             renderRequest, "portlet.software_catalog.edit_framework_version"));
112     }
113 
114     protected void deleteFrameworkVersion(ActionRequest actionRequest)
115         throws Exception {
116 
117         long frameworkVersionId = ParamUtil.getLong(
118             actionRequest, "frameworkVersionId");
119 
120         SCFrameworkVersionServiceUtil.deleteFrameworkVersion(
121             frameworkVersionId);
122     }
123 
124     protected void updateFrameworkVersion(ActionRequest actionRequest)
125         throws Exception {
126 
127         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
128 
129         long frameworkVersionId = ParamUtil.getLong(
130             actionRequest, "frameworkVersionId");
131 
132         String name = ParamUtil.getString(actionRequest, "name");
133         String url = ParamUtil.getString(actionRequest, "url");
134         boolean active = ParamUtil.getBoolean(actionRequest, "active");
135         int priority = ParamUtil.getInteger(actionRequest, "priority");
136 
137         String[] communityPermissions = PortalUtil.getCommunityPermissions(
138             actionRequest);
139         String[] guestPermissions = PortalUtil.getGuestPermissions(
140             actionRequest);
141 
142         if (frameworkVersionId <= 0) {
143 
144             // Add framework version
145 
146             SCFrameworkVersionServiceUtil.addFrameworkVersion(
147                 layout.getPlid(), name, url, active, priority,
148                 communityPermissions, guestPermissions);
149         }
150         else {
151 
152             // Update framework version
153 
154             SCFrameworkVersionServiceUtil.updateFrameworkVersion(
155                 frameworkVersionId, name, url, active, priority);
156         }
157     }
158 
159 }