1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.softwarecatalog.action;
16  
17  import com.liferay.portal.kernel.plugin.Version;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.util.PortalUtil;
21  import com.liferay.portal.util.WebKeys;
22  import com.liferay.portlet.softwarecatalog.model.SCFrameworkVersion;
23  import com.liferay.portlet.softwarecatalog.model.SCLicense;
24  import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
25  import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
26  import com.liferay.portlet.softwarecatalog.service.SCFrameworkVersionServiceUtil;
27  import com.liferay.portlet.softwarecatalog.service.SCLicenseServiceUtil;
28  import com.liferay.portlet.softwarecatalog.service.SCProductEntryServiceUtil;
29  import com.liferay.portlet.softwarecatalog.service.SCProductVersionServiceUtil;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.RenderRequest;
33  
34  import javax.servlet.http.HttpServletRequest;
35  
36  /**
37   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Jorge Ferrer
40   */
41  public class ActionUtil {
42  
43      public static void getFrameworkVersion(ActionRequest actionRequest)
44          throws Exception {
45  
46          HttpServletRequest request = PortalUtil.getHttpServletRequest(
47              actionRequest);
48  
49          getFrameworkVersion(request);
50      }
51  
52      public static void getFrameworkVersion(RenderRequest renderRequest)
53          throws Exception {
54  
55          HttpServletRequest request = PortalUtil.getHttpServletRequest(
56              renderRequest);
57  
58          getFrameworkVersion(request);
59      }
60  
61      public static void getFrameworkVersion(HttpServletRequest request)
62          throws Exception {
63  
64          long frameworkVersionId = ParamUtil.getLong(
65              request, "frameworkVersionId");
66  
67          SCFrameworkVersion frameworkVersion = null;
68  
69          if (frameworkVersionId > 0) {
70              frameworkVersion =
71                  SCFrameworkVersionServiceUtil.getFrameworkVersion(
72                      frameworkVersionId);
73          }
74  
75          request.setAttribute(
76              WebKeys.SOFTWARE_CATALOG_FRAMEWORK_VERSION, frameworkVersion);
77      }
78  
79      public static void getLicense(ActionRequest actionRequest)
80          throws Exception {
81  
82          HttpServletRequest request = PortalUtil.getHttpServletRequest(
83              actionRequest);
84  
85          getLicense(request);
86      }
87  
88      public static void getLicense(RenderRequest renderRequest)
89          throws Exception {
90  
91          HttpServletRequest request = PortalUtil.getHttpServletRequest(
92              renderRequest);
93  
94          getLicense(request);
95      }
96  
97      public static void getLicense(HttpServletRequest request) throws Exception {
98          long licenseId = ParamUtil.getLong(request, "licenseId");
99  
100         SCLicense license = null;
101 
102         if (licenseId > 0) {
103             license = SCLicenseServiceUtil.getLicense(licenseId);
104         }
105 
106         request.setAttribute(WebKeys.SOFTWARE_CATALOG_LICENSE, license);
107     }
108 
109     public static void getProductEntry(ActionRequest actionRequest)
110         throws Exception {
111 
112         HttpServletRequest request = PortalUtil.getHttpServletRequest(
113             actionRequest);
114 
115         getProductEntry(request);
116     }
117 
118     public static void getProductEntry(RenderRequest renderRequest)
119         throws Exception {
120 
121         HttpServletRequest request = PortalUtil.getHttpServletRequest(
122             renderRequest);
123 
124         getProductEntry(request);
125     }
126 
127     public static void getProductEntry(HttpServletRequest request)
128         throws Exception {
129 
130         long productEntryId = ParamUtil.getLong(request, "productEntryId");
131 
132         SCProductEntry productEntry = null;
133 
134         if (productEntryId > 0) {
135             productEntry = SCProductEntryServiceUtil.getProductEntry(
136                 productEntryId);
137         }
138 
139         request.setAttribute(
140             WebKeys.SOFTWARE_CATALOG_PRODUCT_ENTRY, productEntry);
141     }
142 
143     public static void getProductVersion(ActionRequest actionRequest)
144         throws Exception {
145 
146         HttpServletRequest request = PortalUtil.getHttpServletRequest(
147             actionRequest);
148 
149         getProductVersion(request);
150     }
151 
152     public static void getProductVersion(RenderRequest renderRequest)
153         throws Exception {
154 
155         HttpServletRequest request = PortalUtil.getHttpServletRequest(
156             renderRequest);
157 
158         getProductVersion(request);
159     }
160 
161     public static void getProductVersion(HttpServletRequest request)
162         throws Exception {
163 
164         long productVersionId = ParamUtil.getLong(request, "productVersionId");
165         long copyProductVersionId = ParamUtil.getLong(
166             request, "copyProductVersionId");
167 
168         SCProductVersion productVersion = null;
169         SCProductEntry productEntry = null;
170 
171         if (productVersionId > 0) {
172             productVersion = SCProductVersionServiceUtil.getProductVersion(
173                 productVersionId);
174 
175             productEntry = SCProductEntryServiceUtil.getProductEntry(
176                 productVersion.getProductEntryId());
177 
178             request.setAttribute(
179                 WebKeys.SOFTWARE_CATALOG_PRODUCT_VERSION, productVersion);
180 
181             request.setAttribute(
182                 WebKeys.SOFTWARE_CATALOG_PRODUCT_ENTRY, productEntry);
183         }
184         else if (copyProductVersionId > 0) {
185             productVersion = SCProductVersionServiceUtil.getProductVersion(
186                 copyProductVersionId);
187 
188             productEntry = SCProductEntryServiceUtil.getProductEntry(
189                 productVersion.getProductEntryId());
190 
191             String oldVersion = productVersion.getVersion();
192 
193             Version version = Version.getInstance(oldVersion);
194 
195             version = Version.incrementBuildNumber(version);
196 
197             String newVersion = version.toString();
198 
199             productVersion.setVersion(newVersion);
200 
201             String directDownloadURL = productVersion.getDirectDownloadURL();
202 
203             directDownloadURL = StringUtil.replace(
204                 directDownloadURL, oldVersion, newVersion);
205 
206             productVersion.setDirectDownloadURL(directDownloadURL);
207 
208             request.setAttribute(
209                 WebKeys.SOFTWARE_CATALOG_PRODUCT_VERSION, productVersion);
210 
211             request.setAttribute(
212                 WebKeys.SOFTWARE_CATALOG_PRODUCT_ENTRY, productEntry);
213         }
214         else {
215             getProductEntry(request);
216         }
217     }
218 
219 }