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