KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > relation > cascade > CarBean


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: CarBean.java,v 1.3 2005/02/07 17:48:55 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.relation.cascade;
27
28 import java.rmi.RemoteException JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Vector JavaDoc;
31 import javax.ejb.CreateException JavaDoc;
32 import javax.ejb.EJBException JavaDoc;
33 import javax.ejb.EntityBean JavaDoc;
34 import javax.ejb.EntityContext JavaDoc;
35 import javax.ejb.RemoveException JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38
39 import org.objectweb.jonas.common.Log;
40 import org.objectweb.util.monolog.api.BasicLevel;
41 import org.objectweb.util.monolog.api.Logger;
42
43 /**
44  * Implementation for bean Car
45  * @author Ph Durieux
46  */

47 public abstract class CarBean implements EntityBean JavaDoc {
48
49     static protected Logger logger = null;
50     protected EntityContext JavaDoc ejbContext = null;
51     protected InsuranceHL insuranceHL = null;
52     protected InvoiceHL invoiceHL = null;
53
54     public Object JavaDoc ejbCreate(String JavaDoc number, byte type, String JavaDoc name) throws CreateException JavaDoc {
55         logger.log(BasicLevel.DEBUG, "");
56         setNumber(number);
57         setType(type);
58         setName(name);
59         return null;
60     }
61
62     public void ejbPostCreate(String JavaDoc number, byte type, String JavaDoc name) throws CreateException JavaDoc {
63         logger.log(BasicLevel.DEBUG, "");
64         InsuranceL ins = insuranceHL.create("000" + number);
65         setInsurance(ins);
66     }
67
68     // persistent fields
69
public abstract Integer JavaDoc getId();
70     public abstract void setId(Integer JavaDoc id);
71     public abstract String JavaDoc getNumber();
72     public abstract void setNumber(String JavaDoc number);
73     public abstract byte getType();
74     public abstract void setType(byte type);
75     public abstract String JavaDoc getName();
76     public abstract void setName(String JavaDoc name);
77
78     // persistent relationships
79
public abstract InsuranceL getInsurance();
80     public abstract void setInsurance(InsuranceL i);
81     public abstract CustomerL getCustomer();
82     public abstract void setCustomer(CustomerL c);
83     public abstract java.util.Collection JavaDoc getInvoices();
84     public abstract void setInvoices(java.util.Collection JavaDoc invoices);
85
86
87     public void addInvoice(String JavaDoc number) throws NamingException JavaDoc, CreateException JavaDoc {
88         logger.log(BasicLevel.DEBUG, "");
89         InvoiceL invoice = invoiceHL.create(number);
90         Collection JavaDoc invoices = getInvoices();
91         invoices.add(invoice);
92     }
93
94     public void setEntityContext(EntityContext JavaDoc ec) {
95         if (logger == null)
96             logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
97         logger.log(BasicLevel.DEBUG, "");
98         ejbContext = ec;
99         try {
100             InitialContext JavaDoc cntx = new InitialContext JavaDoc();
101             insuranceHL = (InsuranceHL) cntx.lookup("java:comp/env/ejb/InsuranceHomeLocal");
102             invoiceHL = (InvoiceHL) cntx.lookup("java:comp/env/ejb/InvoiceHomeLocal");
103         } catch (Exception JavaDoc e) {
104             throw new javax.ejb.EJBException JavaDoc(e);
105         }
106     }
107
108     public void unsetEntityContext() {
109         logger.log(BasicLevel.DEBUG, "");
110         ejbContext = null;
111     }
112
113     public void ejbLoad() {
114         logger.log(BasicLevel.DEBUG, "");
115         checkCustomerAccess();
116     }
117
118     public void ejbStore() {
119         logger.log(BasicLevel.DEBUG, "");
120         checkCustomerAccess();
121     }
122
123     public void ejbActivate() {
124         logger.log(BasicLevel.DEBUG, "");
125         // This cannot be done: See EJB specs
126
// A bean must not attempt to access its state here.
127
//checkCustomerAccess();
128
}
129
130     public void ejbPassivate() {
131         logger.log(BasicLevel.DEBUG, "");
132         // This cannot be done: See EJB specs
133
// A bean must not attempt to access its state here.
134
//checkCustomerAccess();
135
}
136
137     /**
138      * this instance is being removed.
139      * we must be able to access bean fields here, including the CMRs
140      */

141     public void ejbRemove() throws javax.ejb.RemoveException JavaDoc {
142         logger.log(BasicLevel.DEBUG, "");
143         InsuranceL myins = getInsurance();
144         if (myins == null) {
145             logger.log(BasicLevel.ERROR, "CMR field Insurance is null");
146             throw new RemoveException JavaDoc("Cannot access CMR field Insurance inside ejbRemove");
147         }
148         String JavaDoc insnumber = myins.getNumber();
149         String JavaDoc expect = "000" + getNumber();
150         if (! expect.equals(insnumber)) {
151             throw new RemoveException JavaDoc("Bad insurance number:" + insnumber);
152         }
153         checkCustomerAccess();
154      }
155
156     protected void checkCustomerAccess() {
157         logger.log(BasicLevel.DEBUG, "");
158         CustomerL mycust = getCustomer();
159         if (mycust == null) {
160             logger.log(BasicLevel.ERROR, "CMR field Customer is null");
161             throw new EJBException JavaDoc("CMR field Customer is null");
162         }
163         Name n = mycust.getName();
164         if (n == null || n.getLastName().length() == 0) {
165             logger.log(BasicLevel.ERROR, "Cannot get customer name");
166             throw new EJBException JavaDoc("Cannot get customer name");
167         }
168     }
169 }
170
Popular Tags