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.struts.PortletAction;
22  import com.liferay.portlet.softwarecatalog.LicenseNameException;
23  import com.liferay.portlet.softwarecatalog.NoSuchLicenseException;
24  import com.liferay.portlet.softwarecatalog.service.SCLicenseServiceUtil;
25  
26  import javax.portlet.ActionRequest;
27  import javax.portlet.ActionResponse;
28  import javax.portlet.PortletConfig;
29  import javax.portlet.RenderRequest;
30  import javax.portlet.RenderResponse;
31  
32  import org.apache.struts.action.ActionForm;
33  import org.apache.struts.action.ActionForward;
34  import org.apache.struts.action.ActionMapping;
35  
36  /**
37   * <a href="EditLicenseAction.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Jorge Ferrer
40   */
41  public class EditLicenseAction extends PortletAction {
42  
43      public void processAction(
44              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
45              ActionRequest actionRequest, ActionResponse actionResponse)
46          throws Exception {
47  
48          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
49  
50          try {
51              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
52                  updateLicense(actionRequest);
53              }
54              else if (cmd.equals(Constants.DELETE)) {
55                  deleteLicense(actionRequest);
56              }
57  
58              sendRedirect(actionRequest, actionResponse);
59          }
60          catch (Exception e) {
61              if (e instanceof NoSuchLicenseException ||
62                  e instanceof PrincipalException) {
63  
64                  SessionErrors.add(actionRequest, e.getClass().getName());
65  
66                  setForward(actionRequest, "portlet.software_catalog.error");
67              }
68              else if (e instanceof LicenseNameException) {
69  
70                  SessionErrors.add(actionRequest, e.getClass().getName());
71              }
72              else {
73                  throw e;
74              }
75          }
76      }
77  
78      public ActionForward render(
79              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
80              RenderRequest renderRequest, RenderResponse renderResponse)
81          throws Exception {
82  
83          try {
84              ActionUtil.getLicense(renderRequest);
85          }
86          catch (Exception e) {
87              if (e instanceof NoSuchLicenseException ||
88                  e instanceof PrincipalException) {
89  
90                  SessionErrors.add(renderRequest, e.getClass().getName());
91  
92                  return mapping.findForward("portlet.software_catalog.error");
93              }
94              else {
95                  throw e;
96              }
97          }
98  
99          return mapping.findForward(
100             getForward(renderRequest, "portlet.software_catalog.edit_license"));
101     }
102 
103     protected void deleteLicense(ActionRequest actionRequest) throws Exception {
104         long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
105 
106         SCLicenseServiceUtil.deleteLicense(licenseId);
107     }
108 
109     protected void updateLicense(ActionRequest actionRequest) throws Exception {
110         long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
111 
112         String name = ParamUtil.getString(actionRequest, "name");
113         String url = ParamUtil.getString(actionRequest, "url");
114         boolean openSource = ParamUtil.getBoolean(actionRequest, "openSource");
115         boolean active = ParamUtil.getBoolean(actionRequest, "active");
116         boolean recommended = ParamUtil.getBoolean(
117             actionRequest, "recommended");
118 
119         if (licenseId <= 0) {
120 
121             // Add license
122 
123             SCLicenseServiceUtil.addLicense(
124                 name, url, openSource, active, recommended);
125         }
126         else {
127 
128             // Update license
129 
130             SCLicenseServiceUtil.updateLicense(
131                 licenseId, name, url, openSource, active, recommended);
132         }
133     }
134 
135 }