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