home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / EODEV.Z / ConsistencyExtension.m < prev    next >
Encoding:
Text File  |  1996-08-23  |  6.7 KB  |  144 lines

  1. /*
  2.         Copyright (c) 1996, NeXT Software, Inc.
  3.         All rights reserved.
  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,
  7.         as to its fitness for any particular use.
  8. */
  9. #import "ConsistencyExtension.h"
  10. #import <EOModeler/EOModeler.h>
  11.  
  12. @implementation ConsistencyExtension
  13.  
  14. static ConsistencyExtension *myConsistencyChecker = nil;
  15.  
  16. + (void)load {
  17.     NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  18.     myConsistencyChecker = [[ConsistencyExtension alloc] init];
  19.  
  20.     [nc addObserver:myConsistencyChecker selector:@selector(beginConsistencyCheckNotification:) name:EOMCheckConsistencyBeginNotification object:nil];
  21.     [nc addObserver:myConsistencyChecker selector:@selector(endConsistencyCheckNotification:) name:EOMCheckConsistencyEndNotification object:nil];
  22.  
  23.     [nc addObserver:myConsistencyChecker selector:@selector(checkEntityNotification:) name:EOMCheckConsistencyForEntityNotification object:nil];
  24.     [nc addObserver:myConsistencyChecker selector:@selector(checkStoredProcedureNotification:) name:EOMCheckConsistencyForStoredProcedureNotification object:nil];
  25.     [nc addObserver:myConsistencyChecker selector:@selector(checkModelNotification:) name:EOMCheckConsistencyForModelNotification object:nil];
  26. }
  27.  
  28. - (void)beginConsistencyCheckNotification:(NSNotification *)notification
  29. {
  30.     entitiesAreConsistent = storedProceduresAreConsistent = modelIsConsistent = YES;
  31. }
  32.  
  33. - (void)endConsistencyCheckNotification:(NSNotification *)notification
  34. {
  35.     EOModelerDocument *doc = [notification object];
  36.  
  37.     // For each test that ran successfully, report what was found to be valid.
  38.     if (entitiesAreConsistent) {
  39.         if ([[[doc model] adaptorName] hasPrefix:@"Oracle"])
  40.             [doc appendConsistencyCheckSuccessText:[[NSAttributedString alloc] initWithString:@"All the entities have no more than 1 LONG or LONGRAW column.\n"]];
  41.     }
  42.  
  43.     if (storedProceduresAreConsistent)
  44.         [doc appendConsistencyCheckSuccessText:[[NSAttributedString alloc] initWithString:@"All the stored procedures that are used for DELETE have arguments that match the primary key attributes of the entity for which they will be used.\n"]];
  45.     
  46.     if (modelIsConsistent)
  47.         [doc appendConsistencyCheckSuccessText:[[NSAttributedString alloc] initWithString:@"The model has at least one entity.\n"]];
  48. }
  49.  
  50.  
  51. - (void)checkEntityNotification:(NSNotification *)notification
  52. {
  53.     EOEntity *entity = [[notification userInfo] objectForKey:EOMConsistencyModelObjectKey];
  54.     EOModelerDocument *doc = [notification object];
  55.  
  56.     // If this is an Oracle model, make sure this entity doesn't
  57.     // have more than one blob attribute.
  58.  
  59.     if ([[[entity model] adaptorName] hasPrefix:@"Oracle"]) {
  60.         int i, icount;
  61.         int numberOfBlobs = 0;
  62.         NSArray *atts = [entity attributes];
  63.  
  64.         for (i = 0, icount = [atts count]; i < icount; i++) {
  65.             EOAttribute *att = [atts objectAtIndex:i];
  66.             if (![att isFlattened] && [[att externalType] hasPrefix:@"LONG"])
  67.                 numberOfBlobs++;
  68.         }
  69.         if (numberOfBlobs > 1) {
  70.             [doc appendConsistencyCheckErrorText:[NSMutableAttributedString mutableAttributedStringWithBoldSubstitutionsWithFormat:@"Entity %@ has more than one LONG or LONGRAW  columns and the Oracle database does not support this.\n", [entity name]]];
  71.             // Please note that mutableAttributedStringWithBoldSubstitutionsWithFormat:
  72.             // will only deal with substitutions of the form "%@" and it expects all the
  73.             // args to be NSStrings. It is limited, but very useful for generating error
  74.             // strings for consistency checking.
  75.             entitiesAreConsistent = NO;
  76.         }
  77.     }
  78. }
  79.  
  80. - (void)checkStoredProcedureNotification:(NSNotification *)notification
  81. {
  82.     EOStoredProcedure *sp = [[notification userInfo] objectForKey:EOMConsistencyModelObjectKey];
  83.     EOModelerDocument *doc = [notification object];
  84.     NSArray *entities = [[sp model] entities];
  85.     int i, icount;
  86.  
  87.     // If this stored procedure is used as a delete stored procedure, make sure the args
  88.     // match the primary key attributes.
  89.  
  90.     for (i = 0, icount = [entities count]; i < icount; i++) {
  91.         EOEntity *entity = [entities objectAtIndex:i];
  92.         if ([entity storedProcedureForOperation:EODeleteProcedureOperation] == sp) {
  93.             NSMutableArray *pkAttrs, *args;
  94.  
  95.             pkAttrs = [[entity primaryKeyAttributes] mutableCopy];
  96.             args = [[sp arguments] mutableCopy];
  97.  
  98.             if ([pkAttrs count] != [args count]) {
  99.                 [doc appendConsistencyCheckErrorText:[NSMutableAttributedString mutableAttributedStringWithBoldSubstitutionsWithFormat:@"The number of arguments for stored procedure %@ does not match the number of primary key attributes for entity %@, which lists %@ as its stored procedure for DELETE operations.\n", [sp name], [entity name], [sp name]]];
  100.                 storedProceduresAreConsistent = NO;
  101.             } else {
  102.                 int j, jcount;
  103.                 int k, kcount;
  104.  
  105.                 for (j = 0, jcount = [pkAttrs count]; j < jcount; j++) {
  106.                     EOAttribute *pkAtt = [pkAttrs objectAtIndex:i];
  107.                     EOAttribute *argIndex;
  108.                     EOAttribute *matchingArg = nil;
  109.                     
  110.                     for (k = 0, kcount = [args count]; k < kcount && !matchingArg; k++) {
  111.                         argIndex = [args objectAtIndex:k];
  112.                         if ([[argIndex name] isEqual:[pkAtt name]])
  113.                             matchingArg = argIndex;
  114.                     }
  115.                     if (!matchingArg) {
  116.                         [doc appendConsistencyCheckErrorText:[NSMutableAttributedString mutableAttributedStringWithBoldSubstitutionsWithFormat:@"Stored procedure %@ is used by entity %@ for DELETE, but it does not have an argument named %@ to match the primary key attribute %@.\n", [sp name], [entity name], [pkAtt name], [pkAtt name]]];
  117.                         storedProceduresAreConsistent = NO;
  118.                     } else { // we have a match
  119.                         [pkAttrs removeObjectIdenticalTo:pkAtt];
  120.                         j--;
  121.                         [args removeObjectIdenticalTo:matchingArg];
  122.                     }
  123.                 }
  124.             }
  125.  
  126.             [pkAttrs release];
  127.             [args release];
  128.         }
  129.     }
  130. }
  131.  
  132. - (void)checkModelNotification:(NSNotification *)notification
  133. {
  134.     EOModel *model = [[notification userInfo] objectForKey:EOMConsistencyModelObjectKey];
  135.     EOModelerDocument *doc = [notification object];
  136.  
  137.     if (![[model entities] count]) {
  138.         [doc appendConsistencyCheckErrorText:[[NSAttributedString alloc] initWithString:@"This model does not have any entities!\n"]];
  139.         modelIsConsistent = NO;
  140.     }
  141. }
  142.  
  143. @end
  144.