001: // PersonEC2.java
002:
003: package org.objectweb.jonas.jtests.beans.relation.rcycle;
004:
005: import java.util.ArrayList;
006: import java.util.Collection;
007: import java.util.Iterator;
008:
009: import javax.ejb.FinderException;
010:
011: import org.objectweb.jonas.common.Log;
012: import org.objectweb.util.monolog.api.BasicLevel;
013: import org.objectweb.util.monolog.api.Logger;
014:
015: /**
016: * Class implementation of bean Person which is a CMP2 entity. <br>
017: * This bean Person have several bidirectional relation-ships with itself. <br>
018: * The CMP fields are: <br>
019: * - id <br>
020: * - name <br>
021: * - sex (male or female). <br>
022: * The CMR fields are: <br>
023: * - spouse, (One-One-bi relation), <br>
024: * - parents and children, (Many-Many-bi relation), <br>
025: * - guardian and is-guardian-of, (One-Many-bi relation). <br>
026: *
027: * @author Helene Joanin
028: */
029: public abstract class PersonEC2 implements javax.ejb.EntityBean {
030:
031: private static Logger logger = null;
032: private javax.ejb.EntityContext ejbContext;
033: private PersonHomeLocal plHome = null;
034:
035: // ---------------------------------------------------------------------
036: // Get and Set accessor methods of cmp and cmr fields
037: // ---------------------------------------------------------------------
038:
039: public abstract Integer getId();
040:
041: public abstract void setId(Integer id);
042:
043: public abstract String getName();
044:
045: public abstract void setName(String name);
046:
047: public abstract int getSex();
048:
049: public abstract void setSex(int sex);
050:
051: public abstract PersonLocal getSpouse();
052:
053: public abstract void setSpouse(PersonLocal spouse);
054:
055: public abstract Collection getParents();
056:
057: public abstract void setParents(Collection parents);
058:
059: public abstract Collection getChildren();
060:
061: public abstract void setChildren(Collection parents);
062:
063: public abstract PersonLocal getGuardian();
064:
065: public abstract void setGuardian(PersonLocal guardian);
066:
067: public abstract Collection getGuardianOf();
068:
069: public abstract void setGuardianOf(Collection guardianOf);
070:
071: // ---------------------------------------------------------------------
072: // Call back methods for those defined in the bean interfaces.
073: // ---------------------------------------------------------------------
074:
075: public java.lang.String ejbCreate(Integer id, String name, int sex)
076: throws javax.ejb.CreateException {
077: logger.log(BasicLevel.DEBUG, "id = " + id + ", name = " + name
078: + ", sex = " + sex);
079: // Init here the bean fields
080: setId(id);
081: setName(name);
082: setSex(sex);
083: return null;
084: }
085:
086: public void ejbPostCreate(Integer id, String name, int sex) {
087: logger.log(BasicLevel.DEBUG, "");
088: }
089:
090: public Integer retrieveSpouse() {
091: PersonLocal spouse = getSpouse();
092: if (spouse == null) {
093: logger.log(BasicLevel.DEBUG, "return null");
094: return null;
095: } else {
096: logger.log(BasicLevel.DEBUG, "return " + spouse.getId());
097: return spouse.getId();
098: }
099: }
100:
101: public void assignSpouse(Integer id) throws FinderException {
102: logger.log(BasicLevel.DEBUG, "param=" + id);
103: if (id != null) {
104: PersonLocal spouse = plHome.findByPrimaryKey(id);
105: setSpouse(spouse);
106: } else {
107: setSpouse(null);
108: }
109: }
110:
111: public Collection retrieveParents() {
112: Collection ps = getParents();
113: ArrayList result;
114: if (ps.size() <= 0) {
115: result = new ArrayList();
116: } else {
117: result = new ArrayList(ps.size());
118: }
119: for (Iterator it = ps.iterator(); it.hasNext();) {
120: result.add(((PersonLocal) it.next()).getPrimaryKey());
121: }
122: logger.log(BasicLevel.DEBUG, "return=" + result);
123: return result;
124: }
125:
126: public void assignParents(Collection c) throws FinderException {
127: logger.log(BasicLevel.DEBUG, "param=" + c);
128: ArrayList al;
129: if (c == null) {
130: al = new ArrayList();
131: } else {
132: if (c.size() <= 0) {
133: al = new ArrayList();
134: } else {
135: al = new ArrayList(c.size());
136: for (Iterator it = c.iterator(); it.hasNext();) {
137: al
138: .add(plHome.findByPrimaryKey((Integer) it
139: .next()));
140: }
141: }
142: }
143: setParents(al);
144: }
145:
146: public void addInParents(Integer id) throws FinderException {
147: logger.log(BasicLevel.DEBUG, "param=" + id);
148: getParents().add(plHome.findByPrimaryKey(id));
149: }
150:
151: public Collection retrieveChildren() {
152: Collection ps = getChildren();
153: ArrayList result;
154: if (ps.size() <= 0) {
155: result = new ArrayList();
156: } else {
157: result = new ArrayList(ps.size());
158: }
159: for (Iterator it = ps.iterator(); it.hasNext();) {
160: result.add(((PersonLocal) it.next()).getPrimaryKey());
161: }
162: logger.log(BasicLevel.DEBUG, "return=" + result);
163: return result;
164: }
165:
166: public Collection retrieveChildrenNames() {
167: Collection ps = getChildren();
168: ArrayList result;
169: if (ps.size() <= 0) {
170: result = new ArrayList();
171: } else {
172: result = new ArrayList(ps.size());
173: }
174: for (Iterator it = ps.iterator(); it.hasNext();) {
175: result.add(((PersonLocal) it.next()).getName());
176: }
177: logger.log(BasicLevel.DEBUG, "return=" + result);
178: return result;
179: }
180:
181: public void assignChildren(Collection c) throws FinderException {
182: logger.log(BasicLevel.DEBUG, "param=" + c);
183: ArrayList al;
184: if (c == null) {
185: al = new ArrayList();
186: } else {
187: if (c.size() <= 0) {
188: al = new ArrayList();
189: } else {
190: al = new ArrayList(c.size());
191: for (Iterator it = c.iterator(); it.hasNext();) {
192: al
193: .add(plHome.findByPrimaryKey((Integer) it
194: .next()));
195: }
196: }
197: }
198: setChildren(al);
199: }
200:
201: public void addInChildren(Integer id) throws FinderException {
202: logger.log(BasicLevel.DEBUG, "param=" + id);
203: getChildren().add(plHome.findByPrimaryKey(id));
204: }
205:
206: public Integer retrieveGuardian() {
207: PersonLocal guardian = getGuardian();
208: if (guardian == null) {
209: logger.log(BasicLevel.DEBUG, "return null");
210: return null;
211: } else {
212: logger.log(BasicLevel.DEBUG, "return " + guardian.getId());
213: return guardian.getId();
214: }
215: }
216:
217: public void assignGuardian(Integer id) throws FinderException {
218: logger.log(BasicLevel.DEBUG, "param=" + id);
219: if (id != null) {
220: PersonLocal guardian = plHome.findByPrimaryKey(id);
221: setGuardian(guardian);
222: } else {
223: setGuardian(null);
224: }
225: }
226:
227: public Collection retrieveGuardianOf() {
228: Collection ps = getGuardianOf();
229: ArrayList result;
230: if (ps.size() <= 0) {
231: result = new ArrayList();
232: } else {
233: result = new ArrayList(ps.size());
234: }
235: for (Iterator it = ps.iterator(); it.hasNext();) {
236: result.add(((PersonLocal) it.next()).getPrimaryKey());
237: }
238: logger.log(BasicLevel.DEBUG, "return=" + result);
239: return result;
240: }
241:
242: public void assignInGuardianOf(Collection c) throws FinderException {
243: logger.log(BasicLevel.DEBUG, "param=" + c);
244: ArrayList al;
245: if (c == null) {
246: al = new ArrayList();
247: } else {
248: if (c.size() <= 0) {
249: al = new ArrayList();
250: } else {
251: al = new ArrayList(c.size());
252: for (Iterator it = c.iterator(); it.hasNext();) {
253: al
254: .add(plHome.findByPrimaryKey((Integer) it
255: .next()));
256: }
257: }
258: }
259: setGuardianOf(al);
260: }
261:
262: public void addInGuardianOf(Integer id) throws FinderException {
263: logger.log(BasicLevel.DEBUG, "param=" + id);
264: getGuardianOf().add(plHome.findByPrimaryKey(id));
265: }
266:
267: public boolean testCmrNull() throws FinderException {
268: PersonLocal g = plHome.findByPrimaryKey(new Integer(3));
269: PersonLocal sg = g.getSpouse();
270: if (sg == null) {
271: return true;
272: } else {
273: return false;
274: }
275: }
276:
277: // ---------------------------------------------------------------------
278: // Standard call back methods of those defined in javax.ejb.EntityBean
279: // ---------------------------------------------------------------------
280:
281: public void setEntityContext(javax.ejb.EntityContext ctx) {
282: // init the logger
283: if (logger == null) {
284: logger = Log.getLogger("org.objectweb.jonas_tests");
285: }
286: logger.log(BasicLevel.DEBUG, "");
287: // save the given EntityContext
288: ejbContext = ctx;
289: // get the PersonHomeLocal (local home of the current bean
290: plHome = (PersonHomeLocal) ejbContext.getEJBLocalHome();
291: }
292:
293: public void unsetEntityContext() {
294: logger.log(BasicLevel.DEBUG, "");
295: ejbContext = null;
296: }
297:
298: public void ejbRemove() throws javax.ejb.RemoveException {
299: logger.log(BasicLevel.DEBUG, "");
300: }
301:
302: public void ejbLoad() {
303: logger.log(BasicLevel.DEBUG, "");
304: }
305:
306: public void ejbStore() {
307: logger.log(BasicLevel.DEBUG, "");
308: }
309:
310: public void ejbPassivate() {
311: logger.log(BasicLevel.DEBUG, "");
312: }
313:
314: public void ejbActivate() {
315: logger.log(BasicLevel.DEBUG, "");
316: }
317:
318: }
|