Author: torehalset Date: Mon Oct 9 14:08:43 2006 New Revision: 454514 URL: http://svn.apache.org/viewvc?view=rev&rev=454514 Log: * started to implement property key value validation * NSNull values for attributes are returned as nil
Modified: incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjAttribute.h incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjAttribute.m incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.h incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m Modified: incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjAttribute.h URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjAttribute.h?view=diff&rev=454514&r1=454513&r2=454514 ============================================================================== --- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjAttribute.h (original) +++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjAttribute.h Mon Oct 9 14:08:43 2006 @@ -41,6 +41,4 @@ -(void)setMaxLength:(int)ml; -(int)maxLength; --(BOOL)isValueOfOkType:(id)value; - @end Modified: incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjAttribute.m URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjAttribute.m?view=diff&rev=454514&r1=454513&r2=454514 ============================================================================== --- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjAttribute.m (original) +++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjAttribute.m Mon Oct 9 14:08:43 2006 @@ -85,24 +85,6 @@ return maxLength; } --(BOOL)isValueOfOkType:(id)value -{ - // TODO: create a Dictionary like class mapper. perhaps two, one for each direction. - if([[self javaType] isEqualToString:@"java.lang.String"]) - { - return [value isKindOfClass:[NSString class]]; - } - else if([[self javaType] isEqualToString:@"java.util.Date"]) - { - return [value isKindOfClass:[NSDate class]]; - } - else - { - NSLog(@"ERROR: unhandled java type %@", self); - return NO; - } -} - -(NSString *)description { NSString *result = [[NSString alloc] initWithFormat:@"CAYObjAttribute {name = %@; javaType = %@, mandatory = %i, maxLength = %i}", [self name], [self javaType], [self isMandatory], [self maxLength]]; Modified: incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.h URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.h?view=diff&rev=454514&r1=454513&r2=454514 ============================================================================== --- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.h (original) +++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.h Mon Oct 9 14:08:43 2006 @@ -42,6 +42,7 @@ -(void)setValue:(id)value forKey:(NSString *)key; -(id)valueForKey:(NSString *)key; +-(BOOL)validateValue:(id *)ioValue forKey:(NSString *)key error:(NSError **)outError; -(void)setToOneTarget:(CAYPersistentObject *)value forKey:(NSString *)key setReverse:(BOOL)setrev; -(void)addToManyTarget:(CAYPersistentObject *)value forKey:(NSString *)key setReverse:(BOOL)setrev; Modified: incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m?view=diff&rev=454514&r1=454513&r2=454514 ============================================================================== --- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m (original) +++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m Mon Oct 9 14:08:43 2006 @@ -188,21 +188,13 @@ { // a none-relationship property id oldValue = [self valueForKey:key]; - - // check value type - CAYObjAttribute *attribute = [[objEntity attributes] valueForKey:key]; - if(![attribute isValueOfOkType:value]) - { - NSLog(@"ERROR: [EMAIL PROTECTED]@: %@(%@) not valid value type for attribute %@", [objEntity name], key, value, [value class], attribute); - } - else - { - [[self objectContext] propertyChanged:self forProperty:key fromOld:[values objectForKey:key] toNew:value]; - [values setValue:value forKey:key]; - } - NSLog(@"DEBUG: [EMAIL PROTECTED]@. attribute: [EMAIL PROTECTED] oldValue: [EMAIL PROTECTED] oldValue class: [EMAIL PROTECTED] ok: %d",[objEntity name], key, attribute, oldValue, [oldValue class], [attribute isValueOfOkType:value]); + // create diff and set value. validation done using key/value validation + [[self objectContext] propertyChanged:self forProperty:key fromOld:[values objectForKey:key] toNew:value]; + [values setValue:value forKey:key]; + CAYObjAttribute *attribute = [[objEntity attributes] valueForKey:key]; + NSLog(@"DEBUG: [EMAIL PROTECTED]@. attribute: [EMAIL PROTECTED] oldValue: [EMAIL PROTECTED] oldValue class: %@",[objEntity name], key, attribute, oldValue, [oldValue class]); } } @@ -216,7 +208,85 @@ val = [fault resolveFault]; [values setValue:val forKey:key]; } - return [values objectForKey:key]; + if([val isKindOfClass:[NSNull class]]) + { + // return nil instead of NSNull as it works better for formatters++ + return nil; + } + return val; +} + +-(BOOL)validateValue:(id *)ioValue forKey:(NSString *)key error:(NSError **)outError +{ + CAYObjEntity *objEntity = [[[self objectContext] entityResolver] lookupObjEntity:self]; + CAYObjAttribute *attribute = [[objEntity attributes] valueForKey:key]; + + // check for mandatory field + if([attribute isMandatory]) + { + if((!*ioValue) || [*ioValue isKindOfClass:[NSNull class]]) + { + // TODO: append to NSError + return NO; + } + } + + if([[attribute javaType] isEqualToString:@"java.lang.String"]) + { + // TODO: check for max length + return [*ioValue isKindOfClass:[NSString class]]; + } + else if([[attribute javaType] isEqualToString:@"java.util.Date"]) + { + return [*ioValue isKindOfClass:[NSDate class]]; + } + else if([[attribute javaType] isEqualToString:@"java.lang.Integer"]) + { + if([*ioValue isKindOfClass:[NSNumber class]]) + { + if(strcmp("i",[*ioValue objCType]) != 0) + { + // try to convert non-integer numbers to int + *ioValue = [NSNumber numberWithInt:[*ioValue intValue]]; + } + return YES; + } + // TODO: append to NSError + return NO; + } + else if([[attribute javaType] isEqualToString:@"java.lang.Float"]) + { + if([*ioValue isKindOfClass:[NSNumber class]]) + { + if(strcmp("f",[*ioValue objCType]) != 0) + { + // try to convert non-float numbers to float + *ioValue = [NSNumber numberWithFloat:[*ioValue floatValue]]; + } + return YES; + } + return NO; + } + else if([[attribute javaType] isEqualToString:@"java.lang.Double"]) + { + if([*ioValue isKindOfClass:[NSNumber class]]) + { + if(strcmp("d",[*ioValue objCType]) != 0) + { + // try to convert non-float numbers to float + *ioValue = [NSNumber numberWithDouble:[*ioValue doubleValue]]; + } + return YES; + } + // TODO: append to NSError + return NO; + } + else + { + NSLog(@"ERROR: unhandled java type %@", attribute); + // TODO: append to NSError + return NO; + } } -(void)setToOneTarget:(CAYPersistentObject *)value forKey:(NSString *)key setReverse:(BOOL)setrev @@ -259,7 +329,7 @@ if(!value) { - // TODO: throw + // TODO: throw. NSLog(@"ERROR. can not add nil to [EMAIL PROTECTED]@", [self class], key); return; }