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