home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Database / OTC_EOFBetaExamples_V1.0 / EOFramework / DataSource / DataSource.m < prev    next >
Encoding:
Text File  |  1994-06-08  |  5.2 KB  |  195 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.  
  71. - (BOOL) insertObject: object
  72. {
  73.     // Inserts object into the data source.  Returns YES on success, NO on
  74.     // failure for any reason.
  75.  
  76.     print_trace;
  77.  
  78.     if ([object isKindOf: [Motorcycle class]] == NO)  return NO;
  79.  
  80.     if (workingStore == nil) 
  81.         workingStore = [[NSMutableArray allocWithZone: [self zone]]
  82.              initWithCapacity: 1];
  83.  
  84.     [workingStore addObject: object];
  85.     return YES;
  86. }
  87.  
  88.  
  89. - (BOOL) deleteObject: object
  90. {
  91.     // Deletes object from the data source.  Returns YES on success, NO on
  92.     // failure for any reason.
  93.  
  94.     print_trace;
  95.  
  96.     if ([object isKindOf: [Motorcycle class]] == NO)  return NO;
  97.     if (workingStore == nil)  return NO;
  98.     
  99.     [workingStore removeObject: object];
  100.     return YES;
  101. }
  102.  
  103.  
  104. - (BOOL) updateObject: object
  105. {
  106.     // Saves changes to object to the data source.  Returns YES on success,
  107.     // NO on failure for any reason.
  108.  
  109.     print_trace;
  110.     return YES;
  111. }
  112.  
  113.  
  114. - (NSArray *) fetchObjects
  115. {
  116.     // Returns an array of the data-bearing objects in the data source.
  117.  
  118.     print_trace;
  119.     return persistentStore;
  120. }
  121.  
  122.  
  123. - (BOOL) saveObjects
  124. {
  125.     // Saves objects to persistent storage, if needed.  Returns YES on
  126.     // success, NO on failure for any reason.
  127.  
  128.     print_trace;
  129.  
  130.     if (persistentStore == nil) 
  131.         persistentStore = [[NSMutableArray allocWithZone: [self zone]]
  132.              initWithCapacity: [workingStore count]];
  133.  
  134.     [persistentStore removeAllObjects];
  135.     [persistentStore addObjectsFromArray: workingStore];
  136.     return YES;
  137. }
  138.  
  139.  
  140. - (BOOL) canDelete
  141. {
  142.     // Returns YES if the data source allows objects to be deleted, NO if it
  143.     // doesn't.
  144.  
  145.     print_trace;
  146.     return YES;
  147. }
  148.  
  149.  
  150. /*--------------------------------------------------------------------------
  151.  *    EOQualifiedDataSources Protocol
  152.  *
  153.  *  The EOQualifiedDataSources protocol decalres methods that must be
  154.  *  implemented by data sources that provide sub data sources rooted in the
  155.  *  super data source.
  156.  *
  157.  *------------------------------------------------------------------------*/
  158. - (NSArray *) keysForPath: (NSString *)aPath
  159. {
  160.     // The path supplied is a concatenation of all the keys used to get to
  161.     // a detail datasource with each key separated by a '.'; for example,
  162.     // "toAuthor.toPublisher.address".
  163.     
  164.     print_trace;
  165.     return nil;
  166. }
  167.  
  168.  
  169. - (id <EODataSources>) dataSourceQualifiedByKey:(NSString *)key ofObject:object
  170. {
  171.     // Returns a data source that supplies objects associated with object's
  172.     // key. This can be used for creating master-detail data sources.
  173.  
  174.     print_trace;
  175.     return nil;
  176. }
  177.  
  178.  
  179. /*--------------------------------------------------------------------------
  180.  *    EORollbackDataSources Protocol
  181.  *------------------------------------------------------------------------*/
  182. - (void) rollback
  183. {
  184.     // Reverses any changes made by -insertObject:, -deleteObject:, or
  185.     // -updateObject: since the data source was last sent a -saveObjects
  186.     // message.
  187.  
  188.     print_trace;
  189.     [workingStore removeAllObjects];
  190.     [workingStore addObjectsFromArray: persistentStore];
  191. }
  192.  
  193.  
  194. @end
  195.