001    /**
002     * Copyright (c) 2000-present 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.search.Indexer;
019    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
020    import com.liferay.portal.kernel.util.Http;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.ServiceContext;
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    /**
042     * @author Jorge Ferrer
043     * @author Brian Wing Shun Chan
044     */
045    public class SCProductVersionLocalServiceImpl
046            extends SCProductVersionLocalServiceBaseImpl {
047    
048            @Override
049            public SCProductVersion addProductVersion(
050                            long userId, long productEntryId, String version, String changeLog,
051                            String downloadPageURL, String directDownloadURL,
052                            boolean testDirectDownloadURL, boolean repoStoreArtifact,
053                            long[] frameworkVersionIds, ServiceContext serviceContext)
054                    throws PortalException {
055    
056                    // Product version
057    
058                    User user = userPersistence.findByPrimaryKey(userId);
059                    SCProductEntry productEntry =
060                            scProductEntryPersistence.findByPrimaryKey(productEntryId);
061                    directDownloadURL = StringUtil.toLowerCase(directDownloadURL.trim());
062    
063                    validate(
064                            0, version, changeLog, downloadPageURL, directDownloadURL,
065                            testDirectDownloadURL, frameworkVersionIds);
066    
067                    long productVersionId = counterLocalService.increment();
068    
069                    SCProductVersion productVersion = scProductVersionPersistence.create(
070                            productVersionId);
071    
072                    productVersion.setCompanyId(user.getCompanyId());
073                    productVersion.setUserId(user.getUserId());
074                    productVersion.setUserName(user.getFullName());
075                    productVersion.setProductEntryId(productEntryId);
076                    productVersion.setVersion(version);
077                    productVersion.setChangeLog(changeLog);
078                    productVersion.setDownloadPageURL(downloadPageURL);
079                    productVersion.setDirectDownloadURL(directDownloadURL);
080                    productVersion.setRepoStoreArtifact(repoStoreArtifact);
081    
082                    scProductVersionPersistence.update(productVersion);
083    
084                    // Framework versions
085    
086                    scProductVersionPersistence.setSCFrameworkVersions(
087                            productVersionId, frameworkVersionIds);
088    
089                    // Product entry
090    
091                    productEntry.setModifiedDate(new Date());
092    
093                    scProductEntryPersistence.update(productEntry);
094    
095                    // Indexer
096    
097                    Indexer<SCProductEntry> indexer =
098                            IndexerRegistryUtil.nullSafeGetIndexer(SCProductEntry.class);
099    
100                    indexer.reindex(productEntry);
101    
102                    return productVersion;
103            }
104    
105            @Override
106            public void deleteProductVersion(long productVersionId)
107                    throws PortalException {
108    
109                    SCProductVersion productVersion =
110                            scProductVersionPersistence.findByPrimaryKey(productVersionId);
111    
112                    deleteProductVersion(productVersion);
113            }
114    
115            @Override
116            public void deleteProductVersion(SCProductVersion productVersion) {
117                    scProductVersionPersistence.remove(productVersion);
118            }
119    
120            @Override
121            public void deleteProductVersions(long productEntryId) {
122                    List<SCProductVersion> productVersions =
123                            scProductVersionPersistence.findByProductEntryId(productEntryId);
124    
125                    for (SCProductVersion productVersion : productVersions) {
126                            deleteProductVersion(productVersion);
127                    }
128            }
129    
130            @Override
131            public SCProductVersion getProductVersion(long productVersionId)
132                    throws PortalException {
133    
134                    return scProductVersionPersistence.findByPrimaryKey(productVersionId);
135            }
136    
137            @Override
138            public SCProductVersion getProductVersionByDirectDownloadURL(
139                            String directDownloadURL)
140                    throws PortalException {
141    
142                    return scProductVersionPersistence.findByDirectDownloadURL(
143                            directDownloadURL);
144            }
145    
146            @Override
147            public List<SCProductVersion> getProductVersions(
148                    long productEntryId, int start, int end) {
149    
150                    return scProductVersionPersistence.findByProductEntryId(
151                            productEntryId, start, end);
152            }
153    
154            @Override
155            public int getProductVersionsCount(long productEntryId) {
156                    return scProductVersionPersistence.countByProductEntryId(
157                            productEntryId);
158            }
159    
160            @Override
161            public SCProductVersion updateProductVersion(
162                            long productVersionId, String version, String changeLog,
163                            String downloadPageURL, String directDownloadURL,
164                            boolean testDirectDownloadURL, boolean repoStoreArtifact,
165                            long[] frameworkVersionIds)
166                    throws PortalException {
167    
168                    // Product version
169    
170                    directDownloadURL = StringUtil.toLowerCase(directDownloadURL.trim());
171    
172                    validate(
173                            productVersionId, version, changeLog, downloadPageURL,
174                            directDownloadURL, testDirectDownloadURL, frameworkVersionIds);
175    
176                    SCProductVersion productVersion =
177                            scProductVersionPersistence.findByPrimaryKey(productVersionId);
178    
179                    productVersion.setVersion(version);
180                    productVersion.setChangeLog(changeLog);
181                    productVersion.setDownloadPageURL(downloadPageURL);
182                    productVersion.setDirectDownloadURL(directDownloadURL);
183                    productVersion.setRepoStoreArtifact(repoStoreArtifact);
184    
185                    scProductVersionPersistence.update(productVersion);
186    
187                    // Framework versions
188    
189                    scProductVersionPersistence.setSCFrameworkVersions(
190                            productVersionId, frameworkVersionIds);
191    
192                    // Product entry
193    
194                    SCProductEntry productEntry =
195                            scProductEntryPersistence.findByPrimaryKey(
196                                    productVersion.getProductEntryId());
197    
198                    productEntry.setModifiedDate(new Date());
199    
200                    scProductEntryPersistence.update(productEntry);
201    
202                    // Indexer
203    
204                    Indexer<SCProductEntry> indexer =
205                            IndexerRegistryUtil.nullSafeGetIndexer(SCProductEntry.class);
206    
207                    indexer.reindex(productEntry);
208    
209                    return productVersion;
210            }
211    
212            protected void testDirectDownloadURL(String directDownloadURL)
213                    throws PortalException {
214    
215                    try {
216                            Http.Options options = new Http.Options();
217    
218                            options.setLocation(directDownloadURL);
219                            options.setPost(false);
220    
221                            HttpUtil.URLtoByteArray(options);
222    
223                            Http.Response response = options.getResponse();
224    
225                            if (response.getResponseCode() != HttpServletResponse.SC_OK) {
226                                    throw new UnavailableProductVersionDirectDownloadURLException();
227                            }
228                    }
229                    catch (Exception e) {
230                            throw new UnavailableProductVersionDirectDownloadURLException();
231                    }
232            }
233    
234            protected void validate(
235                            long productVersionId, String version, String changeLog,
236                            String downloadPageURL, String directDownloadURL,
237                            boolean testDirectDownloadURL, long[] frameworkVersionIds)
238                    throws PortalException {
239    
240                    if (Validator.isNull(version)) {
241                            throw new ProductVersionNameException();
242                    }
243                    else if (Validator.isNull(changeLog)) {
244                            throw new ProductVersionChangeLogException();
245                    }
246                    else if (Validator.isNull(downloadPageURL) &&
247                                     Validator.isNull(directDownloadURL)) {
248    
249                            throw new ProductVersionDownloadURLException();
250                    }
251                    else if (Validator.isNotNull(directDownloadURL)) {
252                            SCProductVersion productVersion =
253                                    scProductVersionPersistence.fetchByDirectDownloadURL(
254                                            directDownloadURL);
255    
256                            if ((productVersion != null) &&
257                                    (productVersion.getProductVersionId() != productVersionId)) {
258    
259                                    throw new DuplicateProductVersionDirectDownloadURLException(
260                                            "{productVersionId=" + productVersionId + "}");
261                            }
262    
263                            if (testDirectDownloadURL) {
264                                    testDirectDownloadURL(directDownloadURL);
265                            }
266                    }
267                    else if (frameworkVersionIds.length == 0) {
268                            throw new ProductVersionFrameworkVersionException();
269                    }
270            }
271    
272    }