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.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.search.SearchException;
22  import com.liferay.portal.kernel.util.HttpUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.User;
25  import com.liferay.portal.service.ServiceContext;
26  import com.liferay.portal.util.HttpImpl;
27  import com.liferay.portlet.softwarecatalog.DuplicateProductVersionDirectDownloadURLException;
28  import com.liferay.portlet.softwarecatalog.ProductVersionChangeLogException;
29  import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
30  import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
31  import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
32  import com.liferay.portlet.softwarecatalog.UnavailableProductVersionDirectDownloadURLException;
33  import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
34  import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
35  import com.liferay.portlet.softwarecatalog.service.base.SCProductVersionLocalServiceBaseImpl;
36  import com.liferay.portlet.softwarecatalog.util.Indexer;
37  
38  import java.util.Date;
39  import java.util.List;
40  
41  import javax.servlet.http.HttpServletResponse;
42  
43  import org.apache.commons.httpclient.HostConfiguration;
44  import org.apache.commons.httpclient.HttpClient;
45  import org.apache.commons.httpclient.methods.GetMethod;
46  
47  /**
48   * <a href="SCProductVersionLocalServiceImpl.java.html"><b><i>View Source</i>
49   * </b></a>
50   *
51   * @author Jorge Ferrer
52   * @author Brian Wing Shun Chan
53   */
54  public class SCProductVersionLocalServiceImpl
55      extends SCProductVersionLocalServiceBaseImpl {
56  
57      public SCProductVersion addProductVersion(
58              long userId, long productEntryId, String version, String changeLog,
59              String downloadPageURL, String directDownloadURL,
60              boolean testDirectDownloadURL, boolean repoStoreArtifact,
61              long[] frameworkVersionIds, ServiceContext serviceContext)
62          throws PortalException, SystemException {
63  
64          // Product version
65  
66          User user = userPersistence.findByPrimaryKey(userId);
67          SCProductEntry productEntry =
68              scProductEntryPersistence.findByPrimaryKey(productEntryId);
69          directDownloadURL = directDownloadURL.trim().toLowerCase();
70          Date now = new Date();
71  
72          validate(
73              0, version, changeLog, downloadPageURL, directDownloadURL,
74              testDirectDownloadURL, frameworkVersionIds);
75  
76          long productVersionId = counterLocalService.increment();
77  
78          SCProductVersion productVersion = scProductVersionPersistence.create(
79              productVersionId);
80  
81          productVersion.setCompanyId(user.getCompanyId());
82          productVersion.setUserId(user.getUserId());
83          productVersion.setUserName(user.getFullName());
84          productVersion.setCreateDate(now);
85          productVersion.setModifiedDate(now);
86          productVersion.setProductEntryId(productEntryId);
87          productVersion.setVersion(version);
88          productVersion.setChangeLog(changeLog);
89          productVersion.setDownloadPageURL(downloadPageURL);
90          productVersion.setDirectDownloadURL(directDownloadURL);
91          productVersion.setRepoStoreArtifact(repoStoreArtifact);
92  
93          scProductVersionPersistence.update(productVersion, false);
94  
95          // Framework versions
96  
97          scProductVersionPersistence.setSCFrameworkVersions(
98              productVersionId, frameworkVersionIds);
99  
100         // Product entry
101 
102         productEntry.setModifiedDate(now);
103 
104         scProductEntryPersistence.update(productEntry, false);
105 
106         // Indexer
107 
108         try {
109             Indexer.updateProductEntry(
110                 productEntry.getCompanyId(), productEntry.getGroupId(),
111                 productEntry.getUserId(), productEntry.getUserName(),
112                 productEntry.getProductEntryId(), productEntry.getName(), now,
113                 productVersion.getVersion(), productEntry.getType(),
114                 productEntry.getShortDescription(),
115                 productEntry.getLongDescription(), productEntry.getPageURL(),
116                 productEntry.getRepoGroupId(),
117                 productEntry.getRepoArtifactId(),
118                 productEntry.getExpandoBridge());
119         }
120         catch (SearchException se) {
121             _log.error("Indexing " + productEntry.getProductEntryId(), se);
122         }
123 
124         return productVersion;
125     }
126 
127     public void deleteProductVersion(long productVersionId)
128         throws PortalException, SystemException {
129 
130         SCProductVersion productVersion =
131             scProductVersionPersistence.findByPrimaryKey(productVersionId);
132 
133         deleteProductVersion(productVersion);
134     }
135 
136     public void deleteProductVersion(SCProductVersion productVersion)
137         throws SystemException {
138 
139         scProductVersionPersistence.remove(productVersion);
140     }
141 
142     public void deleteProductVersions(long productEntryId)
143         throws SystemException {
144 
145         List<SCProductVersion> productVersions =
146             scProductVersionPersistence.findByProductEntryId(productEntryId);
147 
148         for (SCProductVersion productVersion : productVersions) {
149             deleteProductVersion(productVersion);
150         }
151     }
152 
153     public SCProductVersion getProductVersion(long productVersionId)
154         throws PortalException, SystemException {
155 
156         return scProductVersionPersistence.findByPrimaryKey(productVersionId);
157     }
158 
159     public SCProductVersion getProductVersionByDirectDownloadURL(
160             String directDownloadURL)
161         throws PortalException, SystemException {
162 
163         return scProductVersionPersistence.findByDirectDownloadURL(
164             directDownloadURL);
165     }
166 
167     public List<SCProductVersion> getProductVersions(
168             long productEntryId, int start, int end)
169         throws SystemException {
170 
171         return scProductVersionPersistence.findByProductEntryId(
172             productEntryId, start, end);
173     }
174 
175     public int getProductVersionsCount(long productEntryId)
176         throws SystemException {
177 
178         return scProductVersionPersistence.countByProductEntryId(
179             productEntryId);
180     }
181 
182     public SCProductVersion updateProductVersion(
183             long productVersionId, String version, String changeLog,
184             String downloadPageURL, String directDownloadURL,
185             boolean testDirectDownloadURL, boolean repoStoreArtifact,
186             long[] frameworkVersionIds)
187         throws PortalException, SystemException {
188 
189         // Product version
190 
191         directDownloadURL = directDownloadURL.trim().toLowerCase();
192         Date now = new Date();
193 
194         validate(
195             productVersionId, version, changeLog, downloadPageURL,
196             directDownloadURL, testDirectDownloadURL, frameworkVersionIds);
197 
198         SCProductVersion productVersion =
199             scProductVersionPersistence.findByPrimaryKey(productVersionId);
200 
201         productVersion.setModifiedDate(now);
202         productVersion.setVersion(version);
203         productVersion.setChangeLog(changeLog);
204         productVersion.setDownloadPageURL(downloadPageURL);
205         productVersion.setDirectDownloadURL(directDownloadURL);
206         productVersion.setRepoStoreArtifact(repoStoreArtifact);
207 
208         scProductVersionPersistence.update(productVersion, false);
209 
210         // Framework versions
211 
212         scProductVersionPersistence.setSCFrameworkVersions(
213             productVersionId, frameworkVersionIds);
214 
215         // Product entry
216 
217         SCProductEntry productEntry =
218             scProductEntryPersistence.findByPrimaryKey(
219                 productVersion.getProductEntryId());
220 
221         productEntry.setModifiedDate(now);
222 
223         scProductEntryPersistence.update(productEntry, false);
224 
225         // Indexer
226 
227         try {
228             Indexer.updateProductEntry(
229                 productEntry.getCompanyId(), productEntry.getGroupId(),
230                 productEntry.getUserId(), productEntry.getUserName(),
231                 productEntry.getProductEntryId(), productEntry.getName(), now,
232                 productVersion.getVersion(), productEntry.getType(),
233                 productEntry.getShortDescription(),
234                 productEntry.getLongDescription(), productEntry.getPageURL(),
235                 productEntry.getRepoGroupId(),
236                 productEntry.getRepoArtifactId(),
237                 productEntry.getExpandoBridge());
238         }
239         catch (SearchException se) {
240             _log.error("Indexing " + productEntry.getProductEntryId(), se);
241         }
242 
243         return productVersion;
244     }
245 
246     protected void testDirectDownloadURL(String directDownloadURL)
247         throws PortalException {
248 
249         try {
250             HttpImpl httpImpl = (HttpImpl)HttpUtil.getHttp();
251 
252             HostConfiguration hostConfig = httpImpl.getHostConfig(
253                 directDownloadURL);
254 
255             HttpClient client = httpImpl.getClient(hostConfig);
256 
257             GetMethod getFileMethod = new GetMethod(directDownloadURL);
258 
259             int responseCode = client.executeMethod(
260                 hostConfig, getFileMethod);
261 
262             if (responseCode != HttpServletResponse.SC_OK) {
263                 throw new UnavailableProductVersionDirectDownloadURLException();
264             }
265         }
266         catch (Exception e) {
267             throw new UnavailableProductVersionDirectDownloadURLException();
268         }
269     }
270 
271     protected void validate(
272             long productVersionId, String version, String changeLog,
273             String downloadPageURL, String directDownloadURL,
274             boolean testDirectDownloadURL, long[] frameworkVersionIds)
275         throws PortalException, SystemException {
276 
277         if (Validator.isNull(version)) {
278             throw new ProductVersionNameException();
279         }
280         else if (Validator.isNull(changeLog)) {
281             throw new ProductVersionChangeLogException();
282         }
283         else if (Validator.isNull(downloadPageURL) &&
284                  Validator.isNull(directDownloadURL)) {
285 
286             throw new ProductVersionDownloadURLException();
287         }
288         else if (Validator.isNotNull(directDownloadURL)) {
289             SCProductVersion productVersion =
290                 scProductVersionPersistence.fetchByDirectDownloadURL(
291                     directDownloadURL);
292 
293             if ((productVersion != null) &&
294                 (productVersion.getProductVersionId() != productVersionId)) {
295 
296                 throw new DuplicateProductVersionDirectDownloadURLException();
297             }
298 
299             if (testDirectDownloadURL) {
300                 testDirectDownloadURL(directDownloadURL);
301             }
302         }
303         else if (frameworkVersionIds.length == 0) {
304             throw new ProductVersionFrameworkVersionException();
305         }
306     }
307 
308     private static Log _log = LogFactoryUtil.getLog(
309         SCProductVersionLocalServiceImpl.class);
310 
311 }