Pārlūkot izejas kodu

Organisation controllers

Andrew Grant 6 mēneši atpakaļ
vecāks
revīzija
4409febc80

+ 1 - 1
src/main/java/scot/carricksoftware/grants/constants/ViewConstants.java

@@ -23,7 +23,7 @@ public class ViewConstants {
     public static final String COUNTRY_LIST = "country/list";
     public static final String COUNTRY_FORM = "country/form";
 
-    public static final String ORGANISATIO_LIST = "organisation/list";
+    public static final String ORGANISATION_LIST = "organisation/list";
     public static final String ORGANISATION_FORM = "organisation/form";
 
     public static final String REGION_LIST = "region/list";

+ 18 - 0
src/main/java/scot/carricksoftware/grants/controllers/places/organisations/OrganisationFormController.java

@@ -0,0 +1,18 @@
+/*
+ * Copyright (c)  20 Feb 2025, Andrew Grant of Carrick Software .
+ * All rights reserved.
+ */
+
+package scot.carricksoftware.grants.controllers.places.organisations;
+
+import org.springframework.ui.Model;
+import org.springframework.validation.BindingResult;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import scot.carricksoftware.grants.commands.places.organisations.OrganisationCommand;
+
+@SuppressWarnings("unused")
+
+public interface OrganisationFormController {
+
+    String saveOrUpdate(@ModelAttribute OrganisationCommand organisationCommand, BindingResult bindingResult, Model model);
+}

+ 105 - 0
src/main/java/scot/carricksoftware/grants/controllers/places/organisations/OrganisationFormControllerImpl.java

@@ -0,0 +1,105 @@
+/*
+ * Copyright (c)  20 Feb 2025, Andrew Grant of Carrick Software .
+ * All rights reserved.
+ */
+
+package scot.carricksoftware.grants.controllers.places.organisations;
+
+import jakarta.validation.Valid;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.validation.BindingResult;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import scot.carricksoftware.grants.commands.places.organisations.OrganisationCommand;
+import scot.carricksoftware.grants.commands.places.organisations.OrganisationCommandImpl;
+import scot.carricksoftware.grants.constants.AttributeConstants;
+import scot.carricksoftware.grants.constants.MappingConstants;
+import scot.carricksoftware.grants.constants.ViewConstants;
+import scot.carricksoftware.grants.converters.Capitalisation;
+import scot.carricksoftware.grants.converters.places.organisations.OrganisationCommandConverter;
+import scot.carricksoftware.grants.converters.places.organisations.OrganisationConverter;
+import scot.carricksoftware.grants.services.places.organisations.OrganisationService;
+import scot.carricksoftware.grants.validators.places.OrganisationCommandValidator;
+
+@SuppressWarnings("LoggingSimilarMessage")
+@Controller
+public class OrganisationFormControllerImpl implements OrganisationFormController {
+
+    private static final Logger logger = LogManager.getLogger(OrganisationFormControllerImpl.class);
+    private final OrganisationService organisationService;
+    @SuppressWarnings({"unused", "FieldCanBeLocal"})
+    private final OrganisationCommandConverter organisationCommandConverter;
+    private final OrganisationConverter organisationConverter;
+    private final Capitalisation capitalisation;
+    private final OrganisationCommandValidator organisationCommandValidator;
+
+
+    public OrganisationFormControllerImpl(OrganisationService organisationService,
+                                          OrganisationCommandConverter organisationCommandConverter,
+                                          OrganisationConverter organisationConverter,
+                                          Capitalisation capitalisation, OrganisationCommandValidator organisationCommandValidator) {
+        this.organisationService = organisationService;
+        this.organisationCommandConverter = organisationCommandConverter;
+
+
+        this.organisationConverter = organisationConverter;
+        this.capitalisation = capitalisation;
+        this.organisationCommandValidator = organisationCommandValidator;
+    }
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(MappingConstants.ORGANISATION_NEW)
+    public final String getNewOrganisation(final Model model) {
+        logger.debug("OrganisationFormControllerImpl::getNewOrganisation");
+        model.addAttribute(AttributeConstants.ORGANISATION_COMMAND, new OrganisationCommandImpl());
+        return ViewConstants.ORGANISATION_FORM;
+    }
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(MappingConstants.ORGANISATION_EDIT)
+    public final String organisationEdit(@Valid @PathVariable final String id, Model model) {
+        logger.debug("OrganisationFormControllerImpl::organisationEdit");
+        model.addAttribute(AttributeConstants.ORGANISATION_COMMAND, organisationService.findById(Long.valueOf(id)));
+        return ViewConstants.ORGANISATION_FORM;
+    }
+
+
+    @Override
+    @PostMapping(MappingConstants.ORGANISATION)
+    public String saveOrUpdate(@Valid @ModelAttribute OrganisationCommand organisationCommand, BindingResult bindingResult, Model model) {
+        logger.debug("OrganisationFormControllerImpl::saveOrUpdate");
+
+        organisationCommandValidator.validate(organisationCommand, bindingResult);
+
+
+        if (bindingResult.hasErrors()) {
+            bindingResult.getAllErrors().forEach(error -> logger.debug(error.getDefaultMessage()));
+            return ViewConstants.ORGANISATION_FORM;
+        }
+
+        cleanUp(organisationCommand);
+        OrganisationCommand savedCommand = organisationService.saveOrganisationCommand(organisationCommand);
+        model.addAttribute(AttributeConstants.ORGANISATION_COMMAND, savedCommand);
+        return MappingConstants.REDIRECT + MappingConstants.ORGANISATION_SHOW.replace("{id}", "" + savedCommand.getId());
+    }
+
+    private void cleanUp(OrganisationCommand organisationCommand) {
+        organisationCommand.setName(capitalisation.getCapitalisation(organisationCommand.getName()));
+    }
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(MappingConstants.ORGANISATION_SHOW)
+    public String showById(@PathVariable String id, Model model) {
+        logger.debug("OrganisationFormControllerImpl::saveOrUpdate");
+        OrganisationCommand savedCommand = organisationConverter.convert(organisationService.findById(Long.valueOf(id)));
+        model.addAttribute(AttributeConstants.ORGANISATION_COMMAND, savedCommand);
+        return ViewConstants.ORGANISATION_FORM;
+    }
+
+
+}

+ 29 - 0
src/main/java/scot/carricksoftware/grants/controllers/places/organisations/OrganisationListController.java

@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 11/03/2025, 22:25. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.controllers.places.organisations;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.PathVariable;
+
+@SuppressWarnings("unused")
+@Controller
+public interface OrganisationListController {
+
+    String getListPage(final Model model);
+
+    String getNextPage(final Model model);
+
+    String getPreviousPage(final Model model);
+
+    String getFirstPage(final Model model);
+
+    String getLastPage(final Model model);
+
+    String organisationDelete(@PathVariable String id);
+
+    int getPageNumber();
+}

+ 107 - 0
src/main/java/scot/carricksoftware/grants/controllers/places/organisations/OrganisationListControllerImpl.java

@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 11/03/2025, 22:25. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.controllers.places.organisations;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import scot.carricksoftware.grants.constants.ApplicationConstants;
+import scot.carricksoftware.grants.constants.AttributeConstants;
+import scot.carricksoftware.grants.constants.MappingConstants;
+import scot.carricksoftware.grants.constants.ViewConstants;
+import scot.carricksoftware.grants.controllers.ControllerHelper;
+import scot.carricksoftware.grants.services.places.organisations.OrganisationService;
+
+import static java.lang.Integer.max;
+
+@Controller
+public class OrganisationListControllerImpl implements OrganisationListController {
+
+    private static final Logger logger = LogManager.getLogger(OrganisationListControllerImpl.class);
+
+
+    private int currentPage = 0;
+    private final ControllerHelper controllerHelper;
+    private final OrganisationService organisationService;
+
+    public OrganisationListControllerImpl(ControllerHelper controllerHelper,
+                                          OrganisationService organisationService) {
+        this.controllerHelper = controllerHelper;
+        this.organisationService = organisationService;
+    }
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(MappingConstants.ORGANISATION_LIST)
+    @Override
+    public final String getListPage(final Model model) {
+        logger.debug("PersonListControllerImpl::getOrganisationPage");
+        currentPage = 0;
+        return sendAttributesAndReturn(model);
+    }
+
+    @SuppressWarnings("SameReturnValue")
+    private String sendAttributesAndReturn(Model model) {
+        model.addAttribute(AttributeConstants.ORGANISATIONS, organisationService.getPagedOrganisations(currentPage));
+        controllerHelper.addAttributes(model);
+        return ViewConstants.ORGANISATION_LIST;
+    }
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(MappingConstants.ORGANISATION_NEXT)
+    @Override
+    public final String getNextPage(final Model model) {
+        logger.debug("OrganisationListControllerImpl::getNextPage");
+        currentPage++;
+        return sendAttributesAndReturn(model);
+    }
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(MappingConstants.ORGANISATION_PREVIOUS)
+    @Override
+    public final String getPreviousPage(final Model model) {
+        logger.debug("OrganisationListControllerImpl::getPreviousPage");
+        currentPage = max(0, currentPage - 1);
+        return sendAttributesAndReturn(model);
+    }
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(MappingConstants.ORGANISATION_REWIND)
+    public final String getFirstPage(final Model model) {
+        logger.debug("OrganisationListControllerImpl::getFirstPage");
+        currentPage = 0;
+        return sendAttributesAndReturn(model);
+    }
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(MappingConstants.ORGANISATION_FF)
+    @Override
+    public final String getLastPage(final Model model) {
+        logger.debug("OrganisationListControllerImpl::getLastPage");
+        long organisationCount = organisationService.count();
+        currentPage = (int) (organisationCount / ApplicationConstants.DEFAULT_PAGE_SIZE);
+        return sendAttributesAndReturn(model);
+    }
+
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(MappingConstants.ORGANISATION_DELETE)
+    @Override
+    public final String organisationDelete(@PathVariable final String id) {
+        logger.debug("OrganisationListControllerImpl::organisationDelete");
+        organisationService.deleteById(Long.valueOf(id));
+        return MappingConstants.REDIRECT + MappingConstants.ORGANISATIONS;
+    }
+
+    @Override
+    public int getPageNumber() {
+        return currentPage;
+    }
+
+
+}

+ 31 - 0
src/main/java/scot/carricksoftware/grants/validators/places/OrganisationCommandValidator.java

@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 19/03/2025, 09:51. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.validators.places;
+
+import org.springframework.stereotype.Component;
+import org.springframework.validation.BindingResult;
+import scot.carricksoftware.grants.commands.places.organisations.OrganisationCommand;
+import scot.carricksoftware.grants.constants.ApplicationConstants;
+import scot.carricksoftware.grants.constants.ValidationConstants;
+
+@Component
+public class OrganisationCommandValidator {
+
+    public void validate(OrganisationCommand organisationCommand, BindingResult bindingResult) {
+        if (organisationCommand.getName().length() < ApplicationConstants.MINIMUM_NAME_LENGTH) {
+            bindingResult.rejectValue("name", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.NAME_IS_TOO_SHORT);
+        } else {
+            if (organisationCommand.getName().length() > ApplicationConstants.MAXIMUM_NAME_LENGTH) {
+                bindingResult.rejectValue("name", ApplicationConstants.EMPTY_STRING,
+                        null,
+                        ValidationConstants.NAME_IS_TOO_LONG);
+            }
+        }
+    }
+}
+