RecordSessionEJB.ejb
001 package com.bea.medrec.controller;
002 
003 import com.bea.medrec.entities.*;
004 import com.bea.medrec.utils.MedRecLog4jFactory;
005 import com.bea.medrec.value.Physician;
006 import com.bea.medrec.value.Prescription;
007 import com.bea.medrec.value.Record;
008 import com.bea.medrec.value.RecordsSummary;
009 import java.rmi.RemoteException;
010 import java.util.ArrayList;
011 import java.util.Collection;
012 import java.util.Iterator;
013 import javax.ejb.CreateException;
014 import javax.ejb.EJBException;
015 import javax.ejb.FinderException;
016 import javax.ejb.SessionContext;
017 import javax.naming.NamingException;
018 import org.apache.log4j.Logger;
019 import weblogic.ejb.GenericSessionBean;
020 import weblogic.ejbgen.*;
021 
022 /**
023  <p>Session facade for all MedRec medical record functionality.</p>
024  *
025  @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
026  */
027 @EjbLocalRefs({
028     @EjbLocalRef(name = "ejb/local/prescription",
029                  home = "com.bea.medrec.entities.PrescriptionLocalHome",
030                  local = "com.bea.medrec.entities.PrescriptionLocal",
031                  type = Constants.RefType.ENTITY,
032                  link = "PrescriptionEJB"),
033     @EjbLocalRef(name = "ejb/local/record",
034                  home = "com.bea.medrec.entities.RecordLocalHome",
035                  local = "com.bea.medrec.entities.RecordLocal",
036                  type = Constants.RefType.ENTITY,
037                  link = "RecordEJB"),
038     @EjbLocalRef(name = "ejb/local/physician",
039                  home = "com.bea.medrec.entities.PhysicianLocalHome",
040                  local = "com.bea.medrec.entities.PhysicianLocal",
041                  type = Constants.RefType.ENTITY,
042                  link = "PhysicianEJB")
043 })
044 @FileGeneration(remoteClass = Constants.Bool.TRUE,
045                 remoteHome = Constants.Bool.TRUE)
046 @JndiName(remote = "RecordSessionEJB.RecordSessionHome")
047 @Session(maxBeansInFreePool = "1000",
048          initialBeansInFreePool = "0",
049          transTimeoutSeconds = "0",
050          type = Session.SessionType.STATELESS,
051          defaultTransaction = Constants.TransactionAttribute.REQUIRED,
052          enableCallByReference = Constants.Bool.TRUE,
053          ejbName = "RecordSessionEJB")
054 
055 public class RecordSessionEJB extends GenericSessionBean {
056   private static Logger logger =
057     MedRecLog4jFactory.getLogger(RecordSessionEJB.class.getName());
058   private SessionContext ctx;
059   private RecordLocalHome recordHome;
060   private PrescriptionLocalHome prescriptionHome;
061   private PhysicianLocalHome physicianHome;
062 
063   /**
064  <p>Sets the session context.  Get handles for all
065  * session and entity and JMS connections used throughout
066  * this session bean.</p>
067  *
068  @param ctx SessionContext Context for session
069  */
070   public void setSessionContext(SessionContext ctx) {
071     this.ctx = ctx;
072     try {
073       // Entity bean homes.
074       recordHome = JNDILookupUtils.getRecordLocalHome();
075       prescriptionHome = JNDILookupUtils.getPrescriptionLocalHome();
076       physicianHome = JNDILookupUtils.getPhysicianLocalHome();
077     catch (NamingException ne) {
078       throw new EJBException(ne);
079     }
080   }
081 
082   // Public Methods
083 
084   //  A D D   R E C O R D
085   /**
086  <p>Adds a record, including vital signs and prescriptions, by
087  * calling the appropriate entity beans.</p>
088  *
089  @param pRecordVO Complete record value object to be added
090  @return Record
091  @throws RemoteException
092  */
093   @RemoteMethod()
094   public Record addRecord(Record pRecordVO)
095       throws NamingException, RemoteException, Exception {
096     logger.debug("Adding record: "+pRecordVO.toString());
097 
098     // Declare local variables.
099     RecordLocal recordLocal = null;
100     Collection prescriptions = null;
101     try {
102       recordLocal = recordHome.create(pRecordVO);
103       if (pRecordVO.getNumOfPrescriptions() 0) {
104         prescriptions = addPrescriptions(pRecordVO.getPrescriptions(),
105             recordLocal);
106         recordLocal.setPrescriptions(prescriptions);
107       }
108     catch (CreateException ce) {
109       logger.error(ce);
110       throw new EJBException(ce);
111     }
112     return recordLocal.getRecord();
113   }
114 
115   /**
116  <p>Add prescriptions and increment a counter property in
117  * RecordSessionEJBMBean.</p>
118  *
119  @param pPrescriptionVOs Array of Prescription value objects
120  @param recordLocal Record entity
121  @return Collection
122  @throws CreateException
123  @throws Exception
124  */
125   private Collection<Object> addPrescriptions(Prescription[] pPrescriptionVOs,
126                                       RecordLocal recordLocal)
127       throws CreateException, Exception {
128     // Declare local variables.
129     Collection<Object> prescriptions = new ArrayList<Object>();
130     if (pPrescriptionVOs != null) {
131       logger.debug("Number of prescriptions: "+pPrescriptionVOs.length);
132       com.bea.medrec.controller.RecordSessionEJBMBeanImpl.incrementTotalRx();
133       for (int i = 0; i < pPrescriptionVOs.length; i++) {
134         Prescription prescriptionVO = pPrescriptionVOs[i];
135         prescriptions.add(prescriptionHome.create(prescriptionVO, recordLocal));
136       }
137     }
138     return prescriptions;
139   }
140 
141   //  G E T   P H Y S I C I A N
142   /**
143  <p>Retrieves a patient.</p>
144  *
145  @param pPhysicianVO Physician value object with first name and last name.
146  @return Physician  Found physician value oject.
147  @throws NamingException
148  @throws NamingException
149  @throws CreateException
150  */
151   @RemoteMethod()
152   public Physician getPhysician(Physician pPhysicianVO)
153       throws NamingException, CreateException {
154     logger.debug("Getting physician entity.");
155     PhysicianLocal physicanLocal = null;
156     try {
157       physicanLocal = physicianHome.findByFirstLastName(
158           pPhysicianVO.getFirstName(), pPhysicianVO.getLastName());
159       logger.debug("Found physician id "+physicanLocal.getId());
160     catch (FinderException fe) {
161       logger.debug("Physician not found.  Will create new physician");
162       physicanLocal = physicianHome.create(pPhysicianVO.getFirstName(),
163           pPhysicianVO.getMiddleName(), pPhysicianVO.getLastName());
164       logger.debug("Created physician id "+physicanLocal.getId());
165     }
166     return physicanLocal.getPhysician();
167   }
168 
169   //  G E T   P R E S C R I P T I O N S
170   /**
171  <p>Retrieves all prescriptions for a patient.</p>
172  *
173  @param pPatientId Id of the prescription.
174  @return Collection  Collection of prescription value objects.
175  @throws NamingException
176  @throws RemoteException
177  @throws Exception
178  */
179   @RemoteMethod()
180   public Prescription[] getPrescriptions(Integer pPatientId)
181       throws NamingException, RemoteException, Exception {
182     // Declare local variables.
183     Collection prescriptions = null;
184     try {
185       // get all prescriptions for a patient
186       prescriptions = prescriptionHome.findByPatientId(pPatientId);
187     catch (FinderException fe) {
188       logger.warn(fe.getClass().getName()+" - "+fe.getMessage());
189     }
190     logger.debug("Num of Prescriptions: "+
191         (prescriptions != null ? String.valueOf(prescriptions.size()) "0"));
192 
193     // convert collection of prescription entities to
194     // prescription vo array and return array
195     return toPrescriptionVOArray(prescriptions);
196   }
197 
198   private Prescription[] toPrescriptionVOArray(Collection pPrescriptionLocals) {
199     // declare local variables
200     Prescription[] prescriptionVOs = null;
201 
202     // convert prescriptions to prescription vo array
203     if (pPrescriptionLocals != null && pPrescriptionLocals.size() 0) {
204       prescriptionVOs = new Prescription[pPrescriptionLocals.size()];
205       int i = 0;
206       for (Iterator iterator = pPrescriptionLocals.iterator();
207            iterator.hasNext(); i++) {
208         PrescriptionLocal prescriptionLocal =
209             (PrescriptionLocaliterator.next();
210         prescriptionVOs[i= prescriptionLocal.getPrescription();
211       }
212     else {
213       prescriptionVOs = new Prescription[0];
214     }
215     return prescriptionVOs;
216   }
217 
218   //  G E T   A L L   R E C O R D S   F O R   A   P A T I E N T
219   /**
220  <p>Retrieves a records for a patient.  Records return are lite weight.</p>
221  *
222  @param pPatientId Id of the patient.
223  @return Collection  Collection of lite record value objects.
224  @throws NamingException
225  @throws RemoteException
226  @throws Exception
227  */
228   @RemoteMethod()
229   public Record[] getRecords(Integer pPatientId)
230       throws NamingException, RemoteException, Exception {
231     // Declare local variables.
232     Collection records = null;
233     try {
234       records = recordHome.findByPatientId(pPatientId);
235     catch (FinderException fe) {
236       logger.warn(fe.getClass().getName()+" - "+fe.getMessage());
237     }
238     logger.debug("Num of records: " +
239         (records != null ? String.valueOf(records.size()) "0"));
240 
241     // convert collection of record entities to record vo array and return array
242     return toRecordVOArray(records);
243   }
244 
245   private Record[] toRecordVOArray(Collection pRecordLocals) {
246     // Declare local variables.
247     Record[] recordVOs = null;
248 
249     // convert collection of record entities to record vo array
250     if (pRecordLocals != null && pRecordLocals.size() 0) {
251       recordVOs = new Record[pRecordLocals.size()];
252       int i = 0;
253       for (Iterator iterator = pRecordLocals.iterator(); iterator.hasNext();
254            i++) {
255         RecordLocal recordLocal = (RecordLocaliterator.next();
256         logger.debug("Record retrieved: " +
257             recordLocal.toString());
258         recordVOs[i= recordLocal.getRecordLite();
259       }
260     else {
261       recordVOs = new Record[0];
262     }
263     return recordVOs;
264   }
265 
266   //  G E T   A   R E C O R D
267   /**
268  <p>Retrieves all information for a visit given a record id.
269  * This includes vital signs, the record, and any prescriptions.</p>
270  *
271  @param pRecordId Id of a record.
272  @return Record  Record value object.
273  @throws NamingException
274  @throws RemoteException
275  @throws Exception
276  */
277   @RemoteMethod()
278   public Record getRecord(Integer pRecordId)
279       throws NamingException, RemoteException, Exception {
280     logger.debug("Getting record: "+pRecordId);
281 
282     // Declare local variables.
283     Record recordVO = null;
284     RecordLocal recordLocal = null;
285     try {
286       recordLocal = recordHome.findByRecordId(pRecordId);
287       recordVO = recordLocal.getRecord();
288       logger.debug(recordVO.toString());
289     catch (FinderException fe) {
290       logger.warn(fe.getClass().getName()+" - "+fe.getMessage());
291     }
292     return recordVO;
293   }
294 
295   //  G E T   R E C O R D S   S U M M A R Y
296   /**
297  <p>Retrieves all medical record summary.
298  * This includes a collection of abbreviated records, and
299  * a collection of current and recent prescriptions.</p>
300  *
301  @param pPatientId Id of the patient.
302  @return RecordsSummary
303  @throws NamingException
304  @throws RemoteException
305  @throws Exception
306  */
307   @RemoteMethod()
308   public RecordsSummary getRecordsSummary(Integer pPatientId)
309       throws NamingException, RemoteException, Exception {
310     logger.debug("Patient id: "+pPatientId.toString());
311     RecordsSummary recSummaryVO = new RecordsSummary();
312     recSummaryVO.setRecords(getRecords(pPatientId));
313     recSummaryVO.setPrescriptions(getPrescriptions(pPatientId));
314     return recSummaryVO;
315   }
316 }