001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.ojb.samples;
018:
019: import javax.jdo.PersistenceManager;
020: import javax.jdo.Transaction;
021:
022: import org.apache.cocoon.ojb.jdo.components.JDO;
023: import org.apache.cocoon.ojb.samples.bean.Employee;
024: import org.apache.ojb.broker.Identity;
025: import org.apache.ojb.broker.PersistenceBroker;
026: import org.apache.ojb.broker.PersistenceBrokerFactory;
027:
028: /**
029: * Employee's Impl
030: *
031: * @author <a href="mailto:antonio@apache.org">Antonio Gallardo</a>
032: * @version CVS $Id: EmployeeDAO.java 433543 2006-08-22 06:22:54Z crossley $
033: */
034: public class EmployeeDAO {
035:
036: public EmployeeDAO() {
037: }
038:
039: public void retrieve(Employee bean, JDO jdo) {
040: // 1. Get the PersistenceManager
041: PersistenceManager persistenceManager = jdo
042: .getPersistenceManager();
043:
044: Employee e = new Employee();
045: e.setId(bean.getId());
046: PersistenceBroker broker = PersistenceBrokerFactory
047: .defaultPersistenceBroker();
048: Identity oid = new Identity(e, broker);
049:
050: Employee b = new Employee();
051: // 2. start transaction
052: persistenceManager.currentTransaction().begin();
053: // 3. Get the Object based on the primary key
054: b = (Employee) persistenceManager.getObjectById(oid, false);
055: // 4. Copy data to bean
056: copyData(b, bean);
057: // 5. End transaction
058: persistenceManager.currentTransaction().commit();
059: }
060:
061: public void insert(Employee e, JDO jdo) {
062: // 1. Get the PersistenceManager
063: PersistenceManager persistenceManager = jdo
064: .getPersistenceManager();
065: // 2. Get current transaction
066: Transaction tx = persistenceManager.currentTransaction();
067: // 3. Start a Transaction
068: tx.begin();
069: // 4. now perform persistence operations. Store the Employee
070: persistenceManager.makePersistent(e);
071: // 5. Commit the transaction
072: tx.commit();
073: }
074:
075: public void update(Employee bean, JDO jdo) {
076: // 1. Get the PersistenceManager
077: PersistenceManager persistenceManager = jdo
078: .getPersistenceManager();
079:
080: Employee e = new Employee();
081: e.setId(bean.getId());
082: PersistenceBroker broker = PersistenceBrokerFactory
083: .defaultPersistenceBroker();
084: Identity oid = new Identity(e, broker);
085:
086: Employee b = new Employee();
087: // 2. start transaction
088: persistenceManager.currentTransaction().begin();
089: // 3. Get the Object based on the primary key
090: b = (Employee) persistenceManager.getObjectById(oid, false);
091: // 4. Copy data from bean
092: copyData(bean, b);
093: // Store to database
094: // persistenceManager.makePersistent(b);
095: // 5. End transaction
096: persistenceManager.currentTransaction().commit();
097: }
098:
099: public void remove(Employee bean, JDO jdo) {
100: // 1. Get the PersistenceManager
101: PersistenceManager persistenceManager = jdo
102: .getPersistenceManager();
103:
104: Employee e = new Employee();
105: e.setId(bean.getId());
106: PersistenceBroker broker = PersistenceBrokerFactory
107: .defaultPersistenceBroker();
108: Identity oid = new Identity(e, broker);
109:
110: Employee b = new Employee();
111: // 2. start transaction
112: persistenceManager.currentTransaction().begin();
113: // 3. Get the Object based on the primary key
114: b = (Employee) persistenceManager.getObjectById(oid, false);
115: // Delete in the database
116: persistenceManager.deletePersistent(b);
117: // 5. End transaction
118: persistenceManager.currentTransaction().commit();
119: }
120:
121: private void copyData(Employee from, Employee to) {
122: to.setId(from.getId());
123: to.setDepartmentId(from.getDepartmentId());
124: to.setName(from.getName());
125: }
126: }
|