Complete MobiLinkOrders listing (.NET)

The following is the complete MobiLinkOrders listing for .NET object-based uploads and downloads. For a step by step explanation, see Lesson 3: Write Java or .NET logic for processing direct row handling.

using iAnywhere.MobiLink.Script;
using System.IO;
using System.Data;
using System.Text;

public class MobiLinkOrders
{
    // class level DBConnectionContext
    private DBConnectionContext _cc = null;

    // instances for file I/O
    private static StreamWriter my_writer = null;
    private static StreamReader my_reader = null;


    public MobiLinkOrders(DBConnectionContext cc)
    {
        _cc = cc;
    }

    public void WriteOrderComment(int comment_id,
        int order_id,
        string comments)
    {
        if (my_writer == null)
        {
            my_writer = new StreamWriter(
                "c:\\MLdirect\\orderComments.txt");
        }
        my_writer.WriteLine("{0}\t{1}\t{2}",
            comment_id, order_id, comments);
        my_writer.Flush();
    }

    // method for the handle_UploadData synchronization event.
    public void GetUpload(UploadData ut)
    {
        // get UploadedTableData for remote table called OrderComments
        UploadedTableData order_comments_table_data =
            ut.GetUploadedTableByName("OrderComments");

        // get inserts uploaded by the MobiLink client
        IDataReader new_comment_reader =
            order_comments_table_data.GetInserts();

        while (new_comment_reader.Read())
        {
            // Columns are
            // 0 - "order_comment"
            // 1 - "comment_id"
            // 2 - "order_id"
            // You can look up these values using the DataTable returned by:
            // order_comments_table_data.GetSchemaTable() if the send 
            // column names option is turned on at the remote. 
            // In this example, you just use the known column order to
            // determine the column indexes

            // Only process this insert if the order_comment is not null
            if (!new_comment_reader.IsDBNull(2))
            {
                int comment_id = new_comment_reader.GetInt32(0);
                int order_id = new_comment_reader.GetInt32(1);
                string comments = new_comment_reader.GetString(2);
                WriteOrderComment(comment_id, order_id, comments);
            }
        }
        // Always close the reader when you are done with it!
        new_comment_reader.Close();
    }

    private const string read_file_path =
        "c:\\MLdirect\\orderResponses.txt";

    // method for the handle_DownloadData synchronization event
    public void SetDownload()
    {
        if ((my_reader == null) && !File.Exists(read_file_path))
        {
            System.Console.Out.Write("There is no file to read.");
            return;
        }
        DownloadTableData comments_for_download =
            _cc.GetDownloadData().GetDownloadTableByName("OrderComments");

        // Add upserts to the set of operation that are going to be 
        // applied at the remote database
        IDbCommand comments_upsert =
            comments_for_download.GetUpsertCommand();

        if (my_reader == null)
        {
            my_reader = new StreamReader(read_file_path);
        }
        string comment_line;
        while ((comment_line = my_reader.ReadLine()) != null)
        {
            // three values are on each line separated by '\t'
            string[] response_details = comment_line.Split('\t');
            if (response_details.Length != 3)
            {
                throw (new SynchronizationException(
                    "Error reading from orderResponses.txt"));
            }
            int comment_id = System.Int32.Parse(response_details[0]);
            int order_id = System.Int32.Parse(response_details[1]);
            string comments = response_details[2];

            // Parameters of the correct number and type have 
            // already been added so you just need to set the 
            // values of the IDataParameters
            ((IDataParameter)(comments_upsert.Parameters[0])).Value =
                comment_id;
            ((IDataParameter)(comments_upsert.Parameters[1])).Value =
                order_id;
            ((IDataParameter)(comments_upsert.Parameters[2])).Value =
                comments;
            // Add the upsert operation
            comments_upsert.ExecuteNonQuery();
        }
    }

    public void EndDownload()
    {
        if (my_writer != null)
        {
            my_writer.Close();
            my_writer = null;
        }
        if (my_reader != null)
        {
            my_reader.Close();
            my_reader = null;
        }
    }
}