1 package org.apache.ojb.ejb.pb; 2 3 17 18 import javax.ejb.CreateException ; 19 import javax.ejb.EJBException ; 20 import javax.ejb.SessionBean ; 21 import javax.naming.Context ; 22 import javax.naming.InitialContext ; 23 import javax.naming.NamingException ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.apache.ojb.broker.PersistenceBroker; 30 import org.apache.ojb.broker.core.PersistenceBrokerFactoryFactory; 31 import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF; 32 import org.apache.ojb.broker.query.Query; 33 import org.apache.ojb.broker.query.QueryByCriteria; 34 import org.apache.ojb.broker.util.logging.Logger; 35 import org.apache.ojb.broker.util.logging.LoggerFactory; 36 import org.apache.ojb.ejb.ArticleVO; 37 import org.apache.ojb.ejb.PersonVO; 38 import org.apache.ojb.ejb.SessionBeanImpl; 39 40 81 public class PersonArticleManagerPBBean extends SessionBeanImpl implements SessionBean 82 { 83 private static final String PERSON_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/pb/PersonManager"; 84 private static final String ARTICLE_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/pb/ArticleManager"; 85 86 private Logger log = LoggerFactory.getLogger(PersonArticleManagerPBBean.class); 87 private ArticleManagerPBLocal am; 88 private PersonManagerPBLocal pm; 89 private PersistenceBrokerFactoryIF pbf; 90 91 public PersonArticleManagerPBBean() 92 { 93 } 94 95 public void ejbCreate() 96 { 97 pbf = PersistenceBrokerFactoryFactory.instance(); 98 } 99 100 105 public void storeUsingNestedPB(List articles, List persons) 106 { 107 PersistenceBroker broker = pbf.defaultPersistenceBroker(); 108 try 109 { 110 Query q = new QueryByCriteria(PersonVO.class); 112 broker.getCollectionByQuery(q); 113 ArticleManagerPBLocal am = getArticleManager(); 117 am.storeArticles(articles); 118 storeObjects(broker, persons); 122 } 124 finally 129 { 130 if(broker != null) broker.close(); 132 } 133 } 134 135 private Collection storeObjects(PersistenceBroker broker, Collection objects) 136 { 137 for (Iterator it = objects.iterator(); it.hasNext();) 138 { 139 broker.store(it.next()); 140 } 141 return objects; 142 } 143 144 149 public void storeUsingSubBeans(List articles, List persons) 150 { 151 ArticleManagerPBLocal am = getArticleManager(); 153 PersonManagerPBLocal pm = getPersonManager(); 154 am.storeArticles(articles); 155 pm.storePersons(persons); 156 } 157 158 161 public List storeArticles(List articles) 162 { 163 return new ArrayList (getArticleManager().storeArticles(articles)); 164 } 165 166 169 public void deleteArticles(List articles) 170 { 171 getArticleManager().deleteArticles(articles); 172 } 173 174 177 public List storeArticlesIntricately(List articles) 178 { 179 ArrayList ret = new ArrayList (); 180 for (int i = 0; i < articles.size(); i++) 181 { 182 ret.add(getArticleManager().storeArticle((ArticleVO) articles.get(i))); 183 } 184 return ret; 185 } 186 187 190 public void deleteArticlesIntricately(List articles) 191 { 192 for (int i = 0; i < articles.size(); i++) 193 { 194 getArticleManager().deleteArticle((ArticleVO) articles.get(i)); 195 } 196 } 197 198 201 public List storePersons(List persons) 202 { 203 return new ArrayList (getPersonManager().storePersons(persons)); 204 } 205 206 209 public void deletePersons(List persons) 210 { 211 getPersonManager().deletePersons(persons); 212 } 213 214 217 public int articleCount() 218 { 219 return getArticleManager().countArticles(); 220 } 221 222 225 public int personCount() 226 { 227 return getPersonManager().countPersons(); 228 } 229 230 private ArticleManagerPBLocal getArticleManager() 231 { 232 if (am == null) 233 { 234 Context context = null; 235 try 236 { 237 context = new InitialContext (); 238 am = ((ArticleManagerPBLocalHome) context.lookup(ARTICLE_MANAGER_EJB_REF_NAME)).create(); 239 log.info("** Found bean: " + am); 240 return am; 241 } 242 catch (NamingException e) 243 { 244 log.error("Lookup using ejb-ref " + ARTICLE_MANAGER_EJB_REF_NAME + " failed", e); 245 throw new EJBException (e); 246 } 247 catch (CreateException e) 248 { 249 log.error("Creation of ArticleManager failed", e); 250 throw new EJBException (e); 251 } 252 } 253 return am; 254 } 255 256 private PersonManagerPBLocal getPersonManager() 257 { 258 if (pm == null) 259 { 260 Context context = null; 261 try 262 { 263 context = new InitialContext (); 264 pm = ((PersonManagerPBLocalHome) context.lookup(PERSON_MANAGER_EJB_REF_NAME)).create(); 265 log.info("** Found bean: " + pm); 266 return pm; 267 } 268 catch (NamingException e) 269 { 270 log.error("Lookup using ejb-ref " + PERSON_MANAGER_EJB_REF_NAME + " failed", e); 271 throw new EJBException (e); 272 } 273 catch (CreateException e) 274 { 275 log.error("Creation of PersonManager failed", e); 276 throw new EJBException (e); 277 } 278 } 279 return pm; 280 } 281 } 282 | Popular Tags |