RecordEJB.ejb
001 package com.bea.medrec.entities;
002 
003 import com.bea.medrec.utils.JNDINames;
004 import com.bea.medrec.utils.MedRecLog4jFactory;
005 import com.bea.medrec.utils.MedRecUtils;
006 import com.bea.medrec.utils.ServiceLocator;
007 import com.bea.medrec.value.Physician;
008 import com.bea.medrec.value.Prescription;
009 import com.bea.medrec.value.Record;
010 import com.bea.medrec.value.VitalSigns;
011 import java.sql.Date;
012 import java.util.*;
013 import javax.ejb.CreateException;
014 import javax.ejb.EJBException;
015 import javax.ejb.EntityContext;
016 import javax.ejb.FinderException;
017 import javax.naming.NamingException;
018 import org.apache.log4j.Logger;
019 import weblogic.ejb.GenericEntityBean;
020 import weblogic.ejbgen.*;
021 
022 /**
023  <p>RecordEJB is an Container Managed EntityBean that
024  * manipulates record persisted data.</p>
025  *
026  @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
027  */
028 @AutomaticKeyGeneration(name = "RECORD_SEQ",
029                         type = AutomaticKeyGeneration.AutomaticKeyGenerationType.SEQUENCE_TABLE,
030                         cacheSize = "10")
031 @CreateDefaultDbmsTables(value = "Disabled")
032 @EjbLocalRefs({
033     @EjbLocalRef(name = "ejb/local/physician",
034                  home = "com.bea.medrec.entities.PhysicianLocalHome",
035                  local = "com.bea.medrec.entities.PhysicianLocal",
036                  type = Constants.RefType.ENTITY,
037                  link = "PhysicianEJB"),
038     @EjbLocalRef(name = "ejb/local/prescription",
039                  home = "com.bea.medrec.entities.PrescriptionLocalHome",
040                  local = "com.bea.medrec.entities.PrescriptionLocal",
041                  type = Constants.RefType.ENTITY,
042                  link = "PrescriptionEJB"),
043     @EjbLocalRef(name = "ejb/local/vitalsigns",
044                  home = "com.bea.medrec.entities.VitalSignsLocalHome",
045                  local = "com.bea.medrec.entities.VitalSignsLocal",
046                  type = Constants.RefType.ENTITY,
047                  link = "VitalSignsEJB")
048 })
049 @Entity(maxBeansInCache = "1000",
050         dataSourceName = "jdbc/MedRecTxDataSource",
051         transTimeoutSeconds = "0",
052         ejbName = "RecordEJB",
053         reentrant = Constants.Bool.FALSE,
054         concurrencyStrategy = Constants.ConcurrencyStrategy.DATABASE,
055         delayDatabaseInsertUntil = Entity.DelayDatabaseInsertUntil.EJB_POST_CREATE,
056         tableName = "record",
057         readTimeoutSeconds = "600",
058         primKeyClass = "java.lang.Integer",
059         defaultTransaction = Constants.TransactionAttribute.MANDATORY,
060         abstractSchemaName = "RecordEJB",
061         databaseType = Entity.DatabaseType.POINTBASE)
062 @FileGeneration(localClass = Constants.Bool.TRUE,
063                 localHome = Constants.Bool.TRUE,
064                 valueClass = Constants.Bool.FALSE)
065 @Finders({
066     @Finder(signature = "com.bea.medrec.entities.RecordLocal findByRecordId(java.lang.Integer n0)",
067             ejbQl = "not used in favor of weblogic-ejb-ql for relationship caching",
068             weblogicEjbQl = "SELECT OBJECT(o) FROM RecordEJB AS o WHERE o.id = ?1",
069             cachingName = "recordCache"),
070     @Finder(signature = "java.util.Collection findByPatientId(java.lang.Integer n0)",
071             ejbQl = "not used in favor of weblogic-ejb-ql for relationship caching",
072             weblogicEjbQl = "SELECT OBJECT(o) FROM RecordEJB AS o WHERE o.patientId = ?1 ORDERBY o.date",
073             groupName = "liteWeight",
074             cachingName =  "recordSummaryCache")
075 })
076 @Relations({
077     @Relation(cascadeDelete = Constants.Bool.FALSE,
078               cmrField = "physician",
079               name = "Record-Physician",
080               roleName = "Record-Has-Physician",
081               multiplicity = Relation.Multiplicity.MANY,
082               targetEjb = "PhysicianEJB",
083               fkColumn = "phys_id"),
084     @Relation(cascadeDelete = Constants.Bool.FALSE,
085               cmrField = "prescriptions",
086               name = "Record-Prescriptions",
087               roleName = "Record-Has-Prescriptions",
088               multiplicity = Relation.Multiplicity.ONE,
089               targetEjb = "PrescriptionEJB"),
090     @Relation(cascadeDelete = Constants.Bool.TRUE,
091               cmrField = "vitalSigns",
092               name = "Record-VitalSigns",
093               roleName = "Record-Has-VitalSigns",
094               multiplicity = Relation.Multiplicity.ONE,
095               targetEjb = "VitalSignsEJB",
096               fkColumn = "vital_id")
097 })
098 @RelationshipCachingElements({
099     @RelationshipCachingElement(cachingName = "recordSummaryCache",
100                                 cmrField = "physician",
101                                 groupName = "physician-group"),
102     @RelationshipCachingElement(cachingName = "recordCache",
103                                 cmrField = "vitalSigns",
104                                 groupName = "vitalSigns-group"),
105     @RelationshipCachingElement(cachingName = "recordCache",
106                                 cmrField = "prescriptions",
107                                 groupName = "prescriptions-group")
108 })
109 public abstract class RecordEJB extends GenericEntityBean {
110     private static Logger logger = MedRecLog4jFactory.getLogger(
111         RecordEJB.class.getName());
112 
113   // Attributes
114   private EntityContext ctx;
115   private VitalSignsLocalHome vitalsHome;
116   private PhysicianLocalHome physicianHome;
117 
118   // Container managed fields
119 
120   @CmpField(column = "id",
121             orderingNumber = "1")
122   @LocalMethod()
123   @PrimkeyField()
124   public abstract Integer getId();
125 
126   @LocalMethod()
127   public abstract void setId(Integer id);
128 
129   @CmpField(column = "pat_id",
130             orderingNumber = "2",
131             groupNames = "liteWeight")
132   @LocalMethod()
133   public abstract Integer getPatientId();
134 
135   @LocalMethod()
136   public abstract void setPatientId(Integer patientId);
137 
138   @CmpField(orderingNumber = "3",
139             column = "phys_id")
140   @LocalMethod()
141   public abstract Integer getPhysicianId();
142 
143   @LocalMethod()
144   public abstract void setPhysicianId(Integer physicianId);
145 
146   @CmpField(column = "record_date",
147             orderingNumber = "4",
148             groupNames = "liteWeight")
149   @LocalMethod()
150   public abstract Date getDate();
151 
152   @LocalMethod()
153   public abstract void setDate(Date date);
154 
155   @CmpField(column = "symptoms",
156             orderingNumber = "5",
157             groupNames = "liteWeight")
158   @LocalMethod()
159   public abstract String getSymptoms();
160 
161   @LocalMethod()
162   public abstract void setSymptoms(String symptons);
163 
164   @CmpField(orderingNumber = "6",
165             column = "diagnosis")
166   @LocalMethod()
167   public abstract String getDiagnosis();
168 
169   @LocalMethod()
170   public abstract void setDiagnosis(String diagnosis);
171 
172   @CmpField(orderingNumber = "7",
173             column = "notes")
174   @LocalMethod()
175   public abstract String getNotes();
176 
177   @LocalMethod()
178   public abstract void setNotes(String notes);
179 
180   @CmpField(orderingNumber = "8",
181             column = "vital_id")
182   @LocalMethod()
183   public abstract Integer getVitalSignsId();
184 
185   @LocalMethod()
186   public abstract void setVitalSignsId(Integer vitalSignsId);
187 
188   // Container managed relation fields
189   @CmrField(orderingNumber = "10",
190             groupNames = "liteWeight")
191   @LocalMethod()
192   public abstract PhysicianLocal getPhysician();
193 
194   @LocalMethod()
195   public abstract void setPhysician(PhysicianLocal physician);
196 
197   @CmrField(orderingNumber = "11")
198   @LocalMethod()
199   public abstract VitalSignsLocal getVitalSigns();
200 
201   @LocalMethod()
202   public abstract void setVitalSigns(VitalSignsLocal vitalSigns);
203 
204   @CmrField(orderingNumber = "12")
205   @LocalMethod()
206   public abstract Collection getPrescriptions();
207 
208   @LocalMethod()
209   public abstract void setPrescriptions(Collection prescriptions);
210 
211      // Lifecycle method
212   /**
213   <p>Sets the entity context.  Get handles for all
214   * required entity beans.</p>
215   *
216   @param ctx  EntityContext for entity
217   */
218   public void setEntityContext(EntityContext ctx) {
219     this.ctx = ctx;
220     try {
221       // Entity bean homes.
222       vitalsHome = getVitalSignsLocalHome();
223       physicianHome = getPhysicianLocalHome();
224     catch (NamingException ne) {
225       throw new EJBException(ne);
226     }
227   }
228 
229   // Home methods
230   /**
231    <p>Record create.</p>
232    */
233   public Object ejbCreate() throws CreateException {
234     return null;
235   }
236 
237   public void ejbPostCreate() throws CreateException {
238     /* not implemented */
239   }
240 
241   /**
242    <p>Record create.</p>
243    */
244   public Object ejbCreate(Record record)
245       throws NamingException, CreateException {
246     logger.debug("Creating record: "+record.toString());
247     setPatientId(record.getPatientId());
248     Date sqlDate = MedRecUtils.getDate(record.getDate());
249     setDate(sqlDate);
250     setSymptoms(record.getSymptoms());
251     setDiagnosis(record.getDiagnosis());
252     setNotes(record.getNotes());
253     return null;
254   }
255 
256   /**
257    <p>Record create.</p>
258    */
259   public void ejbPostCreate(Record record)
260       throws NamingException, CreateException {
261     logger.debug("Record post creating.");
262     // Vital Signs relationship
263     VitalSignsLocal vitalSigns = vitalsHome.create(record.getVitalSigns());
264     setVitalSigns(vitalSigns);
265     // Physician relationship
266     PhysicianLocal phyicianLocal = getPhysicianEntity(record.getPhysician());
267     logger.debug("Just before cmr field set. " +
268         phyicianLocal.getPhysician().toString());
269     setPhysician(phyicianLocal);
270   }
271 
272   /**
273    <p>Record create.</p>
274    */
275   public Object ejbCreate(Integer patientId,
276                           Calendar date,
277                           String symptoms,
278                           String diagnosis,
279                           String notes,
280                           Physician physician,
281                           VitalSigns vitalSigns)
282       throws NamingException, CreateException {
283     setPatientId(patientId);
284     java.sql.Date sqlDate = MedRecUtils.getDate(date);
285     setDate(sqlDate);
286     setSymptoms(symptoms);
287     setDiagnosis(diagnosis);
288     setNotes(notes);
289     return null;
290   }
291 
292   public void ejbPostCreate(Integer patientId,
293                             Calendar date,
294                             String symptoms,
295                             String diagnosis,
296                             String notes,
297                             Physician physician,
298                             VitalSigns vitalSigns)
299       throws NamingException, CreateException {
300     VitalSignsLocal vitalsLocal = vitalsHome.create(vitalSigns.getTemperature(),
301         vitalSigns.getBloodPressure(), vitalSigns.getPulse(),
302         vitalSigns.getWeight(), vitalSigns.getHeight());
303     setVitalSigns(vitalsLocal);
304     PhysicianLocal phyicianLocal = getPhysicianEntity(physician);
305     setPhysician(phyicianLocal);
306   }
307 
308   /**
309   * Returns handle to Physician entity.
310   */
311   private PhysicianLocal getPhysicianEntity(Physician physician)
312       throws NamingException, CreateException {
313     logger.debug("Getting physician entity.");
314     PhysicianLocal physicanLocal = null;
315     try {
316       logger.debug("Finding physician name "+physician.getFirstName()+" "+
317           physician.getLastName());
318       physicanLocal = physicianHome.findByFirstLastName(
319           physician.getFirstName(), physician.getLastName());
320     catch (FinderException fe) {
321       logger.debug("Physician not found.  Will create new physician.");
322       physicanLocal = physicianHome.create(physician);
323     }
324     return physicanLocal;
325   }
326 
327     // Remote methods
328   /**
329   * Returns serializable List of Prescriptions.
330   */
331   private Prescription[] toPrescriptionVOArray(Collection pPrescriptions) {
332     // Declare local variables.
333     Prescription[] prescriptionVOs = null;
334     if (pPrescriptions != null) {
335       prescriptionVOs = new Prescription[pPrescriptions.size()];
336       Iterator itr = pPrescriptions.iterator();
337       int i = 0;
338       while (itr.hasNext()) {
339         PrescriptionLocal prescriptionLocal = (PrescriptionLocalitr.next();
340         prescriptionVOs[i= prescriptionLocal.getPrescription();
341         i++;
342       }
343     }
344     return prescriptionVOs;
345   }
346 
347   /**
348  <p>Returns a value object representation of the bean.</p>
349  *
350  @return Record
351  */
352   @LocalMethod()
353   public Record getRecord() {
354     Record record = new Record();
355     record.setId(getId());
356     record.setPatientId(getPatientId());
357     record.setPhysician(getPhysicianLite());
358     Calendar cal = MedRecUtils.convertSqlDate2Calendar(getDate());
359     record.setDate(cal);
360     record.setSymptoms(getSymptoms());
361     record.setDiagnosis(getDiagnosis());
362     record.setNotes(getNotes());
363     record.setVitalSigns(getVitalSigns().getVitalSigns());
364     record.setPrescriptions(toPrescriptionVOArray(getPrescriptions()));
365     logger.debug(record.toString());
366     return record;
367   }
368 
369   /**
370  <p>Returns an abbreviated value object representation of the bean.
371  *    Fields returned are:<br>
372  *      id<br>
373  *      date<br>
374  *      symptoms<br>
375  *      diagnois</p>
376  *
377  @return Record Lite Record value object
378  */
379   @LocalMethod()
380   public Record getRecordLite() {
381     Record record = new Record();
382     record.setId(getId());
383     record.setPhysician(getPhysicianLite());
384     Calendar cal = MedRecUtils.convertSqlDate2Calendar(getDate());
385     record.setDate(cal);
386     record.setSymptoms(getSymptoms());
387     record.setDiagnosis(getDiagnosis());
388     return record;
389   }
390 
391   // Utility methods
392   private Physician getPhysicianLite() {
393     return new Physician(getPhysician().getFirstName(),
394         getPhysician().getMiddleName(), getPhysician().getLastName());
395   }
396 
397   /**
398    * Get Vital Signs Entity EJB local home
399    */
400   public VitalSignsLocalHome getVitalSignsLocalHome() throws NamingException {
401     return (VitalSignsLocalHome)lookUpLocalHome(JNDINames.VITALSIGNS_LOCAL_HOME,
402         VitalSignsLocalHome.class);
403   }
404 
405   /**
406   * Get Physician Entity EJB local home
407   */
408   public PhysicianLocalHome getPhysicianLocalHome() throws NamingException {
409     return (PhysicianLocalHome)lookUpLocalHome(JNDINames.PHYSICIAN_LOCAL_HOME,
410         PhysicianLocalHome.class);
411   }
412 
413   /**
414   * Get local home
415   */
416   public Object lookUpLocalHome(String pHome, Class pClazz)
417       throws NamingException {
418     ServiceLocator locator = ServiceLocator.getInstance();
419     Object obj = locator.lookupLocalHome(pHome, pClazz);
420     return obj;
421   }
422 
423   @LocalMethod()
424   public String toString() {
425       StringBuffer str = new StringBuffer();
426       str.append("RECORD [Id: "+getId());
427       str.append(" | PatId: "+getPatientId());
428       str.append(" | DOB: "+getDate());
429       str.append(" | Syms: "+getSymptoms());
430       str.append(" | Diag: "+getDiagnosis());
431       str.append(" | Notes: "+getNotes());
432       str.append("\n" +
433         (getPhysician() == null "PHYSICIAN: [null]" : getPhysician().toString()));
434       str.append("\n" +
435         (getVitalSigns() == null "VITALS: [null]" : getVitalSigns().toString()));
436       str.append("\n"+prescriptionsToString());
437       str.append("]");
438       return str.toString();
439   }
440 
441   private String prescriptionsToString() {
442     StringBuffer str = new StringBuffer();
443     if (getPrescriptions() != null && getPrescriptions().size() 0) {
444       str.append(" Num of prescriptions: " +
445           getPrescriptions().size() +
446           " |");
447       Iterator itr = getPrescriptions().iterator();
448       while (itr.hasNext()) {
449         Object obj = (Objectitr.next();
450         str.append("\n" +
451             obj.toString());
452       }
453     else {
454       str.append("PRESCRIPTIONS: [null]");
455     }
456     return str.toString();
457   }
458 }