Complete MobiLinkOrders code listing (Java)

The following listing shows the complete Java MobiLinkOrders class listing used for this tutorial.

import ianywhere.ml.script.*;
import java.io.*;
import java.sql.*;

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;  

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

// For write operation
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;  
import javax.xml.transform.stream.StreamResult; 

public class MobiLinkOrders {

  // class level DBConnectionContext
  DBConnectionContext _cc;
  Document _doc;
  
  public MobiLinkOrders( DBConnectionContext cc ) throws IOException, FileNotFoundException {
    // declare a class-level DBConnectionContext
    _cc = cc;
  }


  //  method for the handle_UploadData synchronization event
  public void GetUpload( UploadData ut ) throws SQLException, IOException {
    // Get an UploadedTableData for the remote table
    UploadedTableData remoteOrdersTable = ut.getUploadedTableByName("OrderComments");

    // Get inserts uploaded by the MobiLink client 
    // as a java.sql.ResultSet    
    ResultSet insertResultSet = remoteOrdersTable.getInserts();
    
    readDom("order_comments.xml");
    
    // Write out each insert in the XML file
    while( insertResultSet.next() ) {  
      buildXML(insertResultSet);
    }
    writeXML();

    // Close the result set of uploaded inserts
    insertResultSet.close();  
  
  }
  
  private void buildXML( ResultSet rs ) throws SQLException {  
    int order_id = rs.getInt(1);
    int comment_id = rs.getInt(2);
    String order_comment = rs.getString(3);    

    //Create the comment object to be added to the XML file
    Element comment = _doc.createElement("comment");
    comment.setAttribute("id", Integer.toString(comment_id));
    comment.appendChild(_doc.createTextNode(order_comment));

    //Get the root element (orders)
    Element root = _doc.getDocumentElement();
    
    //get each individual order
    NodeList rootChildren = root.getChildNodes();
    for(int i = 0; i < rootChildren.getLength(); i++) {
      //if the order exists, add the comment to the order      
      Node n = rootChildren.item(i);
      if(n.getNodeType() == Node.ELEMENT_NODE) {
        Element e = (Element) n;
        int idIntVal = Integer.parseInt(e.getAttribute("id"));
        if(idIntVal == order_id) {
          e.appendChild(comment);
          //The comment has been added to the file, so exit the function
          return;
        }
      }
    }

    //if the order did not exist already, create it
    Element order = _doc.createElement("order");
    order.setAttribute("id", Integer.toString(order_id));
    //add the comment to the new order
    order.appendChild(comment);    
    root.appendChild(order);
  }

  private void writeXML() {

    try {
 
      // Use a Transformer for output
      TransformerFactory tFactory = TransformerFactory.newInstance();
      Transformer transformer = tFactory.newTransformer();
 
      //The XML source is _doc
      DOMSource source = new DOMSource(_doc);
      //write the xml data to order_comments.xml
      StreamResult result = new StreamResult(new File("order_comments.xml"));
      transformer.transform(source, result);
           
    } catch (TransformerConfigurationException tce) {
      // Error generated by the parser
      System.out.println ("\n** Transformer Factory error");
      System.out.println("   " + tce.getMessage() );

      // Use the contained exception, if any
      Throwable x = tce;
      if (tce.getException() != null) x = tce.getException();
      x.printStackTrace();
      
    } catch (TransformerException te) {
      // Error generated by the parser
      System.out.println ("\n** Transformation error");
      System.out.println("   " + te.getMessage() );

      // Use the contained exception, if any
      Throwable x = te;
      if (te.getException() != null) x = te.getException();
      x.printStackTrace();
           
 }

  }

  private void readDom(String filename) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      try {
        //parse the Document data into _doc
        DocumentBuilder builder = factory.newDocumentBuilder();
        _doc = builder.parse( new File(filename) );
 
      } catch (SAXException sxe) {
        // Error generated during parsing)
        Exception x = sxe;
        if (sxe.getException() != null) x = sxe.getException();
        x.printStackTrace();

      } catch (ParserConfigurationException pce) {
        // Parser with specified options can't be built
        pce.printStackTrace();

      } catch (IOException ioe) {
        // I/O error
        ioe.printStackTrace();
      }

  }

}