Code Listing 3: The full code listing of the LoanFlowPlusForms class

public class LoanFlowPlusForms
{

  public static String[] getLoanApplicationList(String pEmail)
  {
    
    String[] taskList = null;
    
    // Get the information needed to connect to the BPEL server.
    Properties props = new java.util.Properties();
    URL url = ClassLoader.getSystemResource("oc4j_context.properties");
    
    try
    {
      // Connect to the BPEL server.
      props.load(url.openStream());
      Locator locator = new Locator("default","bpel", props);
      
      // Get all tasks against our user.
      IWorklistService worklist = (IWorklistService)locator.lookupService( IWorklistService.SERVICE_NAME );
      ITask[] tasks = worklist.listTasksByAssignee(pEmail);
  
      // Loop through the tasks, and look for StarLoan ones as there may be
      // others, which we're not interested in here.
      ITask thisTask;
      String taskId;
      taskList = new String[tasks.length];
      for (int i=0; i<tasks.length; i++) 
      {
        thisTask = tasks[i];
        if (!thisTask.getCreator().equals("StarLoan"))
        {
          // This task has been generated by another BPEL process and should
          // therefore be ignored.
          continue;
        }
  
        // Get the unique id/key associated with this task. This will be
        // needed later when the loan officer approves or rejects the loan
        // associated with this task.
        taskId = thisTask.getTaskId();
  
        // Create the string to return to Forms.     
        taskList[i] = taskId+"|"+getLoanApplicationDetails(thisTask);
      }
      
    }
    catch (Exception e)
    {
      // Handle the error
    }
  
    return taskList;
  
  }
  
  //////////////////////////////////////////////////////////////////////////////
  
  // For the given task, get the information associated with it.
  private static String getLoanApplicationDetails(ITask pTask)
  {
    String customerName=null, ssn=null, email=null, creditRating=null;
    String carModel=null, carYear=null, loanAmount=null;
  
    try
    {
      // The loan details are kept in XML format in an attachment.
      Element e = (Element) pTask.getAttachment();
  
      // Now let's loop over the XML and get the information we want.
      NodeList nodelist = e.getChildNodes();
      Node node;
      for (int i=0; i<nodelist.getLength(); i++)
      {
        node = nodelist.item(i);
  
        if (node.getNodeName() != null)
        {
          if (node.getNodeName().equals("customerName"))
            customerName = node.getNodeValue();
          if (node.getNodeName().equals("email"))
            email = node.getNodeValue();
          if (node.getNodeName().equals("SSN"))
            ssn = node.getNodeValue();
          if (node.getNodeName().equals("creditRating"))
            creditRating = node.getNodeValue();
          if (node.getNodeName().equals("carModel"))
            carModel = node.getNodeValue();
          if (node.getNodeName().equals("carYear"))
            carYear = node.getNodeValue();
          if (node.getNodeName().equals("loanAmount"))
            loanAmount = node.getNodeValue();
        }
      }
    }
    catch (Exception e)
    {
      // Handle the error.
    }
    
    return customerName+"|"+email+"|"+ssn+"|"+creditRating+"|"+carModel+"|"
           + carYear+"|"+loanAmount;
  
  }
  
  //////////////////////////////////////////////////////////////////////////////
  
  public static void approveLoan(String pTaskId, double pApr)
  {
    processLoanApplication(pTaskId, pApr);
  }
  
  //////////////////////////////////////////////////////////////////////////////
  
  public static void rejectLoan(String pTaskId)
  {
    processLoanApplication(pTaskId, -1);
  }
  
  //////////////////////////////////////////////////////////////////////////////
  
  private static void processLoanApplication(String pTaskId, double pApr)
  {
    // Get the information needed to connect to the BPEL server.
    Properties props = new java.util.Properties();
    java.net.URL url = ClassLoader.getSystemResource("oc4j_context.properties");
    
    try
    {
      // Connect to the BPEL server.
      props.load(url.openStream());
      Locator locator = new Locator("default","bpel", props);
      
      // Get the task whose id was passed in in pTaskId.
      IWorklistService worklist = (IWorklistService)locator.lookupService( IWorklistService.SERVICE_NAME );
      ITask task = worklist.lookupTask(pTaskId);
  
      String approved   = "true";
      String conclusion = "Approved";
  
      // If pApr is -1 then the loan wasn't approved.
      if (pApr == -1)
      {
        approved   = "false";
        conclusion = "Rejected";
      }
  
      // Create our attachment, set the conclusion, and complete the task.
      createLoanTaskAttachment(task, approved, pApr);
      task.setConclusion(conclusion);
      worklist.completeTask(task);
  
    }
    catch (Exception e)
    {
      // Handle the error
    }
  
  }
  
  //////////////////////////////////////////////////////////////////////////////
  
  private static void createLoanTaskAttachment(ITask pTask, String pApproved, double pApr)
  {
  
    try
    {
      // Create a new attachment to hold our XML.
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = null;
      builder = factory.newDocumentBuilder();
      Document doc = builder.newDocument();
  
      // Build the XML.
      Element root = (Element)doc.createElement("loanOffer");
      root.setAttribute("xmlns", "http://www.autoloan.com/ns/autoloan");
      doc.appendChild(root);
      
      appendElement(doc, root, "providerName", ""                  );
      appendElement(doc, root, "selected"    , "false"             );
      appendElement(doc, root, "approved"    , pApproved           );
      appendElement(doc, root, "APR"         , String.valueOf(pApr));
  
      // Make our XML into an attachment.
      pTask.setAttachment(XMLUtils.convertToCollaxaElement(root));
         
    }
    catch (Exception e)
    {
      // Handle the error
    }
    
  }
  
  ////////////////////////////////////////////////////////////////
  
  public static void appendElement
  (
    Document pDoc
  , Element pRoot
  , String pNodeName
  , String pNodeValue
  )
  {
    Element element;
    Text text;
  
    element = (Element)pDoc.createElement(pNodeName);
    pRoot.appendChild(element);
    text = pDoc.createTextNode("#text");
    text.setNodeValue(pNodeValue);
    element.appendChild(text);
  }

}