Author: torehalset
Date: Wed Oct 25 15:02:15 2006
New Revision: 467778

URL: http://svn.apache.org/viewvc?view=rev&rev=467778
Log:
validation

Modified:
    incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYCocoaCayenne.h
    incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m

Modified: incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYCocoaCayenne.h
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYCocoaCayenne.h?view=diff&rev=467778&r1=467777&r2=467778
==============================================================================
--- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYCocoaCayenne.h 
(original)
+++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYCocoaCayenne.h Wed 
Oct 25 15:02:15 2006
@@ -38,4 +38,9 @@
 #define SYNCTYPE_FLUSH_CASCADE 2
 #define SYNCTYPE_ROLLBACK_CASCADE 3
 
+// define some validation constants
+#define VALIDATION_MANDATORY 1
+#define VALIDATION_TYPE 3
+#define VALIDATION_LENGTH 3
+
 @end

Modified: 
incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m?view=diff&rev=467778&r1=467777&r2=467778
==============================================================================
--- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m 
(original)
+++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m 
Wed Oct 25 15:02:15 2006
@@ -29,6 +29,12 @@
 #import "CAYToOneFault.h"
 #import "CAYObjAttribute.h"
 
[EMAIL PROTECTED] CAYPersistentObject (PrivateMethods) 
+
+-(void)createValidationError:(NSString *)errorMessage code:(int)errorCode 
error:(NSError **)outError;
+
[EMAIL PROTECTED]
+
 @implementation CAYPersistentObject
 
 -(id)init
@@ -258,25 +264,55 @@
 {
     CAYObjEntity *objEntity = [[[self objectContext] entityResolver] 
lookupObjEntity:self];
     CAYObjAttribute *attribute = [[objEntity attributes] valueForKey:key];
+
+    // 
http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles/cdValidation.html
     
     // check for mandatory field
     if([attribute isMandatory])
     {
         if((!*ioValue) || [*ioValue isKindOfClass:[NSNull class]])
         {
-            // TODO: append to NSError
+            NSString *errorMsg = [[NSString alloc] initWithFormat:@"\"[EMAIL 
PROTECTED]" is mandatory", [attribute name]];
+            [self createValidationError:errorMsg code:VALIDATION_MANDATORY 
error:outError];
+            [errorMsg release];
+            
             return NO;
         }
     }
     
     if([[attribute javaType] isEqualToString:@"java.lang.String"])
     {
-        // TODO: check for max length
-        return [*ioValue isKindOfClass:[NSString class]];
+        if([*ioValue isKindOfClass:[NSString class]])
+        {
+            if(([attribute maxLength] > 0) && ([*ioValue length] > [attribute 
maxLength]))
+            {
+                NSString *errorMsg = [[NSString alloc] 
initWithFormat:@"\"[EMAIL PROTECTED]" exceeds maximum allowed length (%d 
chars): %d", [attribute name], [attribute maxLength], [*ioValue length]];
+                [self createValidationError:errorMsg code:VALIDATION_LENGTH 
error:outError];
+                [errorMsg release];
+                
+                return NO;
+            }
+            return YES;
+        }
+
+        NSString *errorMsg = [[NSString alloc] initWithFormat:@"\"[EMAIL 
PROTECTED]" must be a NSString, not a %@", [attribute name], [*ioValue class]];
+        [self createValidationError:errorMsg code:VALIDATION_TYPE 
error:outError];
+        [errorMsg release];
+
+        return NO;
     }
     else if([[attribute javaType] isEqualToString:@"java.util.Date"])
     {
-        return [*ioValue isKindOfClass:[NSDate class]];
+        if([*ioValue isKindOfClass:[NSDate class]])
+        {
+            return YES;
+        }
+        
+        NSString *errorMsg = [[NSString alloc] initWithFormat:@"\"[EMAIL 
PROTECTED]" must be a NSDate, not a %@", [attribute name], [*ioValue class]];
+        [self createValidationError:errorMsg code:VALIDATION_TYPE 
error:outError];
+        [errorMsg release];
+        
+        return NO;
     }
     else if([[attribute javaType] isEqualToString:@"java.lang.Integer"])
     {
@@ -289,7 +325,11 @@
             }
             return YES;
         }
-        // TODO: append to NSError
+
+        NSString *errorMsg = [[NSString alloc] initWithFormat:@"\"[EMAIL 
PROTECTED]" must be a NSNumber, not a %@", [attribute name], [*ioValue class]];
+        [self createValidationError:errorMsg code:VALIDATION_TYPE 
error:outError];
+        [errorMsg release];
+
         return NO;
     }
     else if([[attribute javaType] isEqualToString:@"java.lang.Float"])
@@ -303,6 +343,11 @@
             }
             return YES;
         }
+        
+        NSString *errorMsg = [[NSString alloc] initWithFormat:@"\"[EMAIL 
PROTECTED]" must be a NSNumber, not a %@", [attribute name], [*ioValue class]];
+        [self createValidationError:errorMsg code:VALIDATION_TYPE 
error:outError];
+        [errorMsg release];
+        
         return NO;
     }
     else if([[attribute javaType] isEqualToString:@"java.lang.Double"])
@@ -316,14 +361,17 @@
             }
             return YES;
         }
-        // TODO: append to NSError
+
+        NSString *errorMsg = [[NSString alloc] initWithFormat:@"\"[EMAIL 
PROTECTED]" must be a NSNumber, not a %@", [attribute name], [*ioValue class]];
+        [self createValidationError:errorMsg code:VALIDATION_TYPE 
error:outError];
+        [errorMsg release];
+        
         return NO;
     }
     else
     {
-        NSLog(@"ERROR: unhandled java type %@", attribute);
-        // TODO: append to NSError
-        return NO;
+        NSLog(@"ERROR: unhandled java type %@ - ignoring any validation 
errors", attribute);
+        return YES;
     }
 }
 
@@ -490,5 +538,19 @@
        [super dealloc];
 }
 
[EMAIL PROTECTED]
+
[EMAIL PROTECTED] CAYPersistentObject (PrivateMethods) 
+
+-(void)createValidationError:(NSString *)errorMessage code:(int)errorCode 
error:(NSError **)outError
+{
+    NSString *errorStr = NSLocalizedStringFromTable(errorMessage, [[self 
class] description], @"");
+    NSDictionary *userInfoDict = [NSDictionary dictionaryWithObject:errorStr
+                                                             
forKey:NSLocalizedDescriptionKey];
+    NSError *error = [[[NSError alloc] initWithDomain:[[self class] 
description]
+                                                 code:errorCode
+                                             userInfo:userInfoDict] 
autorelease];
+    *outError = error;
+}
 
 @end


Reply via email to