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