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