KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > ejb > pb > ArticleManagerPBBean


1 package org.apache.ojb.ejb.pb;
2
3 /* Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import javax.ejb.EJBException JavaDoc;
19 import javax.ejb.SessionBean JavaDoc;
20 import java.util.Collection JavaDoc;
21
22 import org.apache.ojb.broker.PersistenceBroker;
23 import org.apache.ojb.broker.query.Criteria;
24 import org.apache.ojb.broker.query.Query;
25 import org.apache.ojb.broker.query.QueryByCriteria;
26 import org.apache.ojb.ejb.ArticleVO;
27 import org.apache.ojb.ejb.CategoryVO;
28
29 /**
30  * Simple example bean for manage articles using the PB-api
31  * by subclassing {@link org.apache.ojb.ejb.pb.PBBaseBeanImpl}
32  *
33  * @ejb:bean
34  * type="Stateless"
35  * name="ArticleManagerPBBean"
36  * jndi-name="org.apache.ojb.ejb.pb.ArticleManagerPBBean"
37  * local-jndi-name="org.apache.ojb.ejb.pb.ArticleManagerPBBeanLocal"
38  * view-type="both"
39  * transaction-type="Container"
40  *
41  * @ejb:interface
42  * remote-class="org.apache.ojb.ejb.pb.ArticleManagerPBRemote"
43  * local-class="org.apache.ojb.ejb.pb.ArticleManagerPBLocal"
44  * extends="javax.ejb.EJBObject"
45  *
46  * @ejb:home
47  * remote-class="org.apache.ojb.ejb.pb.ArticleManagerPBHome"
48  * local-class="org.apache.ojb.ejb.pb.ArticleManagerPBLocalHome"
49  * extends="javax.ejb.EJBHome"
50  *
51  * @ejb:transaction
52  * type="Required"
53  *
54  *
55  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
56  * @version $Id: ArticleManagerPBBean.java,v 1.4.2.1 2005/12/21 22:21:38 tomdz Exp $
57  */

58 public class ArticleManagerPBBean extends PBBaseBeanImpl implements SessionBean JavaDoc
59 {
60     public ArticleManagerPBBean()
61     {
62     }
63
64     /**
65      * @ejb:interface-method
66      */

67     public ArticleVO storeArticle(ArticleVO article)
68     {
69         return (ArticleVO) this.storeObject(article);
70     }
71
72     /**
73      * @ejb:interface-method
74      */

75     public Collection JavaDoc storeArticles(Collection JavaDoc articles)
76     {
77         return this.storeObjects(articles);
78     }
79
80     /**
81      * @ejb:interface-method
82      */

83     public void deleteArticle(ArticleVO article)
84     {
85         this.deleteObject(article);
86     }
87
88     /**
89      * @ejb:interface-method
90      */

91     public void deleteArticles(Collection JavaDoc articles)
92     {
93         this.deleteObjects(articles);
94     }
95
96     /**
97      * @ejb:interface-method
98      */

99     public int countArticles()
100     {
101         return this.getCount(ArticleVO.class);
102     }
103
104     /**
105      * @ejb:interface-method
106      */

107     public Collection JavaDoc getAllArticles()
108     {
109         return this.getAllObjects(ArticleVO.class);
110     }
111
112     /**
113      * Simulate a failure store.
114      *
115      * @ejb:interface-method
116      */

117     public ArticleVO failureStore(ArticleVO article)
118     {
119         storeArticle(article);
120         // now we want to rollback
121
throw new EJBException JavaDoc("# failureStore method test #");
122     }
123
124     /**
125      * @ejb:interface-method
126      */

127     public Collection JavaDoc getArticles(String JavaDoc articleName)
128     {
129         PersistenceBroker broker = getBroker();
130         Collection JavaDoc result;
131         try
132         {
133             Criteria criteria = new Criteria();
134             if (articleName != null) criteria.addEqualTo("name", articleName);
135             Query q = new QueryByCriteria(ArticleVO.class, criteria);
136             result = broker.getCollectionByQuery(q);
137         }
138         finally
139         {
140             if (broker != null) broker.close();
141         }
142         return result;
143     }
144
145     /**
146      * @ejb:interface-method
147      */

148     public CategoryVO storeCategory(CategoryVO category)
149     {
150         return (CategoryVO) this.storeObject(category);
151     }
152
153     /**
154      * @ejb:interface-method
155      */

156     public void deleteCategory(CategoryVO category)
157     {
158         this.deleteObject(category);
159     }
160
161     /**
162      * @ejb:interface-method
163      */

164     public Collection JavaDoc getCategoryByName(String JavaDoc categoryName)
165     {
166         PersistenceBroker broker = getBroker();
167         Collection JavaDoc result;
168         try
169         {
170             Criteria criteria = new Criteria();
171             if (categoryName != null) criteria.addEqualTo("categoryName", categoryName);
172             Query q = new QueryByCriteria(CategoryVO.class, criteria);
173             result = broker.getCollectionByQuery(q);
174         }
175         finally
176         {
177             if (broker != null) broker.close();
178         }
179         return result;
180     }
181 }
182
Popular Tags