Device Application Example Code

Example code for an object API-based client application.

package test;

import com.sybase.persistence.*;
import com.sybase.collections.*;

class MyCallbackHandler extends DefaultCallbackHandler
{
  public int onSynchronize(ObjectList groups, 
    com.sybase.persistence.SynchronizationContext context)
  {
     System.out.println("OnSynchronize is called with user context: " + context.getUserContext());
     return SynchronizationAction.CONTINUE;
  }
}

       
public class JavaMain extends net.rim.device.api.ui.UiApplication{
  public JavaMain() {    }
    
  public static void main(String [] args) throws Exception
  {
    //Configure synchronization profile to point to your host and port
    TestDB.getSynchronizationProfile().setServerName( "localhost" );
    TestDB.getSynchronizationProfile().setPortNumber(2480);

    //If the application requires a callback (for example, to allow the client framework to provide
    //notification of synchronization results),register the callback
    //function after setting up the connection profile, by executing
    MyCallbackHandler callback = new MyCallbackHandler();
            TestDB.registerCallbackHandler(callback);

    //Login to Unwired Server. This step is required for application initialization.
    TestDB.loginToSync("supAdmin", "s3pAdmin");

    //Subscribe to Unwired Server. Unwired Server creates a subscription for this particular application.
    TestDB.subscribe();

    //Synchronize with Unwired Server. Synchronization uploads all the local changes and downloads new data with related subscriptions.
    ObjectList sgs = new ObjectList();
    sgs.add(TestDB.getSynchronizationGroup("default"));
    TestDB.beginSynchronize(sgs, "mycontext");

    //Wait for synchronization to complete. May not be required for a typical UI application.
    Thread.sleep(1000*10);

    //List all customer MBO instances from the local database using a named query. FindAll is a pre-defined object query. 
    //Alternatively you can use Dynamic Query to MBO instances too.
    ObjectList customers = Customer.findAll();
    for(int i = 0; i < customers.size(); i++)
    {
      Customer customer = (Customer)(customers.elementAt(i));
      System.out.println("customer: " + customer.getFname() + " " + customer.getLname() 
      + " " + customer.getId()  + customer.getCity());
     }

     //Find a particular MBO instance.
     Customer cust = Customer.findByPrimaryKey(441);
     cust.setAddress( "1 Sybase Dr.");
     cust.setPhone( "9252360000");

     //Update the customer MBO instance. Save to the local database.
     cust.save();
     //Submit the pending changes. The changes are ready for upload, but have not yet been uploaded to the Unwired Server. 
     cust.submitPending();

     //Upload the pending changes to the Unwired Server and get the replay results and all the changed MBO instances.
     TestDB.beginSynchronize(sgs, "mycontext");

      //Unsubscribe the device application if the application is no longer used. 
      TestDB.unsubscribe();
  }
}