home *** CD-ROM | disk | FTP | other *** search
/ NeXT Enterprise Objects Framework 1.1 / NeXT Enterprise Objects Framework 1.1.iso / NextDeveloper / Examples / EnterpriseObjects / FlatFileDataSource / DetailTableDataSource.m < prev    next >
Encoding:
Text File  |  1995-02-17  |  3.4 KB  |  121 lines

  1. /* DetailTableDataSource.m
  2.  *
  3.  * A data source implementation for a detail table.
  4.  * 
  5.  * You may freely copy, distribute, and reuse the code in this example.
  6.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  7.  * fitness for any particular use.
  8.  *
  9.  * 
  10.  */
  11.  
  12.  
  13.  
  14. #import <appkit/appkit.h>
  15. #import <eoaccess/eoaccess.h>
  16. #import "DetailTableDataSource.h"
  17. #import "TableDataSourcePrivate.h"
  18.  
  19. @implementation DetailTableDataSource
  20.  
  21. - initWithMasterDataSource:(TableDataSource *)masterSource entity:(EOEntity *)detailEntity {
  22.     NSString *primaryKey;
  23.     NSArray *relations;
  24.     int i;
  25.     NSMutableArray *qualifiers = [[[NSMutableArray alloc] init] autorelease];
  26.  
  27.     [super init];
  28.     entity = [detailEntity retain];
  29.     master = [masterSource retain];
  30.     orderByKey=nil;
  31.     
  32.     primaryKey = [(EOAttribute *)[[entity primaryKeyAttributes] objectAtIndex:0] name];
  33.     relations = [entity relationships];
  34.     for (i=0; i<[relations count]; i++) {
  35.         EORelationship *rel=[relations objectAtIndex:i];
  36.  
  37.         if ([rel isToMany]) {
  38.             EOAttribute *attr = [[rel sourceAttributes] objectAtIndex:0];
  39.             if (!attr) continue;
  40.             else [qualifiers addObject:[attr name]];
  41.         }
  42.     }
  43.     relations = [[master entity] relationships];
  44.     for (i=0; i<[relations count]; i++) {
  45.         EORelationship *rel=[relations objectAtIndex:i];
  46.  
  47.         if ([rel isToMany] && [entity isEqual:[rel destinationEntity]]) {
  48.             EOAttribute *attr = [[rel destinationAttributes] objectAtIndex:0];
  49.             if (!attr) continue;
  50.             else [qualifiers addObject:[attr name]];
  51.         }
  52.     }
  53.  
  54.     return [self initWithTable:[master tablePath] primaryKey:primaryKey qualifierKeys:qualifiers];
  55. }
  56.  
  57. - (void)qualifyWithRelationshipKey:key ofObject:sourceObject {
  58.  
  59.     EORelationship *rel = [[master entity] relationshipNamed:key];
  60.     NSMutableDictionary *qual;
  61.     EOAttribute *source = [[rel sourceAttributes] objectAtIndex:0];
  62.     EOAttribute *dest = [[rel destinationAttributes] objectAtIndex:0];
  63.  
  64.     if (!rel) {
  65.         NSLog(@"Master source does not have relationship for key %@", [(NSString *)key description]);
  66.         [self setQualifier:nil];
  67.     }
  68.     else if (!source || !dest) {
  69.         NSLog(@"Master source does not have source or destination attribute");
  70.         [self setQualifier:nil];
  71.     }
  72.     else if (!sourceObject) {
  73.         [self setQualifier:nil];
  74.     }
  75.     else {
  76.         id linkValue;
  77.         linkValue = [sourceObject objectForKey:[source name]];
  78.         if (linkValue) {
  79.             qual = [[[NSMutableDictionary alloc] init] autorelease];
  80.             [qual setObject:[dest name] forKey:@QPROPERTY];
  81.             [qual setObject:linkValue forKey:@QVALUE];
  82.             [self setQualifier:qual];
  83.         }
  84.         else [self setQualifier:nil];
  85.     }    
  86.     /*[masterObject autorelease];
  87.     masterObject=[sourceObject retain];
  88.     [masterKey autorelease];
  89.     masterKey=[key retain];*/
  90. }
  91.  
  92. - createObject{
  93.     NSMutableDictionary *newObject = [super createObject];
  94.     NSArray *relations = [[master entity] relationships];
  95.     int i;
  96.  
  97.     if (!newObject) return nil;
  98.     for (i=0; i<[relations count]; i++) {
  99.         EORelationship *rel=[relations objectAtIndex:i];
  100.  
  101.         if ( ([rel isToMany]) && ([entity isEqual:[rel destinationEntity]]) ) {
  102.             EOAttribute *attr = [[rel destinationAttributes] objectAtIndex:0];
  103.             NSString *key = [attr name];
  104.  
  105.             if (!attr) continue;
  106.             if ( ![key isEqual:[qualifier objectForKey:@QPROPERTY]] ) {
  107.                 NSLog(@"qualifier %@ differs from relationship key %@ to create detail object %@", 
  108.                     [(NSMutableDictionary *)qualifier description], key, [entity name]);
  109.                 continue;
  110.             }
  111.             else [newObject setObject:[qualifier objectForKey:@QVALUE] forKey:key];
  112.         }
  113.     }
  114.  
  115.  
  116.     return newObject;
  117. }
  118.  
  119.  
  120. @end
  121.