made sure the ClinicService facade is used by all Controllers

This commit is contained in:
Mic 2013-02-12 08:22:39 +08:00
parent 4d6496e18e
commit a994785f73
3 changed files with 28 additions and 9 deletions

View file

@ -19,6 +19,7 @@ import org.springframework.samples.petclinic.Visit;
* @author Ken Krebs
* @author Juergen Hoeller
* @author Sam Brannen
* @author Michael Isvy
*/
public interface ClinicService {
@ -34,4 +35,8 @@ public interface ClinicService {
public Collection<Vet> findVets() throws DataAccessException;
public void saveOwner(Owner owner) throws DataAccessException;
Collection<Owner> findOwnerByLastName(String lastName) throws DataAccessException;
}

View file

@ -42,11 +42,25 @@ public class ClinicServiceImpl implements ClinicService {
return ownerRepository.findById(id);
}
@Override
@Transactional(readOnly=true)
public Collection<Owner> findOwnerByLastName(String lastName) throws DataAccessException {
return ownerRepository.findByLastName(lastName);
}
@Override
@Transactional
public void saveOwner(Owner owner) throws DataAccessException {
ownerRepository.save(owner);
}
@Transactional
public void saveVisit(Visit visit) throws DataAccessException {
visitRepository.save(visit);
}
@Transactional(readOnly=true)
public Pet findPetById(int id) throws DataAccessException {
return petRepository.findById(id);

View file

@ -7,7 +7,7 @@ import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.Owner;
import org.springframework.samples.petclinic.repository.OwnerRepository;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
@ -32,12 +32,12 @@ import org.springframework.web.servlet.ModelAndView;
@SessionAttributes(types = Owner.class)
public class OwnerController {
private final OwnerRepository ownerRepository;
private final ClinicService clinicService;
@Autowired
public OwnerController(OwnerRepository ownerRepository) {
this.ownerRepository = ownerRepository;
public OwnerController(ClinicService clinicService) {
this.clinicService = clinicService;
}
@InitBinder
@ -58,7 +58,7 @@ public class OwnerController {
return "owners/createOrUpdateOwnerForm";
}
else {
this.ownerRepository.save(owner);
this.clinicService.saveOwner(owner);
status.setComplete();
return "redirect:/owners/" + owner.getId();
}
@ -79,7 +79,7 @@ public class OwnerController {
}
// find owners by last name
Collection<Owner> results = this.ownerRepository.findByLastName(owner.getLastName());
Collection<Owner> results = this.clinicService.findOwnerByLastName(owner.getLastName());
if (results.size() < 1) {
// no owners found
result.rejectValue("lastName", "notFound", "not found");
@ -99,7 +99,7 @@ public class OwnerController {
@RequestMapping(value="/owners/{ownerId}/edit", method = RequestMethod.GET)
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
Owner owner = this.ownerRepository.findById(ownerId);
Owner owner = this.clinicService.findOwnerById(ownerId);
model.addAttribute(owner);
return "owners/createOrUpdateOwnerForm";
}
@ -110,7 +110,7 @@ public class OwnerController {
return "owners/createOrUpdateOwnerForm";
}
else {
this.ownerRepository.save(owner);
this.clinicService.saveOwner(owner);
status.setComplete();
return "redirect:/owners/{ownerId}";
}
@ -125,7 +125,7 @@ public class OwnerController {
@RequestMapping("/owners/{ownerId}")
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
ModelAndView mav = new ModelAndView("owners/ownerDetails");
mav.addObject(this.ownerRepository.findById(ownerId));
mav.addObject(this.clinicService.findOwnerById(ownerId));
return mav;
}