home *** CD-ROM | disk | FTP | other *** search
/ NeXT Enterprise Objects Framework 1.1 / NeXT Enterprise Objects Framework 1.1.iso / NextDeveloper / Examples / EnterpriseObjects / SHLExamples / DataSource / DataSource.m < prev    next >
Encoding:
Text File  |  1994-08-23  |  5.3 KB  |  199 lines

  1. /*--------------------------------------------------------------------------
  2.  *
  3.  *     You may freely copy, distribute, and reuse the code in this example.
  4.  *     SHL Systemhouse disclaims any warranty of any kind, expressed or  
  5.  *    implied, as to its fitness for any particular use.
  6.  *
  7.  *
  8.  *    DataSource
  9.  *
  10.  *    Inherits From:        NSObject
  11.  *
  12.  *    Conforms To:        EOQualifiedDataSources, EORollbackDataSources
  13.  *
  14.  *    Declared In:        DataSource.h
  15.  *
  16.  *
  17.  *------------------------------------------------------------------------*/
  18. #import "DataSource.h"
  19. #import "Motorcycle.h"
  20. #import <foundation/NSUtilities.h>
  21. #import <appkit/Application.h>
  22.  
  23. #define print_trace [[NXApp delegate] perform: @selector (console:) with:\
  24. [NSString stringWithFormat: @"DATASOURCE  %s\n", sel_getName(_cmd)]]
  25.  
  26.  
  27.  
  28.  
  29. @implementation DataSource
  30.  
  31. /*--------------------------------------------------------------------------
  32.  *    EODataSources Protocol
  33.  *
  34.  *  The EODataSources protocol defines the interface for a source of
  35.  *  data-bearing objects retrieved from some external store, such as an RDBMS.
  36.  *  EODataSources uses a simple insert/delete/update/fetch model.
  37.  * 
  38.  *  Changes to the objects provided by a data source are made in two phases.
  39.  *  First, you can modify an object independently of the data source.  These
  40.  *  changes don't affect the external store until you send an -insertObject:,
  41.  *  -deleteObject:, or -updateObject: message.  For example, if you release an
  42.  *  object you received from the data source, it isn't deleted from the
  43.  *  external store.  Invoking one of the messages listed sends the changes
  44.  *  associated with the object to the external store.  You must invoke
  45.  *  -saveObjects to make your changes permanent.  If an external store supports
  46.  *  rolling back of changes you can invoke -rollback (declared in the
  47.  *  EORollbackDataSources protocol) to undo the changes made since the last
  48.  *  -saveObjects message.
  49.  *
  50.  *------------------------------------------------------------------------*/
  51. - (NSArray *) keys
  52. {
  53.     // Returns the names of the keys that describe the data-bearing objects.
  54.  
  55.     print_trace;
  56.     return [NSArray arrayWithObject: @"Model"];
  57. }
  58.  
  59.  
  60. - createObject
  61. {
  62.     // Returns a new data bearing object with no values set, or nil if the
  63.     // data source won't allow object insertion.  You're responsible for
  64.     // assigning a proper primary key.
  65.  
  66.     print_trace;
  67.     return [[Motorcycle alloc] init];
  68. }
  69.  
  70. - coerceValue: value forKey: (NSString *)key
  71. {
  72.     return value;
  73. }
  74.  
  75. - (BOOL) insertObject: object
  76. {
  77.     // Inserts object into the data source.  Returns YES on success, NO on
  78.     // failure for any reason.
  79.  
  80.     print_trace;
  81.  
  82.     if ([object isKindOf: [Motorcycle class]] == NO)  return NO;
  83.  
  84.     if (workingStore == nil) 
  85.         workingStore = [[NSMutableArray allocWithZone: [self zone]]
  86.              initWithCapacity: 1];
  87.  
  88.     [workingStore addObject: object];
  89.     return YES;
  90. }
  91.  
  92.  
  93. - (BOOL) deleteObject: object
  94. {
  95.     // Deletes object from the data source.  Returns YES on success, NO on
  96.     // failure for any reason.
  97.  
  98.     print_trace;
  99.  
  100.     if ([object isKindOf: [Motorcycle class]] == NO)  return NO;
  101.     if (workingStore == nil)  return NO;
  102.     
  103.     [workingStore removeObject: object];
  104.     return YES;
  105. }
  106.  
  107.  
  108. - (BOOL) updateObject: object
  109. {
  110.     // Saves changes to object to the data source.  Returns YES on success,
  111.     // NO on failure for any reason.
  112.  
  113.     print_trace;
  114.     return YES;
  115. }
  116.  
  117.  
  118. - (NSArray *) fetchObjects
  119. {
  120.     // Returns an array of the data-bearing objects in the data source.
  121.  
  122.     print_trace;
  123.     return persistentStore;
  124. }
  125.  
  126.  
  127. - (BOOL) saveObjects
  128. {
  129.     // Saves objects to persistent storage, if needed.  Returns YES on
  130.     // success, NO on failure for any reason.
  131.  
  132.     print_trace;
  133.  
  134.     if (persistentStore == nil) 
  135.         persistentStore = [[NSMutableArray allocWithZone: [self zone]]
  136.              initWithCapacity: [workingStore count]];
  137.  
  138.     [persistentStore removeAllObjects];
  139.     [persistentStore addObjectsFromArray: workingStore];
  140.     return YES;
  141. }
  142.  
  143.  
  144. - (BOOL) canDelete
  145. {
  146.     // Returns YES if the data source allows objects to be deleted, NO if it
  147.     // doesn't.
  148.  
  149.     print_trace;
  150.     return YES;
  151. }
  152.  
  153.  
  154. /*--------------------------------------------------------------------------
  155.  *    EOQualifiedDataSources Protocol
  156.  *
  157.  *  The EOQualifiedDataSources protocol decalres methods that must be
  158.  *  implemented by data sources that provide sub data sources rooted in the
  159.  *  super data source.
  160.  *
  161.  *------------------------------------------------------------------------*/
  162. - (NSArray *) keysForPath: (NSString *)aPath
  163. {
  164.     // The path supplied is a concatenation of all the keys used to get to
  165.     // a detail datasource with each key separated by a '.'; for example,
  166.     // "toAuthor.toPublisher.address".
  167.     
  168.     print_trace;
  169.     return nil;
  170. }
  171.  
  172.  
  173. - (id <EODataSources>) dataSourceQualifiedByKey:(NSString *)key ofObject:object
  174. {
  175.     // Returns a data source that supplies objects associated with object's
  176.     // key. This can be used for creating master-detail data sources.
  177.  
  178.     print_trace;
  179.     return nil;
  180. }
  181.  
  182.  
  183. /*--------------------------------------------------------------------------
  184.  *    EORollbackDataSources Protocol
  185.  *------------------------------------------------------------------------*/
  186. - (void) rollback
  187. {
  188.     // Reverses any changes made by -insertObject:, -deleteObject:, or
  189.     // -updateObject: since the data source was last sent a -saveObjects
  190.     // message.
  191.  
  192.     print_trace;
  193.     [workingStore removeAllObjects];
  194.     [workingStore addObjectsFromArray: persistentStore];
  195. }
  196.  
  197.  
  198. @end
  199.