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