From 487001195db1d5c45152bf8e5b188d6aae3f4c61 Mon Sep 17 00:00:00 2001 From: Alexander Date: Mon, 19 Aug 2024 13:34:29 +0300 Subject: [PATCH] refactor: extract common entity retrieval logic to a private helper method --- .../petclinic/service/ClinicServiceImpl.java | 69 ++++--------------- 1 file changed, 15 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java index 27e59ff26..baf81184e 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java @@ -24,10 +24,10 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Set; +import java.util.function.Supplier; /** * Mostly used as a facade for all Petclinic controllers @@ -77,14 +77,7 @@ public void deletePet(Pet pet) throws DataAccessException { @Override @Transactional(readOnly = true) public Visit findVisitById(int visitId) throws DataAccessException { - Visit visit = null; - try { - visit = visitRepository.findById(visitId); - } catch (ObjectRetrievalFailureException | EmptyResultDataAccessException e) { - // just ignore not found exceptions for Jdbc/Jpa realization - return null; - } - return visit; + return findEntityById(() -> visitRepository.findById(visitId)); } @Override @@ -102,14 +95,7 @@ public void deleteVisit(Visit visit) throws DataAccessException { @Override @Transactional(readOnly = true) public Vet findVetById(int id) throws DataAccessException { - Vet vet = null; - try { - vet = vetRepository.findById(id); - } catch (ObjectRetrievalFailureException | EmptyResultDataAccessException e) { - // just ignore not found exceptions for Jdbc/Jpa realization - return null; - } - return vet; + return findEntityById(() -> vetRepository.findById(id)); } @Override @@ -145,14 +131,7 @@ public void deleteOwner(Owner owner) throws DataAccessException { @Override @Transactional(readOnly = true) public PetType findPetTypeById(int petTypeId) { - PetType petType = null; - try { - petType = petTypeRepository.findById(petTypeId); - } catch (ObjectRetrievalFailureException | EmptyResultDataAccessException e) { - // just ignore not found exceptions for Jdbc/Jpa realization - return null; - } - return petType; + return findEntityById(() -> petTypeRepository.findById(petTypeId)); } @Override @@ -176,14 +155,7 @@ public void deletePetType(PetType petType) throws DataAccessException { @Override @Transactional(readOnly = true) public Specialty findSpecialtyById(int specialtyId) { - Specialty specialty = null; - try { - specialty = specialtyRepository.findById(specialtyId); - } catch (ObjectRetrievalFailureException | EmptyResultDataAccessException e) { - // just ignore not found exceptions for Jdbc/Jpa realization - return null; - } - return specialty; + return findEntityById(() -> specialtyRepository.findById(specialtyId)); } @Override @@ -213,27 +185,13 @@ public Collection findPetTypes() throws DataAccessException { @Override @Transactional(readOnly = true) public Owner findOwnerById(int id) throws DataAccessException { - Owner owner = null; - try { - owner = ownerRepository.findById(id); - } catch (ObjectRetrievalFailureException | EmptyResultDataAccessException e) { - // just ignore not found exceptions for Jdbc/Jpa realization - return null; - } - return owner; + return findEntityById(() -> ownerRepository.findById(id)); } @Override @Transactional(readOnly = true) public Pet findPetById(int id) throws DataAccessException { - Pet pet = null; - try { - pet = petRepository.findById(id); - } catch (ObjectRetrievalFailureException | EmptyResultDataAccessException e) { - // just ignore not found exceptions for Jdbc/Jpa realization - return null; - } - return pet; + return findEntityById(() -> petRepository.findById(id)); } @Override @@ -277,13 +235,16 @@ public Collection findVisitsByPetId(int petId) { @Override @Transactional(readOnly = true) public List findSpecialtiesByNameIn(Set names) { - List specialties = new ArrayList<>(); + return findEntityById(() -> specialtyRepository.findSpecialtiesByNameIn(names)); + } + + private T findEntityById(Supplier supplier) { try { - specialties = specialtyRepository.findSpecialtiesByNameIn(names); + return supplier.get(); } catch (ObjectRetrievalFailureException | EmptyResultDataAccessException e) { - // just ignore not found exceptions for Jdbc/Jpa realization - return specialties; + // Just ignore not found exceptions for Jdbc/Jpa realization + return null; } - return specialties; } + }