KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > dao > client > template > ToplinkDaoTemplate


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.dao.client.template;
17
18 import java.util.List JavaDoc;
19
20 import oracle.toplink.expressions.Expression;
21 import oracle.toplink.expressions.ExpressionBuilder;
22 import oracle.toplink.queryframework.ReportQuery;
23 import oracle.toplink.sessions.Session;
24 import oracle.toplink.sessions.UnitOfWork;
25
26 import com.ibatis.dao.client.DaoException;
27 import com.ibatis.dao.client.DaoManager;
28 import com.ibatis.dao.engine.transaction.toplink.ToplinkDaoTransaction;
29
30 /**
31  * A DaoTemplate for Toplink implementations that provides a convenient method
32  * to access the Toplink Session.
33  *
34  * @author Wayne Gentile
35  * @version $Revision: 152595 $ $Date: 2004-12-28 09:46:14 -0700 (Tue, 28 Dec 2004) $
36  */

37 public abstract class ToplinkDaoTemplate extends DaoTemplate {
38
39     //~ Constructors
40
// -----------------------------------------------------------
41

42     /**
43      * The DaoManager that manages this Dao instance will be passed in as the
44      * parameter to this constructor automatically upon instantiation.
45      *
46      * @param daoManager
47      * The Dao manager instance for this template
48      */

49     public ToplinkDaoTemplate(DaoManager daoManager) {
50         super(daoManager);
51
52     }
53
54     //~ Methods
55
// ----------------------------------------------------------------
56

57     /**
58      * Returns a count of the number of objects in a result set given the
59      * specified expression.
60      *
61      * @param referenceClass
62      * The reference class to use as instances in the result set
63      * @param expression
64      * The expression to use as a where clause
65      *
66      * @return The number of rows returned in the result set after executing the
67      * query
68      */

69     protected int getCount(Class JavaDoc referenceClass, Expression expression) {
70
71         int count = 0;
72
73         // Build the query to retrieve the object
74
ExpressionBuilder builder = new ExpressionBuilder();
75         ReportQuery query = new ReportQuery(builder);
76         query.setReferenceClass(referenceClass);
77         query.addCount();
78         query.setSelectionCriteria(expression);
79
80         // Execute the query
81
List JavaDoc results = (List JavaDoc) getSession().executeQuery(query);
82         if ((results != null) && (results.size() > 0)) {
83
84             count = ((Integer JavaDoc) results.get(0)).intValue();
85
86         }
87
88         return count;
89
90     }
91
92     /**
93      * Gets the Toplink session associated with the current DaoTransaction that
94      * this Dao is working under.
95      *
96      * @return A Toplink Session instance.
97      *
98      * @throws DaoException
99      * If a DaoException is thrown
100      */

101     protected Session getSession() throws DaoException {
102
103         ToplinkDaoTransaction trans = (ToplinkDaoTransaction) daoManager
104                 .getTransaction(this);
105
106         return trans.getSession();
107
108     }
109
110     /**
111      * Gets the Toplink UnitOfWork associated with the current DaoTransaction
112      * that this Dao is working under.
113      *
114      * @return A Toplink UnitOfWork instance.
115      *
116      * @throws DaoException
117      * If a DaoException is thrown
118      */

119     protected UnitOfWork getUnitOfWork() throws DaoException {
120
121         // Get a UnitOfWork using the TransactionManager
122
ToplinkDaoTransaction trans = (ToplinkDaoTransaction) daoManager
123                 .getTransaction(this);
124         UnitOfWork uow = trans.getUnitOfWork();
125
126         if ((uow == null) || !uow.isActive()) {
127
128             throw new DaoException("No active unit of work.");
129
130         }
131
132         return uow;
133
134     }
135
136 }
Popular Tags