KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > relation > s1pkcomp > BEC2


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: BEC2.java,v 1.3 2004/12/17 15:08:35 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.relation.s1pkcomp;
27
28 import org.objectweb.util.monolog.api.Logger;
29 import org.objectweb.util.monolog.api.BasicLevel;
30 import org.objectweb.jonas.common.Log;
31
32 import javax.ejb.CreateException JavaDoc;
33 import javax.ejb.DuplicateKeyException JavaDoc;
34 import javax.ejb.EntityContext JavaDoc;
35 import javax.ejb.RemoveException JavaDoc;
36 import javax.ejb.FinderException JavaDoc;
37 import javax.ejb.EJBException JavaDoc;
38 import javax.naming.Context JavaDoc;
39 import javax.naming.InitialContext JavaDoc;
40 import javax.naming.NamingException JavaDoc;
41 import javax.rmi.PortableRemoteObject JavaDoc;
42
43 import java.util.Collection JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Iterator JavaDoc;
46 /**
47  * @author J.Camilleri
48  */

49 public abstract class BEC2 implements javax.ejb.EntityBean JavaDoc {
50
51     private AHomeLocal ahl = null;
52
53     public void m1(){
54     }
55
56     public PK getId() {
57         return (PK) ejbContext.getPrimaryKey();
58     }
59
60     public void assignA(Collection JavaDoc c) throws FinderException JavaDoc {
61     ArrayList JavaDoc al;
62     if (c==null)
63             al = new ArrayList JavaDoc();
64     else {
65             if (c.size()==-1)
66         al = new ArrayList JavaDoc();
67         else {
68                al = new ArrayList JavaDoc(c.size());
69                for(Iterator JavaDoc it = c.iterator(); it.hasNext();) {
70            al.add(ahl.findByPrimaryKey((PK) it.next()));
71            }
72         }
73     }
74     setA(al);
75     }
76
77     public void assignAInNewTx(Collection JavaDoc c) throws FinderException JavaDoc {
78         assignA(c);
79     }
80
81     public Collection JavaDoc retrieveA() {
82        Collection JavaDoc bs = getA();
83         ArrayList JavaDoc result ;
84         if (bs.size()==-1)
85         result = new ArrayList JavaDoc();
86         else result = new ArrayList JavaDoc(bs.size());
87         
88         for(Iterator JavaDoc it = bs.iterator(); it.hasNext();)
89             result.add(((ALocal) it.next()).getPrimaryKey());
90         return result;
91     }
92
93     public Collection JavaDoc retrieveAInNewTx() {
94         return retrieveA();
95     }
96
97     public void addInA(PK pkb) throws FinderException JavaDoc {
98         getA().add(ahl.findByPrimaryKey(pkb));
99     }
100     public void addInAInNewTx(PK pkb) throws FinderException JavaDoc {
101         addInA(pkb);
102     }
103
104     public void addAllInA(Collection JavaDoc pkbs) throws FinderException JavaDoc {
105         ArrayList JavaDoc al = new ArrayList JavaDoc();
106         for (Iterator JavaDoc it = pkbs.iterator(); it.hasNext();)
107             al.add(ahl.findByPrimaryKey((PK) it.next()));
108         getA().addAll(al);
109     }
110     public void addAllInAInNewTx(Collection JavaDoc pkbs) throws FinderException JavaDoc {
111         addAllInA(pkbs);
112     }
113
114     public void removeFromA(PK pkb) throws FinderException JavaDoc {
115         getA().remove(ahl.findByPrimaryKey(pkb));
116     }
117     public void removeFromAInNewTx(PK pkb) throws FinderException JavaDoc {
118         removeFromA(pkb);
119     }
120
121     public void clearA() {
122         getA().clear();
123     }
124
125     public void clearAInNewTx() {
126         clearA();
127     }
128
129     public boolean containAllInA(Collection JavaDoc pkbs) throws FinderException JavaDoc {
130         ArrayList JavaDoc al = new ArrayList JavaDoc(pkbs.size());
131         for(Iterator JavaDoc it = pkbs.iterator(); it.hasNext();)
132             al.add(ahl.findByPrimaryKey((PK) it.next()));
133         return getA().containsAll(al);
134     }
135
136     /**
137      * It returns true the multivalued relation contains the bean A defined
138      * by the primary key specified by the parameter.
139      * This method has the transactional attribut TX_SUPPORTS.
140      * @throw a FinderException if the primary key does not match to a bean.
141      */

142     public boolean containInA(PK pkb) throws FinderException JavaDoc {
143         return (getA().contains(ahl.findByPrimaryKey(pkb)));
144     }
145
146     // ------------------------------------------------------------------
147
// Get and Set accessor methods of the bean's abstract schema
148
// ------------------------------------------------------------------
149
public abstract String JavaDoc getId1();
150
151     public abstract int getId2();
152
153     public abstract void setId1(String JavaDoc id);
154
155     public abstract void setId2(int id);
156
157     public abstract Collection JavaDoc getA();
158
159     public abstract void setA(Collection JavaDoc bl);
160
161     // ------------------------------------------------------------------
162
// EntityBean implementation
163
// ------------------------------------------------------------------
164

165     static protected Logger logger = null;
166     EntityContext JavaDoc ejbContext;
167
168     /**
169      * The Entity bean can define 0 or more ejbCreate methods.
170      *
171      * @throws CreateException Failure to create an entity EJB object.
172      * @throws DuplicateKeyException An object with the same key already exists.
173      */

174     public String JavaDoc ejbCreate(String JavaDoc idb1, int idb2) throws CreateException JavaDoc, DuplicateKeyException JavaDoc {
175         logger.log(BasicLevel.DEBUG, "");
176
177         // Init here the bean fields
178
setId1(idb1);
179         setId2(idb2);
180         // In CMP, should return null.
181
return null;
182     }
183
184     /**
185      * Set the associated entity context. The container invokes this method
186      * on an instance after the instance has been created.
187      * This method is called in an unspecified transaction context.
188      *
189      * @param ctx - An EntityContext interface for the instance. The instance
190      * should store the reference to the context in an instance variable.
191      * @throws EJBException Thrown by the method to indicate a failure caused by a
192      * system-level error.
193      */

194     public void setEntityContext(EntityContext JavaDoc ctx) {
195         if (logger == null)
196             logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
197         logger.log(BasicLevel.DEBUG, "");
198         ejbContext = ctx;
199         try {
200             Context JavaDoc ictx = new InitialContext JavaDoc();
201             ahl = (AHomeLocal) ictx.lookup("java:comp/env/ejb/a");
202         } catch (NamingException JavaDoc e) {
203             throw new EJBException JavaDoc("Impossible to fetch the ", e);
204         }
205     }
206
207     /**
208      * Unset the associated entity context. The container calls this method
209      * before removing the instance.
210      * This is the last method that the container invokes on the instance.
211      * The Java garbage collector will eventually invoke the finalize() method
212      * on the instance.
213      * This method is called in an unspecified transaction context.
214      *
215      * @throws EJBException Thrown by the method to indicate a failure caused by a
216      * system-level error.
217      */

218     public void unsetEntityContext() {
219         logger.log(BasicLevel.DEBUG, "");
220         ejbContext = null;
221     }
222
223     /**
224      * A container invokes this method before it removes the EJB object
225      * that is currently associated with the instance. This method is
226      * invoked when a client invokes a remove operation on the enterprise Bean's
227      * home interface or the EJB object's remote interface. This method
228      * transitions the instance from the ready state to the pool of available
229      * instances.
230      *
231      * This method is called in the transaction context of the remove operation.
232      * @throws RemoveException The enterprise Bean does not allow destruction of the object.
233      * @throws EJBException - Thrown by the method to indicate a failure caused by a system-level
234      * error.
235      */

236     public void ejbRemove() throws RemoveException JavaDoc {
237         logger.log(BasicLevel.DEBUG, "");
238     }
239
240     /**
241      * A container invokes this method to instruct the instance to synchronize
242      * its state by loading it state from the underlying database.
243      * This method always executes in the proper transaction context.
244      *
245      * @throws EJBException Thrown by the method to indicate a failure caused by
246      * a system-level error.
247      */

248     public void ejbLoad() {
249         logger.log(BasicLevel.DEBUG, "");
250     }
251
252     /**
253      * A container invokes this method to instruct the instance to synchronize
254      * its state by storing it to the underlying database.
255      * This method always executes in the proper transaction context.
256      *
257      * @throws EJBException Thrown by the method to indicate a failure caused by
258      * a system-level error.
259      */

260     public void ejbStore() {
261         logger.log(BasicLevel.DEBUG, "");
262     }
263
264     /**
265      * There must be an ejbPostCreate par ejbCreate method
266      *
267      * @throws CreateException Failure to create an entity EJB object.
268      */

269     public void ejbPostCreate(String JavaDoc idb1, int idb2) throws CreateException JavaDoc {
270         logger.log(BasicLevel.DEBUG, "idb1=" + idb1 + " / idb2=" + idb2);
271     }
272
273     /**
274      * A container invokes this method on an instance before the instance
275      * becomes disassociated with a specific EJB object.
276      */

277     public void ejbPassivate() {
278         logger.log(BasicLevel.DEBUG, "");
279     }
280
281     /**
282      * A container invokes this method when the instance is taken out of
283      * the pool of available instances to become associated with a specific
284      * EJB object.
285      */

286     public void ejbActivate() {
287         logger.log(BasicLevel.DEBUG, "");
288     }
289
290 }
291
292
Popular Tags