1
22
23 package com.liferay.portlet.softwarecatalog.service.impl;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.log.Log;
28 import com.liferay.portal.kernel.log.LogFactoryUtil;
29 import com.liferay.portal.kernel.search.SearchException;
30 import com.liferay.portal.kernel.util.HttpUtil;
31 import com.liferay.portal.kernel.util.Validator;
32 import com.liferay.portal.model.User;
33 import com.liferay.portal.util.HttpImpl;
34 import com.liferay.portlet.softwarecatalog.DuplicateProductVersionDirectDownloadURLException;
35 import com.liferay.portlet.softwarecatalog.ProductVersionChangeLogException;
36 import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
37 import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
38 import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
39 import com.liferay.portlet.softwarecatalog.UnavailableProductVersionDirectDownloadURLException;
40 import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
41 import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
42 import com.liferay.portlet.softwarecatalog.service.base.SCProductVersionLocalServiceBaseImpl;
43 import com.liferay.portlet.softwarecatalog.util.Indexer;
44
45 import java.util.Date;
46 import java.util.List;
47
48 import javax.servlet.http.HttpServletResponse;
49
50 import org.apache.commons.httpclient.HostConfiguration;
51 import org.apache.commons.httpclient.HttpClient;
52 import org.apache.commons.httpclient.methods.GetMethod;
53
54
61 public class SCProductVersionLocalServiceImpl
62 extends SCProductVersionLocalServiceBaseImpl {
63
64 public SCProductVersion addProductVersion(
65 long userId, long productEntryId, String version, String changeLog,
66 String downloadPageURL, String directDownloadURL,
67 boolean testDirectDownloadURL, boolean repoStoreArtifact,
68 long[] frameworkVersionIds, boolean addCommunityPermissions,
69 boolean addGuestPermissions)
70 throws PortalException, SystemException {
71
72 return addProductVersion(
73 userId, productEntryId, version, changeLog, downloadPageURL,
74 directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
75 frameworkVersionIds, Boolean.valueOf(addCommunityPermissions),
76 Boolean.valueOf(addGuestPermissions), null, null);
77 }
78
79 public SCProductVersion addProductVersion(
80 long userId, long productEntryId, String version, String changeLog,
81 String downloadPageURL, String directDownloadURL,
82 boolean testDirectDownloadURL, boolean repoStoreArtifact,
83 long[] frameworkVersionIds, String[] communityPermissions,
84 String[] guestPermissions)
85 throws PortalException, SystemException {
86
87 return addProductVersion(
88 userId, productEntryId, version, changeLog, downloadPageURL,
89 directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
90 frameworkVersionIds, null, null, communityPermissions,
91 guestPermissions);
92 }
93
94 public SCProductVersion addProductVersion(
95 long userId, long productEntryId, String version, String changeLog,
96 String downloadPageURL, String directDownloadURL,
97 boolean testDirectDownloadURL, boolean repoStoreArtifact,
98 long[] frameworkVersionIds, Boolean addCommunityPermissions,
99 Boolean addGuestPermissions, String[] communityPermissions,
100 String[] guestPermissions)
101 throws PortalException, SystemException {
102
103
105 User user = userPersistence.findByPrimaryKey(userId);
106 SCProductEntry productEntry =
107 scProductEntryPersistence.findByPrimaryKey(productEntryId);
108 directDownloadURL = directDownloadURL.trim().toLowerCase();
109 Date now = new Date();
110
111 validate(
112 0, version, changeLog, downloadPageURL, directDownloadURL,
113 testDirectDownloadURL, frameworkVersionIds);
114
115 long productVersionId = counterLocalService.increment();
116
117 SCProductVersion productVersion = scProductVersionPersistence.create(
118 productVersionId);
119
120 productVersion.setCompanyId(user.getCompanyId());
121 productVersion.setUserId(user.getUserId());
122 productVersion.setUserName(user.getFullName());
123 productVersion.setCreateDate(now);
124 productVersion.setModifiedDate(now);
125 productVersion.setProductEntryId(productEntryId);
126 productVersion.setVersion(version);
127 productVersion.setChangeLog(changeLog);
128 productVersion.setDownloadPageURL(downloadPageURL);
129 productVersion.setDirectDownloadURL(directDownloadURL);
130 productVersion.setRepoStoreArtifact(repoStoreArtifact);
131
132 scProductVersionPersistence.update(productVersion, false);
133
134
136 productEntry.setModifiedDate(now);
137
138 scProductEntryPersistence.update(productEntry, false);
139
140
142 scProductVersionPersistence.setSCFrameworkVersions(
143 productVersionId, frameworkVersionIds);
144
145
147 try {
148 Indexer.updateProductEntry(
149 productEntry.getCompanyId(), productEntry.getGroupId(),
150 productEntry.getUserId(), productEntry.getUserName(),
151 productEntry.getProductEntryId(), productEntry.getName(), now,
152 productVersion.getVersion(), productEntry.getType(),
153 productEntry.getShortDescription(),
154 productEntry.getLongDescription(), productEntry.getPageURL(),
155 productEntry.getRepoGroupId(),
156 productEntry.getRepoArtifactId());
157 }
158 catch (SearchException se) {
159 _log.error("Indexing " + productEntry.getProductEntryId(), se);
160 }
161
162 return productVersion;
163 }
164
165 public void deleteProductVersion(long productVersionId)
166 throws PortalException, SystemException {
167
168 SCProductVersion productVersion =
169 scProductVersionPersistence.findByPrimaryKey(productVersionId);
170
171 deleteProductVersion(productVersion);
172 }
173
174 public void deleteProductVersion(SCProductVersion productVersion)
175 throws SystemException {
176
177 scProductVersionPersistence.remove(productVersion);
178 }
179
180 public void deleteProductVersions(long productEntryId)
181 throws SystemException {
182
183 List<SCProductVersion> productVersions =
184 scProductVersionPersistence.findByProductEntryId(productEntryId);
185
186 for (SCProductVersion productVersion : productVersions) {
187 deleteProductVersion(productVersion);
188 }
189 }
190
191 public SCProductVersion getProductVersion(long productVersionId)
192 throws PortalException, SystemException {
193
194 return scProductVersionPersistence.findByPrimaryKey(productVersionId);
195 }
196
197 public SCProductVersion getProductVersionByDirectDownloadURL(
198 String directDownloadURL)
199 throws PortalException, SystemException {
200
201 return scProductVersionPersistence.findByDirectDownloadURL(
202 directDownloadURL);
203 }
204
205 public List<SCProductVersion> getProductVersions(
206 long productEntryId, int start, int end)
207 throws SystemException {
208
209 return scProductVersionPersistence.findByProductEntryId(
210 productEntryId, start, end);
211 }
212
213 public int getProductVersionsCount(long productEntryId)
214 throws SystemException {
215
216 return scProductVersionPersistence.countByProductEntryId(
217 productEntryId);
218 }
219
220 public SCProductVersion updateProductVersion(
221 long productVersionId, String version, String changeLog,
222 String downloadPageURL, String directDownloadURL,
223 boolean testDirectDownloadURL, boolean repoStoreArtifact,
224 long[] frameworkVersionIds)
225 throws PortalException, SystemException {
226
227
229 directDownloadURL = directDownloadURL.trim().toLowerCase();
230 Date now = new Date();
231
232 validate(
233 productVersionId, version, changeLog, downloadPageURL,
234 directDownloadURL, testDirectDownloadURL, frameworkVersionIds);
235
236 SCProductVersion productVersion =
237 scProductVersionPersistence.findByPrimaryKey(productVersionId);
238
239 productVersion.setModifiedDate(now);
240 productVersion.setVersion(version);
241 productVersion.setChangeLog(changeLog);
242 productVersion.setDownloadPageURL(downloadPageURL);
243 productVersion.setDirectDownloadURL(directDownloadURL);
244 productVersion.setRepoStoreArtifact(repoStoreArtifact);
245
246 scProductVersionPersistence.update(productVersion, false);
247
248
250 SCProductEntry productEntry =
251 scProductEntryPersistence.findByPrimaryKey(
252 productVersion.getProductEntryId());
253
254 productEntry.setModifiedDate(now);
255
256 scProductEntryPersistence.update(productEntry, false);
257
258
260 scProductVersionPersistence.setSCFrameworkVersions(
261 productVersionId, frameworkVersionIds);
262
263
265 try {
266 Indexer.updateProductEntry(
267 productEntry.getCompanyId(), productEntry.getGroupId(),
268 productEntry.getUserId(), productEntry.getUserName(),
269 productEntry.getProductEntryId(), productEntry.getName(), now,
270 productVersion.getVersion(), productEntry.getType(),
271 productEntry.getShortDescription(),
272 productEntry.getLongDescription(), productEntry.getPageURL(),
273 productEntry.getRepoGroupId(),
274 productEntry.getRepoArtifactId());
275 }
276 catch (SearchException se) {
277 _log.error("Indexing " + productEntry.getProductEntryId(), se);
278 }
279
280 return productVersion;
281 }
282
283 protected void validate(
284 long productVersionId, String version, String changeLog,
285 String downloadPageURL, String directDownloadURL,
286 boolean testDirectDownloadURL, long[] frameworkVersionIds)
287 throws PortalException, SystemException {
288
289 if (Validator.isNull(version)) {
290 throw new ProductVersionNameException();
291 }
292 else if (Validator.isNull(changeLog)) {
293 throw new ProductVersionChangeLogException();
294 }
295 else if (Validator.isNull(downloadPageURL) &&
296 Validator.isNull(directDownloadURL)) {
297
298 throw new ProductVersionDownloadURLException();
299 }
300 else if (Validator.isNotNull(directDownloadURL)) {
301 SCProductVersion productVersion =
302 scProductVersionPersistence.fetchByDirectDownloadURL(
303 directDownloadURL);
304
305 if ((productVersion != null) &&
306 (productVersion.getProductVersionId() != productVersionId)) {
307
308 throw new DuplicateProductVersionDirectDownloadURLException();
309 }
310
311 if (testDirectDownloadURL) {
312 testDirectDownloadURL(directDownloadURL);
313 }
314 }
315 else if (frameworkVersionIds.length == 0) {
316 throw new ProductVersionFrameworkVersionException();
317 }
318 }
319
320 protected void testDirectDownloadURL(String directDownloadURL)
321 throws PortalException {
322
323 try {
324 HttpImpl httpImpl = (HttpImpl)HttpUtil.getHttp();
325
326 HostConfiguration hostConfig = httpImpl.getHostConfig(
327 directDownloadURL);
328
329 HttpClient client = httpImpl.getClient(hostConfig);
330
331 GetMethod getFileMethod = new GetMethod(directDownloadURL);
332
333 int responseCode = client.executeMethod(
334 hostConfig, getFileMethod);
335
336 if (responseCode != HttpServletResponse.SC_OK) {
337 throw new UnavailableProductVersionDirectDownloadURLException();
338 }
339 }
340 catch (Exception e) {
341 throw new UnavailableProductVersionDirectDownloadURLException();
342 }
343 }
344
345 private static Log _log =
346 LogFactoryUtil.getLog(SCProductVersionLocalServiceImpl.class);
347
348 }