PhysicianSessionEJB.ejb
001 package com.bea.medrec.controller;
002 
003 import com.bea.medrec.utils.MedRecLog4jFactory;
004 import com.bea.medrec.utils.MedRecUtils;
005 import com.bea.medrec.value.*;
006 import com.bea.medrec.webservices.client.MedRecWebServicesPortType;
007 import com.bea.medrec.webservices.client.MedRecWebServices_Impl;
008 import com.bea.medrec.webservices.client.MedRecWebServices;
009 import com.bea.medrec.webservices.client.PhysicianWebServicesPortType;
010 import com.bea.medrec.webservices.client.PhysicianWebServices_Impl;
011 import com.bea.medrec.webservices.client.PhysicianWebServices;
012 import java.rmi.RemoteException;
013 import javax.ejb.EJBException;
014 import javax.ejb.SessionContext;
015 import javax.naming.Context;
016 import javax.naming.InitialContext;
017 import javax.xml.rpc.ServiceException;
018 import javax.xml.rpc.Stub;
019 import org.apache.log4j.Logger;
020 import weblogic.ejb.GenericSessionBean;
021 import weblogic.ejbgen.*;
022 import weblogic.wsee.jaxrpc.WLStub;
023 
024 /**
025  <p>Session Bean implementation for physician functionality including
026  * access MedRec web services.</p>
027  *
028  @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
029  */
030 @EnvEntries({
031     @EnvEntry(name = "webservice/username",
032               type = "java.lang.String",
033               value = "medrec_webservice_user"),
034     @EnvEntry(name = "webservice/password",
035               type = "java.lang.String",
036               value = "weblogic"),
037     @EnvEntry(name = "webservice/url",
038               type = "java.lang.String",
039               value = "http://localhost:7101/ws_medrec/MedRecWebServices"),
040     @EnvEntry(name = "rm_webservice_client/url",
041               type = "java.lang.String",
042               value = "http://localhost:7101/ws_phys/PhysicianWebServices"),
043     @EnvEntry(name = "rm_webservice_service/url",
044               type = "java.lang.String",
045               value = "http://localhost:7101/ws_rm_medrec/MedRecRMWebServices")
046 })
047 @JndiName(remote = "PhysicianSessionEJB.PhysicianSessionHome")
048 @Session(maxBeansInFreePool = "1000",
049          initialBeansInFreePool = "0",
050          transTimeoutSeconds = "0",
051          type = Session.SessionType.STATELESS,
052          defaultTransaction = Constants.TransactionAttribute.REQUIRED,
053          enableCallByReference = Constants.Bool.TRUE,
054          ejbName = "PhysicianSessionEJB")
055 public class PhysicianSessionEJB extends GenericSessionBean {
056   private static Logger logger =
057       MedRecLog4jFactory.getLogger(PhysicianSessionEJB.class.getName());
058 
059   // Instance variables
060   private SessionContext ctx;
061   private MedRecWebServicesPortType port;
062   private PhysicianWebServicesPortType rmPort;
063 
064   /**
065    <p>Sets the session context.  Get handles for all
066    * session and entity and JMS connections used throughout
067    * this session bean.</p>
068    *
069    @param ctx SessionContext Context for session
070    */
071   public void setSessionContext(SessionContext ctx) {
072     this.ctx = ctx;
073     try {
074       // Web service handle.
075       port = getMedRecWebServicesPort();
076       rmPort = getPhysicianWebServicesPort();
077     catch (Exception e) {
078       throw new EJBException(e);
079     }
080   }
081 
082   // Public Methods
083 
084   //  A D D   R E C O R D
085   /**
086    <p>Accesses MedRec Web service adding a record, including
087    * vital signs and prescriptions.</p>
088    *
089    @param pPhysicianVO
090    @param pRecordVO
091    @throws RemoteException
092    */
093   @RemoteMethod()
094   public void addRecord(Physician pPhysicianVO, Record pRecordVO)
095       throws RemoteException {
096     logger.info("Adding record to patient id: "+pRecordVO.getPatientId());
097     logger.debug(pRecordVO.toString());
098     try {
099       pRecordVO.setPhysician(pPhysicianVO);
100       Context initCtx = new InitialContext();
101       String wsServiceUrl = (StringinitCtx.lookup(
102           "java:comp/env/rm_webservice_service/url");
103       logger.info("Adding record reliably, asynchronously");
104       rmPort.addRecord(wsServiceUrl, pRecordVO);
105     catch (Exception e) {
106       logger.error(e.getMessage(), e);
107       throw new EJBException(e);
108     }
109   }
110 
111   //  G E T   A   R E C O R D
112   /**
113    <p>Accesses MedRec Web service retrieving all information
114    * for a visit given a record id. This includes vital signs,
115    * the record, and any prescriptions.</p>
116    *
117    @param pRecordId Id of the patient.
118    @return List  List of lite record value objects.
119    @throws RemoteException
120    */
121   @RemoteMethod()
122   public Record getRecord(Integer pRecordIdthrows RemoteException {
123     logger.info("Getting record.");
124     logger.debug("Record Id: "+pRecordId);
125     Record recordVO = null;
126     try {
127       // Get record from MedRec.
128       recordVO = port.getRecord(pRecordId.intValue());
129       logger.debug("Got record from MedRec.");
130     catch (Exception e) {
131       logger.error(e.getMessage(), e);
132       throw new EJBException(e);
133     }
134     return recordVO;
135   }
136 
137   //  G E T   M E D I C A L   R E C O R D   S U M M A R Y
138   /**
139    <p>Accesses MedRec Web service retrieving  all medical record summary.
140    * This includes a List of abbreviated records, and
141    * a List of current and recent prescriptions.</p>
142    *
143    @param pPatientId Id of the record.
144    @return RecordsSummary
145    @throws RemoteException
146    */
147   @RemoteMethod()
148   public RecordsSummary getRecordsSummary(Integer pPatientId)
149       throws RemoteException {
150     logger.info("Getting records summary.");
151     logger.debug("Patient Id: "+pPatientId.toString());
152     RecordsSummary recSummaryVO = null;
153     try {
154       // Get record summary from MedRec.
155       recSummaryVO = port.getRecordsSummary(pPatientId.intValue());
156       logger.debug("Got record summary from MedRec.");
157     catch (Exception e) {
158       logger.error(e.getMessage(), e);
159       throw new EJBException(e);
160     }
161     return recSummaryVO;
162   }
163 
164   //  S E A R C H   P A T I E N T
165   /**
166    <p>Find patient by id.</p>
167    *
168    @param pSearchVO Search value ojbect containing last name or SSN
169    @return Patient[]  Patient value object.
170    @throws RemoteException
171    */
172   @RemoteMethod()
173   public Patient[] searchPatients(Search pSearchVOthrows RemoteException {
174     if (!MedRecUtils.isEmpty(pSearchVO.getLastName()))
175       return searchPatientsByLastNameWild(pSearchVO.getLastName());
176     else if (!MedRecUtils.isEmpty(pSearchVO.getSsn()))
177       return searchPatientsBySSN(pSearchVO.getSsn());
178     else
179       return new Patient[0];
180   }
181 
182   //  S E A R C H   P A T I E N T
183   /**
184    <p>Find patient by id.</p>
185    *
186    @param pSearchString Wild card search last name
187    @return Patient  Patient value object.
188    @throws RemoteException
189    */
190   @RemoteMethod()
191   public Patient[] searchPatientsByLastNameWild(String pSearchString)
192       throws RemoteException {
193     logger.info("By wildcard last name.");
194     logger.debug("Last name: "+pSearchString);
195 
196     // Declare local variables.
197     Patient[] patientVOs = null;
198     try {
199       // Get patients from MedRec.
200       patientVOs = port.findPatientByLastNameWild(pSearchString);
201       logger.debug("Got patients from MedRec.");
202       logger.debug("Num of results: " +
203           patientVOs.length);
204     catch (Exception e) {
205       logger.error(e.getMessage(), e);
206       throw new EJBException(e);
207     }
208     return patientVOs;
209   }
210 
211   //  S E A R C H   P A T I E N T   B Y   S S N
212   /**
213    <p>Find patient by id.</p>
214    *
215    @param pSsn Patient SSN
216    @return List  List of one patient value objects.
217    @throws RemoteException
218    */
219   @RemoteMethod()
220   public Patient[] searchPatientsBySSN(String pSsnthrows RemoteException {
221     logger.info("Search for MedRec patients by ssn.");
222     logger.debug("Ssn: "+pSsn);
223 
224     // Declare local variables.
225     Patient[] patientVOs = null;
226     try {
227       Patient patientVO = port.findPatientBySsn(pSsn);
228       logger.debug("Got patient from MedRec.");
229       if (patientVO != null)
230         patientVOs = new Patient[]{patientVO};
231       else
232         patientVOs = new Patient[0];
233     catch (Exception e) {
234       logger.error(e.getMessage(), e);
235       throw new EJBException(e);
236     }
237     return patientVOs;
238   }
239 
240   // Private methods
241   /**
242    <p>Get web service port.</p>
243    */
244   private MedRecWebServicesPortType getMedRecWebServicesPort()
245       throws RemoteException, ServiceException, Exception {
246     logger.info("Looking up web service.");
247 
248     // Declare local variables
249     MedRecWebServicesPortType port = null;
250     // MedRec Web Service attributes
251     String wsUrl = null;
252     String wsUsername = null;
253     String wsPassword = null;
254     try {
255       Context initCtx = new InitialContext();
256       wsUrl = (StringinitCtx.lookup("java:comp/env/webservice/url");
257       wsUsername = (StringinitCtx.lookup("java:comp/env/webservice/username");
258       wsPassword = (StringinitCtx.lookup("java:comp/env/webservice/password");
259       logger.debug("MedRec Web Service URL: " +
260           wsUrl);
261       MedRecWebServices service = new MedRecWebServices_Impl(wsUrl+"?WSDL");
262       port = service.getMedRecWebServicesPort(wsUsername, wsPassword);
263     catch (ServiceException sve) {
264       logger.error(sve.getLinkedCause().getMessage(), sve);
265       throw sve;
266     catch (Exception e) {
267       logger.error(e.getMessage(), e);
268       throw e;
269     }
270     return port;
271   }
272 
273   /**
274    <p>Get web service port.</p>
275    */
276   private PhysicianWebServicesPortType getPhysicianWebServicesPort()
277       throws RemoteException, ServiceException, Exception {
278     logger.info("Looking up web service.");
279 
280     // Declare local variables
281     PhysicianWebServicesPortType port = null;
282     // MedRec Web Service attributes
283     String wsClientUrl = null;
284     String wsUsername = null;
285     String wsPassword = null;
286     try {
287       Context initCtx = new InitialContext();
288       wsClientUrl = (StringinitCtx.lookup(
289           "java:comp/env/rm_webservice_client/url");
290       wsUsername = (StringinitCtx.lookup("java:comp/env/webservice/username");
291       wsPassword = (StringinitCtx.lookup("java:comp/env/webservice/password");
292       logger.debug("MedRec RM Web Service client URL: " +
293           wsClientUrl);
294       PhysicianWebServices service =  new PhysicianWebServices_Impl(wsClientUrl+
295           "?WSDL");
296       port = service.getPhysicianWebServicesPort(wsUsername, wsPassword);
297       Stub stub = (Stub)port;
298       stub._setProperty(WLStub.COMPLEX, "true");
299     catch (ServiceException sve) {
300       logger.error(sve.getLinkedCause().getMessage(), sve);
301       throw sve;
302     catch (Exception e) {
303       logger.error(e.getMessage(), e);
304       throw e;
305     }
306     return port;
307   }
308 }