-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathSeriesServiceImpl.java
377 lines (301 loc) · 12.2 KB
/
SeriesServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* Copyright (C) 2009-2020 Slava Semushin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ru.mystamps.web.feature.series;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import ru.mystamps.web.feature.image.ImageInfoDto;
import ru.mystamps.web.feature.image.ImageService;
import ru.mystamps.web.support.spring.security.HasAuthority;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
// FIXME: move stamps related methods to separate interface (#88)
// The String literal "Series id must be non null" appears N times in this file
// and we think that it's OK.
@SuppressWarnings({ "PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals" })
@RequiredArgsConstructor
public class SeriesServiceImpl implements SeriesService {
private final Logger log;
private final SeriesDao seriesDao;
private final ImageService imageService;
private final StampsCatalogService michelCatalogService;
private final StampsCatalogService scottCatalogService;
private final StampsCatalogService yvertCatalogService;
private final StampsCatalogService gibbonsCatalogService;
private final StampsCatalogService solovyovCatalogService;
private final StampsCatalogService zagorskiCatalogService;
@Override
@Transactional
@PreAuthorize(HasAuthority.CREATE_SERIES)
public Integer add(AddSeriesDto dto, Integer userId, boolean userCanAddComments) {
Validate.isTrue(dto != null, "DTO must be non null");
Validate.isTrue(dto.getQuantity() != null, "Quantity must be non null");
Validate.isTrue(dto.getPerforated() != null, "Perforated property must be non null");
Validate.isTrue(dto.getCategory() != null, "Category must be non null");
Validate.isTrue(dto.getCategory().getId() != null, "Category id must be non null");
Validate.isTrue(userId != null, "User id must be non null");
AddSeriesDbDto series = new AddSeriesDbDto();
if (dto.getCountry() != null) {
series.setCountryId(dto.getCountry().getId());
}
setDateOfReleaseIfProvided(dto, series);
series.setCategoryId(dto.getCategory().getId());
series.setQuantity(dto.getQuantity());
series.setPerforated(dto.getPerforated());
series.setMichelPrice(dto.getMichelPrice());
series.setScottPrice(dto.getScottPrice());
series.setYvertPrice(dto.getYvertPrice());
series.setGibbonsPrice(dto.getGibbonsPrice());
series.setSolovyovPrice(dto.getSolovyovPrice());
series.setZagorskiPrice(dto.getZagorskiPrice());
if (userCanAddComments && dto.getComment() != null) {
Validate.isTrue(
!dto.getComment().trim().isEmpty(),
"Comment must be non empty"
);
series.setComment(dto.getComment());
}
Date now = new Date();
series.setCreatedAt(now);
series.setUpdatedAt(now);
series.setCreatedBy(userId);
series.setUpdatedBy(userId);
Integer id = seriesDao.add(series);
createCatalogNumbersAndAddToSeries(id, michelCatalogService, dto.getMichelNumbers());
createCatalogNumbersAndAddToSeries(id, scottCatalogService, dto.getScottNumbers());
createCatalogNumbersAndAddToSeries(id, yvertCatalogService, dto.getYvertNumbers());
createCatalogNumbersAndAddToSeries(id, gibbonsCatalogService, dto.getGibbonsNumbers());
createCatalogNumbersAndAddToSeries(id, solovyovCatalogService, dto.getSolovyovNumbers());
createCatalogNumbersAndAddToSeries(id, zagorskiCatalogService, dto.getZagorskiNumbers());
ImageInfoDto imageInfo = imageService.save(dto.getImage());
Integer imageId = imageInfo.getId();
try {
imageService.addToSeries(id, imageId);
} catch (RuntimeException ex) { // NOPMD: AvoidCatchingGenericException
imageService.removeIfPossible(imageInfo);
throw ex;
}
log.info("Series #{} has been created ({})", id, series);
return id;
}
@Override
@Transactional
@PreAuthorize("isAuthenticated()")
public void addImageToSeries(AddImageDto dto, Integer seriesId, Integer userId) {
Validate.isTrue(dto != null, "DTO must be non null");
Validate.isTrue(seriesId != null, "Series id must be non null");
Validate.isTrue(userId != null, "User id must be non null");
seriesDao.markAsModified(seriesId, new Date(), userId);
ImageInfoDto imageInfo = imageService.save(dto.getImage());
Integer imageId = imageInfo.getId();
try {
imageService.addToSeries(seriesId, imageId);
} catch (RuntimeException ex) { // NOPMD: AvoidCatchingGenericException
imageService.removeIfPossible(imageInfo);
throw ex;
}
}
// @todo #1303 SeriesServiceImpl.replaceImage(): add unit tests
@Override
@Transactional
@PreAuthorize(HasAuthority.REPLACE_IMAGE)
public void replaceImage(ReplaceImageDto dto, Integer seriesId, Integer userId) {
Validate.isTrue(dto != null, "DTO must be non null");
Validate.isTrue(seriesId != null, "Series id must be non null");
Validate.isTrue(userId != null, "User id must be non null");
seriesDao.markAsModified(seriesId, new Date(), userId);
imageService.replace(dto.getImageId(), dto.getImage());
}
@Override
@Transactional(readOnly = true)
public long countAll() {
return seriesDao.countAll();
}
@Override
@Transactional(readOnly = true)
public long countAllStamps() {
return seriesDao.countAllStamps();
}
@Override
@Transactional(readOnly = true)
public long countAddedSince(Date date) {
Validate.isTrue(date != null, "Date must be non null");
return seriesDao.countAddedSince(date);
}
@Override
@Transactional(readOnly = true)
public long countUpdatedSince(Date date) {
Validate.isTrue(date != null, "Date must be non null");
return seriesDao.countUpdatedSince(date);
}
@Override
@Transactional(readOnly = true)
public boolean isSeriesExist(Integer seriesId) {
Validate.isTrue(seriesId != null, "Series id must be non null");
return seriesDao.countSeriesById(seriesId) > 0;
}
@Override
@Transactional(readOnly = true)
public Integer findQuantityById(Integer seriesId) {
Validate.isTrue(seriesId != null, "Series id must be non null");
return seriesDao.findQuantityById(seriesId);
}
@Override
@Transactional(readOnly = true)
public SeriesDto findFullInfoById(Integer seriesId, String lang) {
Validate.isTrue(seriesId != null, "Series id must be non null");
SeriesFullInfoDto seriesBaseInfo = seriesDao.findByIdAsSeriesFullInfo(seriesId, lang);
if (seriesBaseInfo == null) {
return null;
}
List<String> michelNumbers = michelCatalogService.findBySeriesId(seriesId);
List<String> scottNumbers = scottCatalogService.findBySeriesId(seriesId);
List<String> yvertNumbers = yvertCatalogService.findBySeriesId(seriesId);
List<String> gibbonsNumbers = gibbonsCatalogService.findBySeriesId(seriesId);
List<String> solovyovNumbers = solovyovCatalogService.findBySeriesId(seriesId);
List<String> zagorskiNumbers = zagorskiCatalogService.findBySeriesId(seriesId);
List<Integer> imageIds = imageService.findBySeriesId(seriesId);
return new SeriesDto(
seriesBaseInfo,
michelNumbers,
scottNumbers,
yvertNumbers,
gibbonsNumbers,
solovyovNumbers,
zagorskiNumbers,
imageIds
);
}
@Override
@Transactional(readOnly = true)
public List<SeriesInfoDto> findByMichelNumber(String michelNumberCode, String lang) {
return findByCatalogNumber(michelCatalogService, michelNumberCode, lang);
}
@Override
@Transactional(readOnly = true)
public List<SeriesInfoDto> findByScottNumber(String scottNumberCode, String lang) {
return findByCatalogNumber(scottCatalogService, scottNumberCode, lang);
}
@Override
@Transactional(readOnly = true)
public List<SeriesInfoDto> findByYvertNumber(String yvertNumberCode, String lang) {
return findByCatalogNumber(yvertCatalogService, yvertNumberCode, lang);
}
@Override
@Transactional(readOnly = true)
public List<SeriesInfoDto> findByGibbonsNumber(String gibbonsNumberCode, String lang) {
return findByCatalogNumber(gibbonsCatalogService, gibbonsNumberCode, lang);
}
@Override
@Transactional(readOnly = true)
public List<SeriesInfoDto> findBySolovyovNumber(String solovyovNumberCode, String lang) {
return findByCatalogNumber(solovyovCatalogService, solovyovNumberCode, lang);
}
@Override
@Transactional(readOnly = true)
public List<SeriesInfoDto> findByZagorskiNumber(String zagorskiNumberCode, String lang) {
return findByCatalogNumber(zagorskiCatalogService, zagorskiNumberCode, lang);
}
@Override
@Transactional(readOnly = true)
public List<SeriesInfoDto> findByCategorySlug(String slug, String lang) {
Validate.isTrue(slug != null, "Category slug must be non null");
return seriesDao.findByCategorySlugAsSeriesInfo(slug, lang);
}
@Override
@Transactional(readOnly = true)
public List<SeriesInfoDto> findByCountrySlug(String slug, String lang) {
Validate.isTrue(slug != null, "Country slug must be non null");
return seriesDao.findByCountrySlugAsSeriesInfo(slug, lang);
}
@Override
@Transactional(readOnly = true)
public List<SeriesLinkDto> findRecentlyAdded(int quantity, String lang) {
Validate.isTrue(quantity > 0, "Quantity of recently added series must be greater than 0");
return seriesDao.findLastAdded(quantity, lang);
}
@Override
@Transactional(readOnly = true)
public List<SeriesLinkDto> findSimilarSeries(Integer seriesId, String lang) {
Validate.isTrue(seriesId != null, "Series id must be non null");
return seriesDao.findSimilarSeries(seriesId, lang);
}
@Override
@Transactional(readOnly = true)
public List<SitemapInfoDto> findAllForSitemap() {
return seriesDao.findAllForSitemap();
}
/**
* @author Sergey Chechenev
*/
@Override
@Transactional(readOnly = true)
@PreAuthorize(HasAuthority.VIEW_SERIES_SALES)
public List<PurchaseAndSaleDto> findPurchasesAndSales(Integer seriesId) {
Validate.isTrue(seriesId != null, "Series id must be non null");
return seriesDao.findPurchasesAndSales(seriesId);
}
// @todo #1280 SeriesServiceImpl.markAsSimilar(): add unit tests
@Override
@Transactional
@PreAuthorize(HasAuthority.MARK_SIMILAR_SERIES)
public void markAsSimilar(AddSimilarSeriesForm dto) {
Validate.isTrue(dto != null, "DTO must be non null");
Integer seriesId = dto.getSeriesId();
Validate.isTrue(seriesId != null, "Series id must be non null");
Integer similarSeriesId = dto.getSimilarSeriesId();
Validate.isTrue(similarSeriesId != null, "Similar series id must be non null");
seriesDao.markAsSimilar(seriesId, similarSeriesId);
log.info("Series #{} has been marked as similar to #{}", seriesId, similarSeriesId);
}
private List<SeriesInfoDto> findByCatalogNumber(
StampsCatalogService catalogService,
String number, String lang) {
List<Integer> seriesIds = catalogService.findSeriesIdsByNumber(number);
if (seriesIds.isEmpty()) {
return Collections.emptyList();
}
return seriesDao.findByIdsAsSeriesInfo(seriesIds, lang);
}
private static void setDateOfReleaseIfProvided(AddSeriesDto dto, AddSeriesDbDto series) {
if (dto.getYear() == null) {
return;
}
series.setReleaseYear(dto.getYear());
if (dto.getMonth() == null) {
return;
}
series.setReleaseMonth(dto.getMonth());
// even if day is null it won't change anything
series.setReleaseDay(dto.getDay());
}
private static void createCatalogNumbersAndAddToSeries(
Integer seriesId,
StampsCatalogService catalogService,
String numbers) {
Set<String> parsedNumbers = CatalogUtils.parseCatalogNumbers(numbers);
if (!parsedNumbers.isEmpty()) {
catalogService.add(parsedNumbers);
catalogService.addToSeries(seriesId, parsedNumbers);
}
}
}