2020import java .io .IOException ;
2121import java .util .Calendar ;
2222import java .util .Collections ;
23+ import java .util .Date ;
2324import java .util .GregorianCalendar ;
2425import java .util .HashMap ;
2526import java .util .LinkedHashMap ;
5657import ru .mystamps .web .controller .converter .annotation .Category ;
5758import ru .mystamps .web .controller .converter .annotation .Country ;
5859import ru .mystamps .web .controller .converter .annotation .CurrentUser ;
60+ import ru .mystamps .web .dao .dto .EntityWithIdDto ;
5961import ru .mystamps .web .dao .dto .LinkEntityDto ;
6062import ru .mystamps .web .dao .dto .PurchaseAndSaleDto ;
6163import ru .mystamps .web .dao .dto .SeriesInfoDto ;
6264import ru .mystamps .web .model .AddImageForm ;
6365import ru .mystamps .web .model .AddSeriesForm ;
66+ import ru .mystamps .web .model .AddSeriesSalesForm ;
6467import ru .mystamps .web .service .CategoryService ;
6568import ru .mystamps .web .service .CollectionService ;
6669import ru .mystamps .web .service .CountryService ;
70+ import ru .mystamps .web .service .SeriesSalesService ;
6771import ru .mystamps .web .service .SeriesService ;
72+ import ru .mystamps .web .service .TransactionParticipantService ;
6873import ru .mystamps .web .service .dto .SeriesDto ;
6974import ru .mystamps .web .support .spring .security .Authority ;
7075import ru .mystamps .web .support .spring .security .CustomUserDetails ;
7883
7984@ Controller
8085@ RequiredArgsConstructor
81- @ SuppressWarnings ({ "PMD.AvoidDuplicateLiterals" , "PMD.TooManyMethods" })
86+ @ SuppressWarnings ({ "PMD.AvoidDuplicateLiterals" , "PMD.TooManyMethods" , "PMD.GodClass" })
8287public class SeriesController {
8388
8489 private static final Integer CURRENT_YEAR = new GregorianCalendar ().get (Calendar .YEAR );
@@ -89,6 +94,8 @@ public class SeriesController {
8994 private final CollectionService collectionService ;
9095 private final CountryService countryService ;
9196 private final SeriesService seriesService ;
97+ private final SeriesSalesService seriesSalesService ;
98+ private final TransactionParticipantService transactionParticipantService ;
9299
93100 static {
94101 YEARS = new LinkedHashMap <>();
@@ -207,6 +214,8 @@ public String showInfo(
207214 Map <String , ?> commonAttrs = prepareCommonAttrsForSeriesInfo (series , currentUserId );
208215 model .addAllAttributes (commonAttrs );
209216
217+ addSeriesSalesFormToModel (model );
218+
210219 AddImageForm form = new AddImageForm ();
211220 model .addAttribute ("addImageForm" , form );
212221
@@ -245,6 +254,8 @@ public String processImage(
245254 Map <String , ?> commonAttrs = prepareCommonAttrsForSeriesInfo (series , currentUserId );
246255 model .addAllAttributes (commonAttrs );
247256
257+ addSeriesSalesFormToModel (model );
258+
248259 // don't try to re-display file upload field
249260 form .setImage (null );
250261
@@ -313,6 +324,49 @@ public String removeFromCollection(
313324 return redirectTo (Url .INFO_COLLECTION_PAGE , collectionSlug );
314325 }
315326
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+
316370 @ PostMapping (Url .SEARCH_SERIES_BY_CATALOG )
317371 public String searchSeriesByCatalog (
318372 @ RequestParam ("catalogNumber" ) String catalogNumber ,
@@ -386,6 +440,25 @@ public String searchSeriesByCatalog(
386440 return model ;
387441 }
388442
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+
389462 private static boolean isAllowedToAddingImages (SeriesDto series ) {
390463 return series .getImageIds ().size () <= series .getQuantity ();
391464 }
0 commit comments