From 94d633502243e42a19d345a284068200fc22d6b8 Mon Sep 17 00:00:00 2001 From: Perumal Raj A Date: Wed, 22 Jan 2025 11:36:31 +0000 Subject: [PATCH 1/5] Enhance_owner_search_functionality_1.0 Signed-off-by: Perumal Raj A --- .../samples/petclinic/owner/OwnerController.java | 11 ++++++++--- .../samples/petclinic/owner/OwnerRepository.java | 6 +++++- src/main/resources/templates/owners/findOwners.html | 5 ++--- 3 files changed, 15 insertions(+), 7 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 fa3506456..00747709a 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java @@ -98,7 +98,7 @@ class OwnerController { } // find owners by last name - Page ownersResults = findPaginatedForOwnersLastName(page, owner.getLastName()); + Page ownersResults = findPaginatedForOwnersName(page, owner.getLastName()); if (ownersResults.isEmpty()) { // no owners found result.rejectValue("lastName", "notFound", "not found"); @@ -124,10 +124,15 @@ class OwnerController { return "owners/ownersList"; } - private Page findPaginatedForOwnersLastName(int page, String lastname) { + // private Page findPaginatedForOwnersLastName(int page, String lastname) { + // int pageSize = 5; + // Pageable pageable = PageRequest.of(page - 1, pageSize); + // return owners.findByLastNameStartingWith(lastname, pageable); + // } + private Page findPaginatedForOwnersName(int page, String lastname) { int pageSize = 5; Pageable pageable = PageRequest.of(page - 1, pageSize); - return owners.findByLastNameStartingWith(lastname, pageable); + return owners.findByNameContaining(lastname, pageable); } @GetMapping("/owners/{ownerId}/edit") diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java index 5d7a40fbb..ee6ba4222 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -23,6 +23,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.transaction.annotation.Transactional; /** @@ -53,7 +54,10 @@ public interface OwnerRepository extends JpaRepository { * @return a Collection of matching {@link Owner}s (or an empty Collection if none * found) */ - Page findByLastNameStartingWith(String lastName, Pageable pageable); + // Page findByLastNameStartingWith(String lastName, Pageable pageable); + @Query("SELECT o FROM Owner o " + "WHERE LOWER(o.firstName) LIKE LOWER(CONCAT('%', :namePart, '%')) " + + "OR LOWER(o.lastName) LIKE LOWER(CONCAT('%', :namePart, '%'))") + Page findByNameContaining(@Param("namePart") String namePart, Pageable pageable); /** * Retrieve an {@link Owner} from the data store by id. diff --git a/src/main/resources/templates/owners/findOwners.html b/src/main/resources/templates/owners/findOwners.html index 0a818fc79..bb77523eb 100644 --- a/src/main/resources/templates/owners/findOwners.html +++ b/src/main/resources/templates/owners/findOwners.html @@ -9,7 +9,7 @@ class="form-horizontal" id="search-owner-form">
- +
- +
From 98ccc1e2e4db7ff92ef8ac378f75d56b2a94e2c1 Mon Sep 17 00:00:00 2001 From: Perumal Raj A Date: Wed, 5 Feb 2025 09:44:41 +0000 Subject: [PATCH 2/5] Updated PR based on feedback Signed-off-by: Perumal Raj A --- .../samples/petclinic/owner/OwnerController.java | 5 ----- .../samples/petclinic/owner/OwnerRepository.java | 2 +- 2 files changed, 1 insertion(+), 6 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 00747709a..333f717b1 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java @@ -124,11 +124,6 @@ class OwnerController { return "owners/ownersList"; } - // private Page findPaginatedForOwnersLastName(int page, String lastname) { - // int pageSize = 5; - // Pageable pageable = PageRequest.of(page - 1, pageSize); - // return owners.findByLastNameStartingWith(lastname, pageable); - // } private Page findPaginatedForOwnersName(int page, String lastname) { int pageSize = 5; Pageable pageable = PageRequest.of(page - 1, pageSize); diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java index ee6ba4222..fd5e3af41 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -54,7 +54,7 @@ public interface OwnerRepository extends JpaRepository { * @return a Collection of matching {@link Owner}s (or an empty Collection if none * found) */ - // Page findByLastNameStartingWith(String lastName, Pageable pageable); + @Query("SELECT o FROM Owner o " + "WHERE LOWER(o.firstName) LIKE LOWER(CONCAT('%', :namePart, '%')) " + "OR LOWER(o.lastName) LIKE LOWER(CONCAT('%', :namePart, '%'))") Page findByNameContaining(@Param("namePart") String namePart, Pageable pageable); From 6bc5bd4af64b4a793f174ce7f8640d052978f19b Mon Sep 17 00:00:00 2001 From: Perumal Raj A Date: Wed, 5 Feb 2025 12:45:20 +0000 Subject: [PATCH 3/5] Resolved conflict in OwnerRepository.java Signed-off-by: Perumal Raj A --- .../springframework/samples/petclinic/owner/OwnerRepository.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java index fd5e3af41..7ac1a6404 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -26,6 +26,7 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.transaction.annotation.Transactional; + /** * Repository class for Owner domain objects All method names are compliant * with Spring Data naming conventions so this interface can easily be extended for Spring From 2ab1cef91f029ba012887d576879fb0a80efda32 Mon Sep 17 00:00:00 2001 From: Perumal Raj A Date: Sat, 22 Feb 2025 05:32:51 +0000 Subject: [PATCH 4/5] Query Tunned for the Search Functionality Signed-off-by: Perumal Raj A --- .../petclinic/owner/OwnerController.java | 48 +++++++++++-------- .../petclinic/owner/OwnerRepository.java | 7 +-- src/main/resources/application.properties | 6 ++- 3 files changed, 33 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 333f717b1..596058d96 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java @@ -92,27 +92,33 @@ class OwnerController { @GetMapping("/owners") public String processFindForm(@RequestParam(defaultValue = "1") int page, Owner owner, BindingResult result, Model model) { - // allow parameterless GET request for /owners to return all records - if (owner.getLastName() == null) { - owner.setLastName(""); // empty string signifies broadest possible search + 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); + } + 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 = findPaginatedForOwnersName(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) { @@ -124,10 +130,10 @@ class OwnerController { return "owners/ownersList"; } - private Page findPaginatedForOwnersName(int page, String lastname) { + private Page findPaginatedForOwnersLastName(int page, String lastname) { int pageSize = 5; Pageable pageable = PageRequest.of(page - 1, pageSize); - return owners.findByNameContaining(lastname, pageable); + return owners.findByLastNameStartingWith(lastname, pageable); } @GetMapping("/owners/{ownerId}/edit") diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java index d8c389da1..3326e64dd 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -27,8 +27,6 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.transaction.annotation.Transactional; - - /** * Repository class for Owner domain objects All method names are compliant * with Spring Data naming conventions so this interface can easily be extended for Spring @@ -58,9 +56,8 @@ public interface OwnerRepository extends JpaRepository { * found) */ - @Query("SELECT o FROM Owner o " + "WHERE LOWER(o.firstName) LIKE LOWER(CONCAT('%', :namePart, '%')) " - + "OR LOWER(o.lastName) LIKE LOWER(CONCAT('%', :namePart, '%'))") - Page findByNameContaining(@Param("namePart") String namePart, Pageable pageable); + @Query("SELECT o FROM Owner o WHERE LOWER(CONCAT(o.firstName, ' ', o.lastName)) LIKE LOWER(CONCAT('%', :namePart, '%'))") + Page findByLastNameStartingWith(@Param("namePart") String namePart, Pageable pageable); /** * Retrieve an {@link Owner} from the data store by id. diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6ed985654..4a45d2046 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -18,8 +18,10 @@ 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.web=DEBUG +logging.level.org.springframework.context.annotation=TRACE +logging.level.org.springframework=DEBUG +logging.level.org.hibernate.SQL=DEBUG # Maximum time static resources should be cached spring.web.resources.cache.cachecontrol.max-age=12h From e15623d232af8890a30e5b59c31fd8017f911daf Mon Sep 17 00:00:00 2001 From: Perumal Raj A Date: Sat, 22 Feb 2025 05:38:18 +0000 Subject: [PATCH 5/5] 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