SampleApp.m Example Code

SampleApp.m contains this example code.

SampleApp.m

#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
+(void)subscribeToDB
{
  [SampleApp_SampleAppDB subscribe];
}

+(void)beginLogin
{
  if ([SampleApp_SampleAppDB getOnlineLoginStatus].status != SUPLoginSuccess) {
      [SampleApp_SampleAppDB beginOnlineLogin:@"supAdmin" password:@"s3pAdmin"];
  }
}

+(void)runAPITests
{
  MBOLog(@"=============================");
  MBOLog(@"TestPersonalizationParameters");   
  MBOLog(@"=============================");
  [SampleApp TestPersonalizationParameters];
	
  MBOLog(@"=============================");
  MBOLog(@"TestSynchronizationParameters");   
  MBOLog(@"=============================");
  [SampleApp TestSynchronizationParameters];
	
  MBOLog(@"=============================");
  MBOLog(@"TestfindAll");   
  MBOLog(@"=============================");
  [SampleApp Testfind];
	
  MBOLog(@"=============================");
  MBOLog(@"TestCreate");   
  MBOLog(@"=============================");
  [SampleApp TestCreate];
	
  MBOLog(@"=============================");
  MBOLog(@"TestUpdate");   
  MBOLog(@"=============================");
  [SampleApp TestUpdate];
	
  MBOLog(@"=============================");
  MBOLog(@"TestDelete");   
  MBOLog(@"=============================");
  [SampleApp TestDelete];
	
  MBOLog(@"=============================");
  MBOLog(@"Print Logs");   
  MBOLog(@"=============================");
  [SampleApp printLogs];
	
  [SampleApp_SampleAppDB unsubscribe];
}

+(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.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 autorelease];  
  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 autorelease];
  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