001
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.security.lang.DoPrivilegedBean;
025 import com.liferay.portal.service.ServiceContext;
026 import com.liferay.portal.util.HttpImpl;
027 import com.liferay.portlet.softwarecatalog.DuplicateProductVersionDirectDownloadURLException;
028 import com.liferay.portlet.softwarecatalog.ProductVersionChangeLogException;
029 import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
030 import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
031 import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
032 import com.liferay.portlet.softwarecatalog.UnavailableProductVersionDirectDownloadURLException;
033 import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
034 import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
035 import com.liferay.portlet.softwarecatalog.service.base.SCProductVersionLocalServiceBaseImpl;
036
037 import java.util.Date;
038 import java.util.List;
039
040 import javax.servlet.http.HttpServletResponse;
041
042 import org.apache.commons.httpclient.HostConfiguration;
043 import org.apache.commons.httpclient.HttpClient;
044 import org.apache.commons.httpclient.methods.GetMethod;
045
046
050 public class SCProductVersionLocalServiceImpl
051 extends SCProductVersionLocalServiceBaseImpl {
052
053 public SCProductVersion addProductVersion(
054 long userId, long productEntryId, String version, String changeLog,
055 String downloadPageURL, String directDownloadURL,
056 boolean testDirectDownloadURL, boolean repoStoreArtifact,
057 long[] frameworkVersionIds, ServiceContext serviceContext)
058 throws PortalException, SystemException {
059
060
061
062 User user = userPersistence.findByPrimaryKey(userId);
063 SCProductEntry productEntry =
064 scProductEntryPersistence.findByPrimaryKey(productEntryId);
065 directDownloadURL = directDownloadURL.trim().toLowerCase();
066 Date now = new Date();
067
068 validate(
069 0, version, changeLog, downloadPageURL, directDownloadURL,
070 testDirectDownloadURL, frameworkVersionIds);
071
072 long productVersionId = counterLocalService.increment();
073
074 SCProductVersion productVersion = scProductVersionPersistence.create(
075 productVersionId);
076
077 productVersion.setCompanyId(user.getCompanyId());
078 productVersion.setUserId(user.getUserId());
079 productVersion.setUserName(user.getFullName());
080 productVersion.setCreateDate(now);
081 productVersion.setModifiedDate(now);
082 productVersion.setProductEntryId(productEntryId);
083 productVersion.setVersion(version);
084 productVersion.setChangeLog(changeLog);
085 productVersion.setDownloadPageURL(downloadPageURL);
086 productVersion.setDirectDownloadURL(directDownloadURL);
087 productVersion.setRepoStoreArtifact(repoStoreArtifact);
088
089 scProductVersionPersistence.update(productVersion);
090
091
092
093 scProductVersionPersistence.setSCFrameworkVersions(
094 productVersionId, frameworkVersionIds);
095
096
097
098 productEntry.setModifiedDate(now);
099
100 scProductEntryPersistence.update(productEntry);
101
102
103
104 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
105 SCProductEntry.class);
106
107 indexer.reindex(productEntry);
108
109 return productVersion;
110 }
111
112 public void deleteProductVersion(long productVersionId)
113 throws PortalException, SystemException {
114
115 SCProductVersion productVersion =
116 scProductVersionPersistence.findByPrimaryKey(productVersionId);
117
118 deleteProductVersion(productVersion);
119 }
120
121 public void deleteProductVersion(SCProductVersion productVersion)
122 throws SystemException {
123
124 scProductVersionPersistence.remove(productVersion);
125 }
126
127 public void deleteProductVersions(long productEntryId)
128 throws SystemException {
129
130 List<SCProductVersion> productVersions =
131 scProductVersionPersistence.findByProductEntryId(productEntryId);
132
133 for (SCProductVersion productVersion : productVersions) {
134 deleteProductVersion(productVersion);
135 }
136 }
137
138 public SCProductVersion getProductVersion(long productVersionId)
139 throws PortalException, SystemException {
140
141 return scProductVersionPersistence.findByPrimaryKey(productVersionId);
142 }
143
144 public SCProductVersion getProductVersionByDirectDownloadURL(
145 String directDownloadURL)
146 throws PortalException, SystemException {
147
148 return scProductVersionPersistence.findByDirectDownloadURL(
149 directDownloadURL);
150 }
151
152 public List<SCProductVersion> getProductVersions(
153 long productEntryId, int start, int end)
154 throws SystemException {
155
156 return scProductVersionPersistence.findByProductEntryId(
157 productEntryId, start, end);
158 }
159
160 public int getProductVersionsCount(long productEntryId)
161 throws SystemException {
162
163 return scProductVersionPersistence.countByProductEntryId(
164 productEntryId);
165 }
166
167 public SCProductVersion updateProductVersion(
168 long productVersionId, String version, String changeLog,
169 String downloadPageURL, String directDownloadURL,
170 boolean testDirectDownloadURL, boolean repoStoreArtifact,
171 long[] frameworkVersionIds)
172 throws PortalException, SystemException {
173
174
175
176 directDownloadURL = directDownloadURL.trim().toLowerCase();
177 Date now = new Date();
178
179 validate(
180 productVersionId, version, changeLog, downloadPageURL,
181 directDownloadURL, testDirectDownloadURL, frameworkVersionIds);
182
183 SCProductVersion productVersion =
184 scProductVersionPersistence.findByPrimaryKey(productVersionId);
185
186 productVersion.setModifiedDate(now);
187 productVersion.setVersion(version);
188 productVersion.setChangeLog(changeLog);
189 productVersion.setDownloadPageURL(downloadPageURL);
190 productVersion.setDirectDownloadURL(directDownloadURL);
191 productVersion.setRepoStoreArtifact(repoStoreArtifact);
192
193 scProductVersionPersistence.update(productVersion);
194
195
196
197 scProductVersionPersistence.setSCFrameworkVersions(
198 productVersionId, frameworkVersionIds);
199
200
201
202 SCProductEntry productEntry =
203 scProductEntryPersistence.findByPrimaryKey(
204 productVersion.getProductEntryId());
205
206 productEntry.setModifiedDate(now);
207
208 scProductEntryPersistence.update(productEntry);
209
210
211
212 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
213 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 = null;
225
226 Object httpObject = HttpUtil.getHttp();
227
228 if (httpObject instanceof DoPrivilegedBean) {
229 DoPrivilegedBean doPrivilegedBean =
230 (DoPrivilegedBean)httpObject;
231
232 httpImpl = (HttpImpl)doPrivilegedBean.getActualBean();
233 }
234 else {
235 httpImpl = (HttpImpl)httpObject;
236 }
237
238 HostConfiguration hostConfiguration = httpImpl.getHostConfiguration(
239 directDownloadURL);
240
241 HttpClient httpClient = httpImpl.getClient(hostConfiguration);
242
243 GetMethod getFileMethod = new GetMethod(directDownloadURL);
244
245 int responseCode = httpClient.executeMethod(
246 hostConfiguration, getFileMethod);
247
248 if (responseCode != HttpServletResponse.SC_OK) {
249 throw new UnavailableProductVersionDirectDownloadURLException();
250 }
251 }
252 catch (Exception e) {
253 throw new UnavailableProductVersionDirectDownloadURLException();
254 }
255 }
256
257 protected void validate(
258 long productVersionId, String version, String changeLog,
259 String downloadPageURL, String directDownloadURL,
260 boolean testDirectDownloadURL, long[] frameworkVersionIds)
261 throws PortalException, SystemException {
262
263 if (Validator.isNull(version)) {
264 throw new ProductVersionNameException();
265 }
266 else if (Validator.isNull(changeLog)) {
267 throw new ProductVersionChangeLogException();
268 }
269 else if (Validator.isNull(downloadPageURL) &&
270 Validator.isNull(directDownloadURL)) {
271
272 throw new ProductVersionDownloadURLException();
273 }
274 else if (Validator.isNotNull(directDownloadURL)) {
275 SCProductVersion productVersion =
276 scProductVersionPersistence.fetchByDirectDownloadURL(
277 directDownloadURL);
278
279 if ((productVersion != null) &&
280 (productVersion.getProductVersionId() != productVersionId)) {
281
282 throw new DuplicateProductVersionDirectDownloadURLException();
283 }
284
285 if (testDirectDownloadURL) {
286 testDirectDownloadURL(directDownloadURL);
287 }
288 }
289 else if (frameworkVersionIds.length == 0) {
290 throw new ProductVersionFrameworkVersionException();
291 }
292 }
293
294 }