home *** CD-ROM | disk | FTP | other *** search
/ NeXT Enterprise Objects Framework 1.1 / NeXT Enterprise Objects Framework 1.1.iso / NextDeveloper / Examples / EnterpriseObjects / FlatFileDataSource / AppController.m < prev    next >
Encoding:
Text File  |  1994-12-31  |  3.3 KB  |  104 lines

  1. /* AppController.m
  2.  *
  3.  * Sets up an EOController and a DataSource objects which are not based on an underlying
  4.  * RDBMS.
  5.  * 
  6.  * You may freely copy, distribute, and reuse the code in this example.
  7.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  8.  * fitness for any particular use.
  9.  *
  10.  * 
  11.  */
  12.  
  13. #import <eoaccess/eoaccess.h>
  14. #import <eointerface/eointerface.h>
  15. #import<foundation/NSString.h>
  16. #import <foundation/NSException.h>
  17.  
  18. #import "AppController.h"
  19. #import "TableDataSource.h"
  20.  
  21. @implementation AppController
  22.  
  23.  
  24. #define EXAMPLE_MODEL @"example"
  25. #define PRODUCT_ENTITY @"Product"
  26. #define DATABASE @"DATABASE"
  27.  
  28. void MyTopLevelErrorHandler(NXHandler *errorState);
  29.  
  30. - awakeFromNib {
  31.     NSString *modelPath = [EOModel findPathForModelNamed:EXAMPLE_MODEL];
  32.     EOEntity *productEntity;
  33.     TableDataSource *masterSource;
  34.     EOModel *model;
  35.     NSString *databasePath;
  36.  
  37.     
  38.     NXSetTopLevelErrorHandler(MyTopLevelErrorHandler);
  39.     
  40.     if (!modelPath) {
  41.         NSString *emess = [NSString stringWithFormat:@"Cannot find path for model %@", EXAMPLE_MODEL];
  42.         NXRunAlertPanel("ERROR", [emess cString], "OK", NULL, NULL);
  43.         return self;
  44.     }
  45.  
  46.     // Get the Product entity from the model. We are in trouble if it is not there!!!
  47.     model = [[EOModel alloc] initWithContentsOfFile:modelPath];
  48.     [model autorelease];
  49.     if ( !(productEntity = [model entityNamed:PRODUCT_ENTITY]) ) {
  50.         NSString *emess = [NSString stringWithFormat:@"Cannot find entity %@ in model %@", PRODUCT_ENTITY,  EXAMPLE_MODEL];
  51.         NXRunAlertPanel("ERROR", [emess cString], "OK", NULL, NULL);
  52.         return self;
  53.     }
  54.  
  55.     // Instantiate a TableDataSource for the product entity and connect
  56.     // the product controller to it.
  57.     // The detail item data source will be automatically created and attached to
  58.     // the detail controller (look at the qualified association set in IB
  59.     // that connects the master controller (Product) to its detail (Item).
  60.     // The association key is @"toItem"). That's the only thing that needs to be done, 
  61.     // et voila...
  62.     databasePath = [NSString stringWithFormat:@"%@/../%@", [self applicationPath], DATABASE];
  63.     masterSource = [[TableDataSource alloc] initWithEntity:productEntity tablePath:databasePath];
  64.     [masterSource autorelease];
  65.     if (!masterSource) {
  66.         NSString *emess = [NSString stringWithCString:"Cannot access the flat file database!!! You need to set the absolute path to the flat file db (AppController.m) or run the application from the example directory..."];
  67.         NXRunAlertPanel("ERROR", [emess cString], "OK", NULL, NULL);
  68.         return self;
  69.     }
  70.     [productController setDataSource:masterSource];
  71.     [productController fetch:self];
  72.     return self;
  73. }
  74.  
  75. void MyTopLevelErrorHandler(NXHandler *errorState)
  76. {
  77.      if (errorState->code >= NSExceptionBase &&
  78.        errorState->code <= NSLastException) {
  79.        NSException *exception = (id) errorState->data1;
  80.  
  81.      NSLog(@"%@: %@\n", [exception exceptionName], [exception exceptionReason]);
  82.         }
  83. }
  84.  
  85. #undef EXAMPLE_MODEL
  86. #undef PRODUCT_ENTITY
  87. #undef DATABASE
  88.  
  89. - (NSString *)applicationPath {
  90.     int           i;
  91.     NSString   *appPathName = nil;
  92.  
  93.     for (i = strlen(NXArgv[0]) - 1; (i >= 0) && (NXArgv[0][i] != '/'); i--);
  94.     appPathName = [NSString stringWithCString:NXArgv[0] length:i];
  95.     if (NXArgv[0][0] != '/') {
  96.         char path[MAXPATHLEN+1];
  97.         appPathName = [NSString stringWithFormat:@"%s/%@", getwd(path), appPathName];
  98.     }
  99.     return appPathName;
  100. }
  101.  
  102.  
  103. @end
  104.