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.search.Indexer;
019 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
020 import com.liferay.portal.kernel.util.Http;
021 import com.liferay.portal.kernel.util.HttpUtil;
022 import com.liferay.portal.kernel.util.StringUtil;
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 {
055
056
057
058 User user = userPersistence.findByPrimaryKey(userId);
059 SCProductEntry productEntry =
060 scProductEntryPersistence.findByPrimaryKey(productEntryId);
061 directDownloadURL = StringUtil.toLowerCase(directDownloadURL.trim());
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 {
111
112 SCProductVersion productVersion =
113 scProductVersionPersistence.findByPrimaryKey(productVersionId);
114
115 deleteProductVersion(productVersion);
116 }
117
118 @Override
119 public void deleteProductVersion(SCProductVersion productVersion) {
120 scProductVersionPersistence.remove(productVersion);
121 }
122
123 @Override
124 public void deleteProductVersions(long productEntryId) {
125 List<SCProductVersion> productVersions =
126 scProductVersionPersistence.findByProductEntryId(productEntryId);
127
128 for (SCProductVersion productVersion : productVersions) {
129 deleteProductVersion(productVersion);
130 }
131 }
132
133 @Override
134 public SCProductVersion getProductVersion(long productVersionId)
135 throws PortalException {
136
137 return scProductVersionPersistence.findByPrimaryKey(productVersionId);
138 }
139
140 @Override
141 public SCProductVersion getProductVersionByDirectDownloadURL(
142 String directDownloadURL)
143 throws PortalException {
144
145 return scProductVersionPersistence.findByDirectDownloadURL(
146 directDownloadURL);
147 }
148
149 @Override
150 public List<SCProductVersion> getProductVersions(
151 long productEntryId, int start, int end) {
152
153 return scProductVersionPersistence.findByProductEntryId(
154 productEntryId, start, end);
155 }
156
157 @Override
158 public int getProductVersionsCount(long productEntryId) {
159 return scProductVersionPersistence.countByProductEntryId(
160 productEntryId);
161 }
162
163 @Override
164 public SCProductVersion updateProductVersion(
165 long productVersionId, String version, String changeLog,
166 String downloadPageURL, String directDownloadURL,
167 boolean testDirectDownloadURL, boolean repoStoreArtifact,
168 long[] frameworkVersionIds)
169 throws PortalException {
170
171
172
173 directDownloadURL = StringUtil.toLowerCase(directDownloadURL.trim());
174 Date now = new Date();
175
176 validate(
177 productVersionId, version, changeLog, downloadPageURL,
178 directDownloadURL, testDirectDownloadURL, frameworkVersionIds);
179
180 SCProductVersion productVersion =
181 scProductVersionPersistence.findByPrimaryKey(productVersionId);
182
183 productVersion.setModifiedDate(now);
184 productVersion.setVersion(version);
185 productVersion.setChangeLog(changeLog);
186 productVersion.setDownloadPageURL(downloadPageURL);
187 productVersion.setDirectDownloadURL(directDownloadURL);
188 productVersion.setRepoStoreArtifact(repoStoreArtifact);
189
190 scProductVersionPersistence.update(productVersion);
191
192
193
194 scProductVersionPersistence.setSCFrameworkVersions(
195 productVersionId, frameworkVersionIds);
196
197
198
199 SCProductEntry productEntry =
200 scProductEntryPersistence.findByPrimaryKey(
201 productVersion.getProductEntryId());
202
203 productEntry.setModifiedDate(now);
204
205 scProductEntryPersistence.update(productEntry);
206
207
208
209 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
210 SCProductEntry.class);
211
212 indexer.reindex(productEntry);
213
214 return productVersion;
215 }
216
217 protected void testDirectDownloadURL(String directDownloadURL)
218 throws PortalException {
219
220 try {
221 Http.Options options = new Http.Options();
222
223 options.setLocation(directDownloadURL);
224 options.setPost(false);
225
226 HttpUtil.URLtoByteArray(options);
227
228 Http.Response response = options.getResponse();
229
230 if (response.getResponseCode() != HttpServletResponse.SC_OK) {
231 throw new UnavailableProductVersionDirectDownloadURLException();
232 }
233 }
234 catch (Exception e) {
235 throw new UnavailableProductVersionDirectDownloadURLException();
236 }
237 }
238
239 protected void validate(
240 long productVersionId, String version, String changeLog,
241 String downloadPageURL, String directDownloadURL,
242 boolean testDirectDownloadURL, long[] frameworkVersionIds)
243 throws PortalException {
244
245 if (Validator.isNull(version)) {
246 throw new ProductVersionNameException();
247 }
248 else if (Validator.isNull(changeLog)) {
249 throw new ProductVersionChangeLogException();
250 }
251 else if (Validator.isNull(downloadPageURL) &&
252 Validator.isNull(directDownloadURL)) {
253
254 throw new ProductVersionDownloadURLException();
255 }
256 else if (Validator.isNotNull(directDownloadURL)) {
257 SCProductVersion productVersion =
258 scProductVersionPersistence.fetchByDirectDownloadURL(
259 directDownloadURL);
260
261 if ((productVersion != null) &&
262 (productVersion.getProductVersionId() != productVersionId)) {
263
264 throw new DuplicateProductVersionDirectDownloadURLException(
265 "{productVersionId=" + productVersionId + "}");
266 }
267
268 if (testDirectDownloadURL) {
269 testDirectDownloadURL(directDownloadURL);
270 }
271 }
272 else if (frameworkVersionIds.length == 0) {
273 throw new ProductVersionFrameworkVersionException();
274 }
275 }
276
277 }