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.service.ServiceContext;
30  import com.liferay.portal.service.ServiceContextFactory;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portlet.softwarecatalog.DuplicateProductVersionDirectDownloadURLException;
33  import com.liferay.portlet.softwarecatalog.NoSuchProductVersionException;
34  import com.liferay.portlet.softwarecatalog.ProductVersionChangeLogException;
35  import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
36  import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
37  import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
38  import com.liferay.portlet.softwarecatalog.UnavailableProductVersionDirectDownloadURLException;
39  import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
40  import com.liferay.portlet.softwarecatalog.service.SCProductVersionServiceUtil;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.RenderRequest;
46  import javax.portlet.RenderResponse;
47  
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="EditProductVersionAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Jorge Ferrer
56   *
57   */
58  public class EditProductVersionAction extends PortletAction {
59  
60      public void processAction(
61              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
62              ActionRequest actionRequest, ActionResponse actionResponse)
63          throws Exception {
64  
65          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
66  
67          try {
68              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
69                  updateProductVersion(actionRequest);
70              }
71              else if (cmd.equals(Constants.DELETE)) {
72                  deleteProductVersion(actionRequest);
73              }
74  
75              sendRedirect(actionRequest, actionResponse);
76          }
77          catch (Exception e) {
78              if (e instanceof NoSuchProductVersionException ||
79                  e instanceof PrincipalException) {
80  
81                  SessionErrors.add(actionRequest, e.getClass().getName());
82  
83                  setForward(actionRequest, "portlet.software_catalog.error");
84              }
85              else if (e instanceof
86                          DuplicateProductVersionDirectDownloadURLException ||
87                       e instanceof ProductVersionChangeLogException ||
88                       e instanceof ProductVersionDownloadURLException ||
89                       e instanceof ProductVersionFrameworkVersionException ||
90                       e instanceof ProductVersionNameException ||
91                       e instanceof
92                          UnavailableProductVersionDirectDownloadURLException) {
93  
94                  SessionErrors.add(actionRequest, e.getClass().getName());
95              }
96              else {
97                  throw e;
98              }
99          }
100     }
101 
102     public ActionForward render(
103             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
104             RenderRequest renderRequest, RenderResponse renderResponse)
105         throws Exception {
106 
107         try {
108             ActionUtil.getProductVersion(renderRequest);
109         }
110         catch (Exception e) {
111             if (e instanceof NoSuchProductVersionException ||
112                 e instanceof PrincipalException) {
113 
114                 SessionErrors.add(renderRequest, e.getClass().getName());
115 
116                 return mapping.findForward("portlet.software_catalog.error");
117             }
118             else {
119                 throw e;
120             }
121         }
122 
123         return mapping.findForward(getForward(
124             renderRequest, "portlet.software_catalog.edit_product_version"));
125     }
126 
127     protected void deleteProductVersion(ActionRequest actionRequest)
128         throws Exception {
129 
130         long productVersionId = ParamUtil.getLong(
131             actionRequest, "productVersionId");
132 
133         SCProductVersionServiceUtil.deleteProductVersion(productVersionId);
134     }
135 
136     protected void updateProductVersion(ActionRequest actionRequest)
137         throws Exception {
138 
139         long productVersionId = ParamUtil.getLong(
140             actionRequest, "productVersionId");
141 
142         long productEntryId = ParamUtil.getLong(
143             actionRequest, "productEntryId");
144         String version = ParamUtil.getString(actionRequest, "version");
145         String changeLog = ParamUtil.getString(actionRequest, "changeLog");
146         String downloadPageURL = ParamUtil.getString(
147             actionRequest, "downloadPageURL");
148         String directDownloadURL = ParamUtil.getString(
149             actionRequest, "directDownloadURL");
150         boolean testDirectDownloadURL = ParamUtil.getBoolean(
151             actionRequest, "testDirectDownloadURL");
152         boolean repoStoreArtifact = ParamUtil.getBoolean(
153             actionRequest, "repoStoreArtifact");
154 
155         long[] frameworkVersionIds = ParamUtil.getLongValues(
156             actionRequest, "frameworkVersions");
157 
158         ServiceContext serviceContext = ServiceContextFactory.getInstance(
159             SCProductVersion.class.getName(), actionRequest);
160 
161         if (productVersionId <= 0) {
162 
163             // Add product version
164 
165             SCProductVersionServiceUtil.addProductVersion(
166                 productEntryId, version, changeLog, downloadPageURL,
167                 directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
168                 frameworkVersionIds, serviceContext);
169         }
170         else {
171 
172             // Update product version
173 
174             SCProductVersionServiceUtil.updateProductVersion(
175                 productVersionId, version, changeLog, downloadPageURL,
176                 directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
177                 frameworkVersionIds);
178         }
179     }
180 
181 }