1 package org.apache.ojb.ejb.odmg; 2 3 17 18 import javax.ejb.EJBException ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 22 import org.apache.ojb.broker.PersistenceBroker; 23 import org.apache.ojb.broker.query.QueryByCriteria; 24 import org.apache.ojb.broker.util.logging.Logger; 25 import org.apache.ojb.broker.util.logging.LoggerFactory; 26 import org.apache.ojb.ejb.SessionBeanImpl; 27 import org.apache.ojb.odmg.HasBroker; 28 import org.apache.ojb.odmg.TransactionExt; 29 import org.odmg.Database; 30 import org.odmg.Implementation; 31 import org.odmg.LockNotGrantedException; 32 import org.odmg.OQLQuery; 33 import org.odmg.Transaction; 34 35 58 public abstract class ODMGBaseBeanImpl extends SessionBeanImpl 59 { 60 private Logger log = LoggerFactory.getLogger(ODMGBaseBeanImpl.class); 61 private Implementation odmg; 62 63 69 public void ejbCreate() 70 { 71 if (log.isDebugEnabled()) log.debug("ejbCreate was called"); 72 odmg = ODMGHelper.getODMG(); 73 } 74 75 78 public void ejbRemove() 79 { 80 super.ejbRemove(); 81 odmg = null; 82 } 83 84 88 public Database getDatabase() 89 { 90 return odmg.getDatabase(null); 91 } 92 93 97 public Implementation getImplementation() 98 { 99 return odmg; 100 } 101 102 105 public Object storeObject(Object object) 106 { 107 109 try 110 { 111 TransactionExt tx = (TransactionExt) odmg.currentTransaction(); 112 tx.lock(object, Transaction.WRITE); 113 tx.markDirty(object); 114 } 115 catch (LockNotGrantedException e) 116 { 117 log.error("Failure while storing object " + object, e); 118 throw new EJBException ("Failure while storing object", e); 119 } 120 return object; 121 } 122 123 126 public void deleteObject(Object object) 127 { 128 getDatabase().deletePersistent(object); 129 } 130 131 134 public Collection storeObjects(Collection objects) 135 { 136 try 137 { 138 140 Transaction tx = odmg.currentTransaction(); 141 for (Iterator iterator = objects.iterator(); iterator.hasNext();) 142 { 143 tx.lock(iterator.next(), Transaction.WRITE); 144 } 145 } 146 catch (LockNotGrantedException e) 147 { 148 log.error("Failure while storing objects " + objects, e); 149 throw new EJBException ("Failure while storing objects", e); 150 } 151 return objects; 152 } 153 154 157 public void deleteObjects(Collection objects) 158 { 159 for (Iterator iterator = objects.iterator(); iterator.hasNext();) 160 { 161 getDatabase().deletePersistent(iterator.next()); 162 } 163 } 164 165 170 public int getCount(Class target) 171 { 172 PersistenceBroker broker = ((HasBroker) odmg.currentTransaction()).getBroker(); 173 int result = broker.getCount(new QueryByCriteria(target)); 174 return result; 175 } 176 177 public Collection getAllObjects(Class target) 178 { 179 OQLQuery query = odmg.newOQLQuery(); 180 try 181 { 182 query.create("select allObjects from " + target.getName()); 183 return (Collection ) query.execute(); 184 } 185 catch (Exception e) 186 { 187 log.error("OQLQuery failed", e); 188 throw new EJBException ("OQLQuery failed", e); 189 } 190 } 191 } 192 | Popular Tags |