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