From e15623d232af8890a30e5b59c31fd8017f911daf Mon Sep 17 00:00:00 2001 From: Perumal Raj A Date: Sat, 22 Feb 2025 05:38:18 +0000 Subject: [PATCH] Improved the Owner search query Signed-off-by: Perumal Raj A --- .../petclinic/owner/OwnerController.java | 42 ++++++++----------- src/main/resources/application.properties | 6 +-- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java index 596058d96..e0663f785 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java @@ -92,33 +92,27 @@ class OwnerController { @GetMapping("/owners") public String processFindForm(@RequestParam(defaultValue = "1") int page, Owner owner, BindingResult result, Model model) { - try { - // allow parameterless GET request for /owners to return all records - if (owner.getLastName() == null) { - owner.setLastName(""); // empty string signifies broadest possible search - } - - // find owners by last name - Page ownersResults = findPaginatedForOwnersLastName(page, owner.getLastName()); - if (ownersResults.isEmpty()) { - // no owners found - result.rejectValue("lastName", "notFound", "not found"); - return "owners/findOwners"; - } - - if (ownersResults.getTotalElements() == 1) { - // 1 owner found - owner = ownersResults.iterator().next(); - return "redirect:/owners/" + owner.getId(); - } - // multiple owners found - return addPaginationModel(page, model, ownersResults); + // allow parameterless GET request for /owners to return all records + if (owner.getLastName() == null) { + owner.setLastName(""); // empty string signifies broadest possible search } - catch (Exception e) { - e.printStackTrace();// print exception to console or log in file - return "error"; // redirect to generic error page + + // find owners by last name + Page ownersResults = findPaginatedForOwnersLastName(page, owner.getLastName()); + if (ownersResults.isEmpty()) { + // no owners found + result.rejectValue("lastName", "notFound", "not found"); + return "owners/findOwners"; } + if (ownersResults.getTotalElements() == 1) { + // 1 owner found + owner = ownersResults.iterator().next(); + return "redirect:/owners/" + owner.getId(); + } + // multiple owners found + return addPaginationModel(page, model, ownersResults); + } private String addPaginationModel(int page, Model model, Page paginated) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4a45d2046..6ed985654 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -18,10 +18,8 @@ management.endpoints.web.exposure.include=* # Logging logging.level.org.springframework=INFO -logging.level.org.springframework.web=DEBUG -logging.level.org.springframework.context.annotation=TRACE -logging.level.org.springframework=DEBUG -logging.level.org.hibernate.SQL=DEBUG +# logging.level.org.springframework.web=DEBUG +# logging.level.org.springframework.context.annotation=TRACE # Maximum time static resources should be cached spring.web.resources.cache.cachecontrol.max-age=12h