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