1
14
15 package com.liferay.portlet.softwarecatalog.service.impl;
16
17 import com.liferay.portal.kernel.exception.PortalException;
18 import com.liferay.portal.kernel.exception.SystemException;
19 import com.liferay.portal.kernel.search.Indexer;
20 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
21 import com.liferay.portal.kernel.util.HttpUtil;
22 import com.liferay.portal.kernel.util.Validator;
23 import com.liferay.portal.model.User;
24 import com.liferay.portal.service.ServiceContext;
25 import com.liferay.portal.util.HttpImpl;
26 import com.liferay.portlet.softwarecatalog.DuplicateProductVersionDirectDownloadURLException;
27 import com.liferay.portlet.softwarecatalog.ProductVersionChangeLogException;
28 import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
29 import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
30 import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
31 import com.liferay.portlet.softwarecatalog.UnavailableProductVersionDirectDownloadURLException;
32 import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
33 import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
34 import com.liferay.portlet.softwarecatalog.service.base.SCProductVersionLocalServiceBaseImpl;
35
36 import java.util.Date;
37 import java.util.List;
38
39 import javax.servlet.http.HttpServletResponse;
40
41 import org.apache.commons.httpclient.HostConfiguration;
42 import org.apache.commons.httpclient.HttpClient;
43 import org.apache.commons.httpclient.methods.GetMethod;
44
45
52 public class SCProductVersionLocalServiceImpl
53 extends SCProductVersionLocalServiceBaseImpl {
54
55 public SCProductVersion addProductVersion(
56 long userId, long productEntryId, String version, String changeLog,
57 String downloadPageURL, String directDownloadURL,
58 boolean testDirectDownloadURL, boolean repoStoreArtifact,
59 long[] frameworkVersionIds, ServiceContext serviceContext)
60 throws PortalException, SystemException {
61
62
64 User user = userPersistence.findByPrimaryKey(userId);
65 SCProductEntry productEntry =
66 scProductEntryPersistence.findByPrimaryKey(productEntryId);
67 directDownloadURL = directDownloadURL.trim().toLowerCase();
68 Date now = new Date();
69
70 validate(
71 0, version, changeLog, downloadPageURL, directDownloadURL,
72 testDirectDownloadURL, frameworkVersionIds);
73
74 long productVersionId = counterLocalService.increment();
75
76 SCProductVersion productVersion = scProductVersionPersistence.create(
77 productVersionId);
78
79 productVersion.setCompanyId(user.getCompanyId());
80 productVersion.setUserId(user.getUserId());
81 productVersion.setUserName(user.getFullName());
82 productVersion.setCreateDate(now);
83 productVersion.setModifiedDate(now);
84 productVersion.setProductEntryId(productEntryId);
85 productVersion.setVersion(version);
86 productVersion.setChangeLog(changeLog);
87 productVersion.setDownloadPageURL(downloadPageURL);
88 productVersion.setDirectDownloadURL(directDownloadURL);
89 productVersion.setRepoStoreArtifact(repoStoreArtifact);
90
91 scProductVersionPersistence.update(productVersion, false);
92
93
95 scProductVersionPersistence.setSCFrameworkVersions(
96 productVersionId, frameworkVersionIds);
97
98
100 productEntry.setModifiedDate(now);
101
102 scProductEntryPersistence.update(productEntry, false);
103
104
106 Indexer indexer = IndexerRegistryUtil.getIndexer(SCProductEntry.class);
107
108 indexer.reindex(productEntry);
109
110 return productVersion;
111 }
112
113 public void deleteProductVersion(long productVersionId)
114 throws PortalException, SystemException {
115
116 SCProductVersion productVersion =
117 scProductVersionPersistence.findByPrimaryKey(productVersionId);
118
119 deleteProductVersion(productVersion);
120 }
121
122 public void deleteProductVersion(SCProductVersion productVersion)
123 throws SystemException {
124
125 scProductVersionPersistence.remove(productVersion);
126 }
127
128 public void deleteProductVersions(long productEntryId)
129 throws SystemException {
130
131 List<SCProductVersion> productVersions =
132 scProductVersionPersistence.findByProductEntryId(productEntryId);
133
134 for (SCProductVersion productVersion : productVersions) {
135 deleteProductVersion(productVersion);
136 }
137 }
138
139 public SCProductVersion getProductVersion(long productVersionId)
140 throws PortalException, SystemException {
141
142 return scProductVersionPersistence.findByPrimaryKey(productVersionId);
143 }
144
145 public SCProductVersion getProductVersionByDirectDownloadURL(
146 String directDownloadURL)
147 throws PortalException, SystemException {
148
149 return scProductVersionPersistence.findByDirectDownloadURL(
150 directDownloadURL);
151 }
152
153 public List<SCProductVersion> getProductVersions(
154 long productEntryId, int start, int end)
155 throws SystemException {
156
157 return scProductVersionPersistence.findByProductEntryId(
158 productEntryId, start, end);
159 }
160
161 public int getProductVersionsCount(long productEntryId)
162 throws SystemException {
163
164 return scProductVersionPersistence.countByProductEntryId(
165 productEntryId);
166 }
167
168 public SCProductVersion updateProductVersion(
169 long productVersionId, String version, String changeLog,
170 String downloadPageURL, String directDownloadURL,
171 boolean testDirectDownloadURL, boolean repoStoreArtifact,
172 long[] frameworkVersionIds)
173 throws PortalException, SystemException {
174
175
177 directDownloadURL = directDownloadURL.trim().toLowerCase();
178 Date now = new Date();
179
180 validate(
181 productVersionId, version, changeLog, downloadPageURL,
182 directDownloadURL, testDirectDownloadURL, frameworkVersionIds);
183
184 SCProductVersion productVersion =
185 scProductVersionPersistence.findByPrimaryKey(productVersionId);
186
187 productVersion.setModifiedDate(now);
188 productVersion.setVersion(version);
189 productVersion.setChangeLog(changeLog);
190 productVersion.setDownloadPageURL(downloadPageURL);
191 productVersion.setDirectDownloadURL(directDownloadURL);
192 productVersion.setRepoStoreArtifact(repoStoreArtifact);
193
194 scProductVersionPersistence.update(productVersion, false);
195
196
198 scProductVersionPersistence.setSCFrameworkVersions(
199 productVersionId, frameworkVersionIds);
200
201
203 SCProductEntry productEntry =
204 scProductEntryPersistence.findByPrimaryKey(
205 productVersion.getProductEntryId());
206
207 productEntry.setModifiedDate(now);
208
209 scProductEntryPersistence.update(productEntry, false);
210
211
213 Indexer indexer = IndexerRegistryUtil.getIndexer(SCProductEntry.class);
214
215 indexer.reindex(productEntry);
216
217 return productVersion;
218 }
219
220 protected void testDirectDownloadURL(String directDownloadURL)
221 throws PortalException {
222
223 try {
224 HttpImpl httpImpl = (HttpImpl)HttpUtil.getHttp();
225
226 HostConfiguration hostConfig = httpImpl.getHostConfig(
227 directDownloadURL);
228
229 HttpClient client = httpImpl.getClient(hostConfig);
230
231 GetMethod getFileMethod = new GetMethod(directDownloadURL);
232
233 int responseCode = client.executeMethod(
234 hostConfig, getFileMethod);
235
236 if (responseCode != HttpServletResponse.SC_OK) {
237 throw new UnavailableProductVersionDirectDownloadURLException();
238 }
239 }
240 catch (Exception e) {
241 throw new UnavailableProductVersionDirectDownloadURLException();
242 }
243 }
244
245 protected void validate(
246 long productVersionId, String version, String changeLog,
247 String downloadPageURL, String directDownloadURL,
248 boolean testDirectDownloadURL, long[] frameworkVersionIds)
249 throws PortalException, SystemException {
250
251 if (Validator.isNull(version)) {
252 throw new ProductVersionNameException();
253 }
254 else if (Validator.isNull(changeLog)) {
255 throw new ProductVersionChangeLogException();
256 }
257 else if (Validator.isNull(downloadPageURL) &&
258 Validator.isNull(directDownloadURL)) {
259
260 throw new ProductVersionDownloadURLException();
261 }
262 else if (Validator.isNotNull(directDownloadURL)) {
263 SCProductVersion productVersion =
264 scProductVersionPersistence.fetchByDirectDownloadURL(
265 directDownloadURL);
266
267 if ((productVersion != null) &&
268 (productVersion.getProductVersionId() != productVersionId)) {
269
270 throw new DuplicateProductVersionDirectDownloadURLException();
271 }
272
273 if (testDirectDownloadURL) {
274 testDirectDownloadURL(directDownloadURL);
275 }
276 }
277 else if (frameworkVersionIds.length == 0) {
278 throw new ProductVersionFrameworkVersionException();
279 }
280 }
281
282 }