1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.softwarecatalog.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.security.auth.PrincipalException;
21  import com.liferay.portal.service.ServiceContext;
22  import com.liferay.portal.service.ServiceContextFactory;
23  import com.liferay.portal.struts.PortletAction;
24  import com.liferay.portlet.softwarecatalog.DuplicateProductVersionDirectDownloadURLException;
25  import com.liferay.portlet.softwarecatalog.NoSuchProductVersionException;
26  import com.liferay.portlet.softwarecatalog.ProductVersionChangeLogException;
27  import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
28  import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
29  import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
30  import com.liferay.portlet.softwarecatalog.UnavailableProductVersionDirectDownloadURLException;
31  import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
32  import com.liferay.portlet.softwarecatalog.service.SCProductVersionServiceUtil;
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="EditProductVersionAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Jorge Ferrer
48   */
49  public class EditProductVersionAction 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                  updateProductVersion(actionRequest);
61              }
62              else if (cmd.equals(Constants.DELETE)) {
63                  deleteProductVersion(actionRequest);
64              }
65  
66              sendRedirect(actionRequest, actionResponse);
67          }
68          catch (Exception e) {
69              if (e instanceof NoSuchProductVersionException ||
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
77                          DuplicateProductVersionDirectDownloadURLException ||
78                       e instanceof ProductVersionChangeLogException ||
79                       e instanceof ProductVersionDownloadURLException ||
80                       e instanceof ProductVersionFrameworkVersionException ||
81                       e instanceof ProductVersionNameException ||
82                       e instanceof
83                          UnavailableProductVersionDirectDownloadURLException) {
84  
85                  SessionErrors.add(actionRequest, e.getClass().getName());
86              }
87              else {
88                  throw e;
89              }
90          }
91      }
92  
93      public ActionForward render(
94              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
95              RenderRequest renderRequest, RenderResponse renderResponse)
96          throws Exception {
97  
98          try {
99              ActionUtil.getProductVersion(renderRequest);
100         }
101         catch (Exception e) {
102             if (e instanceof NoSuchProductVersionException ||
103                 e instanceof PrincipalException) {
104 
105                 SessionErrors.add(renderRequest, 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             renderRequest, "portlet.software_catalog.edit_product_version"));
116     }
117 
118     protected void deleteProductVersion(ActionRequest actionRequest)
119         throws Exception {
120 
121         long productVersionId = ParamUtil.getLong(
122             actionRequest, "productVersionId");
123 
124         SCProductVersionServiceUtil.deleteProductVersion(productVersionId);
125     }
126 
127     protected void updateProductVersion(ActionRequest actionRequest)
128         throws Exception {
129 
130         long productVersionId = ParamUtil.getLong(
131             actionRequest, "productVersionId");
132 
133         long productEntryId = ParamUtil.getLong(
134             actionRequest, "productEntryId");
135         String version = ParamUtil.getString(actionRequest, "version");
136         String changeLog = ParamUtil.getString(actionRequest, "changeLog");
137         String downloadPageURL = ParamUtil.getString(
138             actionRequest, "downloadPageURL");
139         String directDownloadURL = ParamUtil.getString(
140             actionRequest, "directDownloadURL");
141         boolean testDirectDownloadURL = ParamUtil.getBoolean(
142             actionRequest, "testDirectDownloadURL");
143         boolean repoStoreArtifact = ParamUtil.getBoolean(
144             actionRequest, "repoStoreArtifact");
145 
146         long[] frameworkVersionIds = ParamUtil.getLongValues(
147             actionRequest, "frameworkVersions");
148 
149         ServiceContext serviceContext = ServiceContextFactory.getInstance(
150             SCProductVersion.class.getName(), actionRequest);
151 
152         if (productVersionId <= 0) {
153 
154             // Add product version
155 
156             SCProductVersionServiceUtil.addProductVersion(
157                 productEntryId, version, changeLog, downloadPageURL,
158                 directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
159                 frameworkVersionIds, serviceContext);
160         }
161         else {
162 
163             // Update product version
164 
165             SCProductVersionServiceUtil.updateProductVersion(
166                 productVersionId, version, changeLog, downloadPageURL,
167                 directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
168                 frameworkVersionIds);
169         }
170     }
171 
172 }