add some exception handling in ClinicServiceExtImpl

This commit is contained in:
Vitaliy Fedoriv 2016-11-12 22:32:51 +02:00
parent 50da6d65fb
commit bbae5a66f9

View file

@ -1,3 +1,19 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.petclinic.service;
import java.util.Collection;
@ -6,6 +22,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;
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;
@ -21,6 +39,11 @@ import org.springframework.samples.petclinic.repository.VisitRepositoryExt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Vitaliy Fedoriv
*
*/
@Service
@Qualifier("ClinicServiceExt")
public class ClinicServiceExtImpl implements ClinicServiceExt {
@ -63,7 +86,14 @@ public class ClinicServiceExtImpl implements ClinicServiceExt {
@Override
@Transactional(readOnly = true)
public Visit findVisitById(int visitId) throws DataAccessException {
return visitRepositoryExt.findById(visitId);
Visit visit = null;
try {
visit = visitRepositoryExt.findById(visitId);
} catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) {
// just ignore not found exceptions for Jdbc/Jpa realization
return null;
}
return visit;
}
@Override
@ -81,7 +111,14 @@ public class ClinicServiceExtImpl implements ClinicServiceExt {
@Override
@Transactional(readOnly = true)
public Vet findVetById(int id) throws DataAccessException {
return vetRepositoryExt.findById(id);
Vet vet = null;
try {
vet = vetRepositoryExt.findById(id);
} catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) {
// just ignore not found exceptions for Jdbc/Jpa realization
return null;
}
return vet;
}
@Override
@ -117,7 +154,14 @@ public class ClinicServiceExtImpl implements ClinicServiceExt {
@Override
@Transactional(readOnly = true)
public PetType findPetTypeById(int petTypeId) {
return petTypeRepositoryExt.findById(petTypeId);
PetType petType = null;
try {
petType = petTypeRepositoryExt.findById(petTypeId);
} catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) {
// just ignore not found exceptions for Jdbc/Jpa realization
return null;
}
return petType;
}
@Override
@ -141,7 +185,14 @@ public class ClinicServiceExtImpl implements ClinicServiceExt {
@Override
@Transactional(readOnly = true)
public Specialty findSpecialtyById(int specialtyId) {
return specialtyRepositoryExt.findById(specialtyId);
Specialty specialty = null;
try {
specialty = specialtyRepositoryExt.findById(specialtyId);
} catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) {
// just ignore not found exceptions for Jdbc/Jpa realization
return null;
}
return specialty;
}
@Override
@ -171,13 +222,27 @@ public class ClinicServiceExtImpl implements ClinicServiceExt {
@Override
@Transactional(readOnly = true)
public Owner findOwnerById(int id) throws DataAccessException {
return ownerRepositoryExt.findById(id);
Owner owner = null;
try {
owner = ownerRepositoryExt.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 {
return petRepositoryExt.findById(id);
Pet pet = null;
try {
pet = petRepositoryExt.findById(id);
} catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) {
// just ignore not found exceptions for Jdbc/Jpa realization
return null;
}
return pet;
}
@Override