Complete MobiLinkOrders code listing (Java)

The following is the complete MobiLinkOrders listing for Java direct row handling. For a step by step explanation, see Lesson 5: Creating a Java or .NET class for MobiLink direct row handling.



import ianywhere.ml.script.*;
    import java.io.*;
    import java.sql.*;
    
    public class MobiLinkOrders {
    
    // Class level DBConnectionContext
    DBConnectionContext _cc;
        
    // Java objects for file i/o
    FileWriter my_writer;
    BufferedReader my_reader;

    public MobiLinkOrders( DBConnectionContext cc )
        throws IOException, FileNotFoundException
    {
        // Declare a class-level DBConnectionContext
        _cc = cc; 
    }
    
    public void writeOrderComment( int _commentID, int _orderID, String _comments ) 
        throws IOException
    {
        if (my_writer == null)
            // A FileWriter for writing order comments
            my_writer = new FileWriter( "C:\\MLdirect\\orderResponses.txt",true);
              
        // Write out the order comments to remoteOrderComments.txt
        my_writer.write(_commentID + "\t" + _orderID + "\t" + _comments);
        my_writer.write( "\n" );
        my_writer.flush();
    }
    
    //  Method for the handle_UploadData synchronization event
    public void GetUpload( UploadData ut ) 
        throws SQLException, IOException
    {
        // Get an UploadedTableData for OrderComments
        UploadedTableData orderCommentsTbl = ut.getUploadedTableByName("OrderComments");
         
        // Get inserts uploaded by the MobiLink client
        ResultSet insertResultSet = orderCommentsTbl.getInserts();
         
        while ( insertResultSet.next() ) 
        {
            // Get order comments
            int _commentID = insertResultSet.getInt("comment_id");
            int _orderID = insertResultSet.getInt("order_id");
            String _specialComments = insertResultSet.getString("order_comment");              
            if (_specialComments != null) {
                writeOrderComment(_commentID,_orderID,_specialComments);
            }
        }
        insertResultSet.close();
    }
        
    public void SetDownload() 
        throws SQLException, IOException
    {
        DownloadData download_d = _cc.getDownloadData();
         
        DownloadTableData download_td = download_d.getDownloadTableByName( "OrderComments" );
          
        PreparedStatement update_ps = download_td.getUpsertPreparedStatement();
	try {
	    // A BufferedReader for reading in responses
	    if (my_reader == null)
		my_reader = new BufferedReader(new FileReader("C:\\MLdirect\\orderResponses.txt"));
	    
	    // Get the next line from orderResponses
	    String commentLine;
	    commentLine = my_reader.readLine();
	    
	    // Send comment responses down to clients
	    while (commentLine != null) {
		// Get the next line from orderResponses.txt
		String[] response_details = commentLine.split("\t");
		
		if (response_details.length != 3) {
		    System.err.println("Error reading from orderResponses.txt");
		    System.err.println("Error setting direct row handling download");
		    return;
		}
		int comment_id = Integer.parseInt(response_details[0]);
		int order_id = Integer.parseInt(response_details[1]);
		String updated_comment = response_details[2];
		
		// Set an order comment response in the MobiLink download
		update_ps.setInt(1, comment_id);
		update_ps.setInt(2, order_id);
		update_ps.setString(3, updated_comment);
		update_ps.executeUpdate();
		
		// Get next line
		commentLine = my_reader.readLine();
	    }
	} finally {
	    update_ps.close();
	}
    }
        
    public void EndDownload() 
        throws IOException
    {
        // Close i/o resources
        if (my_reader != null) {
            my_reader.close();
            my_reader = null;
        }
        if (my_writer != null) {
            my_writer.close();
            my_writer = null;
        }
    }
}