KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > ejb > odmg > ArticleManagerODMGBean


1 package org.apache.ojb.ejb.odmg;
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.OJBRuntimeException;
23 import org.apache.ojb.broker.util.logging.Logger;
24 import org.apache.ojb.broker.util.logging.LoggerFactory;
25 import org.apache.ojb.ejb.ArticleVO;
26 import org.apache.ojb.ejb.CategoryVO;
27 import org.odmg.OQLQuery;
28
29 /**
30  * Simple example bean for manage articles using the ODMG-api
31  * by subclassing {@link org.apache.ojb.ejb.odmg.ODMGBaseBeanImpl}
32  *
33  * @ejb:bean
34  * type="Stateless"
35  * name="ArticleManagerODMGBean"
36  * jndi-name="org.apache.ojb.ejb.odmg.ArticleManagerODMGBean"
37  * local-jndi-name="org.apache.ojb.ejb.odmg.ArticleManagerODMGBeanLocal"
38  * view-type="both"
39  * transaction-type="Container"
40  *
41  * @ejb:interface
42  * remote-class="org.apache.ojb.ejb.odmg.ArticleManagerODMGRemote"
43  * local-class="org.apache.ojb.ejb.odmg.ArticleManagerODMGLocal"
44  * extends="javax.ejb.EJBObject"
45  *
46  * @ejb:home
47  * remote-class="org.apache.ojb.ejb.odmg.ArticleManagerODMGHome"
48  * local-class="org.apache.ojb.ejb.odmg.ArticleManagerODMGLocalHome"
49  * extends="javax.ejb.EJBHome"
50  *
51  * @ejb:transaction
52  * type="Required"
53  *
54  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
55  * @version $Id: ArticleManagerODMGBean.java,v 1.4.2.1 2005/12/21 22:21:39 tomdz Exp $
56  */

57 public class ArticleManagerODMGBean extends ODMGBaseBeanImpl implements SessionBean JavaDoc
58 {
59     private Logger log = LoggerFactory.getLogger(ArticleManagerODMGBean.class);
60
61     public ArticleManagerODMGBean()
62     {
63     }
64
65     /**
66      * @ejb:interface-method
67      */

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

76     public Collection JavaDoc storeArticles(Collection JavaDoc articles)
77     {
78         return this.storeObjects(articles);
79     }
80
81     /**
82      * Simulate a failure store.
83      *
84      * @ejb:interface-method
85      */

86     public ArticleVO failureStore(ArticleVO article)
87     {
88         storeArticle(article);
89         // now we want to rollback
90
throw new EJBException JavaDoc("# failureStore method test #");
91     }
92
93     /**
94      * @ejb:interface-method
95      */

96     public void deleteArticle(ArticleVO article)
97     {
98         this.deleteObject(article);
99     }
100
101     /**
102      * @ejb:interface-method
103      */

104     public void deleteArticles(Collection JavaDoc articles)
105     {
106         this.deleteObjects(articles);
107     }
108
109     /**
110      * @ejb:interface-method
111      */

112     public int countArticles()
113     {
114         return this.getCount(ArticleVO.class);
115     }
116
117     /**
118      * @ejb:interface-method
119      */

120     public Collection JavaDoc getAllArticles()
121     {
122         return this.getAllObjects(ArticleVO.class);
123     }
124
125     /**
126      * @ejb:interface-method
127      */

128     public Collection JavaDoc getArticles(String JavaDoc articleName)
129     {
130         OQLQuery query = getImplementation().newOQLQuery();
131         try
132         {
133             StringBuffer JavaDoc buf = new StringBuffer JavaDoc("select allObjects from " + ArticleVO.class.getName());
134             // buf.append(" where articleId not null");
135
if (articleName != null)
136                 buf.append(" where name = $1");
137             else
138                 buf.append(" where name is null");
139             query.create(buf.toString());
140             if (articleName != null) query.bind(articleName);
141             return (Collection JavaDoc) query.execute();
142         }
143         catch (Exception JavaDoc e)
144         {
145             log.error("OQLQuery failed", e);
146             throw new OJBRuntimeException("OQLQuery failed", e);
147         }
148     }
149
150     /**
151      * @ejb:interface-method
152      */

153     public CategoryVO storeCategory(CategoryVO category)
154     {
155         return (CategoryVO) this.storeObject(category);
156     }
157
158     /**
159      * @ejb:interface-method
160      */

161     public void deleteCategory(CategoryVO category)
162     {
163         this.deleteObject(category);
164     }
165
166     /**
167      * @ejb:interface-method
168      */

169     public Collection JavaDoc getCategoryByName(String JavaDoc categoryName)
170     {
171         OQLQuery query = getImplementation().newOQLQuery();
172         try
173         {
174             StringBuffer JavaDoc buf = new StringBuffer JavaDoc("select allObjects from " + CategoryVO.class.getName());
175             if (categoryName != null)
176                 buf.append(" where categoryName = $1");
177             else
178                 buf.append(" where categoryName is null");
179             query.create(buf.toString());
180             if (categoryName != null) query.bind(categoryName);
181             return (Collection JavaDoc) query.execute();
182         }
183         catch (Exception JavaDoc e)
184         {
185             log.error("OQLQuery failed", e);
186             throw new OJBRuntimeException("OQLQuery failed", e);
187         }
188     }
189 }
190
Popular Tags