Device Application Example Code

The example code consists of five files.

main.m

#import <UIKit/UIKit.h>
#import "SampleApp.h"


int main(int argc, char *argv[]) {


  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
  SampleApp *app = [SampleApp getInstance];
  [app run];
  
  int retVal = UIApplicationMain(argc, argv, nil, nil);
  [pool release];
  return retVal;
}
#import <Foundation/Foundation.h>
#import "SUPDefaultCallbackHandler.h"

@interface CallbackHandler : SUPDefaultCallbackHandler
{
	SUPInt field_importCount;
	SUPInt field_replaySuccessCount;
	SUPInt field_replayFailureCount;
	SUPInt field_loginSuccessCount;
	SUPInt field_loginFailureCount;
	SUPInt field_importSuccessCount;
	
}

+ (CallbackHandler*)getInstance;
- (CallbackHandler*)init;
- (SUPInt)importCount;
- (void)setImportCount:(SUPInt)_importCount;
@property(assign) SUPInt importCount;
- (SUPInt)replaySuccessCount;
- (void)setReplaySuccessCount:(SUPInt)_replaySuccessCount;
@property(assign) SUPInt replaySuccessCount;
- (SUPInt)replayFailureCount;
- (void)setReplayFailureCount:(SUPInt)_replayFailureCount;
@property(assign) SUPInt replayFailureCount;
- (SUPInt)loginSuccessCount;
- (void)setLoginSuccessCount:(SUPInt)_loginSuccessCount;
@property(assign) SUPInt loginSuccessCount;
- (SUPInt)loginFailureCount;
- (void)setLoginFailureCount:(SUPInt)_loginFailureCount;
@property(assign) SUPInt loginFailureCount;
- (SUPInt)importSuccessCount;
- (void)setImportSuccessCount:(SUPInt)_importSuccessCount;
@property(assign) SUPInt importSuccessCount;

- (void)onImport:(id)theObject;
- (void)onReplayFailure:(id)theObject;
- (void)onReplaySuccess:(id)theObject;
- (void)onLoginFailure;
- (void)onLoginSuccess;
- (void)onSubscribeSuccess;
- (void)onSubscribeFailure;
- (void)onImportSuccess;
- (CallbackHandler*)finishInit;
- (void)initFields;
+ (void)staticInit;
- (void)dealloc;

@end
#import "CallbackHandler.h"

@implementation CallbackHandler
+ (CallbackHandler*)getInstance
{
  CallbackHandler* _me_1 = [[CallbackHandler alloc] init];
  [_me_1 autorelease];
  return _me_1;
}

- (CallbackHandler*)init
{
  [CallbackHandler staticInit];
  [self initFields];
  return self;
}

- (SUPInt)importCount
{
  return field_importCount;
}

- (void)setImportCount:(SUPInt)_importCount
{
  field_importCount = _importCount;
}

- (SUPInt)replaySuccessCount
{
  return field_replaySuccessCount;
}

- (void)setReplaySuccessCount:(SUPInt)_replaySuccessCount
{
  field_replaySuccessCount = _replaySuccessCount;
}

- (SUPInt)replayFailureCount
{
  return field_replayFailureCount;
}

- (void)setReplayFailureCount:(SUPInt)_replayFailureCount
{
  field_replayFailureCount = _replayFailureCount;
}

- (SUPInt)loginSuccessCount
{
  return field_loginSuccessCount;
}

- (void)setLoginSuccessCount:(SUPInt)_loginSuccessCount
{
	field_loginSuccessCount = _loginSuccessCount;
}

- (SUPInt)loginFailureCount
{
	return field_loginFailureCount;
}

- (void)setLoginFailureCount:(SUPInt)_loginFailureCount
{
	field_loginFailureCount = _loginFailureCount;
}

- (SUPInt)importSuccessCount
{
	return field_importSuccessCount;
}

- (SUPInt)importSuccessCount
{
  return field_importSuccessCount;
}

- (void)setImportSuccessCount:(SUPInt)_importSuccessCount
{
  Field_importSuccessCount = _importSuccessCount;
}


- (void)onImport:(id)theObject
{
  self.importCount = self.importCount + 1;
}

- (void)onReplayFailure:(id)theObject
{
  self.replayFailureCount = self.replayFailureCount + 1;
  MBOLog(@"=============================");
  MBOLogError(@"Replay Failed");
  MBOLog(@"=============================");
}

- (void)onReplaySuccess:(id)theObject
{
  self.replaySuccessCount = self.replaySuccessCount + 1;
  MBOLog(@"=============================");
  MBOLog(@"Replay Successful");
  MBOLog(@"=============================");
}

- (void)onLoginFailure
{
	MBOLog(@"=============================");
	MBOLogError(@"Login Failed");
	MBOLog(@"=============================");
	self.loginFailureCount++;
}

- (void)onLoginSuccess
{
  MBOLog(@"=============================");
  MBOLog(@"Login Successful");
  MBOLog(@"=============================");
  self.loginSuccessCount++;
}

- (void)onSubscribeSuccess
{
  MBOLog(@"=============================");
  MBOLog(@"Subscribe Successful");
  MBOLog(@"=============================");
  
}
-(void)onSubscribeFailure
{
  MBOLog(@"=============================");
  MBOLogError(@"Subscribe Failed");
  MBOLog(@"=============================");
}

- (void)onImportSuccess
{
  MBOLog(@"=============================");
  MBOLog(@"Import Ends Successfully");
  MBOLog(@"=============================");
  self.importSuccessCount++;
}


- (CallbackHandler*)finishInit
{
  return self;
}

- (void)initFields
{
}

+ (void)staticInit
{
}

- (void)dealloc
{
  [super dealloc];
}

@end
@interface SampleApp: NSObject
{
}

+ (SampleApp*)getInstance;
- (SampleApp*)init;
- (void)run;
- (SampleApp*)finishInit;
- (void)initFields;
+ (void)staticInit;
- (void)dealloc;
-(void)runAPITests;


/*Test functions that call Client Object APIs */

-(void)Testfind;
-(void)TestSynchronizationParameters;
-(void)TestPersonalizationParameters;
-(void)TestCreate;
-(void)TestUpdate;
-(void)TestDelete;
-(void)printLogs;
-(void)PrintCustomerSalesOrderData;

@end
#import "SampleApp.h"
#import "SampleApp_Customer.h"
#import "CallbackHandler.h"
#import "SampleApp_SampleAppDB.h"
#import "SampleApp_LogRecordImpl.h"
#import "SampleApp_Sales_order.h"
#import "SampleApp_LocalKeyGenerator.h";
#import "SampleApp_KeyGenerator.h"
#import "SUPMessageClient.h"

@implementation SampleApp

+ (SampleApp*)getInstance
{
  SampleApp* _me_1 = [[SampleApp alloc] init];
  [_me_1 autorelease];
  return _me_1;
}

- (SampleApp*)init
{
  [SampleApp staticInit];
  [self initFields];
  return self;
}


- (void)run
{
   NSInteger connectionResult ;
  
  // Set log level 
  [MBOLogger setLogLevel:LOG_INFO];

  //Delete the old database and create a new one.
  if([SampleApp_SampleAppDB databaseExists])
  [SampleApp_SampleAppDB deleteDatabase];
  [SampleApp_SampleAppDB createDatabase];
   
// Set up synchronization profile .
  SUPConnectionProfile* cp = [SampleApp_SampleAppDB getSynchronizationProfile];
  [cp setDomainName:@"default"];
  
  //Register a callback handler.
  CallbackHandler* databaseCH = [CallbackHandler getInstance];
  [SampleApp_SampleAppDB registerCallbackHandler:databaseCH];
  
  //Start backgroundsynchronization.
  [SampleApp_SampleAppDB startBackgroundSynchronization];
 //Connect to the server
  connectionResult = [SUPMessageClient start];

  if(connectionResult == kSUPMessageClientSuccess)
  
  {
		  NSLog(@"Cannot start SUPMessageClient");
		  exit(0);
 	} 
    while([SUPMessageClient status] != STATUS_START_CONNECTED) [NSThread sleepForTimeInterval:0.2];
    [SampleApp_SampleAppDB beginOnlineLogin:@"supUser" password:@"s3pUser"];
    while([SampleApp_SampleAppDB getOnlineLoginStatus].status == SUPLoginPending)
	 {
		  [NSThread sleepForTimeInterval:0.2];
		  if(databaseCH.loginFailureCount > 0)
		  {
			  NSLog(@"SampleApp_SampleAppDB login failed.");
			  exit(0);
		  }
  	}

    //Subscribe to the package.
    [SampleApp_SampleAppDB subscribe];

    // Wait for imports to come back from server
    while([databaseCH importSuccessCount] < 1)
    [NSThread sleepForTimeInterval:0.2];
        
    //Call the functions that execute the client APIs for synchronization 
    //parameters, personalization keys read, create, update and delete
    [self runAPITests]; 
    // Unsubscribe
    [SampleApp_SampleAppDB unsubscribe];
    //Disconnect from server.
    [SUPMessageClient stop];
            
  }
}

- (SampleApp*)finishInit
{
  return self;
}

- (void)initFields
{
}

+ (void)staticInit
{
}

- (void)dealloc
{
  [super dealloc];
}


-(void)runAPITests
{
  MBOLog(@"=============================");
  MBOLog(@"TestPersonalizationParameters");   
  MBOLog(@"=============================");
  [self TestPersonalizationParameters];

  MBOLog(@"=============================");
  MBOLog(@"TestSynchronizationParameters");   
  MBOLog(@"=============================");
  [self TestSynchronizationParameters];
  
  MBOLog(@"=============================");
  MBOLog(@"TestfindAll");   
  MBOLog(@"=============================");
  [self Testfind];
  
  MBOLog(@"=============================");
  MBOLog(@"TestCreate");   
  MBOLog(@"=============================");
  [self TestCreate];
  
  MBOLog(@"=============================");
  MBOLog(@"TestUpdate");   
  MBOLog(@"=============================");
  [self TestUpdate];
  
  MBOLog(@"=============================");
  MBOLog(@"TestDelete");   
  MBOLog(@"=============================");
  [self TestDelete];
  
  MBOLog(@"=============================");
  MBOLog(@"Print Logs");   
  MBOLog(@"=============================");
  [self printLogs];

}

-(void)PrintCustomerSalesOrderData
{
	SampleApp_Customer *onecustomer = nil;
	SUPObjectList *cl = nil;
	MBOLog(@"Customer data is:");
	cl = [SampleApp_Customer findAll];
  
  if(cl &&  [cl length] > 0 )
  {
    int i;
    for(i=0; i<[cl length]; i++)
    {
      onecustomer = [cl item:i];
      if (onecustomer) {
        MBOLog(@"%@ %@, %@, %@, %@",onecustomer.fname,
        onecustomer.lname,onecustomer.address,onecustomer.city,
        onecustomer.state);
        SUPObjectList *sl = [onecustomer salesOrders];
				    if(sl)
			    	{
					     if([sl length] > 0)
            MBOLog(@"   This customer's sales orders are");
          else
            MBOLog(@"   This customer has no sales orders");
          for(SampleApp_Sales_order *so in sl)
            MBOLog(@"%@ %@,%d",so.order_date,so.region,so.sales_rep);
        }
      }     
    }
    
  }
}


/***Retrieve data based on the synchronization parameter value.***/
 
- (void)TestSynchronizationParameters
{
  SampleApp_CustomerSynchronizationParameters* sp 
    = [SampleApp_Customer getSynchronizationParameters];
  
  sp.size = 3;
  sp.user = @"userone";
  sp.param_city = @"Raleigh";
  [sp save];
  while ([SampleApp_SampleAppDB hasPendingOperations])
  {
    [NSThread sleepForTimeInterval:1];
  }     

  [self PrintCustomerSalesOrderData];
}
    


/******Retrieve data based on the personalization parameter value*****/

- (void)TestPersonalizationParameters
{
    
  SampleApp_PersonalizationParameters *pp = nil;
  pp = [SampleApp_SampleAppDB getPersonalizationParameters];

  pp.PKCity = @"New York";
  [pp save];
  while ([SampleApp_SampleAppDB hasPendingOperations])
  {
    [NSThread sleepForTimeInterval:1];
  }
  [self PrintCustomerSalesOrderData];
}

/*******Print logs record data from LogrecordImpl*****/
-(void)printLogs
{
  MBOLog(@"******* printLogs *******");
  SUPQuery *query = [SUPQuery getInstance];
  SUPObjectList *loglist = [SampleApp_SampleAppDB getLogRecords:query];

  for(id o in loglist)
  {
    SampleApp_LogRecordImpl *log = (SampleApp_LogRecordImpl*)o;
    MBOLog(@"Log Record %llu: Operation = %@, Timestamp = %@, MBO = %@, 
      key = %@, message = %@",log.messageId,log.operation,
    [SUPDateTimeUtil toString:log.timestamp],log.component,log.entityKey,log.message);
  }
}


/************************************find************************************/
/***Find all the customer records and print the first record to the console*/

-(void)Testfind
{
  SampleApp_Customer *onecustomer = nil;
  SUPObjectList *cl = [SampleApp_Customer findAll];
  if(cl && [cl length] > 0 )
  {
    onecustomer = [cl item:0];
    if (onecustomer)
    {    
      MBOLog(@"the full customer record data is : %@", onecustomer);
    }
  }

}


/*****************************Create ***********************************/
/*****Create new customer and sales order records in the local database
       and call submitPending to send the changes  to the server *****/

-(void)TestCreate
{
  long key1 = [SampleApp_KeyGenerator generateId];
  long key2 = [SampleApp_KeyGenerator generateId];
  [SampleApp_KeyGenerator submitPendingOperations];
  while ([SampleApp_SampleAppDB hasPendingOperations])
  {
    [NSThread sleepForTimeInterval:1];
  }     
  SampleApp_Customer *c = [[ SampleApp_Customer alloc] init];

  c.id_ = [SampleApp_LocalKeyGenerator generateId];
  c.fname = @"Dorothi"; 
  c.lname = @"Scranton";
  c.address = @"One Money Street";
  c.city = @"smallVille";
  c.state = @"MA";
  c.zip = @"97429";
  c.phone = @"2112222345";
  c.company_name = @"iAnywhere";
  c.surrogateKey = key1;
  SUPObjectList *orderlist = [ SampleApp_Sales_orderList getInstance];
  SampleApp_Sales_order *o1 = [[SampleApp_Sales_order alloc] init];

  o1.id_ = [SampleApp_LocalKeyGenerator generateId];
  o1.order_date = [NSDate date];
  o1.fin_code_id = @"r1";
	 o1.region = @"Eastern";
	 o1.sales_rep = 902;
	 o1.surrogateKey = key2;
	 [ o1 setCustomer:c];
	 [orderlist add:o1];
	 [c setSalesOrders:orderlist];
	 [c save];
	 [c refresh];
	 [c submitPending];
	 assert(c.pending == YES);
	 while ([SampleApp_SampleAppDB hasPendingOperations])
	 {
	 	[NSThread sleepForTimeInterval:1];
	 }     
 }


/******************************Update *********************************/
/****Update an existing customer and sales record in the device database
  and call submitPending to send the changes to the server. **********/

- (void)TestUpdate
{
  SUPObjectList *cl = [SampleApp_Customer findAll];
  SampleApp_Customer *onecustomer = [cl item:0];
  SampleApp_Sales_order *order = [onecustomer.salesOrders item:0];

  onecustomer.fname = @"Johnny";    
  order.region = @"South";
  [onecustomer save];
  [onecustomer refresh];
  [order refresh];
  [onecustomer submitPending];
  while ([SampleApp_SampleAppDB hasPendingOperations])
  {
    [NSThread sleepForTimeInterval:1];
  }     
    

}

/***************************** Delete ***************************/
/*Delete an existing record from the database and call 
  submitPending to send the changes to the server.****/

-(void) TestDelete
{
  SUPObjectList *sl = [SampleApp_Sales_order findAll];
  SampleApp_Sales_order *order = [sl item:0];
  [order delete];
  [order.customer submitPending];
  while ([SampleApp_SampleAppDB hasPendingOperations])
  {
    [NSThread sleepForTimeInterval:1];
  }   
}



@end