Chapter 3
Chapter 5
Chapter 7
Chapter 8
Chapter 9
Download All Code
Home
 
Chapter 8 Examples
8.1 to 8.12   8.13 to 8.24   8.25 to 8.35

Download all Chapter 8 code

View code examples 8.1 to 8.12
8.1   8.2   8.3   8.4   8.5   8.6
8.7   8.8   8.9   8.10   8.11   8.12

Example 8.1 Implementing Business Delegate Pattern – ResourceDelegate

// imports
...

public class ResourceDelegate {

  // Remote reference for Session Facade
  private ResourceSession session;

  // Class for Session Facade's Home object
  private static final Class homeClazz =
  corepatterns.apps.psa.ejb.ResourceSessionHome.class;

  // Default Constructor. Looks up home and connects
  // to session by creating a new one
  public ResourceDelegate() throws ResourceException {
    try {
      ResourceSessionHome home = (ResourceSessionHome)
        ServiceLocator.getInstance().getHome(
          "Resource", homeClazz);
      session = home.create();
    } catch(ServiceLocatorException ex) {
      // Translate Service Locator exception into
      // application exception
      throw new ResourceException(...);
    } catch(CreateException ex) {
      // Translate the Session create exception into
      // application exception
      throw new ResourceException(...);
    } catch(RemoteException ex) {
      // Translate the Remote exception into
      // application exception
      throw new ResourceException(...);
    }
  }

  // Constructor that accepts an ID (Handle id) and 
  // reconnects to the prior session bean instead
  // of creating a new one
  public BusinessDelegate(String id) 
    throws ResourceException {
    super();
    reconnect(id);
  }

  // Returns a String ID the client can use at a
  // later time to reconnect to the session bean
  public String getID() {
    try {
      return ServiceLocator.getId(session);
    } catch (Exception e) {
      // Throw an application exception
    }
 }

  // method to reconnect using String ID
  public void reconnect(String id) 
    throws ResourceException {
    try {
      session = (ResourceSession) 
                ServiceLocator.getService(id);
    } catch (RemoteException ex) {
      // Translate the Remote exception into
      // application exception
      throw new ResourceException(...);
    }
  }

  // The following are the business methods
  // proxied to the Session Facade. If any service 
  // exception is encountered, these methods convert
  // them into application exceptions such as
  // ResourceException, SkillSetException, and so 
  // forth.

  public ResourceVO setCurrentResource(
    String resourceId) 			
    throws ResourceException {
    try {
      return session.setCurrentResource(resourceId);
    } catch (RemoteException ex) {
      // Translate the service exception into
      // application exception
      throw new ResourceException(...);
    }
  }

  public ResourceVO getResourceDetails()
    throws ResourceException {

    try {
      return session.getResourceDetails();
    } catch(RemoteException ex) {
      // Translate the service exception into
      // application exception
      throw new ResourceException(...);
    }
  }

  public void setResourceDetails(ResourceVO vo)
    throws ResourceException {
    try {
      session.setResourceDetails(vo);
    } catch(RemoteException ex) {
      throw new ResourceException(...);
    }
  }

  public void addNewResource(ResourceVO vo)
    throws ResourceException {
    try {
      session.addResource(vo);
    } catch(RemoteException ex) {
      throw new ResourceException(...);
    }
  }

  // all other proxy method to session bean
  ...
}

Example 8.2 Remote Interface for ResourceSession

// imports
...
public interface ResourceSession extends EJBObject {
  
  public ResourceVO setCurrentResource(
    String resourceId) throws 
    RemoteException, ResourceException;
	
  public ResourceVO getResourceDetails() 
     throws RemoteException, ResourceException;
  public void setResourceDetails(ResourceVO resource) 
     throws RemoteException, ResourceException;
	
  public void addResource(ResourceVO resource) 
      throws RemoteException, ResourceException;
	
  public void removeResource() 
      throws RemoteException, ResourceException;
	
  // methods for managing blockout time by the 
  // resource
  public void addBlockoutTime(Collection blockoutTime)
      throws RemoteException, BlockoutTimeException;
	
  public void updateBlockoutTime(
    Collection blockoutTime) 
      throws RemoteException, BlockoutTimeException;
	
  public void removeBlockoutTime(
    Collection blockoutTime) 
      throws RemoteException, BlockoutTimeException;
	
  public void removeAllBlockoutTime() 
      throws RemoteException, BlockoutTimeException;
	
  // methods for resource skillsets time by the 
  //resource
  public void addSkillSets(Collection skillSet) 
      throws RemoteException, SkillSetException;
	
  public void updateSkillSets(Collection skillSet) 
      throws RemoteException, SkillSetException;
	
  public void removeSkillSet(Collection skillSet) 
      throws RemoteException, SkillSetException;

  ...
}	

Example 8.3 Implementing the Value Object Pattern – Value Object Class

// Value Object to hold the details for Project
public class ProjectVO implements java.io.Serializable {
    public String projectId;
    public String projectName;
    public String managerId;
    public String customerId;
    public Date startDate;
    public Date endDate;
    public boolean started;
    public boolean completed;
    public boolean accepted;
    public Date acceptedDate;
    public String projectDescription;
    public String projectStatus;

    // Value object constructors...
}

Example 8.4 Implementing the Value Object Pattern – Entity Bean Class

...
public class ProjectEntity implements EntityBean {
    private EntityContext context;
    public String projectId;
    public String projectName;
    public String managerId;
    public String customerId;
    public Date startDate;
    public Date endDate;
    public boolean started;
    public boolean completed;
    public boolean accepted;
    public Date acceptedDate;
    public String projectDescription;
    public String projectStatus;
    private boolean closed;

    // other attributes...
    private ArrayList commitments;
    ...

    // Method to get value object for Project data
    public ProjectVO getProjectData() {
      return createProjectVO();
    }

    // method to create a new value object and 
    // copy data from entity bean into the value 
    // object
    private ProjectVO createProjectVO() {
        ProjectVO proj = new ProjectVO();
        proj.projectId = projectId;
        proj.projectName = projectName;
        proj.managerId = managerId;
        proj.startDate = startDate;
        proj.endDate = endDate;
        proj.customerId = customerId;
        proj.projectDescription = projectDescription;
        proj.projectStatus = projectStatus;
        proj.started = started;
        proj.completed = completed;
        proj.accepted = accepted;
        proj.closed = closed;
        return proj;
    }
    ...
}

Example 8.5 Implementing Updatable Value Objects Strategy

...
public class ProjectEntity implements EntityBean {
    private EntityContext context;
  ...
  // attributes and other methods as in Example 8.4
  ...

  // method to set entity values with a value object
  public void setProjectData(ProjectVO updatedProj) {
    mergeProjectData(updatedProj);
  }

  // method to merge values from the value object into
  // the entity bean attributes
  private void mergeProjectData(ProjectVO updatedProj) {
    // version control check may be necessary here 
    // before merging changes in order to 
    // prevent losing updates by other clients
    projectId = updatedProj.projectId;
    projectName = updatedProj.projectName;
    managerId = updatedProj.managerId;
    startDate = updatedProj.startDate;
    endDate = updatedProj.endDate;
    customerId = updatedProj.customerId;
    projectDescription 			= 
        updatedProj.projectDescription;
    projectStatus = updatedProj.projectStatus;
    started = updatedProj.started;
    completed = updatedProj.completed;
    accepted = updatedProj.accepted;
    closed = updatedProj.closed;
  }
  ...
}

Example 8.6 Multiple Value Objects Strategy – ResourceVO

// ResourceVO: This class holds basic information
// about the resource
public class ResourceVO implements 
  java.io.Serializable {
  public String resourceId;
  public String lastName;
  public String firstName;
  public String department;
  public String grade;
  ...
}

Example 8.7 Multiple Value Objects Strategy – ResourceDetailsVO

// ResourceDetailsVO This class holds detailed 
// information about resource
public class ResourceDetailsVO {
  public String resourceId;
  public String lastName;
  public String firstName;
  public String department;
  public String grade;
  // other data...
  public Collection commitments;
  public Collection blockoutTimes;
  public Collection skillSets;
}

Example 8.8 Multiple Value Objects Strategy – Resource Entity Bean

// imports
...
public class ResourceEntity implements EntityBean {
  // entity bean attributes
  ...

  // entity bean business methods
  ...

  // Multiple Value Object method : Get ResourceVO
  public ResourceVO getResourceData() {

    // create new ResourceVO instance and copy
    // attribute values from entity bean into VO
    ...
    return createResourceVO();
  }

  // Multiple Value Object method : Get 
  // ResourceDetailsVO
  public ResourceDetailsVO getResourceDetailsData() {

    // create new ResourceDetailsVO instance and copy
    // attribute values from entity bean into VO
    ...
    return createResourceDetailsVO(); 
  }

  // other entity bean methods
  ...
}

Example 8.9 Multiple Value Objects Strategy – Entity Bean Client

...
  private ResourceEntity resourceEntity;
  private static final Class homeClazz =

  corepatterns.apps.psa.ejb.ResourceEntityHome.class;
  ...
  try {
    ResourceEntityHome home =
      (ResourceEntityHome)
        ServiceLocator.getInstance().getHome(
            "Resource", homeClazz);
        resourceEntity = home.findByPrimaryKey( 
                            resourceId);
  } catch(ServiceLocatorException ex) {
    // Translate Service Locator exception into
    // application exception
    throw new ResourceException(...);
  } catch(FinderException ex) {
    // Translate the entity bean finder exception into
    // application exception
    throw new ResourceException(...);
  } catch(RemoteException ex) {
    // Translate the Remote exception into
    // application exception
    throw new ResourceException(...);
  }
  ...
  // retrieve basic Resource data 
  ResourceVO vo = resourceEntity.getResourceData();
  ...
  // retrieve detailed Resource data
  ResourceDetailsVO = 		
    resourceEntity.getResourceDetailsData();
  ...

Example 8.10 Entity Inherits Value Object Strategy – Value Object Class

// This is the value object class inherited by
// the entity bean
public class ContactVO 
  implements java.io.Serializable { 

  // public members
  public String firstName;
  public String lastName; 
  public String address;

  // default constructor
  public ContactVO() {}

  // constructor accepting all values
  public ContactVO(String firstName, 
    String lastName, String address){
      init(firstName, lastName, address);
  }

  // constructor to create a new VO based 
  // using an existing VO instance
  public ContactVO(ContactVO contact) {
    init (contact.firstName, 
      contact.lastName, contact.address);
  }

  // method to set all the values
  public void init(String firstName, String 
              lastName, String address) { 
    this.firstName = firstName; 
    this.lastName = lastName; 
    this.address = address;
  } 

  // create a new value object 
  public ContactVO getData() { 
    return new ContactVO(this);
  } 
}

Example 8.11 Entity Inherits Value Object Strategy – Entity Bean Class

public class ContactEntity extends ContactVO implements 
javax.ejb.EntityBean { 
  ...
  // the client calls the getData method
  // on the ContactEntity bean instance.
  // getData() is inherited from the value object
  // and returns the ContactVO value object
  ...
}

Example 8.12 Value Object Factory Strategy – Value Objects and Interfaces

public interface Contact 
  extends java.io.Serializable {
  public String getFirstName();
  public String getLastName();
  public String getContactAddress();
  public void setFirstName(String firstName);
  public void setLastName(String lastName);
  public void setContactAddress(String address);
}

public class ContactVO implements Contact {
  // member attributes
  public String firstName;
  public String lastName;
  public String contactAddress;

  // implement get and set methods per the 
  // Contact interface here. 
  ...
        }
public interface Customer 
  extends java.io.Serializable {
  public String getCustomerName();
  public String getCustomerAddress();
  public void setCustomerName(String customerName);
  public void setCustomerAddress(String 
      customerAddress);
}

public class CustomerVO implements Customer {
  public String customerName;
  public String customerAddress;

  // implement get and set methods per the 
  // Customer interface here.
  ...
}

public class CustomerContactVO implements Customer, 
  Contact {
  public String firstName;
  public String lastName;
  public String contactAddress;
  public String customerName;
  public String customerAddress;

  // implement get and set methods per the 
  // Customer and Contact interfaces here.
  ...
}
Ex. 8.1 to 8.12 | Ex 8.13 to 8.24 | Ex. 8.25 to 8.35