1 package org.apache.ojb.ejb.pb; 2 3 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 21 import org.apache.ojb.broker.PBKey; 22 import org.apache.ojb.broker.PersistenceBroker; 23 import org.apache.ojb.broker.core.PersistenceBrokerFactoryFactory; 24 import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF; 25 import org.apache.ojb.broker.query.Query; 26 import org.apache.ojb.broker.query.QueryByCriteria; 27 import org.apache.ojb.broker.util.logging.Logger; 28 import org.apache.ojb.broker.util.logging.LoggerFactory; 29 import org.apache.ojb.ejb.SessionBeanImpl; 30 31 51 public abstract class PBBaseBeanImpl extends SessionBeanImpl 52 { 53 private Logger log = LoggerFactory.getLogger(PBBaseBeanImpl.class); 54 private PersistenceBrokerFactoryIF pbf; 55 56 public void ejbRemove() 57 { 58 super.ejbRemove(); 59 pbf = null; 61 log = null; 62 } 63 64 public void ejbCreate() 65 { 66 if (log.isDebugEnabled()) log.info("ejbCreate was called"); 67 pbf = PersistenceBrokerFactoryFactory.instance(); 68 } 69 70 73 public PersistenceBroker getBroker() 74 { 75 return pbf.defaultPersistenceBroker(); 76 } 77 78 82 public PersistenceBroker getBroker(PBKey key) 83 { 84 return pbf.createPersistenceBroker(key); 85 } 86 87 91 public int getCount(Class target) 92 { 93 PersistenceBroker broker = getBroker(); 94 int result; 95 try 96 { 97 result = broker.getCount(new QueryByCriteria(target)); 98 } 99 finally 100 { 101 if (broker != null) broker.close(); 102 } 103 return result; 104 } 105 106 109 public Collection getAllObjects(Class target) 110 { 111 PersistenceBroker broker = getBroker(); 112 Collection result; 113 try 114 { 115 Query q = new QueryByCriteria(target); 116 result = broker.getCollectionByQuery(q); 117 } 118 finally 119 { 120 if (broker != null) broker.close(); 121 } 122 return result; 123 } 124 125 128 public Object storeObject(Object object) 129 { 130 PersistenceBroker broker = getBroker(); 131 try 132 { 133 broker.store(object); 134 } 135 finally 136 { 137 if (broker != null) broker.close(); 138 } 139 return object; 140 } 141 142 145 public void deleteObject(Object object) 146 { 147 PersistenceBroker broker = null; 148 try 149 { 150 broker = getBroker(); 151 broker.delete(object); 152 } 153 finally 154 { 155 if (broker != null) broker.close(); 156 } 157 } 158 159 162 public Collection storeObjects(Collection objects) 163 { 164 PersistenceBroker broker = null; 165 Collection stored; 166 try 167 { 168 broker = getBroker(); 169 stored = this.storeObjects(broker, objects); 170 } 171 finally 172 { 173 if (broker != null) broker.close(); 174 } 175 return stored; 176 } 177 178 181 public void deleteObjects(Collection objects) 182 { 183 PersistenceBroker broker = null; 184 try 185 { 186 broker = getBroker(); 187 this.deleteObjects(broker, objects); 188 } 189 finally 190 { 191 if (broker != null) broker.close(); 192 } 193 } 194 195 protected Collection storeObjects(PersistenceBroker broker, Collection objects) 196 { 197 for (Iterator it = objects.iterator(); it.hasNext();) 198 { 199 broker.store(it.next()); 200 } 201 return objects; 202 } 203 204 protected void deleteObjects(PersistenceBroker broker, Collection objects) 205 { 206 for (Iterator it = objects.iterator(); it.hasNext();) 207 { 208 broker.delete(it.next()); 209 } 210 } 211 } 212 | Popular Tags |