001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.softwarecatalog.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.model.User;
027: import com.liferay.portlet.softwarecatalog.DuplicateProductVersionDirectDownloadURLException;
028: import com.liferay.portlet.softwarecatalog.NoSuchProductVersionException;
029: import com.liferay.portlet.softwarecatalog.ProductVersionChangeLogException;
030: import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
031: import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
032: import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
033: import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
034: import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
035: import com.liferay.portlet.softwarecatalog.service.base.SCProductVersionLocalServiceBaseImpl;
036: import com.liferay.portlet.softwarecatalog.util.Indexer;
037:
038: import java.io.IOException;
039:
040: import java.util.Date;
041: import java.util.Iterator;
042: import java.util.List;
043:
044: import org.apache.commons.logging.Log;
045: import org.apache.commons.logging.LogFactory;
046:
047: /**
048: * <a href="SCProductVersionLocalServiceImpl.java.html"><b><i>View Source</i>
049: * </b></a>
050: *
051: * @author Jorge Ferrer
052: * @author Brian Wing Shun Chan
053: *
054: */
055: public class SCProductVersionLocalServiceImpl extends
056: SCProductVersionLocalServiceBaseImpl {
057:
058: public SCProductVersion addProductVersion(long userId,
059: long productEntryId, String version, String changeLog,
060: String downloadPageURL, String directDownloadURL,
061: boolean repoStoreArtifact, long[] frameworkVersionIds,
062: boolean addCommunityPermissions, boolean addGuestPermissions)
063: throws PortalException, SystemException {
064:
065: return addProductVersion(userId, productEntryId, version,
066: changeLog, downloadPageURL, directDownloadURL,
067: repoStoreArtifact, frameworkVersionIds, Boolean
068: .valueOf(addCommunityPermissions), Boolean
069: .valueOf(addGuestPermissions), null, null);
070: }
071:
072: public SCProductVersion addProductVersion(long userId,
073: long productEntryId, String version, String changeLog,
074: String downloadPageURL, String directDownloadURL,
075: boolean repoStoreArtifact, long[] frameworkVersionIds,
076: String[] communityPermissions, String[] guestPermissions)
077: throws PortalException, SystemException {
078:
079: return addProductVersion(userId, productEntryId, version,
080: changeLog, downloadPageURL, directDownloadURL,
081: repoStoreArtifact, frameworkVersionIds, null, null,
082: communityPermissions, guestPermissions);
083: }
084:
085: public SCProductVersion addProductVersion(long userId,
086: long productEntryId, String version, String changeLog,
087: String downloadPageURL, String directDownloadURL,
088: boolean repoStoreArtifact, long[] frameworkVersionIds,
089: Boolean addCommunityPermissions,
090: Boolean addGuestPermissions, String[] communityPermissions,
091: String[] guestPermissions) throws PortalException,
092: SystemException {
093:
094: // Product version
095:
096: User user = userPersistence.findByPrimaryKey(userId);
097: SCProductEntry productEntry = scProductEntryPersistence
098: .findByPrimaryKey(productEntryId);
099: directDownloadURL = directDownloadURL.trim().toLowerCase();
100: Date now = new Date();
101:
102: validate(0, version, changeLog, downloadPageURL,
103: directDownloadURL, frameworkVersionIds);
104:
105: long productVersionId = counterLocalService.increment();
106:
107: SCProductVersion productVersion = scProductVersionPersistence
108: .create(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);
123:
124: // Product entry
125:
126: productEntry.setModifiedDate(now);
127:
128: scProductEntryPersistence.update(productEntry);
129:
130: // Framework versions
131:
132: scProductVersionPersistence.setSCFrameworkVersions(
133: productVersionId, frameworkVersionIds);
134:
135: // Lucene
136:
137: try {
138: Indexer.updateProductEntry(productEntry.getCompanyId(),
139: productEntry.getGroupId(),
140: productEntry.getUserId(), productEntry
141: .getUserName(), productEntry
142: .getProductEntryId(), productEntry
143: .getName(), now, productVersion
144: .getVersion(), productEntry.getType(),
145: productEntry.getShortDescription(), productEntry
146: .getLongDescription(), productEntry
147: .getPageURL(), productEntry
148: .getRepoGroupId(), productEntry
149: .getRepoArtifactId());
150: } catch (IOException ioe) {
151: _log.error("Indexing " + productEntry.getProductEntryId(),
152: ioe);
153: }
154:
155: return productVersion;
156: }
157:
158: public void deleteProductVersion(long productVersionId)
159: throws PortalException, SystemException {
160:
161: SCProductVersion productVersion = scProductVersionPersistence
162: .findByPrimaryKey(productVersionId);
163:
164: deleteProductVersion(productVersion);
165: }
166:
167: public void deleteProductVersion(SCProductVersion productVersion)
168: throws PortalException, SystemException {
169:
170: scProductVersionPersistence.remove(productVersion);
171: }
172:
173: public void deleteProductVersions(long productEntryId)
174: throws PortalException, SystemException {
175:
176: Iterator itr = scProductVersionPersistence
177: .findByProductEntryId(productEntryId).iterator();
178:
179: while (itr.hasNext()) {
180: SCProductVersion productVersion = (SCProductVersion) itr
181: .next();
182:
183: deleteProductVersion(productVersion);
184: }
185: }
186:
187: public SCProductVersion getProductVersion(long productVersionId)
188: throws PortalException, SystemException {
189:
190: return scProductVersionPersistence
191: .findByPrimaryKey(productVersionId);
192: }
193:
194: public SCProductVersion getProductVersionByDirectDownloadURL(
195: String directDownloadURL) throws PortalException,
196: SystemException {
197:
198: return scProductVersionPersistence
199: .findByDirectDownloadURL(directDownloadURL);
200: }
201:
202: public List getProductVersions(long productEntryId, int begin,
203: int end) throws SystemException {
204:
205: return scProductVersionPersistence.findByProductEntryId(
206: productEntryId, begin, end);
207: }
208:
209: public int getProductVersionsCount(long productEntryId)
210: throws SystemException {
211:
212: return scProductVersionPersistence
213: .countByProductEntryId(productEntryId);
214: }
215:
216: public SCProductVersion updateProductVersion(long productVersionId,
217: String version, String changeLog, String downloadPageURL,
218: String directDownloadURL, boolean repoStoreArtifact,
219: long[] frameworkVersionIds) throws PortalException,
220: SystemException {
221:
222: // Product version
223:
224: directDownloadURL = directDownloadURL.trim().toLowerCase();
225: Date now = new Date();
226:
227: validate(productVersionId, version, changeLog, downloadPageURL,
228: directDownloadURL, frameworkVersionIds);
229:
230: SCProductVersion productVersion = scProductVersionPersistence
231: .findByPrimaryKey(productVersionId);
232:
233: productVersion.setModifiedDate(now);
234: productVersion.setVersion(version);
235: productVersion.setChangeLog(changeLog);
236: productVersion.setDownloadPageURL(downloadPageURL);
237: productVersion.setDirectDownloadURL(directDownloadURL);
238: productVersion.setRepoStoreArtifact(repoStoreArtifact);
239:
240: scProductVersionPersistence.update(productVersion);
241:
242: // Product entry
243:
244: SCProductEntry productEntry = scProductEntryPersistence
245: .findByPrimaryKey(productVersion.getProductEntryId());
246:
247: productEntry.setModifiedDate(now);
248:
249: scProductEntryPersistence.update(productEntry);
250:
251: // Framework versions
252:
253: scProductVersionPersistence.setSCFrameworkVersions(
254: productVersionId, frameworkVersionIds);
255:
256: // Lucene
257:
258: try {
259: Indexer.updateProductEntry(productEntry.getCompanyId(),
260: productEntry.getGroupId(),
261: productEntry.getUserId(), productEntry
262: .getUserName(), productEntry
263: .getProductEntryId(), productEntry
264: .getName(), now, productVersion
265: .getVersion(), productEntry.getType(),
266: productEntry.getShortDescription(), productEntry
267: .getLongDescription(), productEntry
268: .getPageURL(), productEntry
269: .getRepoGroupId(), productEntry
270: .getRepoArtifactId());
271: } catch (IOException ioe) {
272: _log.error("Indexing " + productEntry.getProductEntryId(),
273: ioe);
274: }
275:
276: return productVersion;
277: }
278:
279: protected void validate(long productVersionId, String version,
280: String changeLog, String downloadPageURL,
281: String directDownloadURL, long[] frameworkVersionIds)
282: throws PortalException, SystemException {
283:
284: if (Validator.isNull(version)) {
285: throw new ProductVersionNameException();
286: } else if (Validator.isNull(changeLog)) {
287: throw new ProductVersionChangeLogException();
288: } else if (Validator.isNull(downloadPageURL)
289: && Validator.isNull(directDownloadURL)) {
290:
291: throw new ProductVersionDownloadURLException();
292: } else if (Validator.isNotNull(directDownloadURL)) {
293: try {
294: SCProductVersion productVersion = scProductVersionPersistence
295: .findByDirectDownloadURL(directDownloadURL);
296:
297: if (productVersion.getProductVersionId() != productVersionId) {
298: throw new DuplicateProductVersionDirectDownloadURLException();
299: }
300: } catch (NoSuchProductVersionException nspve) {
301: }
302: } else if (frameworkVersionIds.length == 0) {
303: throw new ProductVersionFrameworkVersionException();
304: }
305: }
306:
307: private static Log _log = LogFactory
308: .getLog(SCProductVersionLocalServiceImpl.class);
309:
310: }
|