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.security.auth.PrincipalException;
29  import com.liferay.portal.service.ServiceContext;
30  import com.liferay.portal.service.ServiceContextFactory;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portlet.softwarecatalog.FrameworkVersionNameException;
33  import com.liferay.portlet.softwarecatalog.NoSuchFrameworkVersionException;
34  import com.liferay.portlet.softwarecatalog.model.SCFrameworkVersion;
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   */
53  public class EditFrameworkVersionAction extends PortletAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              ActionRequest actionRequest, ActionResponse actionResponse)
58          throws Exception {
59  
60          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
61  
62          try {
63              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
64                  updateFrameworkVersion(actionRequest);
65              }
66              else if (cmd.equals(Constants.DELETE)) {
67                  deleteFrameworkVersion(actionRequest);
68              }
69  
70              sendRedirect(actionRequest, actionResponse);
71          }
72          catch (Exception e) {
73              if (e instanceof NoSuchFrameworkVersionException ||
74                  e instanceof PrincipalException) {
75  
76                  SessionErrors.add(actionRequest, e.getClass().getName());
77  
78                  setForward(actionRequest, "portlet.software_catalog.error");
79              }
80              else if (e instanceof FrameworkVersionNameException) {
81  
82                  SessionErrors.add(actionRequest, e.getClass().getName());
83              }
84              else {
85                  throw e;
86              }
87          }
88      }
89  
90      public ActionForward render(
91              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
92              RenderRequest renderRequest, RenderResponse renderResponse)
93          throws Exception {
94  
95          try {
96              ActionUtil.getFrameworkVersion(renderRequest);
97          }
98          catch (Exception e) {
99              if (e instanceof NoSuchFrameworkVersionException ||
100                 e instanceof PrincipalException) {
101 
102                 SessionErrors.add(renderRequest, e.getClass().getName());
103 
104                 return mapping.findForward("portlet.software_catalog.error");
105             }
106             else {
107                 throw e;
108             }
109         }
110 
111         return mapping.findForward(getForward(
112             renderRequest, "portlet.software_catalog.edit_framework_version"));
113     }
114 
115     protected void deleteFrameworkVersion(ActionRequest actionRequest)
116         throws Exception {
117 
118         long frameworkVersionId = ParamUtil.getLong(
119             actionRequest, "frameworkVersionId");
120 
121         SCFrameworkVersionServiceUtil.deleteFrameworkVersion(
122             frameworkVersionId);
123     }
124 
125     protected void updateFrameworkVersion(ActionRequest actionRequest)
126         throws Exception {
127 
128         long frameworkVersionId = ParamUtil.getLong(
129             actionRequest, "frameworkVersionId");
130 
131         String name = ParamUtil.getString(actionRequest, "name");
132         String url = ParamUtil.getString(actionRequest, "url");
133         boolean active = ParamUtil.getBoolean(actionRequest, "active");
134         int priority = ParamUtil.getInteger(actionRequest, "priority");
135 
136         ServiceContext serviceContext = ServiceContextFactory.getInstance(
137             SCFrameworkVersion.class.getName(), actionRequest);
138 
139         if (frameworkVersionId <= 0) {
140 
141             // Add framework version
142 
143             SCFrameworkVersionServiceUtil.addFrameworkVersion(
144                 name, url, active, priority, serviceContext);
145         }
146         else {
147 
148             // Update framework version
149 
150             SCFrameworkVersionServiceUtil.updateFrameworkVersion(
151                 frameworkVersionId, name, url, active, priority);
152         }
153     }
154 
155 }