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