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