PersonFormControllerImpl.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 20 Feb 2025, Andrew Grant of Carrick Software .
  3. * All rights reserved.
  4. */
  5. package scot.carricksoftware.grants.controllers.people;
  6. import jakarta.validation.Valid;
  7. import org.apache.logging.log4j.LogManager;
  8. import org.apache.logging.log4j.Logger;
  9. import org.springframework.stereotype.Controller;
  10. import org.springframework.ui.Model;
  11. import org.springframework.validation.BindingResult;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.ModelAttribute;
  14. import org.springframework.web.bind.annotation.PathVariable;
  15. import org.springframework.web.bind.annotation.PostMapping;
  16. import scot.carricksoftware.grants.commands.people.PersonCommand;
  17. import scot.carricksoftware.grants.commands.people.PersonCommandImpl;
  18. import scot.carricksoftware.grants.constants.AttributeConstants;
  19. import scot.carricksoftware.grants.constants.MappingConstants;
  20. import scot.carricksoftware.grants.constants.ViewConstants;
  21. import scot.carricksoftware.grants.converters.Capitalisation;
  22. import scot.carricksoftware.grants.converters.people.PersonCommandConverterImpl;
  23. import scot.carricksoftware.grants.converters.people.PersonConverterImpl;
  24. import scot.carricksoftware.grants.services.people.PersonService;
  25. import scot.carricksoftware.grants.validators.people.PersonCommandValidator;
  26. @SuppressWarnings("LoggingSimilarMessage")
  27. @Controller
  28. public class PersonFormControllerImpl implements PersonFormController {
  29. private static final Logger logger = LogManager.getLogger(PersonFormControllerImpl.class);
  30. private final PersonService personService;
  31. @SuppressWarnings({"FieldCanBeLocal", "unused"})
  32. private final PersonCommandConverterImpl personCommandConverter;
  33. private final PersonConverterImpl personConverter;
  34. private final Capitalisation capitalisation;
  35. private final PersonCommandValidator personCommandValidator;
  36. public PersonFormControllerImpl(PersonService personService,
  37. PersonCommandConverterImpl personCommandConverter,
  38. PersonConverterImpl personConverter,
  39. Capitalisation capitalisation, PersonCommandValidator personCommandValidator) {
  40. this.personService = personService;
  41. this.personCommandConverter = personCommandConverter;
  42. this.personConverter = personConverter;
  43. this.capitalisation = capitalisation;
  44. this.personCommandValidator = personCommandValidator;
  45. }
  46. @SuppressWarnings("SameReturnValue")
  47. @GetMapping(MappingConstants.PERSON_NEW)
  48. public final String getNewPerson(final Model model) {
  49. logger.debug("PersonFormControllerImpl::getNewPerson");
  50. model.addAttribute(AttributeConstants.PERSON_COMMAND, new PersonCommandImpl());
  51. return ViewConstants.PERSON_FORM;
  52. }
  53. @SuppressWarnings("SameReturnValue")
  54. @GetMapping(MappingConstants.PERSON_EDIT)
  55. public final String personEdit(@Valid @PathVariable final String id, Model model) {
  56. logger.debug("PersonFormControllerImpl::personEdit");
  57. model.addAttribute(AttributeConstants.PERSON_COMMAND, personService.findById(Long.valueOf(id)));
  58. return ViewConstants.PERSON_FORM;
  59. }
  60. @Override
  61. @PostMapping(MappingConstants.PERSON)
  62. public String saveOrUpdate(@Valid @ModelAttribute PersonCommand personCommand, BindingResult bindingResult, Model model) {
  63. logger.debug("PersonFormControllerImpl::saveOrUpdate");
  64. personCommandValidator.validate(personCommand, bindingResult);
  65. if (bindingResult.hasErrors()) {
  66. bindingResult.getAllErrors().forEach(error -> logger.debug(error.getDefaultMessage()));
  67. return ViewConstants.PERSON_FORM;
  68. }
  69. cleanUp(personCommand);
  70. PersonCommand savedCommand = personService.savePersonCommand(personCommand);
  71. model.addAttribute(AttributeConstants.PERSON_COMMAND, savedCommand);
  72. return MappingConstants.REDIRECT + MappingConstants.PERSON_SHOW.replace("{id}", "" + savedCommand.getId());
  73. }
  74. private void cleanUp(PersonCommand personCommand) {
  75. personCommand.setFirstName(capitalisation.getCapitalisation(personCommand.getFirstName()));
  76. personCommand.setLastName(capitalisation.getCapitalisation(personCommand.getLastName()));
  77. }
  78. @SuppressWarnings("SameReturnValue")
  79. @GetMapping(MappingConstants.PERSON_SHOW)
  80. public String showById(@PathVariable String id, Model model) {
  81. logger.debug("PersonFormControllerImpl::saveOrUpdate");
  82. PersonCommand savedCommand = personConverter.convert(personService.findById(Long.valueOf(id)));
  83. model.addAttribute(AttributeConstants.PERSON_COMMAND, savedCommand);
  84. return ViewConstants.PERSON_FORM;
  85. }
  86. }