KickJava   Java API By Example, From Geeks To Geeks.

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


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: AEC2.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.jonas.common.Log;
29 import org.objectweb.util.monolog.api.BasicLevel;
30 import org.objectweb.util.monolog.api.Logger;
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 /**
48  * @author J.Camilleri
49  */

50 public abstract class AEC2 implements javax.ejb.EntityBean JavaDoc {
51
52     private BHomeLocal bhl = null;
53
54     public void m1(){
55     }
56     public PK getId() {
57         return (PK) ejbContext.getPrimaryKey();
58     }
59
60     public void assignB(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(bhl.findByPrimaryKey((PK) it.next()));
71         }
72     }
73     setB(al);
74     }
75     public void assignBInNewTx(Collection JavaDoc c) throws FinderException JavaDoc {
76         assignB(c);
77     }
78
79     public Collection JavaDoc retrieveB() {
80         Collection JavaDoc bs = getB();
81         ArrayList JavaDoc result ;
82         if (bs.size()==-1)
83         result = new ArrayList JavaDoc();
84         else result = new ArrayList JavaDoc(bs.size());
85         
86         for(Iterator JavaDoc it = bs.iterator(); it.hasNext();)
87             result.add(((BLocal) it.next()).getPrimaryKey());
88         return result;
89     }
90
91     public Collection JavaDoc retrieveBInNewTx() {
92         return retrieveB();
93     }
94
95     public void addInB(PK pkb) throws FinderException JavaDoc {
96         getB().add(bhl.findByPrimaryKey(pkb));
97     }
98     public void addInBInNewTx(PK pkb) throws FinderException JavaDoc {
99         addInB(pkb);
100     }
101
102     public void addAllInB(Collection JavaDoc pkbs) throws FinderException JavaDoc {
103         ArrayList JavaDoc al = new ArrayList JavaDoc();
104         for (Iterator JavaDoc it = pkbs.iterator(); it.hasNext();)
105             al.add(bhl.findByPrimaryKey((PK) it.next()));
106         getB().addAll(al);
107     }
108     public void addAllInBInNewTx(Collection JavaDoc pkbs) throws FinderException JavaDoc {
109         addAllInB(pkbs);
110     }
111
112     public void removeFromB(PK pkb) throws FinderException JavaDoc {
113         getB().remove(bhl.findByPrimaryKey(pkb));
114     }
115     public void removeFromBInNewTx(PK pkb) throws FinderException JavaDoc {
116         removeFromB(pkb);
117     }
118
119     public void clearB() {
120         getB().clear();
121     }
122
123     public void clearBInNewTx() {
124         clearB();
125     }
126
127     public boolean containAllInB(Collection JavaDoc pkbs) throws FinderException JavaDoc {
128         ArrayList JavaDoc al = new ArrayList JavaDoc(pkbs.size());
129         for(Iterator JavaDoc it = pkbs.iterator(); it.hasNext();)
130             al.add(bhl.findByPrimaryKey((PK) it.next()));
131         return getB().containsAll(al);
132     }
133
134     /**
135      * It returns true the multivalued relation contains the bean B defined
136      * by the primary key specified by the parameter.
137      * This method has the transactional attribut TX_SUPPORTS.
138      * @throw a FinderException if the primary key does not match to a bean.
139      */

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

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

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

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

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

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

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

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

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

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

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