From a745565dc0fb00971247148780be329cdd6ed9ed Mon Sep 17 00:00:00 2001 From: Alexander Dudkin <152862359+earlspilner@users.noreply.github.com> Date: Tue, 20 Aug 2024 20:41:33 +0300 Subject: [PATCH] Refactor ClinicServiceImpl (#143) * refactor: make fields final in ClinicServiceImpl * refactor: extract common entity retrieval logic to a private helper method --- .../petclinic/service/ClinicServiceImpl.java | 447 ++++++++---------- 1 file changed, 199 insertions(+), 248 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 e1f456d8b..baf81184e 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java @@ -15,30 +15,20 @@ */ package org.springframework.samples.petclinic.service; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Set; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.orm.ObjectRetrievalFailureException; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.model.Specialty; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.model.Visit; -import org.springframework.samples.petclinic.repository.OwnerRepository; -import org.springframework.samples.petclinic.repository.PetRepository; -import org.springframework.samples.petclinic.repository.PetTypeRepository; -import org.springframework.samples.petclinic.repository.SpecialtyRepository; -import org.springframework.samples.petclinic.repository.VetRepository; -import org.springframework.samples.petclinic.repository.VisitRepository; +import org.springframework.samples.petclinic.model.*; +import org.springframework.samples.petclinic.repository.*; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +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 * Also a placeholder for @Transactional and @Cacheable annotations @@ -49,251 +39,212 @@ @Service public class ClinicServiceImpl implements ClinicService { - private PetRepository petRepository; - private VetRepository vetRepository; - private OwnerRepository ownerRepository; - private VisitRepository visitRepository; - private SpecialtyRepository specialtyRepository; - private PetTypeRepository petTypeRepository; + private final PetRepository petRepository; + private final VetRepository vetRepository; + private final OwnerRepository ownerRepository; + private final VisitRepository visitRepository; + private final SpecialtyRepository specialtyRepository; + private final PetTypeRepository petTypeRepository; @Autowired - public ClinicServiceImpl( - PetRepository petRepository, - VetRepository vetRepository, - OwnerRepository ownerRepository, - VisitRepository visitRepository, - SpecialtyRepository specialtyRepository, - PetTypeRepository petTypeRepository) { + public ClinicServiceImpl( + PetRepository petRepository, + VetRepository vetRepository, + OwnerRepository ownerRepository, + VisitRepository visitRepository, + SpecialtyRepository specialtyRepository, + PetTypeRepository petTypeRepository) { this.petRepository = petRepository; this.vetRepository = vetRepository; this.ownerRepository = ownerRepository; this.visitRepository = visitRepository; this.specialtyRepository = specialtyRepository; - this.petTypeRepository = petTypeRepository; + this.petTypeRepository = petTypeRepository; + } + + @Override + @Transactional(readOnly = true) + public Collection findAllPets() throws DataAccessException { + return petRepository.findAll(); + } + + @Override + @Transactional + public void deletePet(Pet pet) throws DataAccessException { + petRepository.delete(pet); + } + + @Override + @Transactional(readOnly = true) + public Visit findVisitById(int visitId) throws DataAccessException { + return findEntityById(() -> visitRepository.findById(visitId)); + } + + @Override + @Transactional(readOnly = true) + public Collection findAllVisits() throws DataAccessException { + return visitRepository.findAll(); + } + + @Override + @Transactional + public void deleteVisit(Visit visit) throws DataAccessException { + visitRepository.delete(visit); + } + + @Override + @Transactional(readOnly = true) + public Vet findVetById(int id) throws DataAccessException { + return findEntityById(() -> vetRepository.findById(id)); + } + + @Override + @Transactional(readOnly = true) + public Collection findAllVets() throws DataAccessException { + return vetRepository.findAll(); + } + + @Override + @Transactional + public void saveVet(Vet vet) throws DataAccessException { + vetRepository.save(vet); + } + + @Override + @Transactional + public void deleteVet(Vet vet) throws DataAccessException { + vetRepository.delete(vet); + } + + @Override + @Transactional(readOnly = true) + public Collection findAllOwners() throws DataAccessException { + return ownerRepository.findAll(); } - @Override - @Transactional(readOnly = true) - public Collection findAllPets() throws DataAccessException { - return petRepository.findAll(); - } - - @Override - @Transactional - public void deletePet(Pet pet) throws DataAccessException { - petRepository.delete(pet); - } - - @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; - } - - @Override - @Transactional(readOnly = true) - public Collection findAllVisits() throws DataAccessException { - return visitRepository.findAll(); - } - - @Override - @Transactional - public void deleteVisit(Visit visit) throws DataAccessException { - visitRepository.delete(visit); - } - - @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; - } - - @Override - @Transactional(readOnly = true) - public Collection findAllVets() throws DataAccessException { - return vetRepository.findAll(); - } - - @Override - @Transactional - public void saveVet(Vet vet) throws DataAccessException { - vetRepository.save(vet); - } - - @Override - @Transactional - public void deleteVet(Vet vet) throws DataAccessException { - vetRepository.delete(vet); - } - - @Override - @Transactional(readOnly = true) - public Collection findAllOwners() throws DataAccessException { - return ownerRepository.findAll(); - } - - @Override - @Transactional - public void deleteOwner(Owner owner) throws DataAccessException { - ownerRepository.delete(owner); - } - - @Override + @Override + @Transactional + public void deleteOwner(Owner owner) throws DataAccessException { + ownerRepository.delete(owner); + } + + @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; - } - - @Override - @Transactional(readOnly = true) - public Collection findAllPetTypes() throws DataAccessException { - return petTypeRepository.findAll(); - } - - @Override - @Transactional - public void savePetType(PetType petType) throws DataAccessException { - petTypeRepository.save(petType); - } - - @Override - @Transactional - public void deletePetType(PetType petType) throws DataAccessException { - petTypeRepository.delete(petType); - } - - @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; - } - - @Override - @Transactional(readOnly = true) - public Collection findAllSpecialties() throws DataAccessException { - return specialtyRepository.findAll(); - } - - @Override - @Transactional - public void saveSpecialty(Specialty specialty) throws DataAccessException { - specialtyRepository.save(specialty); - } - - @Override - @Transactional - public void deleteSpecialty(Specialty specialty) throws DataAccessException { - specialtyRepository.delete(specialty); - } - - @Override - @Transactional(readOnly = true) - public Collection findPetTypes() throws DataAccessException { - return petRepository.findPetTypes(); - } - - @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; - } - - @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; - } - - @Override - @Transactional - public void savePet(Pet pet) throws DataAccessException { - petRepository.save(pet); - } - - @Override - @Transactional - public void saveVisit(Visit visit) throws DataAccessException { - visitRepository.save(visit); - - } - - @Override - @Transactional(readOnly = true) - public Collection findVets() throws DataAccessException { - return vetRepository.findAll(); - } - - @Override - @Transactional - public void saveOwner(Owner owner) throws DataAccessException { - ownerRepository.save(owner); - - } - - @Override - @Transactional(readOnly = true) - public Collection findOwnerByLastName(String lastName) throws DataAccessException { - return ownerRepository.findByLastName(lastName); - } - - @Override - @Transactional(readOnly = true) - public Collection findVisitsByPetId(int petId) { - return visitRepository.findByPetId(petId); - } + public PetType findPetTypeById(int petTypeId) { + return findEntityById(() -> petTypeRepository.findById(petTypeId)); + } @Override @Transactional(readOnly = true) - public List findSpecialtiesByNameIn(Set names){ - List specialties = new ArrayList<>(); + public Collection findAllPetTypes() throws DataAccessException { + return petTypeRepository.findAll(); + } + + @Override + @Transactional + public void savePetType(PetType petType) throws DataAccessException { + petTypeRepository.save(petType); + } + + @Override + @Transactional + public void deletePetType(PetType petType) throws DataAccessException { + petTypeRepository.delete(petType); + } + + @Override + @Transactional(readOnly = true) + public Specialty findSpecialtyById(int specialtyId) { + return findEntityById(() -> specialtyRepository.findById(specialtyId)); + } + + @Override + @Transactional(readOnly = true) + public Collection findAllSpecialties() throws DataAccessException { + return specialtyRepository.findAll(); + } + + @Override + @Transactional + public void saveSpecialty(Specialty specialty) throws DataAccessException { + specialtyRepository.save(specialty); + } + + @Override + @Transactional + public void deleteSpecialty(Specialty specialty) throws DataAccessException { + specialtyRepository.delete(specialty); + } + + @Override + @Transactional(readOnly = true) + public Collection findPetTypes() throws DataAccessException { + return petRepository.findPetTypes(); + } + + @Override + @Transactional(readOnly = true) + public Owner findOwnerById(int id) throws DataAccessException { + return findEntityById(() -> ownerRepository.findById(id)); + } + + @Override + @Transactional(readOnly = true) + public Pet findPetById(int id) throws DataAccessException { + return findEntityById(() -> petRepository.findById(id)); + } + + @Override + @Transactional + public void savePet(Pet pet) throws DataAccessException { + petRepository.save(pet); + } + + @Override + @Transactional + public void saveVisit(Visit visit) throws DataAccessException { + visitRepository.save(visit); + + } + + @Override + @Transactional(readOnly = true) + public Collection findVets() throws DataAccessException { + return vetRepository.findAll(); + } + + @Override + @Transactional + public void saveOwner(Owner owner) throws DataAccessException { + ownerRepository.save(owner); + + } + + @Override + @Transactional(readOnly = true) + public Collection findOwnerByLastName(String lastName) throws DataAccessException { + return ownerRepository.findByLastName(lastName); + } + + @Override + @Transactional(readOnly = true) + public Collection findVisitsByPetId(int petId) { + return visitRepository.findByPetId(petId); + } + + @Override + @Transactional(readOnly = true) + public List findSpecialtiesByNameIn(Set names) { + return findEntityById(() -> specialtyRepository.findSpecialtiesByNameIn(names)); + } + + private T findEntityById(Supplier supplier) { try { - specialties = specialtyRepository.findSpecialtiesByNameIn(names); - } catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) { - // just ignore not found exceptions for Jdbc/Jpa realization - return specialties; + return supplier.get(); + } catch (ObjectRetrievalFailureException | EmptyResultDataAccessException e) { + // Just ignore not found exceptions for Jdbc/Jpa realization + return null; } - return specialties; } + }