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.ProductVersionChangeLogException;
30  import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
31  import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
32  import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
33  import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
34  import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
35  import com.liferay.portlet.softwarecatalog.service.base.SCProductVersionLocalServiceBaseImpl;
36  import com.liferay.portlet.softwarecatalog.util.Indexer;
37  
38  import java.io.IOException;
39  
40  import java.util.Date;
41  import java.util.Iterator;
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          Date now = new Date();
99  
100         validate(
101             version, changeLog, downloadPageURL, directDownloadURL,
102             frameworkVersionIds);
103 
104         long productVersionId = counterLocalService.increment();
105 
106         SCProductVersion productVersion = scProductVersionPersistence.create(
107             productVersionId);
108 
109         productVersion.setCompanyId(user.getCompanyId());
110         productVersion.setUserId(user.getUserId());
111         productVersion.setUserName(user.getFullName());
112         productVersion.setCreateDate(now);
113         productVersion.setModifiedDate(now);
114         productVersion.setProductEntryId(productEntryId);
115         productVersion.setVersion(version);
116         productVersion.setChangeLog(changeLog);
117         productVersion.setDownloadPageURL(downloadPageURL);
118         productVersion.setDirectDownloadURL(directDownloadURL);
119         productVersion.setRepoStoreArtifact(repoStoreArtifact);
120 
121         scProductVersionPersistence.update(productVersion);
122 
123         // Product entry
124 
125         productEntry.setModifiedDate(now);
126 
127         scProductEntryPersistence.update(productEntry);
128 
129         // Framework versions
130 
131         scProductVersionPersistence.setSCFrameworkVersions(
132             productVersionId, frameworkVersionIds);
133 
134         // Lucene
135 
136         try {
137             Indexer.updateProductEntry(
138                 productEntry.getCompanyId(), productEntry.getGroupId(),
139                 productEntry.getUserId(), productEntry.getUserName(),
140                 productEntry.getProductEntryId(), productEntry.getName(), now,
141                 productVersion.getVersion(), productEntry.getType(),
142                 productEntry.getShortDescription(),
143                 productEntry.getLongDescription(), productEntry.getPageURL(),
144                 productEntry.getRepoGroupId(),
145                 productEntry.getRepoArtifactId());
146         }
147         catch (IOException ioe) {
148             _log.error("Indexing " + productEntry.getProductEntryId(), ioe);
149         }
150 
151         return productVersion;
152     }
153 
154     public void deleteProductVersion(long productVersionId)
155         throws PortalException, SystemException {
156 
157         SCProductVersion productVersion =
158             scProductVersionPersistence.findByPrimaryKey(productVersionId);
159 
160         deleteProductVersion(productVersion);
161     }
162 
163     public void deleteProductVersion(SCProductVersion productVersion)
164         throws PortalException, SystemException {
165 
166         scProductVersionPersistence.remove(productVersion);
167     }
168 
169     public void deleteProductVersions(long productEntryId)
170         throws PortalException, SystemException {
171 
172         Iterator itr = scProductVersionPersistence.findByProductEntryId(
173             productEntryId).iterator();
174 
175         while (itr.hasNext()) {
176             SCProductVersion productVersion = (SCProductVersion)itr.next();
177 
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 List getProductVersions(long productEntryId, int begin, int end)
189         throws SystemException {
190 
191         return scProductVersionPersistence.findByProductEntryId(
192             productEntryId, begin, end);
193     }
194 
195     public int getProductVersionsCount(long productEntryId)
196         throws SystemException {
197 
198         return scProductVersionPersistence.countByProductEntryId(
199             productEntryId);
200     }
201 
202     public SCProductVersion updateProductVersion(
203             long productVersionId, String version, String changeLog,
204             String downloadPageURL, String directDownloadURL,
205             boolean repoStoreArtifact, long[] frameworkVersionIds)
206         throws PortalException, SystemException {
207 
208         // Product version
209 
210         Date now = new Date();
211 
212         validate(
213             version, changeLog, downloadPageURL, directDownloadURL,
214             frameworkVersionIds);
215 
216         SCProductVersion productVersion =
217             scProductVersionPersistence.findByPrimaryKey(productVersionId);
218 
219         productVersion.setModifiedDate(now);
220         productVersion.setVersion(version);
221         productVersion.setChangeLog(changeLog);
222         productVersion.setDownloadPageURL(downloadPageURL);
223         productVersion.setDirectDownloadURL(directDownloadURL);
224         productVersion.setRepoStoreArtifact(repoStoreArtifact);
225 
226         scProductVersionPersistence.update(productVersion);
227 
228         // Product entry
229 
230         SCProductEntry productEntry =
231             scProductEntryPersistence.findByPrimaryKey(
232                 productVersion.getProductEntryId());
233 
234         productEntry.setModifiedDate(now);
235 
236         scProductEntryPersistence.update(productEntry);
237 
238         // Framework versions
239 
240         scProductVersionPersistence.setSCFrameworkVersions(
241             productVersionId, frameworkVersionIds);
242 
243         // Lucene
244 
245         try {
246             Indexer.updateProductEntry(
247                 productEntry.getCompanyId(), productEntry.getGroupId(),
248                 productEntry.getUserId(), productEntry.getUserName(),
249                 productEntry.getProductEntryId(), productEntry.getName(), now,
250                 productVersion.getVersion(), productEntry.getType(),
251                 productEntry.getShortDescription(),
252                 productEntry.getLongDescription(), productEntry.getPageURL(),
253                 productEntry.getRepoGroupId(),
254                 productEntry.getRepoArtifactId());
255         }
256         catch (IOException ioe) {
257             _log.error("Indexing " + productEntry.getProductEntryId(), ioe);
258         }
259 
260         return productVersion;
261     }
262 
263     private void validate(
264             String version, String changeLog, String downloadPageURL,
265             String directDownloadURL, long[] frameworkVersionIds)
266         throws PortalException {
267 
268         if (Validator.isNull(version)) {
269             throw new ProductVersionNameException();
270         }
271         else if (Validator.isNull(changeLog)) {
272             throw new ProductVersionChangeLogException();
273         }
274         else if (Validator.isNull(downloadPageURL) &&
275                  Validator.isNull(directDownloadURL)) {
276 
277             throw new ProductVersionDownloadURLException();
278         }
279         else if (frameworkVersionIds.length == 0) {
280             throw new ProductVersionFrameworkVersionException();
281         }
282     }
283 
284     private static Log _log =
285         LogFactory.getLog(SCProductVersionLocalServiceImpl.class);
286 
287 }