1 package org.apache.ojb.ejb.odmg; 2 3 17 18 19 import javax.ejb.EJBException ; 20 import javax.ejb.SessionBean ; 21 import javax.ejb.SessionContext ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.apache.ojb.broker.OJBRuntimeException; 27 import org.apache.ojb.broker.util.logging.Logger; 28 import org.apache.ojb.broker.util.logging.LoggerFactory; 29 import org.apache.ojb.ejb.ArticleVO; 30 import org.apache.ojb.ejb.PersonVO; 31 import org.odmg.Database; 32 import org.odmg.Implementation; 33 import org.odmg.OQLQuery; 34 import org.odmg.QueryException; 35 import org.odmg.Transaction; 36 37 98 public class ODMGSessionBean implements SessionBean 99 { 100 private Logger log = LoggerFactory.getLogger(ODMGSessionBean.class); 101 private SessionContext ctx; 102 private Implementation odmg; 103 private Database db; 104 105 public ODMGSessionBean() 106 { 107 } 108 109 115 public void ejbCreate() 116 { 117 log.info("ejbCreate was called"); 118 odmg = ODMGHelper.getODMG(); 119 db = odmg.getDatabase(null); 120 } 121 122 public void ejbRemove() 123 { 124 db = null; 125 odmg = null; 126 ctx = null; 127 } 128 129 132 public List storeObjects(List objects) 133 { 134 if(log.isDebugEnabled()) log.debug("storeObjects"); 135 136 138 Transaction tx = odmg.currentTransaction(); 139 for (Iterator iterator = objects.iterator(); iterator.hasNext();) 140 { 141 tx.lock(iterator.next(), Transaction.WRITE); 142 } 143 return objects; 144 } 145 146 149 public void deleteObjects(List objects) 150 { 151 if(log.isDebugEnabled()) log.debug("deleteObjects"); 152 db = odmg.getDatabase(null); 153 for (Iterator iterator = objects.iterator(); iterator.hasNext();) 154 { 155 db.deletePersistent(iterator.next()); 156 } 157 } 158 159 protected int getObjectCount(Implementation ojb, Class target) 160 { 161 if(log.isDebugEnabled()) log.debug("getObjectCount was called"); 162 List list; 163 try 164 { 165 OQLQuery query = ojb.newOQLQuery(); 166 query.create("select allObjects from " + target.getName()); 167 list = (List ) query.execute(); 168 return list.size(); 169 } 170 catch (QueryException e) 171 { 172 throw new EJBException ("Query objects failed", e); 173 } 174 } 175 176 179 public int getArticleCount() 180 { 181 if(log.isDebugEnabled()) log.debug("getArticleCount was called"); 182 return getObjectCount(odmg, ArticleVO.class); 183 } 184 185 188 public Collection getArticlesByName(String articleName) 189 { 190 if(log.isDebugEnabled()) log.debug("getArticlesByName was called"); 191 try 192 { 193 OQLQuery query = odmg.newOQLQuery(); 194 query.create("select allArticles from " + ArticleVO.class.getName() + " where name like $1"); 195 query.bind(articleName); 196 return (Collection ) query.execute(); 197 } 198 catch (QueryException e) 199 { 200 throw new EJBException ("Query objects failed", e); 201 } 202 } 203 204 207 public int getPersonCount() 208 { 209 if(log.isDebugEnabled()) log.debug("getPersonCount was called"); 210 return getObjectCount(odmg, PersonVO.class); 211 } 212 213 216 public boolean allInOne(List articles, List persons) 217 { 218 boolean passedWell = true; 219 if(log.isDebugEnabled()) log.debug("allInOne method was called"); 220 StringBuffer buf = new StringBuffer (); 221 222 String sep = System.getProperty("line.separator"); 223 224 db = odmg.getDatabase(null); 225 226 int personsBefore = getPersonCount(); 227 int articlesBefore = getArticleCount(); 228 buf.append(sep + "# Start with " + personsBefore + " persons"); 229 buf.append(sep + "# Start with " + articlesBefore + " articles"); 230 storeObjects(articles); 231 storeObjects(persons); 232 int personsAfterStore = getPersonCount(); 233 int articlesAfterStore = getArticleCount(); 234 buf.append(sep + "# After store: " + personsAfterStore + " persons"); 235 buf.append(sep + "# After store: " + articlesAfterStore + " articles"); 236 deleteObjects(articles); 237 deleteObjects(persons); 238 int personsAfterDelete = getPersonCount(); 239 int articlesAfterDelete = getArticleCount(); 240 buf.append(sep + "# After delete: " + personsAfterDelete + " persons"); 241 buf.append(sep + "# After delete: " + articlesAfterDelete + " articles"); 242 log.info("## allInOne-Method call: " + buf.toString()); 243 passedWell = (personsBefore) == personsAfterStore && 250 (articlesBefore) == articlesAfterStore && 251 (personsBefore) == personsAfterDelete && 252 (personsBefore) == personsAfterDelete; 253 return passedWell; 254 } 255 256 259 public Collection getAllObjects(Class target) 260 { 261 if(log.isDebugEnabled()) log.debug("getAllObjects was called"); 262 OQLQuery query = odmg.newOQLQuery(); 263 try 264 { 265 query.create("select allObjects from " + target.getName()); 266 return (Collection ) query.execute(); 267 } 268 catch (Exception e) 269 { 270 log.error("OQLQuery failed", e); 271 throw new OJBRuntimeException("OQLQuery failed", e); 272 } 273 } 274 275 public void ejbActivate() 276 { 277 } 278 279 public void ejbPassivate() 280 { 281 } 282 283 public void setSessionContext(SessionContext ctx) 284 { 285 this.ctx = ctx; 286 } 287 288 public SessionContext getSessionContext() 289 { 290 return ctx; 291 } 292 } 293 | Popular Tags |