1 2 package org.springframework.samples.petclinic.toplink; 3 4 import java.util.Collection ; 5 6 import oracle.toplink.expressions.ExpressionBuilder; 7 import oracle.toplink.queryframework.ReadAllQuery; 8 9 import org.springframework.dao.DataAccessException; 10 import org.springframework.orm.toplink.support.TopLinkDaoSupport; 11 import org.springframework.samples.petclinic.Clinic; 12 import org.springframework.samples.petclinic.Owner; 13 import org.springframework.samples.petclinic.Pet; 14 import org.springframework.samples.petclinic.PetType; 15 import org.springframework.samples.petclinic.Vet; 16 import org.springframework.samples.petclinic.Visit; 17 18 29 public class TopLinkClinic extends TopLinkDaoSupport implements Clinic { 30 31 32 private final ReadAllQuery getVetsQuery; 33 34 35 private final ReadAllQuery getPetTypesQuery; 36 37 38 private final ReadAllQuery findOwnersQuery; 39 40 public TopLinkClinic() { 41 this.getVetsQuery = new ReadAllQuery(Vet.class); 43 this.getVetsQuery.addAscendingOrdering("lastName"); 44 this.getVetsQuery.addAscendingOrdering("firstName"); 45 this.getVetsQuery.conformResultsInUnitOfWork(); 46 47 this.getPetTypesQuery = new ReadAllQuery(PetType.class); 49 this.getPetTypesQuery.addOrdering( 50 this.getPetTypesQuery.getExpressionBuilder().get("name").ascending()); 51 this.getPetTypesQuery.conformResultsInUnitOfWork(); 52 53 this.findOwnersQuery = new ReadAllQuery(Owner.class); 55 this.findOwnersQuery.addArgument("LastName"); 56 ExpressionBuilder builder = this.findOwnersQuery.getExpressionBuilder(); 57 this.findOwnersQuery.setSelectionCriteria( 58 builder.get("lastName").like(builder.getParameter("LastName"))); 59 this.findOwnersQuery.conformResultsInUnitOfWork(); 60 } 61 62 65 public Collection getVets() throws DataAccessException { 66 return (Collection ) getTopLinkTemplate().executeQuery(this.getVetsQuery); 67 } 68 69 72 public Collection getPetTypes() throws DataAccessException { 73 return (Collection ) getTopLinkTemplate().executeQuery(this.getPetTypesQuery); 74 } 75 76 80 public Collection findOwners(final String lastName) throws DataAccessException { 81 return (Collection ) getTopLinkTemplate().executeQuery( 82 this.findOwnersQuery, new Object [] {lastName + "%"}); 83 } 84 85 88 public Owner loadOwner(int id) throws DataAccessException { 89 return (Owner) getTopLinkTemplate().readAndCopy(Owner.class, new Integer (id)); 90 } 91 92 95 public Pet loadPet(int id) throws DataAccessException { 96 return (Pet) getTopLinkTemplate().readAndCopy(Pet.class, new Integer (id)); 97 } 98 99 102 public void storeOwner(Owner owner) throws DataAccessException { 103 getTopLinkTemplate().deepMerge(owner); 108 } 109 110 113 public void storePet(Pet pet) throws DataAccessException { 114 getTopLinkTemplate().deepMerge(pet); 115 } 116 117 120 public void storeVisit(Visit visit) throws DataAccessException { 121 getTopLinkTemplate().deepMerge(visit); 122 } 123 124 } 125 | Popular Tags |