001: package org.apache.ojb.ejb.odmg;
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.OJBRuntimeException;
023: import org.apache.ojb.broker.util.logging.Logger;
024: import org.apache.ojb.broker.util.logging.LoggerFactory;
025: import org.apache.ojb.ejb.ArticleVO;
026: import org.apache.ojb.ejb.CategoryVO;
027: import org.odmg.OQLQuery;
028:
029: /**
030: * Simple example bean for manage articles using the ODMG-api
031: * by subclassing {@link org.apache.ojb.ejb.odmg.ODMGBaseBeanImpl}
032: *
033: * @ejb:bean
034: * type="Stateless"
035: * name="ArticleManagerODMGBean"
036: * jndi-name="org.apache.ojb.ejb.odmg.ArticleManagerODMGBean"
037: * local-jndi-name="org.apache.ojb.ejb.odmg.ArticleManagerODMGBeanLocal"
038: * view-type="both"
039: * transaction-type="Container"
040: *
041: * @ejb:interface
042: * remote-class="org.apache.ojb.ejb.odmg.ArticleManagerODMGRemote"
043: * local-class="org.apache.ojb.ejb.odmg.ArticleManagerODMGLocal"
044: * extends="javax.ejb.EJBObject"
045: *
046: * @ejb:home
047: * remote-class="org.apache.ojb.ejb.odmg.ArticleManagerODMGHome"
048: * local-class="org.apache.ojb.ejb.odmg.ArticleManagerODMGLocalHome"
049: * extends="javax.ejb.EJBHome"
050: *
051: * @ejb:transaction
052: * type="Required"
053: *
054: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
055: * @version $Id: ArticleManagerODMGBean.java,v 1.4.2.1 2005/12/21 22:21:39 tomdz Exp $
056: */
057: public class ArticleManagerODMGBean extends ODMGBaseBeanImpl implements
058: SessionBean {
059: private Logger log = LoggerFactory
060: .getLogger(ArticleManagerODMGBean.class);
061:
062: public ArticleManagerODMGBean() {
063: }
064:
065: /**
066: * @ejb:interface-method
067: */
068: public ArticleVO storeArticle(ArticleVO article) {
069: return (ArticleVO) this .storeObject(article);
070: }
071:
072: /**
073: * @ejb:interface-method
074: */
075: public Collection storeArticles(Collection articles) {
076: return this .storeObjects(articles);
077: }
078:
079: /**
080: * Simulate a failure store.
081: *
082: * @ejb:interface-method
083: */
084: public ArticleVO failureStore(ArticleVO article) {
085: storeArticle(article);
086: // now we want to rollback
087: throw new EJBException("# failureStore method test #");
088: }
089:
090: /**
091: * @ejb:interface-method
092: */
093: public void deleteArticle(ArticleVO article) {
094: this .deleteObject(article);
095: }
096:
097: /**
098: * @ejb:interface-method
099: */
100: public void deleteArticles(Collection articles) {
101: this .deleteObjects(articles);
102: }
103:
104: /**
105: * @ejb:interface-method
106: */
107: public int countArticles() {
108: return this .getCount(ArticleVO.class);
109: }
110:
111: /**
112: * @ejb:interface-method
113: */
114: public Collection getAllArticles() {
115: return this .getAllObjects(ArticleVO.class);
116: }
117:
118: /**
119: * @ejb:interface-method
120: */
121: public Collection getArticles(String articleName) {
122: OQLQuery query = getImplementation().newOQLQuery();
123: try {
124: StringBuffer buf = new StringBuffer(
125: "select allObjects from "
126: + ArticleVO.class.getName());
127: // buf.append(" where articleId not null");
128: if (articleName != null)
129: buf.append(" where name = $1");
130: else
131: buf.append(" where name is null");
132: query.create(buf.toString());
133: if (articleName != null)
134: query.bind(articleName);
135: return (Collection) query.execute();
136: } catch (Exception e) {
137: log.error("OQLQuery failed", e);
138: throw new OJBRuntimeException("OQLQuery failed", e);
139: }
140: }
141:
142: /**
143: * @ejb:interface-method
144: */
145: public CategoryVO storeCategory(CategoryVO category) {
146: return (CategoryVO) this .storeObject(category);
147: }
148:
149: /**
150: * @ejb:interface-method
151: */
152: public void deleteCategory(CategoryVO category) {
153: this .deleteObject(category);
154: }
155:
156: /**
157: * @ejb:interface-method
158: */
159: public Collection getCategoryByName(String categoryName) {
160: OQLQuery query = getImplementation().newOQLQuery();
161: try {
162: StringBuffer buf = new StringBuffer(
163: "select allObjects from "
164: + CategoryVO.class.getName());
165: if (categoryName != null)
166: buf.append(" where categoryName = $1");
167: else
168: buf.append(" where categoryName is null");
169: query.create(buf.toString());
170: if (categoryName != null)
171: query.bind(categoryName);
172: return (Collection) query.execute();
173: } catch (Exception e) {
174: log.error("OQLQuery failed", e);
175: throw new OJBRuntimeException("OQLQuery failed", e);
176: }
177: }
178: }
|