20
20
import java .io .IOException ;
21
21
import java .util .Calendar ;
22
22
import java .util .Collections ;
23
+ import java .util .Date ;
23
24
import java .util .GregorianCalendar ;
24
25
import java .util .HashMap ;
25
26
import java .util .LinkedHashMap ;
56
57
import ru .mystamps .web .controller .converter .annotation .Category ;
57
58
import ru .mystamps .web .controller .converter .annotation .Country ;
58
59
import ru .mystamps .web .controller .converter .annotation .CurrentUser ;
60
+ import ru .mystamps .web .dao .dto .EntityWithIdDto ;
59
61
import ru .mystamps .web .dao .dto .LinkEntityDto ;
60
62
import ru .mystamps .web .dao .dto .PurchaseAndSaleDto ;
61
63
import ru .mystamps .web .dao .dto .SeriesInfoDto ;
62
64
import ru .mystamps .web .model .AddImageForm ;
63
65
import ru .mystamps .web .model .AddSeriesForm ;
66
+ import ru .mystamps .web .model .AddSeriesSalesForm ;
64
67
import ru .mystamps .web .service .CategoryService ;
65
68
import ru .mystamps .web .service .CollectionService ;
66
69
import ru .mystamps .web .service .CountryService ;
70
+ import ru .mystamps .web .service .SeriesSalesService ;
67
71
import ru .mystamps .web .service .SeriesService ;
72
+ import ru .mystamps .web .service .TransactionParticipantService ;
68
73
import ru .mystamps .web .service .dto .SeriesDto ;
69
74
import ru .mystamps .web .support .spring .security .Authority ;
70
75
import ru .mystamps .web .support .spring .security .CustomUserDetails ;
78
83
79
84
@ Controller
80
85
@ RequiredArgsConstructor
81
- @ SuppressWarnings ({ "PMD.AvoidDuplicateLiterals" , "PMD.TooManyMethods" })
86
+ @ SuppressWarnings ({ "PMD.AvoidDuplicateLiterals" , "PMD.TooManyMethods" , "PMD.GodClass" })
82
87
public class SeriesController {
83
88
84
89
private static final Integer CURRENT_YEAR = new GregorianCalendar ().get (Calendar .YEAR );
@@ -89,6 +94,8 @@ public class SeriesController {
89
94
private final CollectionService collectionService ;
90
95
private final CountryService countryService ;
91
96
private final SeriesService seriesService ;
97
+ private final SeriesSalesService seriesSalesService ;
98
+ private final TransactionParticipantService transactionParticipantService ;
92
99
93
100
static {
94
101
YEARS = new LinkedHashMap <>();
@@ -207,6 +214,8 @@ public String showInfo(
207
214
Map <String , ?> commonAttrs = prepareCommonAttrsForSeriesInfo (series , currentUserId );
208
215
model .addAllAttributes (commonAttrs );
209
216
217
+ addSeriesSalesFormToModel (model );
218
+
210
219
AddImageForm form = new AddImageForm ();
211
220
model .addAttribute ("addImageForm" , form );
212
221
@@ -245,6 +254,8 @@ public String processImage(
245
254
Map <String , ?> commonAttrs = prepareCommonAttrsForSeriesInfo (series , currentUserId );
246
255
model .addAllAttributes (commonAttrs );
247
256
257
+ addSeriesSalesFormToModel (model );
258
+
248
259
// don't try to re-display file upload field
249
260
form .setImage (null );
250
261
@@ -313,6 +324,49 @@ public String removeFromCollection(
313
324
return redirectTo (Url .INFO_COLLECTION_PAGE , collectionSlug );
314
325
}
315
326
327
+ @ PostMapping (Url .ADD_SERIES_ASK_PAGE )
328
+ public String processAskForm (
329
+ @ Valid AddSeriesSalesForm form ,
330
+ BindingResult result ,
331
+ @ PathVariable ("id" ) Integer seriesId ,
332
+ Model model ,
333
+ @ CurrentUser Integer currentUserId ,
334
+ Locale userLocale ,
335
+ HttpServletResponse response )
336
+ throws IOException {
337
+
338
+ if (seriesId == null ) {
339
+ response .sendError (HttpServletResponse .SC_NOT_FOUND );
340
+ return null ;
341
+ }
342
+
343
+ String lang = LocaleUtils .getLanguageOrNull (userLocale );
344
+ SeriesDto series = seriesService .findFullInfoById (seriesId , lang );
345
+ if (series == null ) {
346
+ response .sendError (HttpServletResponse .SC_NOT_FOUND );
347
+ return null ;
348
+ }
349
+
350
+ boolean maxQuantityOfImagesExceeded = !isAdmin () && !isAllowedToAddingImages (series );
351
+ model .addAttribute ("maxQuantityOfImagesExceeded" , maxQuantityOfImagesExceeded );
352
+
353
+ AddImageForm addImageForm = new AddImageForm ();
354
+ model .addAttribute ("addImageForm" , addImageForm );
355
+
356
+ if (result .hasErrors () || maxQuantityOfImagesExceeded ) {
357
+ Map <String , ?> commonAttrs = prepareCommonAttrsForSeriesInfo (series , currentUserId );
358
+ model .addAllAttributes (commonAttrs );
359
+
360
+ addSeriesSalesFormToModel (model );
361
+
362
+ return "series/info" ;
363
+ }
364
+
365
+ seriesSalesService .add (form , series .getId (), currentUserId );
366
+
367
+ return redirectTo (Url .INFO_SERIES_PAGE , series .getId ());
368
+ }
369
+
316
370
@ PostMapping (Url .SEARCH_SERIES_BY_CATALOG )
317
371
public String searchSeriesByCatalog (
318
372
@ RequestParam ("catalogNumber" ) String catalogNumber ,
@@ -386,6 +440,25 @@ public String searchSeriesByCatalog(
386
440
return model ;
387
441
}
388
442
443
+ private void addSeriesSalesFormToModel (Model model ) {
444
+ if (!(Features .ADD_PURCHASES_AND_SALES .isActive ()
445
+ && SecurityContextUtils .hasAuthority (Authority .ADD_SERIES_SALES ))) {
446
+ return ;
447
+ }
448
+
449
+ if (!model .containsAttribute ("addSeriesSalesForm" )) {
450
+ AddSeriesSalesForm addSeriesSalesForm = new AddSeriesSalesForm ();
451
+ addSeriesSalesForm .setDate (new Date ());
452
+ model .addAttribute ("addSeriesSalesForm" , addSeriesSalesForm );
453
+ }
454
+
455
+ List <EntityWithIdDto > sellers = transactionParticipantService .findAllSellers ();
456
+ model .addAttribute ("sellers" , sellers );
457
+
458
+ List <EntityWithIdDto > buyers = transactionParticipantService .findAllBuyers ();
459
+ model .addAttribute ("buyers" , buyers );
460
+ }
461
+
389
462
private static boolean isAllowedToAddingImages (SeriesDto series ) {
390
463
return series .getImageIds ().size () <= series .getQuantity ();
391
464
}
0 commit comments