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