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.Http;
022 import com.liferay.portal.kernel.util.HttpUtil;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.model.User;
025 import com.liferay.portal.service.ServiceContext;
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
045 public class SCProductVersionLocalServiceImpl
046 extends SCProductVersionLocalServiceBaseImpl {
047
048 @Override
049 public SCProductVersion addProductVersion(
050 long userId, long productEntryId, String version, String changeLog,
051 String downloadPageURL, String directDownloadURL,
052 boolean testDirectDownloadURL, boolean repoStoreArtifact,
053 long[] frameworkVersionIds, ServiceContext serviceContext)
054 throws PortalException, SystemException {
055
056
057
058 User user = userPersistence.findByPrimaryKey(userId);
059 SCProductEntry productEntry =
060 scProductEntryPersistence.findByPrimaryKey(productEntryId);
061 directDownloadURL = directDownloadURL.trim().toLowerCase();
062 Date now = new Date();
063
064 validate(
065 0, version, changeLog, downloadPageURL, directDownloadURL,
066 testDirectDownloadURL, frameworkVersionIds);
067
068 long productVersionId = counterLocalService.increment();
069
070 SCProductVersion productVersion = scProductVersionPersistence.create(
071 productVersionId);
072
073 productVersion.setCompanyId(user.getCompanyId());
074 productVersion.setUserId(user.getUserId());
075 productVersion.setUserName(user.getFullName());
076 productVersion.setCreateDate(now);
077 productVersion.setModifiedDate(now);
078 productVersion.setProductEntryId(productEntryId);
079 productVersion.setVersion(version);
080 productVersion.setChangeLog(changeLog);
081 productVersion.setDownloadPageURL(downloadPageURL);
082 productVersion.setDirectDownloadURL(directDownloadURL);
083 productVersion.setRepoStoreArtifact(repoStoreArtifact);
084
085 scProductVersionPersistence.update(productVersion);
086
087
088
089 scProductVersionPersistence.setSCFrameworkVersions(
090 productVersionId, frameworkVersionIds);
091
092
093
094 productEntry.setModifiedDate(now);
095
096 scProductEntryPersistence.update(productEntry);
097
098
099
100 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
101 SCProductEntry.class);
102
103 indexer.reindex(productEntry);
104
105 return productVersion;
106 }
107
108 @Override
109 public void deleteProductVersion(long productVersionId)
110 throws PortalException, SystemException {
111
112 SCProductVersion productVersion =
113 scProductVersionPersistence.findByPrimaryKey(productVersionId);
114
115 deleteProductVersion(productVersion);
116 }
117
118 @Override
119 public void deleteProductVersion(SCProductVersion productVersion)
120 throws SystemException {
121
122 scProductVersionPersistence.remove(productVersion);
123 }
124
125 @Override
126 public void deleteProductVersions(long productEntryId)
127 throws SystemException {
128
129 List<SCProductVersion> productVersions =
130 scProductVersionPersistence.findByProductEntryId(productEntryId);
131
132 for (SCProductVersion productVersion : productVersions) {
133 deleteProductVersion(productVersion);
134 }
135 }
136
137 @Override
138 public SCProductVersion getProductVersion(long productVersionId)
139 throws PortalException, SystemException {
140
141 return scProductVersionPersistence.findByPrimaryKey(productVersionId);
142 }
143
144 @Override
145 public SCProductVersion getProductVersionByDirectDownloadURL(
146 String directDownloadURL)
147 throws PortalException, SystemException {
148
149 return scProductVersionPersistence.findByDirectDownloadURL(
150 directDownloadURL);
151 }
152
153 @Override
154 public List<SCProductVersion> getProductVersions(
155 long productEntryId, int start, int end)
156 throws SystemException {
157
158 return scProductVersionPersistence.findByProductEntryId(
159 productEntryId, start, end);
160 }
161
162 @Override
163 public int getProductVersionsCount(long productEntryId)
164 throws SystemException {
165
166 return scProductVersionPersistence.countByProductEntryId(
167 productEntryId);
168 }
169
170 @Override
171 public SCProductVersion updateProductVersion(
172 long productVersionId, String version, String changeLog,
173 String downloadPageURL, String directDownloadURL,
174 boolean testDirectDownloadURL, boolean repoStoreArtifact,
175 long[] frameworkVersionIds)
176 throws PortalException, SystemException {
177
178
179
180 directDownloadURL = directDownloadURL.trim().toLowerCase();
181 Date now = new Date();
182
183 validate(
184 productVersionId, version, changeLog, downloadPageURL,
185 directDownloadURL, testDirectDownloadURL, frameworkVersionIds);
186
187 SCProductVersion productVersion =
188 scProductVersionPersistence.findByPrimaryKey(productVersionId);
189
190 productVersion.setModifiedDate(now);
191 productVersion.setVersion(version);
192 productVersion.setChangeLog(changeLog);
193 productVersion.setDownloadPageURL(downloadPageURL);
194 productVersion.setDirectDownloadURL(directDownloadURL);
195 productVersion.setRepoStoreArtifact(repoStoreArtifact);
196
197 scProductVersionPersistence.update(productVersion);
198
199
200
201 scProductVersionPersistence.setSCFrameworkVersions(
202 productVersionId, frameworkVersionIds);
203
204
205
206 SCProductEntry productEntry =
207 scProductEntryPersistence.findByPrimaryKey(
208 productVersion.getProductEntryId());
209
210 productEntry.setModifiedDate(now);
211
212 scProductEntryPersistence.update(productEntry);
213
214
215
216 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
217 SCProductEntry.class);
218
219 indexer.reindex(productEntry);
220
221 return productVersion;
222 }
223
224 protected void testDirectDownloadURL(String directDownloadURL)
225 throws PortalException {
226
227 try {
228 Http.Options options = new Http.Options();
229
230 options.setLocation(directDownloadURL);
231 options.setPost(false);
232
233 HttpUtil.URLtoByteArray(options);
234
235 Http.Response response = options.getResponse();
236
237 if (response.getResponseCode() != HttpServletResponse.SC_OK) {
238 throw new UnavailableProductVersionDirectDownloadURLException();
239 }
240 }
241 catch (Exception e) {
242 throw new UnavailableProductVersionDirectDownloadURLException();
243 }
244 }
245
246 protected void validate(
247 long productVersionId, String version, String changeLog,
248 String downloadPageURL, String directDownloadURL,
249 boolean testDirectDownloadURL, long[] frameworkVersionIds)
250 throws PortalException, SystemException {
251
252 if (Validator.isNull(version)) {
253 throw new ProductVersionNameException();
254 }
255 else if (Validator.isNull(changeLog)) {
256 throw new ProductVersionChangeLogException();
257 }
258 else if (Validator.isNull(downloadPageURL) &&
259 Validator.isNull(directDownloadURL)) {
260
261 throw new ProductVersionDownloadURLException();
262 }
263 else if (Validator.isNotNull(directDownloadURL)) {
264 SCProductVersion productVersion =
265 scProductVersionPersistence.fetchByDirectDownloadURL(
266 directDownloadURL);
267
268 if ((productVersion != null) &&
269 (productVersion.getProductVersionId() != productVersionId)) {
270
271 throw new DuplicateProductVersionDirectDownloadURLException();
272 }
273
274 if (testDirectDownloadURL) {
275 testDirectDownloadURL(directDownloadURL);
276 }
277 }
278 else if (frameworkVersionIds.length == 0) {
279 throw new ProductVersionFrameworkVersionException();
280 }
281 }
282
283 }