home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / EODEV.Z / Customer.m < prev    next >
Encoding:
Text File  |  1996-08-23  |  4.0 KB  |  140 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 <BusinessLogic/Customer.h>
  10. #import <BusinessLogic/Rental.h>
  11. #import <BusinessLogic/Fee.h>
  12. #import <BusinessLogic/Unit.h>
  13. #import <BusinessLogic/NSObjectAdditions.h>
  14.  
  15. @implementation Customer
  16.  
  17. - (NSArray *)allRentals
  18. {
  19.     return rentals;
  20. }
  21.  
  22. - (NSArray *)fees
  23. {
  24.     NSEnumerator *rentalsEnumerator = [[self allRentals] objectEnumerator];
  25.     NSMutableArray *fees;
  26.     Rental *next;
  27.  
  28.     fees = [NSMutableArray array];
  29.     while(next = [rentalsEnumerator nextObject])
  30.         [fees addObjectsFromArray:[next fees]];
  31.  
  32.     return fees;
  33. }
  34.  
  35. - (NSDecimalNumber *)costRestriction;
  36. {
  37.     [NSException raise:NSInvalidArgumentException
  38.                 format:@"-%@ cannot be sent to an abstract object of class %@.",
  39.                 NSStringFromSelector(_cmd), [[self class] name]];
  40.  
  41.     return nil; // Just to keep the compiler happy.
  42. }
  43.  
  44. - (Member *)member
  45. {
  46.     [NSException raise:NSInvalidArgumentException
  47.                 format:@"-%@ cannot be sent to an abstract object of class %@.",
  48.                 NSStringFromSelector(_cmd), [[self class] name]];
  49.  
  50.     return nil; // Just to keep the compiler happy.
  51. }
  52.  
  53. - (void)dealloc
  54. {
  55.     [customerID release];
  56.     [firstName release];
  57.     [lastName release];
  58.     [rentals release];
  59.     [super dealloc];
  60. }
  61.  
  62.  
  63. - (NSArray *)unpaidFees
  64. {
  65.     static EOQualifier *qualifier = nil;
  66.  
  67.     if(!qualifier) {
  68.         qualifier = [[EOKeyValueQualifier alloc] initWithKey:@"datePaid"
  69.                                             operatorSelector:EOQualifierOperatorEqual
  70.                                                        value:[EONull null]];
  71.     }
  72.     return [[self fees] filteredArrayUsingQualifier:qualifier];
  73. }
  74.  
  75. - (BOOL)hasOverdueProducts
  76. {
  77.     NSEnumerator *rentalsEnumerator = [[self nonReturnedRentals] objectEnumerator];
  78.     Rental *next;
  79.  
  80.     while(next = [rentalsEnumerator nextObject])
  81.         if([next isOverdue]) return YES;
  82.  
  83.     return NO;
  84. }
  85.  
  86. - (NSArray *)nonReturnedRentals
  87. {
  88.     static EOQualifier *qualifier = nil;
  89.  
  90.     if(!qualifier) {
  91.         qualifier = [[EOKeyValueQualifier alloc] initWithKey:@"isOut"
  92.                                             operatorSelector:EOQualifierOperatorEqual
  93.                                                        value:[NSNumber numberWithBool:YES]];
  94.     }
  95.     return [[self allRentals] filteredArrayUsingQualifier:qualifier];
  96. }
  97.  
  98. - (NSString *)fullName
  99. {
  100.     return [NSString stringWithFormat:@"%@ %@", firstName, lastName];
  101. }
  102.  
  103. - (void)rentUnit:(Unit *)unit
  104. {
  105.     Rental *rental;
  106.     Fee *fee;
  107.  
  108.     // I need to create two objects here.
  109.     rental = [[[Rental alloc] init] autorelease];
  110.     fee = [[[Fee alloc] init] autorelease];
  111.  
  112.     [rental addObject:fee toBothSidesOfRelationshipWithKey:@"fees"];
  113.     [rental addObject:unit toBothSidesOfRelationshipWithKey:@"unit"];
  114.  
  115.     [self addObject:rental toBothSidesOfRelationshipWithKey:@"rentals"];
  116.  
  117.     // Insert the two objects in the editingContext.
  118.     [[self editingContext] insertObject:rental];
  119.     [[self editingContext] insertObject:fee];
  120. }
  121.  
  122. - (NSException *)validateForSave
  123. {
  124.     // this method will be called before we are inserted or updated in the database
  125.     NSDecimalNumber *currentDeposit = [[self allRentals] sumArrayWithKey:@"unit.product.rentalTerms.depositAmount"];
  126.     NSDecimalNumber *maxDeposit = [self costRestriction];
  127.     
  128.     // sum all the rentals curently out and check that their value is < [self costRestriction]
  129.  
  130.     if([currentDeposit compare:maxDeposit] == NSOrderedDescending) {
  131.         return [NSException validationExceptionWithFormat:@"The total value of the rented goods (%@) exceed %@ limitations (%@).", currentDeposit, [self fullName], maxDeposit];
  132.     }
  133.  
  134.     return [super validateForSave];  // pass the check on to the EOClassDesciption
  135. }
  136.  
  137. - (NSString *)description { return [self eoDescription]; }
  138.  
  139. @end
  140.