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