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