KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > petclinic > toplink > TopLinkClinic


1
2 package org.springframework.samples.petclinic.toplink;
3
4 import java.util.Collection JavaDoc;
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 /**
19  * Toplink implementation of the Clinic interface.
20  *
21  * <p>The mappings are defined in "toplink-mappings.xml";
22  * session data is specified in "toplink-sessions.xml"
23  * (both are located in the root of the class path).
24  *
25  * @author Juergen Hoeller
26  * @author <a HREF="mailto:james.x.clark@oracle.com">James Clark</a>
27  * @since 1.2
28  */

29 public class TopLinkClinic extends TopLinkDaoSupport implements Clinic {
30
31     /** Prepared TopLink query object for the getVets method */
32     private final ReadAllQuery getVetsQuery;
33
34     /** Prepared TopLink query object for the getPetTypes method */
35     private final ReadAllQuery getPetTypesQuery;
36
37     /** Prepared TopLink query object for the findOwners method */
38     private final ReadAllQuery findOwnersQuery;
39
40     public TopLinkClinic() {
41         // Prepare TopLink query object for the getVets method.
42
this.getVetsQuery = new ReadAllQuery(Vet.class);
43         this.getVetsQuery.addAscendingOrdering("lastName");
44         this.getVetsQuery.addAscendingOrdering("firstName");
45         this.getVetsQuery.conformResultsInUnitOfWork();
46
47         // Prepare TopLink query object for the getPetTypes method.
48
this.getPetTypesQuery = new ReadAllQuery(PetType.class);
49         this.getPetTypesQuery.addOrdering(
50                 this.getPetTypesQuery.getExpressionBuilder().get("name").ascending());
51         this.getPetTypesQuery.conformResultsInUnitOfWork();
52
53         // Prepare TopLink query object for the findOwners method.
54
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     /**
63      * Return all Vet objects from the shared cache.
64      */

65     public Collection JavaDoc getVets() throws DataAccessException {
66         return (Collection JavaDoc) getTopLinkTemplate().executeQuery(this.getVetsQuery);
67     }
68
69     /**
70      * Return all PetType objects from the shared cache.
71      */

72     public Collection JavaDoc getPetTypes() throws DataAccessException {
73         return (Collection JavaDoc) getTopLinkTemplate().executeQuery(this.getPetTypesQuery);
74     }
75
76     /**
77      * Return a set of Owner objects from the shared cache.
78      * Uses a "LASTNAME LIKE arg%" query.
79      */

80     public Collection JavaDoc findOwners(final String JavaDoc lastName) throws DataAccessException {
81         return (Collection JavaDoc) getTopLinkTemplate().executeQuery(
82                 this.findOwnersQuery, new Object JavaDoc[] {lastName + "%"});
83     }
84
85     /**
86      * Return a copy of the specified Owner object.
87      */

88     public Owner loadOwner(int id) throws DataAccessException {
89         return (Owner) getTopLinkTemplate().readAndCopy(Owner.class, new Integer JavaDoc(id));
90     }
91
92     /**
93      * Return a copy of the specified Pet object.
94      */

95     public Pet loadPet(int id) throws DataAccessException {
96         return (Pet) getTopLinkTemplate().readAndCopy(Pet.class, new Integer JavaDoc(id));
97     }
98
99     /**
100      * Merge the given Owner object into the current UnitOfWork.
101      */

102     public void storeOwner(Owner owner) throws DataAccessException {
103         // Note: TopLink's merge operation does not reassociate the object with the
104
// current TopLink Session. Instead, it will always copy the state over to
105
// a registered representation of the entity. In case of a new entity, it will
106
// register a copy as well, but will also update the id of the passed-in object.
107
getTopLinkTemplate().deepMerge(owner);
108     }
109
110     /**
111      * Merge the given Pet object into the current UnitOfWork.
112      */

113     public void storePet(Pet pet) throws DataAccessException {
114         getTopLinkTemplate().deepMerge(pet);
115     }
116
117     /**
118      * Merge the given Visit object into the current UnitOfWork.
119      */

120     public void storeVisit(Visit visit) throws DataAccessException {
121         getTopLinkTemplate().deepMerge(visit);
122     }
123
124 }
125
Popular Tags