001: package org.apache.ojb.ejb.pb;
002:
003: /* Copyright 2004-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import javax.ejb.SessionBean;
019: import javax.ejb.SessionContext;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.ojb.broker.PersistenceBroker;
025: import org.apache.ojb.broker.core.PersistenceBrokerFactoryFactory;
026: import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF;
027: import org.apache.ojb.broker.query.Criteria;
028: import org.apache.ojb.broker.query.Query;
029: import org.apache.ojb.broker.query.QueryByCriteria;
030: import org.apache.ojb.broker.util.logging.Logger;
031: import org.apache.ojb.broker.util.logging.LoggerFactory;
032: import org.apache.ojb.ejb.ArticleVO;
033: import org.apache.ojb.ejb.PersonVO;
034:
035: /**
036: * This is an session bean implementation using PB-api.
037: * <br>
038: * For more structured implementations take a look at<br/>
039: * {@link org.apache.ojb.ejb.SessionBeanImpl}<br/>
040: * {@link org.apache.ojb.ejb.odmg.ODMGBaseBeanImpl}<br/>
041: * {@link org.apache.ojb.ejb.pb.PBBaseBeanImpl}<br/>
042: *
043: * @ejb:bean
044: * type="Stateless"
045: * name="PBSessionBean"
046: * jndi-name="org.apache.ojb.ejb.pb.PBSessionBean"
047: * local-jndi-name="org.apache.ojb.ejb.pb.PBSessionBeanLocal"
048: * view-type="both"
049: * transaction-type="Container"
050: *
051: * @ejb:interface
052: * remote-class="org.apache.ojb.ejb.pb.PBSessionRemote"
053: * local-class="org.apache.ojb.ejb.pb.PBSessionLocal"
054: * extends="javax.ejb.EJBObject"
055: *
056: * @ejb:home
057: * remote-class="org.apache.ojb.ejb.pb.PBSessionHome"
058: * local-class="org.apache.ojb.ejb.pb.PBSessionLocalHome"
059: * extends="javax.ejb.EJBHome"
060: *
061: * @ejb:transaction
062: * type="Required"
063: *
064: * @jonas.bean
065: * ejb-name="org.apache.ojb.ejb.pb.PBSessionBean"
066: * jndi-name="org.apache.ojb.ejb.pb.PBSessionBean"
067: *
068: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
069: * @version $Id: PBSessionBean.java,v 1.5.2.3 2005/12/21 22:21:38 tomdz Exp $
070: */
071: public class PBSessionBean implements SessionBean {
072: private Logger log = LoggerFactory.getLogger(PBSessionBean.class);
073: private SessionContext ctx;
074: private PersistenceBrokerFactoryIF pbf;
075:
076: public PBSessionBean() {
077: }
078:
079: public void ejbActivate() {/* unused */
080: }
081:
082: public void ejbPassivate() {/* unused */
083: }
084:
085: public void setSessionContext(SessionContext ctx) {
086: this .ctx = ctx;
087: }
088:
089: public SessionContext getSessionContext() {
090: return ctx;
091: }
092:
093: public void ejbRemove() {
094: ctx = null;
095: }
096:
097: public void ejbCreate() {
098: log.info("ejbCreate was called");
099: pbf = PersistenceBrokerFactoryFactory.instance();
100: }
101:
102: protected PersistenceBroker getBroker() {
103: if (log.isDebugEnabled())
104: log.debug("getBroker was called");
105: return pbf.defaultPersistenceBroker();
106: }
107:
108: protected List storeObjects(PersistenceBroker broker, List objects) {
109: for (Iterator it = objects.iterator(); it.hasNext();) {
110: broker.store(it.next());
111: }
112: return objects;
113: }
114:
115: protected void deleteObjects(PersistenceBroker broker, List objects) {
116: for (Iterator it = objects.iterator(); it.hasNext();) {
117: broker.delete(it.next());
118: }
119: }
120:
121: protected int getCount(Class target) {
122: PersistenceBroker broker = getBroker();
123: int result = broker.getCount(new QueryByCriteria(target));
124: broker.close();
125: return result;
126: }
127:
128: /**
129: * @ejb:interface-method
130: */
131: public Collection getAllObjects(Class target) {
132: if (log.isDebugEnabled())
133: log.debug("getAllObjects was called");
134: PersistenceBroker broker = getBroker();
135: Query q = new QueryByCriteria(target);
136: Collection result = broker.getCollectionByQuery(q);
137: broker.close();
138: return result;
139: }
140:
141: /**
142: * @ejb:interface-method
143: */
144: public Iterator iterateAllObjects(Class target) {
145: if (log.isDebugEnabled())
146: log.debug("getAllObjects was called");
147: PersistenceBroker broker = getBroker();
148: Query q = new QueryByCriteria(target);
149: Iterator result = broker.getIteratorByQuery(q);
150: broker.close();
151: return result;
152: }
153:
154: /**
155: * @ejb:interface-method
156: */
157: public int getArticleCount() {
158: if (log.isDebugEnabled())
159: log.debug("getArticleCount was called");
160: return getCount(ArticleVO.class);
161: }
162:
163: /**
164: * @ejb:interface-method
165: */
166: public Collection getArticlesByName(String articleName) {
167: if (log.isDebugEnabled())
168: log.debug("getArticlesByName was called");
169: PersistenceBroker broker = getBroker();
170: Criteria crit = new Criteria();
171: crit.addLike("name", articleName);
172: Query q = new QueryByCriteria(ArticleVO.class, crit);
173: Collection result = broker.getCollectionByQuery(q);
174: broker.close();
175: return result;
176: }
177:
178: /**
179: * @ejb:interface-method
180: */
181: public int getPersonCount() {
182: if (log.isDebugEnabled())
183: log.debug("getPersonCount was called");
184: return getCount(PersonVO.class);
185: }
186:
187: /**
188: * @ejb:interface-method
189: */
190: public List storeObjects(List objects) {
191: if (log.isDebugEnabled())
192: log.debug("storeObjects was called");
193: PersistenceBroker broker = getBroker();
194: List stored = this .storeObjects(broker, objects);
195: broker.close();
196: return stored;
197: }
198:
199: /**
200: * @ejb:interface-method
201: */
202: public void deleteObjects(List objects) {
203: if (log.isDebugEnabled())
204: log.debug("deleteObjects was called");
205: PersistenceBroker broker = getBroker();
206: this .deleteObjects(broker, objects);
207: broker.close();
208: }
209:
210: /**
211: * @ejb:interface-method
212: */
213: public boolean allInOne(List articles, List persons) {
214: if (log.isDebugEnabled())
215: log.debug("allInOne was called");
216: StringBuffer buf = new StringBuffer();
217: boolean passedWell = true;
218: String sep = System.getProperty("line.separator");
219:
220: int personsBefore = getPersonCount();
221: int articlesBefore = getArticleCount();
222: buf.append(sep + "# Start with " + personsBefore + " persons");
223: buf
224: .append(sep + "# Start with " + articlesBefore
225: + " articles");
226: storeObjects(articles);
227: storeObjects(persons);
228: int personsAfterStore = getPersonCount();
229: int articlesAfterStore = getArticleCount();
230: buf.append(sep + "# After store: " + personsAfterStore
231: + " persons");
232: buf.append(sep + "# After store: " + articlesAfterStore
233: + " articles");
234: deleteObjects(articles);
235: deleteObjects(persons);
236: int personsAfterDelete = getPersonCount();
237: int articlesAfterDelete = getArticleCount();
238: buf.append(sep + "# After delete: " + personsAfterDelete
239: + " persons");
240: buf.append(sep + "# After delete: " + articlesAfterDelete
241: + " articles");
242: log.info("## allInOne-Method call: " + buf.toString());
243: passedWell = (personsBefore + persons.size()) == personsAfterStore
244: && (articlesBefore + articles.size()) == articlesAfterStore
245: && (personsBefore) == personsAfterDelete
246: && (personsBefore) == personsAfterDelete;
247: return passedWell;
248: }
249: }
|