home *** CD-ROM | disk | FTP | other *** search
/ NeXT Enterprise Objects Framework 1.1 / NeXT Enterprise Objects Framework 1.1.iso / NextDeveloper / Examples / EnterpriseObjects / SHLExamples / AccessLayer / DatabaseLevel.m < prev    next >
Encoding:
Text File  |  1994-09-13  |  4.7 KB  |  173 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.  *    DatabaseLevel
  9.  *
  10.  *    Inherits From:        NSObject
  11.  *
  12.  *    Conforms To:        None
  13.  *
  14.  *    Declared In:        DatabaseLevel.h
  15.  *
  16.  *------------------------------------------------------------------------*/
  17. #import "DatabaseLevel.h"
  18. #import "ScrollViewExtensions.h"
  19. #import <eoaccess/eoaccess.h>
  20. #import <appkit/appkit.h>
  21.  
  22.  
  23.  
  24. @implementation DatabaseLevel
  25.  
  26. - connect: sender
  27. {
  28.     id    path;
  29.     id    adaptor;
  30.     
  31.     [messageConsole clear: nil];
  32.     [messageConsole sprintf: "connecting to database...\n"];
  33.  
  34.     [messageConsole sprintf: "obtaining model path...\n"];
  35.     if ((path = [EOModel findPathForModelNamed:
  36.         [NSString stringWithCString:"Authors"]]) == nil)
  37.         {
  38.         NXRunAlertPanel (NULL, "Unable to obtain model path", 
  39.             NULL, NULL, NULL);
  40.         [messageConsole sprintf: "unable to obtain model path\n"];
  41.         return nil;
  42.         }
  43.  
  44.     [modelPath setStringValue: [path cString]];
  45.         
  46.     [messageConsole sprintf: "initializing model...\n"];
  47.     if ((model = [[EOModel alloc] initWithContentsOfFile:path]) == nil)
  48.         {
  49.         NXRunAlertPanel (NULL, "Unable to initialize model", NULL, NULL, NULL);
  50.         [messageConsole sprintf: "unable to initialize model\n"];
  51.         return nil;
  52.         }
  53.     
  54.     [messageConsole sprintf: "creating database...\n"];
  55.     if ((database = [[EODatabase alloc] initWithModel:model]) == nil)
  56.         {
  57.         NXRunAlertPanel (NULL, "Unable to create database", NULL, NULL, NULL);
  58.         [messageConsole sprintf: "unable to create database\n"];
  59.         return nil;
  60.         }
  61.     
  62.     [messageConsole sprintf: "creating context...\n"];
  63.     if ((context = [[EODatabaseContext alloc] 
  64.         initWithDatabase:database]) == nil)
  65.         {
  66.         NXRunAlertPanel (NULL, "Unable to create context", NULL, NULL, NULL);
  67.         [messageConsole sprintf: "unable to create context\n"];
  68.         return nil;
  69.         }
  70.         
  71.     [messageConsole sprintf: "creating channel...\n"];
  72.     if ((channel = [[EODatabaseChannel alloc] 
  73.         initWithDatabaseContext:context]) == nil)
  74.         {
  75.         NXRunAlertPanel (NULL, "Unable to create channel", NULL, NULL, NULL);
  76.         [messageConsole sprintf: "unable to create channel\n"];
  77.         return nil;
  78.         }
  79.         
  80.     // Bring up a login panel and get the valid connection dictionary
  81.     adaptor = [database adaptor];
  82.      if(![adaptor runLoginPanelAndValidateConnectionDictionary]) {
  83.             [messageConsole sprintf: "invalid login\n"]; 
  84.             return nil;
  85.         }
  86.     else { 
  87.            [adaptor setConnectionDictionary: [adaptor connectionDictionary]];
  88.         [messageConsole sprintf: "opening channel...\n"];
  89.         if ([channel openChannel] == NO)
  90.         {
  91.         NXRunAlertPanel (NULL, "Unable to open channel", 
  92.                                                         NULL, NULL, NULL);
  93.         [messageConsole sprintf: "unable to open channel\n"];
  94.         return nil;
  95.         }
  96.     }    
  97.     [messageConsole sprintf:"channel %s open\n\n", 
  98.         [channel isOpen] ? "is" : "is not"];
  99.     [connectButton setEnabled: ! [channel isOpen]];
  100.     [selectButton setEnabled: [channel isOpen]];
  101.     return self;
  102. }
  103.  
  104.  
  105. - select: sender
  106. {
  107.     int    count = 0;
  108.     id    entity, qualifier, object;
  109.     
  110.     [resultConsole clear: nil];
  111.     [messageConsole sprintf: "beginning transaction...\n"];
  112.  
  113.     if ([context beginTransaction] == NO)
  114.         {
  115.         NXRunAlertPanel (NULL, "Unable to begin transaction", 
  116.             NULL, NULL, NULL);
  117.         [messageConsole sprintf: "unable to begin transaction\n"];
  118.         return nil;
  119.         }
  120.         
  121.     [messageConsole sprintf: "obtaining entity...\n"];
  122.     if ((entity = [model entityNamed: @"authors"]) == nil)
  123.         {
  124.         NXRunAlertPanel (NULL, "Unable to obtain entity authors", 
  125.             NULL, NULL, NULL);
  126.         [messageConsole sprintf: "unable to obtain entity authors\n"];
  127.         return nil;
  128.         }
  129.         
  130.     [messageConsole sprintf: "obtaining qualifier...\n"];
  131.     if ((qualifier = [entity qualifier]) == nil)
  132.         {
  133.         NXRunAlertPanel (NULL, "Unable to obtain qualifier", 
  134.             NULL, NULL, NULL);
  135.         [messageConsole sprintf: "unable to obtain qualifier\n"];
  136.         return nil;
  137.         }
  138.         
  139.     [messageConsole sprintf: "selecting objects...\n"];
  140.     if ([channel selectObjectsDescribedByQualifier:qualifier
  141.         fetchOrder:nil] == NO)
  142.         {
  143.         NXRunAlertPanel (NULL, "Unable to select objects", 
  144.             NULL, NULL, NULL);
  145.         [messageConsole sprintf: "unable to select objects\n"];
  146.         return nil;
  147.         }
  148.         
  149.     while ((object = [channel fetchWithZone:[(EODatabase*)database zone]]) != nil)
  150.         {
  151.         [resultConsole sprintf: "%s\n", 
  152.             [[(NSString*)object description] cString]];
  153.         count++;
  154.         }
  155.         
  156.     [messageConsole sprintf: "\n%d records found\n\n", count];
  157.         
  158.     [messageConsole sprintf: "rolling back transaction...\n"];
  159.     if ([context rollbackTransaction] == NO)
  160.         {
  161.         NXRunAlertPanel (NULL, "Unable to rollback transaction", 
  162.             NULL, NULL, NULL);
  163.         [messageConsole sprintf: "unable to rollback transaction\n"];
  164.         return nil;
  165.         }
  166.         
  167.     [messageConsole sprintf: "done\n\n"];
  168.     return self;
  169. }
  170.  
  171.  
  172. @end
  173.