001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.softwarecatalog.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.search.Indexer;
020    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.util.HttpImpl;
026    import com.liferay.portlet.softwarecatalog.DuplicateProductVersionDirectDownloadURLException;
027    import com.liferay.portlet.softwarecatalog.ProductVersionChangeLogException;
028    import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
029    import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
030    import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
031    import com.liferay.portlet.softwarecatalog.UnavailableProductVersionDirectDownloadURLException;
032    import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
033    import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
034    import com.liferay.portlet.softwarecatalog.service.base.SCProductVersionLocalServiceBaseImpl;
035    
036    import java.util.Date;
037    import java.util.List;
038    
039    import javax.servlet.http.HttpServletResponse;
040    
041    import org.apache.commons.httpclient.HostConfiguration;
042    import org.apache.commons.httpclient.HttpClient;
043    import org.apache.commons.httpclient.methods.GetMethod;
044    
045    /**
046     * @author Jorge Ferrer
047     * @author Brian Wing Shun Chan
048     */
049    public class SCProductVersionLocalServiceImpl
050            extends SCProductVersionLocalServiceBaseImpl {
051    
052            public SCProductVersion addProductVersion(
053                            long userId, long productEntryId, String version, String changeLog,
054                            String downloadPageURL, String directDownloadURL,
055                            boolean testDirectDownloadURL, boolean repoStoreArtifact,
056                            long[] frameworkVersionIds, ServiceContext serviceContext)
057                    throws PortalException, SystemException {
058    
059                    // Product version
060    
061                    User user = userPersistence.findByPrimaryKey(userId);
062                    SCProductEntry productEntry =
063                            scProductEntryPersistence.findByPrimaryKey(productEntryId);
064                    directDownloadURL = directDownloadURL.trim().toLowerCase();
065                    Date now = new Date();
066    
067                    validate(
068                            0, version, changeLog, downloadPageURL, directDownloadURL,
069                            testDirectDownloadURL, frameworkVersionIds);
070    
071                    long productVersionId = counterLocalService.increment();
072    
073                    SCProductVersion productVersion = scProductVersionPersistence.create(
074                            productVersionId);
075    
076                    productVersion.setCompanyId(user.getCompanyId());
077                    productVersion.setUserId(user.getUserId());
078                    productVersion.setUserName(user.getFullName());
079                    productVersion.setCreateDate(now);
080                    productVersion.setModifiedDate(now);
081                    productVersion.setProductEntryId(productEntryId);
082                    productVersion.setVersion(version);
083                    productVersion.setChangeLog(changeLog);
084                    productVersion.setDownloadPageURL(downloadPageURL);
085                    productVersion.setDirectDownloadURL(directDownloadURL);
086                    productVersion.setRepoStoreArtifact(repoStoreArtifact);
087    
088                    scProductVersionPersistence.update(productVersion);
089    
090                    // Framework versions
091    
092                    scProductVersionPersistence.setSCFrameworkVersions(
093                            productVersionId, frameworkVersionIds);
094    
095                    // Product entry
096    
097                    productEntry.setModifiedDate(now);
098    
099                    scProductEntryPersistence.update(productEntry);
100    
101                    // Indexer
102    
103                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
104                            SCProductEntry.class);
105    
106                    indexer.reindex(productEntry);
107    
108                    return productVersion;
109            }
110    
111            public void deleteProductVersion(long productVersionId)
112                    throws PortalException, SystemException {
113    
114                    SCProductVersion productVersion =
115                            scProductVersionPersistence.findByPrimaryKey(productVersionId);
116    
117                    deleteProductVersion(productVersion);
118            }
119    
120            public void deleteProductVersion(SCProductVersion productVersion)
121                    throws SystemException {
122    
123                    scProductVersionPersistence.remove(productVersion);
124            }
125    
126            public void deleteProductVersions(long productEntryId)
127                    throws SystemException {
128    
129                    List<SCProductVersion> productVersions =
130                            scProductVersionPersistence.findByProductEntryId(productEntryId);
131    
132                    for (SCProductVersion productVersion : productVersions) {
133                            deleteProductVersion(productVersion);
134                    }
135            }
136    
137            public SCProductVersion getProductVersion(long productVersionId)
138                    throws PortalException, SystemException {
139    
140                    return scProductVersionPersistence.findByPrimaryKey(productVersionId);
141            }
142    
143            public SCProductVersion getProductVersionByDirectDownloadURL(
144                            String directDownloadURL)
145                    throws PortalException, SystemException {
146    
147                    return scProductVersionPersistence.findByDirectDownloadURL(
148                            directDownloadURL);
149            }
150    
151            public List<SCProductVersion> getProductVersions(
152                            long productEntryId, int start, int end)
153                    throws SystemException {
154    
155                    return scProductVersionPersistence.findByProductEntryId(
156                            productEntryId, start, end);
157            }
158    
159            public int getProductVersionsCount(long productEntryId)
160                    throws SystemException {
161    
162                    return scProductVersionPersistence.countByProductEntryId(
163                            productEntryId);
164            }
165    
166            public SCProductVersion updateProductVersion(
167                            long productVersionId, String version, String changeLog,
168                            String downloadPageURL, String directDownloadURL,
169                            boolean testDirectDownloadURL, boolean repoStoreArtifact,
170                            long[] frameworkVersionIds)
171                    throws PortalException, SystemException {
172    
173                    // Product version
174    
175                    directDownloadURL = directDownloadURL.trim().toLowerCase();
176                    Date now = new Date();
177    
178                    validate(
179                            productVersionId, version, changeLog, downloadPageURL,
180                            directDownloadURL, testDirectDownloadURL, frameworkVersionIds);
181    
182                    SCProductVersion productVersion =
183                            scProductVersionPersistence.findByPrimaryKey(productVersionId);
184    
185                    productVersion.setModifiedDate(now);
186                    productVersion.setVersion(version);
187                    productVersion.setChangeLog(changeLog);
188                    productVersion.setDownloadPageURL(downloadPageURL);
189                    productVersion.setDirectDownloadURL(directDownloadURL);
190                    productVersion.setRepoStoreArtifact(repoStoreArtifact);
191    
192                    scProductVersionPersistence.update(productVersion);
193    
194                    // Framework versions
195    
196                    scProductVersionPersistence.setSCFrameworkVersions(
197                            productVersionId, frameworkVersionIds);
198    
199                    // Product entry
200    
201                    SCProductEntry productEntry =
202                            scProductEntryPersistence.findByPrimaryKey(
203                                    productVersion.getProductEntryId());
204    
205                    productEntry.setModifiedDate(now);
206    
207                    scProductEntryPersistence.update(productEntry);
208    
209                    // Indexer
210    
211                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
212                            SCProductEntry.class);
213    
214                    indexer.reindex(productEntry);
215    
216                    return productVersion;
217            }
218    
219            protected void testDirectDownloadURL(String directDownloadURL)
220                    throws PortalException {
221    
222                    try {
223                            HttpImpl httpImpl = (HttpImpl)HttpUtil.getHttp();
224    
225                            HostConfiguration hostConfiguration = httpImpl.getHostConfiguration(
226                                    directDownloadURL);
227    
228                            HttpClient httpClient = httpImpl.getClient(hostConfiguration);
229    
230                            GetMethod getFileMethod = new GetMethod(directDownloadURL);
231    
232                            int responseCode = httpClient.executeMethod(
233                                    hostConfiguration, getFileMethod);
234    
235                            if (responseCode != HttpServletResponse.SC_OK) {
236                                    throw new UnavailableProductVersionDirectDownloadURLException();
237                            }
238                    }
239                    catch (Exception e) {
240                            throw new UnavailableProductVersionDirectDownloadURLException();
241                    }
242            }
243    
244            protected void validate(
245                            long productVersionId, String version, String changeLog,
246                            String downloadPageURL, String directDownloadURL,
247                            boolean testDirectDownloadURL, long[] frameworkVersionIds)
248                    throws PortalException, SystemException {
249    
250                    if (Validator.isNull(version)) {
251                            throw new ProductVersionNameException();
252                    }
253                    else if (Validator.isNull(changeLog)) {
254                            throw new ProductVersionChangeLogException();
255                    }
256                    else if (Validator.isNull(downloadPageURL) &&
257                                     Validator.isNull(directDownloadURL)) {
258    
259                            throw new ProductVersionDownloadURLException();
260                    }
261                    else if (Validator.isNotNull(directDownloadURL)) {
262                            SCProductVersion productVersion =
263                                    scProductVersionPersistence.fetchByDirectDownloadURL(
264                                            directDownloadURL);
265    
266                            if ((productVersion != null) &&
267                                    (productVersion.getProductVersionId() != productVersionId)) {
268    
269                                    throw new DuplicateProductVersionDirectDownloadURLException();
270                            }
271    
272                            if (testDirectDownloadURL) {
273                                    testDirectDownloadURL(directDownloadURL);
274                            }
275                    }
276                    else if (frameworkVersionIds.length == 0) {
277                            throw new ProductVersionFrameworkVersionException();
278                    }
279            }
280    
281    }