1 package org.apache.ojb.ejb.pb; 2 3 17 18 19 import javax.ejb.SessionBean ; 20 import javax.ejb.SessionContext ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import org.apache.ojb.broker.PersistenceBroker; 26 import org.apache.ojb.broker.core.PersistenceBrokerFactoryFactory; 27 import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF; 28 import org.apache.ojb.broker.query.Criteria; 29 import org.apache.ojb.broker.query.Query; 30 import org.apache.ojb.broker.query.QueryByCriteria; 31 import org.apache.ojb.broker.util.logging.Logger; 32 import org.apache.ojb.broker.util.logging.LoggerFactory; 33 import org.apache.ojb.ejb.ArticleVO; 34 import org.apache.ojb.ejb.PersonVO; 35 36 72 public class PBSessionBean implements SessionBean 73 { 74 private Logger log = LoggerFactory.getLogger(PBSessionBean.class); 75 private SessionContext ctx; 76 private PersistenceBrokerFactoryIF pbf; 77 78 public PBSessionBean() 79 { 80 } 81 82 public void ejbActivate() 83 { 84 } 85 86 public void ejbPassivate() 87 { 88 } 89 90 public void setSessionContext(SessionContext ctx) 91 { 92 this.ctx = ctx; 93 } 94 95 public SessionContext getSessionContext() 96 { 97 return ctx; 98 } 99 100 public void ejbRemove() 101 { 102 ctx = null; 103 } 104 105 public void ejbCreate() 106 { 107 log.info("ejbCreate was called"); 108 pbf = PersistenceBrokerFactoryFactory.instance(); 109 } 110 111 protected PersistenceBroker getBroker() 112 { 113 if(log.isDebugEnabled()) log.debug("getBroker was called"); 114 return pbf.defaultPersistenceBroker(); 115 } 116 117 protected List storeObjects(PersistenceBroker broker, List objects) 118 { 119 for (Iterator it = objects.iterator(); it.hasNext();) 120 { 121 broker.store(it.next()); 122 } 123 return objects; 124 } 125 126 protected void deleteObjects(PersistenceBroker broker, List objects) 127 { 128 for (Iterator it = objects.iterator(); it.hasNext();) 129 { 130 broker.delete(it.next()); 131 } 132 } 133 134 protected int getCount(Class target) 135 { 136 PersistenceBroker broker = getBroker(); 137 int result = broker.getCount(new QueryByCriteria(target)); 138 broker.close(); 139 return result; 140 } 141 142 145 public Collection getAllObjects(Class target) 146 { 147 if(log.isDebugEnabled()) log.debug("getAllObjects was called"); 148 PersistenceBroker broker = getBroker(); 149 Query q = new QueryByCriteria(target); 150 Collection result = broker.getCollectionByQuery(q); 151 broker.close(); 152 return result; 153 } 154 155 158 public Iterator iterateAllObjects(Class target) 159 { 160 if(log.isDebugEnabled()) log.debug("getAllObjects was called"); 161 PersistenceBroker broker = getBroker(); 162 Query q = new QueryByCriteria(target); 163 Iterator result = broker.getIteratorByQuery(q); 164 broker.close(); 165 return result; 166 } 167 168 171 public int getArticleCount() 172 { 173 if(log.isDebugEnabled()) log.debug("getArticleCount was called"); 174 return getCount(ArticleVO.class); 175 } 176 177 180 public Collection getArticlesByName(String articleName) 181 { 182 if(log.isDebugEnabled()) log.debug("getArticlesByName was called"); 183 PersistenceBroker broker = getBroker(); 184 Criteria crit = new Criteria(); 185 crit.addLike("name", articleName); 186 Query q = new QueryByCriteria(ArticleVO.class, crit); 187 Collection result = broker.getCollectionByQuery(q); 188 broker.close(); 189 return result; 190 } 191 192 195 public int getPersonCount() 196 { 197 if(log.isDebugEnabled()) log.debug("getPersonCount was called"); 198 return getCount(PersonVO.class); 199 } 200 201 204 public List storeObjects(List objects) 205 { 206 if(log.isDebugEnabled()) log.debug("storeObjects was called"); 207 PersistenceBroker broker = getBroker(); 208 List stored = this.storeObjects(broker, objects); 209 broker.close(); 210 return stored; 211 } 212 213 216 public void deleteObjects(List objects) 217 { 218 if(log.isDebugEnabled()) log.debug("deleteObjects was called"); 219 PersistenceBroker broker = getBroker(); 220 this.deleteObjects(broker, objects); 221 broker.close(); 222 } 223 224 227 public boolean allInOne(List articles, List persons) 228 { 229 if(log.isDebugEnabled()) log.debug("allInOne was called"); 230 StringBuffer buf = new StringBuffer (); 231 boolean passedWell = true; 232 String sep = System.getProperty("line.separator"); 233 234 int personsBefore = getPersonCount(); 235 int articlesBefore = getArticleCount(); 236 buf.append(sep + "# Start with " + personsBefore + " persons"); 237 buf.append(sep + "# Start with " + articlesBefore + " articles"); 238 storeObjects(articles); 239 storeObjects(persons); 240 int personsAfterStore = getPersonCount(); 241 int articlesAfterStore = getArticleCount(); 242 buf.append(sep + "# After store: " + personsAfterStore + " persons"); 243 buf.append(sep + "# After store: " + articlesAfterStore + " articles"); 244 deleteObjects(articles); 245 deleteObjects(persons); 246 int personsAfterDelete = getPersonCount(); 247 int articlesAfterDelete = getArticleCount(); 248 buf.append(sep + "# After delete: " + personsAfterDelete + " persons"); 249 buf.append(sep + "# After delete: " + articlesAfterDelete + " articles"); 250 log.info("## allInOne-Method call: " + buf.toString()); 251 passedWell = (personsBefore + persons.size()) == personsAfterStore && 252 (articlesBefore + articles.size()) == articlesAfterStore && 253 (personsBefore) == personsAfterDelete && 254 (personsBefore) == personsAfterDelete; 255 return passedWell; 256 } 257 } 258 | Popular Tags |