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.EJBException;
019: import javax.ejb.SessionBean;
020: import java.util.Collection;
021:
022: import org.apache.ojb.broker.PersistenceBroker;
023: import org.apache.ojb.broker.query.Criteria;
024: import org.apache.ojb.broker.query.Query;
025: import org.apache.ojb.broker.query.QueryByCriteria;
026: import org.apache.ojb.ejb.ArticleVO;
027: import org.apache.ojb.ejb.CategoryVO;
028:
029: /**
030: * Simple example bean for manage articles using the PB-api
031: * by subclassing {@link org.apache.ojb.ejb.pb.PBBaseBeanImpl}
032: *
033: * @ejb:bean
034: * type="Stateless"
035: * name="ArticleManagerPBBean"
036: * jndi-name="org.apache.ojb.ejb.pb.ArticleManagerPBBean"
037: * local-jndi-name="org.apache.ojb.ejb.pb.ArticleManagerPBBeanLocal"
038: * view-type="both"
039: * transaction-type="Container"
040: *
041: * @ejb:interface
042: * remote-class="org.apache.ojb.ejb.pb.ArticleManagerPBRemote"
043: * local-class="org.apache.ojb.ejb.pb.ArticleManagerPBLocal"
044: * extends="javax.ejb.EJBObject"
045: *
046: * @ejb:home
047: * remote-class="org.apache.ojb.ejb.pb.ArticleManagerPBHome"
048: * local-class="org.apache.ojb.ejb.pb.ArticleManagerPBLocalHome"
049: * extends="javax.ejb.EJBHome"
050: *
051: * @ejb:transaction
052: * type="Required"
053: *
054: *
055: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
056: * @version $Id: ArticleManagerPBBean.java,v 1.4.2.1 2005/12/21 22:21:38 tomdz Exp $
057: */
058: public class ArticleManagerPBBean extends PBBaseBeanImpl implements
059: SessionBean {
060: public ArticleManagerPBBean() {
061: }
062:
063: /**
064: * @ejb:interface-method
065: */
066: public ArticleVO storeArticle(ArticleVO article) {
067: return (ArticleVO) this .storeObject(article);
068: }
069:
070: /**
071: * @ejb:interface-method
072: */
073: public Collection storeArticles(Collection articles) {
074: return this .storeObjects(articles);
075: }
076:
077: /**
078: * @ejb:interface-method
079: */
080: public void deleteArticle(ArticleVO article) {
081: this .deleteObject(article);
082: }
083:
084: /**
085: * @ejb:interface-method
086: */
087: public void deleteArticles(Collection articles) {
088: this .deleteObjects(articles);
089: }
090:
091: /**
092: * @ejb:interface-method
093: */
094: public int countArticles() {
095: return this .getCount(ArticleVO.class);
096: }
097:
098: /**
099: * @ejb:interface-method
100: */
101: public Collection getAllArticles() {
102: return this .getAllObjects(ArticleVO.class);
103: }
104:
105: /**
106: * Simulate a failure store.
107: *
108: * @ejb:interface-method
109: */
110: public ArticleVO failureStore(ArticleVO article) {
111: storeArticle(article);
112: // now we want to rollback
113: throw new EJBException("# failureStore method test #");
114: }
115:
116: /**
117: * @ejb:interface-method
118: */
119: public Collection getArticles(String articleName) {
120: PersistenceBroker broker = getBroker();
121: Collection result;
122: try {
123: Criteria criteria = new Criteria();
124: if (articleName != null)
125: criteria.addEqualTo("name", articleName);
126: Query q = new QueryByCriteria(ArticleVO.class, criteria);
127: result = broker.getCollectionByQuery(q);
128: } finally {
129: if (broker != null)
130: broker.close();
131: }
132: return result;
133: }
134:
135: /**
136: * @ejb:interface-method
137: */
138: public CategoryVO storeCategory(CategoryVO category) {
139: return (CategoryVO) this .storeObject(category);
140: }
141:
142: /**
143: * @ejb:interface-method
144: */
145: public void deleteCategory(CategoryVO category) {
146: this .deleteObject(category);
147: }
148:
149: /**
150: * @ejb:interface-method
151: */
152: public Collection getCategoryByName(String categoryName) {
153: PersistenceBroker broker = getBroker();
154: Collection result;
155: try {
156: Criteria criteria = new Criteria();
157: if (categoryName != null)
158: criteria.addEqualTo("categoryName", categoryName);
159: Query q = new QueryByCriteria(CategoryVO.class, criteria);
160: result = broker.getCollectionByQuery(q);
161: } finally {
162: if (broker != null)
163: broker.close();
164: }
165: return result;
166: }
167: }
|