HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"E-Business with EJBs"
Volume: 5 Issue: 2, p.66

	

Listing 1: 

package PatientTrackSession; 

import java.rmi.RemoteException; 
import java.util.Vector; 
import javax.ejb.EJBObject; 

/** 
 *  Patient is the remote interface representing a stateful 
 *  session bean, PatientSessionBean. 
 */ 
public interface Patient extends EJBObject 
{ 
    public String getDoctor() throws RemoteException; 
    public boolean isDoctor(String aDoctor) throws RemoteException; 
  
    public void setName(String aName) throws RemoteException; 
    public String getName() throws RemoteException; 
  
    public void setDOBirth(String aDOB) throws RemoteException; 
    public String getDOBirth() throws RemoteException; 
  
    public void setGender(String aGender) throws RemoteException; 
    public String getGender() throws RemoteException; 
  
    public void setAllergies(Vector aAllergies) throws RemoteException; 
    public Vector getAllergies() throws RemoteException; 
  
    public void setLastVisitDate(String aLVD) throws RemoteException; 
    public String getLastVisitDate() throws RemoteException; 
  
    public void setDiagnosis(String aDiagnosis) throws RemoteException; 
    public String getDiagnosis() throws RemoteException; 
} 

Listing 2: 

package PatientTrackSession; 

import javax.ejb.EJBHome; 
import javax.ejb.CreateException; 
import java.rmi.RemoteException; 

public interface PatientHome extends EJBHome 
{ 
    public Patient create(String aDoctorName) throws Create 
    Exception, RemoteException; 
} 

Listing 3: 

package PatientTrackSession; 

import javax.ejb.SessionBean; 
import java.util.Vector; 
import java.rmi.RemoteException; 
import javax.ejb.SessionContext; 
import javax.ejb.CreateException; 

public class PatientSessionBean implements SessionBean 
{ 
    // Business Attributes 
    private String doctor; 
    private String name; 
    private String DOBirth; 
    private String gender; 
    private Vector allergies; 
    private String lastVisitDate; 
    private String diagnosis; 
  
    // Business Methods 
    public String getDoctor() { 
        return doctor; 
    } 
  
    public boolean isDoctor(String aDoctor) { 
        if (aDoctor == null) 
            return false; 
  
        String curDoctor = this.getDoctor(); 
        return curDoctor.equalsIgnoreCase(aDoctor); 
    } 
  
    public void setName(String aName) { 
        name = aName; 
    } 
    public String getName(){ 
        return name; 
    } 

 public void setDOBirth(String aDOB) { 
        DOBirth = aDOB; 
    } 
    public String getDOBirth() { 
        return DOBirth; 
    } 
  
    public void setGender(String aGender) { 
        gender = aGender; 
    } 
  
    public String getGender(){ 
        return gender; 
    } 
  
    public void setAllergies(Vector 
    aAllergies) { 
        allergies = aAllergies; 
    } 
    public Vector getAllergies() { 
        return allergies; 
    } 
  
    public void setLastVisitDate(String 
    aLVD) { 
        lastVisitDate = aLVD; 
    } 
    public String getLastVisitDate() { 
        return lastVisitDate; 
    } 
  
    public void setDiagnosis(String 
    aDiagnosis) { 
        diagnosis = aDiagnosis; 
    } 
    public String getDiagnosis() { 
        return diagnosis; 
    } 
  
    // These method(s) satisfy the Ses- 
    // sionBean interface contract 
    private transient SessionContext ctx; 
  
    public void ejbCreate(String aDoctor) 
        throws CreateException, Remote- 
        Exception 
    { 
        doctor = aDoctor; 
    } 
  
    public void setSessionContext(Ses- 
    sionContext aCtx) { 
        ctx = aCtx; 
    } 
  
    public void ejbActivate() { 
        System.out.println("ejbActivat- 
        ing..."+doctor); 
    } 
  
    public void ejbPassivate() { 
        System.out.println("ejbPassi- 
        vate..."+doctor); 
    } 
  
    public void ejbRemove() { 
        System.out.println("ejbRe- 
        move..."+doctor); 
    } 
} 

Listing 4: 

// get parameters from HttpRequest above… 

// check to see if servlet has handle   // to the patient yet 
        handle = (Handle)patient 
        Cache.get(name); 
        if (handle == null) { 
            patient = newPatient(name, 
            doctor, gender, dateOfBirth, 
            dateOfLastVisit, allergies, 
            diagnosis); 
  
            // add patient's handle to cache 
            patientCache.put(name, 
            patient.getHandle()); 
        } 
        else { 
            // if patient already 
            // exists in servlet 
            // cache, get his/her 
            // latest diagnosis from 
            // the doctor. 
            System.out.println("Patient: "+name+" handle found on cache."); 
            patient = (Patient)handle- 
            .getEJBObject(); // get 
            EJBObject from handle 
  
            if (patient != null) { 
                // only same doctor 
                // can make diagnosis 
                if (patient.isDoctor(doctor)) { 
                    patient.setDiagno- 
                    sis(diagnosis); 
                } 
                else { 
                    out.println("You 
                    are not authorized 
                    to modify Patient 
                    "+name); 
                    out.flush(); 
                    out.close(); 
                    return; 
                } 
  
            } 
            else { 
                System.out.println("no EJBObject for handle"); 
                out.println("ERROR: Could not find patient!"); 
                out.flush(); 
                out.close(); 
                return; 
            } 
        } 
  



 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.