KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > ojb > samples > EmployeeDAO


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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 org.apache.cocoon.ojb.samples;
17
18 import javax.jdo.PersistenceManager;
19 import javax.jdo.Transaction;
20
21 import org.apache.cocoon.ojb.jdo.components.JDO;
22 import org.apache.cocoon.ojb.samples.bean.Employee;
23 import org.apache.ojb.broker.Identity;
24 import org.apache.ojb.broker.PersistenceBroker;
25 import org.apache.ojb.broker.PersistenceBrokerFactory;
26
27 /**
28  * Employee's Impl
29  *
30  * @author <a HREF="mailto:antonio@apache.org">Antonio Gallardo</a>
31  * @version CVS $Id: EmployeeDAO.java 155366 2005-02-25 19:54:12Z vgritsenko $
32 */

33 public class EmployeeDAO {
34
35     public EmployeeDAO(){}
36
37     public void retrieve(Employee bean, JDO jdo) {
38         // 1. Get the PersistenceManager
39
PersistenceManager persistenceManager = jdo.getPersistenceManager();
40         
41         Employee e = new Employee();
42         e.setId(bean.getId());
43         PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
44         Identity oid = new Identity(e, broker);
45         
46         Employee b = new Employee();
47         // 2. start transaction
48
persistenceManager.currentTransaction().begin();
49         // 3. Get the Object based on the primary key
50
b = (Employee) persistenceManager.getObjectById(oid, false);
51         // 4. Copy data to bean
52
copyData(b, bean);
53         // 5. End transaction
54
persistenceManager.currentTransaction().commit();
55     }
56
57     public void insert(Employee e, JDO jdo) {
58         // 1. Get the PersistenceManager
59
PersistenceManager persistenceManager = jdo.getPersistenceManager();
60         // 2. Get current transaction
61
Transaction tx = persistenceManager.currentTransaction();
62         // 3. Start a Transaction
63
tx.begin();
64         // 4. now perform persistence operations. Store the Employee
65
persistenceManager.makePersistent(e);
66         // 5. Commit the transaction
67
tx.commit();
68     }
69
70     public void update(Employee bean, JDO jdo) {
71         // 1. Get the PersistenceManager
72
PersistenceManager persistenceManager = jdo.getPersistenceManager();
73         
74         Employee e = new Employee();
75         e.setId(bean.getId());
76         PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
77         Identity oid = new Identity(e, broker);
78         
79         Employee b = new Employee();
80         // 2. start transaction
81
persistenceManager.currentTransaction().begin();
82         // 3. Get the Object based on the primary key
83
b = (Employee) persistenceManager.getObjectById(oid, false);
84         // 4. Copy data from bean
85
copyData(bean, b);
86         // Store to database
87
// persistenceManager.makePersistent(b);
88
// 5. End transaction
89
persistenceManager.currentTransaction().commit();
90     }
91
92     public void remove(Employee bean, JDO jdo) {
93         // 1. Get the PersistenceManager
94
PersistenceManager persistenceManager = jdo.getPersistenceManager();
95         
96         Employee e = new Employee();
97         e.setId(bean.getId());
98         PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
99         Identity oid = new Identity(e, broker);
100         
101         Employee b = new Employee();
102         // 2. start transaction
103
persistenceManager.currentTransaction().begin();
104         // 3. Get the Object based on the primary key
105
b = (Employee) persistenceManager.getObjectById(oid, false);
106         // Delete in the database
107
persistenceManager.deletePersistent(b);
108         // 5. End transaction
109
persistenceManager.currentTransaction().commit();
110     }
111
112     private void copyData(Employee from, Employee to) {
113         to.setId(from.getId());
114         to.setDepartmentId(from.getDepartmentId());
115         to.setName(from.getName());
116     }
117 }
118
Popular Tags