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