1   /**
2    * Copyright (c) 2000-2007 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.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portlet.softwarecatalog.NoSuchProductVersionException;
30  import com.liferay.portlet.softwarecatalog.ProductVersionChangeLogException;
31  import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
32  import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
33  import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
34  import com.liferay.portlet.softwarecatalog.service.SCProductVersionServiceUtil;
35  import com.liferay.util.servlet.SessionErrors;
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="EditProductVersionAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Jorge Ferrer
51   *
52   */
53  public class EditProductVersionAction extends PortletAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig config,
57              ActionRequest req, ActionResponse res)
58          throws Exception {
59  
60          String cmd = ParamUtil.getString(req, Constants.CMD);
61  
62          try {
63              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
64                  updateProductVersion(req);
65              }
66              else if (cmd.equals(Constants.DELETE)) {
67                  deleteProductVersion(req);
68              }
69  
70              sendRedirect(req, res);
71          }
72          catch (Exception e) {
73              if (e instanceof NoSuchProductVersionException ||
74                  e instanceof PrincipalException) {
75  
76                  SessionErrors.add(req, e.getClass().getName());
77  
78                  setForward(req, "portlet.software_catalog.error");
79              }
80              else if (e instanceof ProductVersionChangeLogException ||
81                       e instanceof ProductVersionDownloadURLException ||
82                       e instanceof ProductVersionFrameworkVersionException ||
83                       e instanceof ProductVersionNameException) {
84  
85                  SessionErrors.add(req, e.getClass().getName());
86              }
87              else {
88                  throw e;
89              }
90          }
91      }
92  
93      public ActionForward render(
94              ActionMapping mapping, ActionForm form, PortletConfig config,
95              RenderRequest req, RenderResponse res)
96          throws Exception {
97  
98          try {
99              ActionUtil.getProductVersion(req);
100         }
101         catch (Exception e) {
102             if (e instanceof NoSuchProductVersionException ||
103                 e instanceof PrincipalException) {
104 
105                 SessionErrors.add(req, e.getClass().getName());
106 
107                 return mapping.findForward("portlet.software_catalog.error");
108             }
109             else {
110                 throw e;
111             }
112         }
113 
114         return mapping.findForward(getForward(
115             req, "portlet.software_catalog.edit_product_version"));
116     }
117 
118     protected void deleteProductVersion(ActionRequest req) throws Exception {
119         long productVersionId = ParamUtil.getLong(req, "productVersionId");
120 
121         SCProductVersionServiceUtil.deleteProductVersion(productVersionId);
122     }
123 
124     protected void updateProductVersion(ActionRequest req) throws Exception {
125         long productVersionId = ParamUtil.getLong(req, "productVersionId");
126 
127         long productEntryId = ParamUtil.getLong(req, "productEntryId");
128         String version = ParamUtil.getString(req, "version");
129         String changeLog = ParamUtil.getString(req, "changeLog");
130         String downloadPageURL = ParamUtil.getString(req, "downloadPageURL");
131         String directDownloadURL = ParamUtil.getString(
132             req, "directDownloadURL");
133         boolean repoStoreArtifact = ParamUtil.getBoolean(
134             req, "repoStoreArtifact");
135 
136         long[] frameworkVersionIds = ParamUtil.getLongValues(
137             req, "frameworkVersions");
138 
139         String[] communityPermissions = req.getParameterValues(
140             "communityPermissions");
141         String[] guestPermissions = req.getParameterValues(
142             "guestPermissions");
143 
144         if (productVersionId <= 0) {
145 
146             // Add product version
147 
148             SCProductVersionServiceUtil.addProductVersion(
149                 productEntryId, version, changeLog, downloadPageURL,
150                 directDownloadURL, repoStoreArtifact, frameworkVersionIds,
151                 communityPermissions, guestPermissions);
152         }
153         else {
154 
155             // Update product version
156 
157             SCProductVersionServiceUtil.updateProductVersion(
158                 productVersionId, version, changeLog, downloadPageURL,
159                 directDownloadURL, repoStoreArtifact, frameworkVersionIds);
160         }
161     }
162 
163 }