001: package org.mockejb.test.entity;
002:
003: import java.util.*;
004:
005: import javax.ejb.*;
006:
007: /**
008: * Bare-bones example of a BMP entity bean.
009: * Some methods defined on the Person interface are not implemented.
010: * ejbLoad and ejbStore are not implemented either, since they
011: * are not relevant for MockEJB testing - MockEJB does not
012: * call them directly.
013: *
014: * @author Alexander Ananiev
015: */
016: public class PersonBMPBean implements EntityBean {
017:
018: private EntityContext context = null;
019:
020: private long id;
021:
022: private String firstName;
023: private String lastName;
024:
025: // id sequence simulation
026: private static long idSequence = 0;
027:
028: public synchronized static long generateId() {
029: return idSequence++;
030: }
031:
032: // ** Create methods
033:
034: public long ejbCreate(String firstName, String lastName) {
035: setFirstName(firstName);
036: setLastName(lastName);
037:
038: // generate fake id
039: id = generateId();
040: return id;
041: }
042:
043: public void ejbPostCreate(String firstName, String lastName) {
044: }
045:
046: /**
047: * @return firstName.
048: */
049: public String getFirstName() {
050: return firstName;
051: }
052:
053: /**
054: * @param firstName The firstName to set.
055: */
056: public void setFirstName(String firstName) {
057: this .firstName = firstName;
058: }
059:
060: /**
061: * @return lastName.
062: */
063: public String getLastName() {
064: return lastName;
065: }
066:
067: /**
068: * @param lastName The lastName to set.
069: */
070: public void setLastName(String lastName) {
071: this .lastName = lastName;
072: }
073:
074: // Fake finders implementation
075:
076: /**
077: * This finder always returns the same PK value (1).
078: */
079: public long ejbFindByName(String firstName, String lastName) {
080:
081: return 1;
082: }
083:
084: /**
085: * This finder always returns the collection with only one PK value (1).
086: */
087: public Collection ejbFindByFirstName(String firstName) {
088: Collection pks = new ArrayList();
089: pks.add(new Long(1));
090: return pks;
091: }
092:
093: // Lifecycle Methods
094: public void setEntityContext(EntityContext c) {
095: context = c;
096: }
097:
098: public void unsetEntityContext() {
099: context = null;
100: }
101:
102: public void ejbRemove() throws RemoveException {
103: }
104:
105: public void ejbActivate() {
106: }
107:
108: public void ejbPassivate() {
109: }
110:
111: /**
112: * MockEJB never calls this method.
113: * @see javax.ejb.EntityBean#ejbStore()
114: */
115: public void ejbStore() {
116: }
117:
118: /**
119: * MockEJB calls this method after every finder.
120: * This is fake implementation that simply sets the PK.
121: * The real entity loads itself from the database
122: *
123: * @see javax.ejb.EntityBean#ejbLoad()
124: */
125: public void ejbLoad() {
126:
127: System.out.println("Inside ejbLoad");
128: id = ((Long) context.getPrimaryKey()).longValue();
129:
130: }
131:
132: /**
133: * @return id.
134: */
135: public long getId() {
136: return id;
137: }
138:
139: /**
140: * @param id The id to set.
141: */
142: public void setId(long id) {
143: this.id = id;
144: }
145: }
|