Skip to content

Commit

Permalink
refactor: extract common entity retrieval logic to a private helper m…
Browse files Browse the repository at this point in the history
…ethod
  • Loading branch information
thisdudkin committed Aug 19, 2024
1 parent 1a3ec08 commit 4870011
Showing 1 changed file with 15 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -213,27 +185,13 @@ public Collection<PetType> 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
Expand Down Expand Up @@ -277,13 +235,16 @@ public Collection<Visit> findVisitsByPetId(int petId) {
@Override
@Transactional(readOnly = true)
public List<Specialty> findSpecialtiesByNameIn(Set<String> names) {
List<Specialty> specialties = new ArrayList<>();
return findEntityById(() -> specialtyRepository.findSpecialtiesByNameIn(names));
}

private <T> T findEntityById(Supplier<T> 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;
}

}

0 comments on commit 4870011

Please sign in to comment.