001    /**
002     * Copyright (c) 2000-2013 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.security.lang.DoPrivilegedBean;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.util.HttpImpl;
027    import com.liferay.portlet.softwarecatalog.DuplicateProductVersionDirectDownloadURLException;
028    import com.liferay.portlet.softwarecatalog.ProductVersionChangeLogException;
029    import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
030    import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
031    import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
032    import com.liferay.portlet.softwarecatalog.UnavailableProductVersionDirectDownloadURLException;
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    
037    import java.util.Date;
038    import java.util.List;
039    
040    import javax.servlet.http.HttpServletResponse;
041    
042    import org.apache.commons.httpclient.HostConfiguration;
043    import org.apache.commons.httpclient.HttpClient;
044    import org.apache.commons.httpclient.methods.GetMethod;
045    
046    /**
047     * @author Jorge Ferrer
048     * @author Brian Wing Shun Chan
049     */
050    public class SCProductVersionLocalServiceImpl
051            extends SCProductVersionLocalServiceBaseImpl {
052    
053            @Override
054            public SCProductVersion addProductVersion(
055                            long userId, long productEntryId, String version, String changeLog,
056                            String downloadPageURL, String directDownloadURL,
057                            boolean testDirectDownloadURL, boolean repoStoreArtifact,
058                            long[] frameworkVersionIds, ServiceContext serviceContext)
059                    throws PortalException, SystemException {
060    
061                    // Product version
062    
063                    User user = userPersistence.findByPrimaryKey(userId);
064                    SCProductEntry productEntry =
065                            scProductEntryPersistence.findByPrimaryKey(productEntryId);
066                    directDownloadURL = directDownloadURL.trim().toLowerCase();
067                    Date now = new Date();
068    
069                    validate(
070                            0, version, changeLog, downloadPageURL, directDownloadURL,
071                            testDirectDownloadURL, frameworkVersionIds);
072    
073                    long productVersionId = counterLocalService.increment();
074    
075                    SCProductVersion productVersion = scProductVersionPersistence.create(
076                            productVersionId);
077    
078                    productVersion.setCompanyId(user.getCompanyId());
079                    productVersion.setUserId(user.getUserId());
080                    productVersion.setUserName(user.getFullName());
081                    productVersion.setCreateDate(now);
082                    productVersion.setModifiedDate(now);
083                    productVersion.setProductEntryId(productEntryId);
084                    productVersion.setVersion(version);
085                    productVersion.setChangeLog(changeLog);
086                    productVersion.setDownloadPageURL(downloadPageURL);
087                    productVersion.setDirectDownloadURL(directDownloadURL);
088                    productVersion.setRepoStoreArtifact(repoStoreArtifact);
089    
090                    scProductVersionPersistence.update(productVersion, false);
091    
092                    // Framework versions
093    
094                    scProductVersionPersistence.setSCFrameworkVersions(
095                            productVersionId, frameworkVersionIds);
096    
097                    // Product entry
098    
099                    productEntry.setModifiedDate(now);
100    
101                    scProductEntryPersistence.update(productEntry, false);
102    
103                    // Indexer
104    
105                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
106                            SCProductEntry.class);
107    
108                    indexer.reindex(productEntry);
109    
110                    return productVersion;
111            }
112    
113            @Override
114            public void deleteProductVersion(long productVersionId)
115                    throws PortalException, SystemException {
116    
117                    SCProductVersion productVersion =
118                            scProductVersionPersistence.findByPrimaryKey(productVersionId);
119    
120                    deleteProductVersion(productVersion);
121            }
122    
123            @Override
124            public void deleteProductVersion(SCProductVersion productVersion)
125                    throws SystemException {
126    
127                    scProductVersionPersistence.remove(productVersion);
128            }
129    
130            @Override
131            public void deleteProductVersions(long productEntryId)
132                    throws SystemException {
133    
134                    List<SCProductVersion> productVersions =
135                            scProductVersionPersistence.findByProductEntryId(productEntryId);
136    
137                    for (SCProductVersion productVersion : productVersions) {
138                            deleteProductVersion(productVersion);
139                    }
140            }
141    
142            @Override
143            public SCProductVersion getProductVersion(long productVersionId)
144                    throws PortalException, SystemException {
145    
146                    return scProductVersionPersistence.findByPrimaryKey(productVersionId);
147            }
148    
149            @Override
150            public SCProductVersion getProductVersionByDirectDownloadURL(
151                            String directDownloadURL)
152                    throws PortalException, SystemException {
153    
154                    return scProductVersionPersistence.findByDirectDownloadURL(
155                            directDownloadURL);
156            }
157    
158            @Override
159            public List<SCProductVersion> getProductVersions(
160                            long productEntryId, int start, int end)
161                    throws SystemException {
162    
163                    return scProductVersionPersistence.findByProductEntryId(
164                            productEntryId, start, end);
165            }
166    
167            @Override
168            public int getProductVersionsCount(long productEntryId)
169                    throws SystemException {
170    
171                    return scProductVersionPersistence.countByProductEntryId(
172                            productEntryId);
173            }
174    
175            @Override
176            public SCProductVersion updateProductVersion(
177                            long productVersionId, String version, String changeLog,
178                            String downloadPageURL, String directDownloadURL,
179                            boolean testDirectDownloadURL, boolean repoStoreArtifact,
180                            long[] frameworkVersionIds)
181                    throws PortalException, SystemException {
182    
183                    // Product version
184    
185                    directDownloadURL = directDownloadURL.trim().toLowerCase();
186                    Date now = new Date();
187    
188                    validate(
189                            productVersionId, version, changeLog, downloadPageURL,
190                            directDownloadURL, testDirectDownloadURL, frameworkVersionIds);
191    
192                    SCProductVersion productVersion =
193                            scProductVersionPersistence.findByPrimaryKey(productVersionId);
194    
195                    productVersion.setModifiedDate(now);
196                    productVersion.setVersion(version);
197                    productVersion.setChangeLog(changeLog);
198                    productVersion.setDownloadPageURL(downloadPageURL);
199                    productVersion.setDirectDownloadURL(directDownloadURL);
200                    productVersion.setRepoStoreArtifact(repoStoreArtifact);
201    
202                    scProductVersionPersistence.update(productVersion, false);
203    
204                    // Framework versions
205    
206                    scProductVersionPersistence.setSCFrameworkVersions(
207                            productVersionId, frameworkVersionIds);
208    
209                    // Product entry
210    
211                    SCProductEntry productEntry =
212                            scProductEntryPersistence.findByPrimaryKey(
213                                    productVersion.getProductEntryId());
214    
215                    productEntry.setModifiedDate(now);
216    
217                    scProductEntryPersistence.update(productEntry, false);
218    
219                    // Indexer
220    
221                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
222                            SCProductEntry.class);
223    
224                    indexer.reindex(productEntry);
225    
226                    return productVersion;
227            }
228    
229            protected void testDirectDownloadURL(String directDownloadURL)
230                    throws PortalException {
231    
232                    try {
233                            HttpImpl httpImpl = null;
234    
235                            Object httpObject = HttpUtil.getHttp();
236    
237                            if (httpObject instanceof DoPrivilegedBean) {
238                                    DoPrivilegedBean doPrivilegedBean =
239                                            (DoPrivilegedBean)httpObject;
240    
241                                    httpImpl = (HttpImpl)doPrivilegedBean.getActualBean();
242                            }
243                            else {
244                                    httpImpl = (HttpImpl)httpObject;
245                            }
246    
247                            HostConfiguration hostConfiguration = httpImpl.getHostConfiguration(
248                                    directDownloadURL);
249    
250                            HttpClient httpClient = httpImpl.getClient(hostConfiguration);
251    
252                            GetMethod getFileMethod = new GetMethod(directDownloadURL);
253    
254                            int responseCode = httpClient.executeMethod(
255                                    hostConfiguration, getFileMethod);
256    
257                            if (responseCode != HttpServletResponse.SC_OK) {
258                                    throw new UnavailableProductVersionDirectDownloadURLException();
259                            }
260                    }
261                    catch (Exception e) {
262                            throw new UnavailableProductVersionDirectDownloadURLException();
263                    }
264            }
265    
266            protected void validate(
267                            long productVersionId, String version, String changeLog,
268                            String downloadPageURL, String directDownloadURL,
269                            boolean testDirectDownloadURL, long[] frameworkVersionIds)
270                    throws PortalException, SystemException {
271    
272                    if (Validator.isNull(version)) {
273                            throw new ProductVersionNameException();
274                    }
275                    else if (Validator.isNull(changeLog)) {
276                            throw new ProductVersionChangeLogException();
277                    }
278                    else if (Validator.isNull(downloadPageURL) &&
279                                     Validator.isNull(directDownloadURL)) {
280    
281                            throw new ProductVersionDownloadURLException();
282                    }
283                    else if (Validator.isNotNull(directDownloadURL)) {
284                            SCProductVersion productVersion =
285                                    scProductVersionPersistence.fetchByDirectDownloadURL(
286                                            directDownloadURL);
287    
288                            if ((productVersion != null) &&
289                                    (productVersion.getProductVersionId() != productVersionId)) {
290    
291                                    throw new DuplicateProductVersionDirectDownloadURLException();
292                            }
293    
294                            if (testDirectDownloadURL) {
295                                    testDirectDownloadURL(directDownloadURL);
296                            }
297                    }
298                    else if (frameworkVersionIds.length == 0) {
299                            throw new ProductVersionFrameworkVersionException();
300                    }
301            }
302    
303    }