001    /**
002     * Copyright (c) 2000-2010 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, false);
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, false);
100    
101                    // Indexer
102    
103                    Indexer indexer = IndexerRegistryUtil.getIndexer(SCProductEntry.class);
104    
105                    indexer.reindex(productEntry);
106    
107                    return productVersion;
108            }
109    
110            public void deleteProductVersion(long productVersionId)
111                    throws PortalException, SystemException {
112    
113                    SCProductVersion productVersion =
114                            scProductVersionPersistence.findByPrimaryKey(productVersionId);
115    
116                    deleteProductVersion(productVersion);
117            }
118    
119            public void deleteProductVersion(SCProductVersion productVersion)
120                    throws SystemException {
121    
122                    scProductVersionPersistence.remove(productVersion);
123            }
124    
125            public void deleteProductVersions(long productEntryId)
126                    throws SystemException {
127    
128                    List<SCProductVersion> productVersions =
129                            scProductVersionPersistence.findByProductEntryId(productEntryId);
130    
131                    for (SCProductVersion productVersion : productVersions) {
132                            deleteProductVersion(productVersion);
133                    }
134            }
135    
136            public SCProductVersion getProductVersion(long productVersionId)
137                    throws PortalException, SystemException {
138    
139                    return scProductVersionPersistence.findByPrimaryKey(productVersionId);
140            }
141    
142            public SCProductVersion getProductVersionByDirectDownloadURL(
143                            String directDownloadURL)
144                    throws PortalException, SystemException {
145    
146                    return scProductVersionPersistence.findByDirectDownloadURL(
147                            directDownloadURL);
148            }
149    
150            public List<SCProductVersion> getProductVersions(
151                            long productEntryId, int start, int end)
152                    throws SystemException {
153    
154                    return scProductVersionPersistence.findByProductEntryId(
155                            productEntryId, start, end);
156            }
157    
158            public int getProductVersionsCount(long productEntryId)
159                    throws SystemException {
160    
161                    return scProductVersionPersistence.countByProductEntryId(
162                            productEntryId);
163            }
164    
165            public SCProductVersion updateProductVersion(
166                            long productVersionId, String version, String changeLog,
167                            String downloadPageURL, String directDownloadURL,
168                            boolean testDirectDownloadURL, boolean repoStoreArtifact,
169                            long[] frameworkVersionIds)
170                    throws PortalException, SystemException {
171    
172                    // Product version
173    
174                    directDownloadURL = directDownloadURL.trim().toLowerCase();
175                    Date now = new Date();
176    
177                    validate(
178                            productVersionId, version, changeLog, downloadPageURL,
179                            directDownloadURL, testDirectDownloadURL, frameworkVersionIds);
180    
181                    SCProductVersion productVersion =
182                            scProductVersionPersistence.findByPrimaryKey(productVersionId);
183    
184                    productVersion.setModifiedDate(now);
185                    productVersion.setVersion(version);
186                    productVersion.setChangeLog(changeLog);
187                    productVersion.setDownloadPageURL(downloadPageURL);
188                    productVersion.setDirectDownloadURL(directDownloadURL);
189                    productVersion.setRepoStoreArtifact(repoStoreArtifact);
190    
191                    scProductVersionPersistence.update(productVersion, false);
192    
193                    // Framework versions
194    
195                    scProductVersionPersistence.setSCFrameworkVersions(
196                            productVersionId, frameworkVersionIds);
197    
198                    // Product entry
199    
200                    SCProductEntry productEntry =
201                            scProductEntryPersistence.findByPrimaryKey(
202                                    productVersion.getProductEntryId());
203    
204                    productEntry.setModifiedDate(now);
205    
206                    scProductEntryPersistence.update(productEntry, false);
207    
208                    // Indexer
209    
210                    Indexer indexer = IndexerRegistryUtil.getIndexer(SCProductEntry.class);
211    
212                    indexer.reindex(productEntry);
213    
214                    return productVersion;
215            }
216    
217            protected void testDirectDownloadURL(String directDownloadURL)
218                    throws PortalException {
219    
220                    try {
221                            HttpImpl httpImpl = (HttpImpl)HttpUtil.getHttp();
222    
223                            HostConfiguration hostConfig = httpImpl.getHostConfig(
224                                    directDownloadURL);
225    
226                            HttpClient client = httpImpl.getClient(hostConfig);
227    
228                            GetMethod getFileMethod = new GetMethod(directDownloadURL);
229    
230                            int responseCode = client.executeMethod(
231                                    hostConfig, getFileMethod);
232    
233                            if (responseCode != HttpServletResponse.SC_OK) {
234                                    throw new UnavailableProductVersionDirectDownloadURLException();
235                            }
236                    }
237                    catch (Exception e) {
238                            throw new UnavailableProductVersionDirectDownloadURLException();
239                    }
240            }
241    
242            protected void validate(
243                            long productVersionId, String version, String changeLog,
244                            String downloadPageURL, String directDownloadURL,
245                            boolean testDirectDownloadURL, long[] frameworkVersionIds)
246                    throws PortalException, SystemException {
247    
248                    if (Validator.isNull(version)) {
249                            throw new ProductVersionNameException();
250                    }
251                    else if (Validator.isNull(changeLog)) {
252                            throw new ProductVersionChangeLogException();
253                    }
254                    else if (Validator.isNull(downloadPageURL) &&
255                                     Validator.isNull(directDownloadURL)) {
256    
257                            throw new ProductVersionDownloadURLException();
258                    }
259                    else if (Validator.isNotNull(directDownloadURL)) {
260                            SCProductVersion productVersion =
261                                    scProductVersionPersistence.fetchByDirectDownloadURL(
262                                            directDownloadURL);
263    
264                            if ((productVersion != null) &&
265                                    (productVersion.getProductVersionId() != productVersionId)) {
266    
267                                    throw new DuplicateProductVersionDirectDownloadURLException();
268                            }
269    
270                            if (testDirectDownloadURL) {
271                                    testDirectDownloadURL(directDownloadURL);
272                            }
273                    }
274                    else if (frameworkVersionIds.length == 0) {
275                            throw new ProductVersionFrameworkVersionException();
276                    }
277            }
278    
279    }