KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.ibatis.dao.client.DaoManager;
19 import com.ibatis.dao.engine.transaction.ojb.OjbBrokerDaoTransaction;
20 import org.apache.ojb.broker.Identity;
21 import org.apache.ojb.broker.PersistenceBroker;
22 import org.apache.ojb.broker.query.Query;
23 import org.apache.ojb.broker.query.QueryByCriteria;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27
28 /**
29  * A DaoTemplate for OJB broker implementations that provides a
30  * convenient method to access the broker.
31  * <p/>
32  * This is the base class for OJB DAOs and provides the CRUD
33  * pattern necessary methods plus an additional
34  * method to retrieve all instances of a given class.
35  */

36 public abstract class OjbBrokerDaoTemplate
37     extends DaoTemplate {
38
39   /**
40    * The DaoManager that manages this Dao instance will be passed in as the parameter to this constructor automatically upon instantiation.
41    *
42    * @param daoManager
43    */

44   public OjbBrokerDaoTemplate(final DaoManager daoManager) {
45     super(daoManager);
46   }
47
48   /**
49    * Puts an instance/value object on the persistentence layer.
50    *
51    * @param vo the value object.
52    */

53   public final void create(final Object JavaDoc vo) {
54
55     if (vo == null) {
56       throw new IllegalArgumentException JavaDoc("The value object to be created is null.");
57     }
58
59     PersistenceBroker broker = getPersistenceBroker();
60
61     broker.store(vo);
62   }
63
64   /**
65    * Retrieves an instance/value object, according to its primary key,
66    *
67    * @param vo the value object criteria.
68    * @return the value object.
69    */

70   public final Object JavaDoc retrieveByPk(final Object JavaDoc vo) {
71
72     PersistenceBroker broker = getPersistenceBroker();
73
74     Identity identity = new Identity(vo, broker);
75
76     return broker.getObjectByIdentity(identity);
77   }
78
79   /**
80    * Updates the instance/value object representation on the persistence layer.
81    *
82    * @param vo the value object.
83    */

84   public final void update(final Object JavaDoc vo) {
85
86     if (vo == null) {
87       throw new IllegalArgumentException JavaDoc("The value object to be updated is null.");
88     }
89
90     PersistenceBroker broker = getPersistenceBroker();
91
92     broker.store(vo);
93   }
94
95   /**
96    * Removes an instance/value object from the persistence layer.
97    *
98    * @param vo the value object.
99    */

100   public final void delete(final Object JavaDoc vo) {
101
102     PersistenceBroker broker = getPersistenceBroker();
103
104     broker.delete(vo);
105   }
106
107   /**
108    * Retrieves all instances of a given class, from the persistence layer.
109    *
110    * @param clazz the class of the instances to be fetched.
111    * @return all instances of the given class.
112    */

113   public final Collection JavaDoc retrieveExtent(final Class JavaDoc clazz) {
114
115     PersistenceBroker broker = getPersistenceBroker();
116
117     Query query = new QueryByCriteria(clazz, null);
118     Collection JavaDoc collection = new ArrayList JavaDoc();
119
120     collection = broker.getCollectionByQuery(query);
121
122     return collection;
123   }
124
125   /**
126    * Gets the OJB persistence broker associated with the current
127    * DaoTransaction that this Dao is working under.
128    *
129    * @return A Hibernate Session instance.
130    */

131   protected final PersistenceBroker getPersistenceBroker() {
132     OjbBrokerDaoTransaction trans = (OjbBrokerDaoTransaction) daoManager.getTransaction(this);
133     return trans.getBroker();
134   }
135
136 }
137
Popular Tags