cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From torehal...@apache.org
Subject svn commit: r442865 - in /incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne: CAYObjectContext.m CAYPersistentObject.h CAYPersistentObject.m CAYRelationshipQuery.h CAYRelationshipQuery.m CAYToOneFault.m
Date Wed, 13 Sep 2006 06:31:21 GMT
Author: torehalset
Date: Tue Sep 12 23:31:20 2006
New Revision: 442865

URL: http://svn.apache.org/viewvc?view=rev&rev=442865
Log:
support non-refreshing relationship query

Modified:
    incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjectContext.m
    incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.h
    incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m
    incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYRelationshipQuery.h
    incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYRelationshipQuery.m
    incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYToOneFault.m

Modified: incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjectContext.m
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjectContext.m?view=diff&rev=442865&r1=442864&r2=442865
==============================================================================
--- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjectContext.m (original)
+++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjectContext.m Tue Sep 12 23:31:20
2006
@@ -27,15 +27,13 @@
 #import "CAYSyncMessage.h"
 #import "CAYObjEntity.h"
 #import "CAYClientObjRelationship.h"
-#import "CAYFault.h"
-#import "CAYToManyFault.h"
-#import "CAYToOneFault.h"
 #import "CAYNodeCreateOperation.h"
 #import "CAYNodeDeleteOperation.h"
 #import "CAYCocoaCayenne.h"
 #import "CAYBootstrapMessage.h"
 #import "CAYArcCreateOperation.h"
 #import "CAYArcDeleteOperation.h"
+#import "CAYRelationshipQuery.h"
 
 @implementation CAYObjectContext
 
@@ -57,6 +55,17 @@
 	id result = [[self connection] sendMessage:queryMessage];
 	[queryMessage release];
     
+    // a hack to let toOneFaults not refresh. without this hack
+    // removing a element in a one-to-many relationship will not
+    // be visible before a commit/query
+    // TODO: clean up! perhaps support refreshing parameter on all queries?
+    BOOL refreshing = YES;
+    if([query isKindOfClass:[CAYRelationshipQuery class]])
+    {
+        CAYRelationshipQuery *relq = (CAYRelationshipQuery *)query;
+        refreshing = [relq refreshing];
+    }
+    
     // TODO: results/rows are a mess. clean up!
     NSArray *results = [result results];
 	NSArray *rows = [results objectAtIndex:0];
@@ -79,55 +88,34 @@
             {
                 NSLog(@"old exist for oid. merge over values. %@ == %@", [row objectId],
[old objectId]);
 				
-				// TODO: handle the state of the old object
+				// TODO: handle the state of the old object. merge over changes? or delete diffs
+                // for objects refreshed from server?
                 
                 // clear out old values and set new values
-                [[old valuesRaw] setDictionary:[row valuesRaw]];
+                if(refreshing)
+                {
+                    [[old valuesRaw] setDictionary:[row valuesRaw]];
+                    [row setPersistenceState:PSTATE_COMMITTED];
+                }
                 row = old;
             }
             else
             {
                 NSLog(@"old does not exist for oid %@", [row objectId]);
                 [objectByObjectId setObject:row forKey:[row objectId]];
+                [row setPersistenceState:PSTATE_COMMITTED];
             }
 
             // update resultRows as soon as possible
             [resultRows addObject:row];
         
             [row setObjectContext:self];
-            // TODO: set initial persistant state
             
-            CAYObjEntity *objEntity = [[self entityResolver] objEntityForClass:[row class]];
-            if(objEntity)
-            {
-                NSDictionary *relations = [objEntity relationships];
-                NSEnumerator *enumerator = [relations keyEnumerator];
-                NSString *relname;
-                while(relname = [enumerator nextObject])
-                {
-                    CAYClientObjRelationship *rel = (CAYClientObjRelationship *)[relations
objectForKey:relname];
-                    NSLog(@"create fault for relation named %@: %@", relname, rel);
-                    CAYFault *fault = nil;
-                    if([rel isToMany])
-                    {
-                        fault = [[CAYToManyFault alloc] initWithSourceObject:row relationshipName:relname];
-                    }
-                    else
-                    {
-                        fault = [[CAYToOneFault alloc] initWithSourceObject:row relationshipName:relname];
-                    }
-                    [[row valuesRaw] setValue:fault forKey:relname];
-                    [fault release];
-                }
-            }
-            else
+            // create faults for all relationships
+            if(refreshing)
             {
-                NSLog(@"ERROR: Could not find ObjEntity for class %@", [row class]);
+                [row createFaults];
             }
-			
-			// set persistence state to comitted as this rows values are fresh from
-			// the database
-			[row setPersistenceState:PSTATE_COMMITTED];
         }
         else
         {

Modified: incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.h
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.h?view=diff&rev=442865&r1=442864&r2=442865
==============================================================================
--- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.h (original)
+++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.h Tue Sep 12 23:31:20
2006
@@ -38,6 +38,7 @@
 -(CAYObjectContext *)objectContext;
 
 -(NSMutableDictionary *)valuesRaw;
+-(void)createFaults;
 
 -(void)setValue:(id)value forKey:(NSString *)key;
 -(id)valueForKey:(NSString *)key;
@@ -47,6 +48,6 @@
 -(void)removeToManyTarget:(CAYPersistentObject *)value forKey:(NSString *)key setReverse:(BOOL)setrev;
 -(void)setReverseRelationship:(CAYPersistentObject *)value forKey:(NSString *)key;
 -(void)unsetReverseRelationship:(CAYPersistentObject *)value forKey:(NSString *)key;
-
+-(void)willConnect:(CAYPersistentObject *)value forKey:(NSString *)key;
 
 @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=442865&r1=442864&r2=442865
==============================================================================
--- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m (original)
+++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m Tue Sep 12 23:31:20
2006
@@ -24,6 +24,9 @@
 #import "CAYObjEntity.h"
 #import "CAYClientObjRelationship.h"
 #import "CAYCocoaCayenne.h"
+#import "CAYFault.h"
+#import "CAYToManyFault.h"
+#import "CAYToOneFault.h"
 
 @implementation CAYPersistentObject
 
@@ -99,6 +102,30 @@
     return values;
 }
 
+-(void)createFaults
+{
+    CAYObjEntity *objEntity = [[[self objectContext] entityResolver] lookupObjEntity:self];
+    NSDictionary *relations = [objEntity relationships];
+    NSEnumerator *enumerator = [relations keyEnumerator];
+    NSString *relname;
+    while(relname = [enumerator nextObject])
+    {
+        CAYClientObjRelationship *rel = (CAYClientObjRelationship *)[relations objectForKey:relname];
+        NSLog(@"create fault for relation %@.%@: %@", [objEntity name], relname, rel);
+        CAYFault *fault = nil;
+        if([rel isToMany])
+        {
+            fault = [[CAYToManyFault alloc] initWithSourceObject:self relationshipName:relname];
+        }
+        else
+        {
+            fault = [[CAYToOneFault alloc] initWithSourceObject:self relationshipName:relname];
+        }
+        [[self valuesRaw] setValue:fault forKey:relname];
+        [fault release];
+    }
+}
+
 -(void)setValue:(id)value forKey:(NSString *)key
 {
     // NSLog(@"DEBUG: set value %@ for key %@ of type %@", value, key, [value class]);
@@ -120,17 +147,8 @@
 			
 			NSArray *oldValue = [self valueForKey:key];
             
-			// register new objects in the context
-			// TODO: replace with autoregister stuff in willConnect
-            NSEnumerator *enumerator = [value objectEnumerator];
+            NSEnumerator *enumerator;
             CAYPersistentObject *element;
-            while(element = [enumerator nextObject])
-            {
-                if(![element objectContext])
-                {
-                    [[self objectContext] registerNewObject:element];
-                }
-            }
 
 			// handle new objects for the relationship
 			NSMutableArray *newElements = [[NSMutableArray alloc] init];
@@ -151,7 +169,7 @@
 			enumerator = [removedElements objectEnumerator];
             while(element = [enumerator nextObject])
             {
-				NSLog(@"removed element for rel %@: %@", key, element);
+				NSLog(@"remove element for rel %@: %@", key, element);
 				[self removeToManyTarget:element forKey:key setReverse:YES];
 			}
 			[removedElements release];
@@ -190,7 +208,10 @@
 
 -(void)setToOneTarget:(CAYPersistentObject *)value forKey:(NSString *)key setReverse:(BOOL)setrev
 {
-	NSLog(@"DEBUG: setToOneTarget value class: %@, key: %@, setrev: %@", [value class], key,
setrev ? @"YES" : @"NO");
+	// NSLog(@"DEBUG: setToOneTarget value class: %@, key: %@, setrev: %@", [value class], key,
setrev ? @"YES" : @"NO");
+
+	[self willConnect:value forKey:key];
+	
 	id oldTarget = [self valueForKey:key];
 	
 	// check for special case of no change
@@ -219,8 +240,10 @@
 
 -(void)addToManyTarget:(CAYPersistentObject *)value forKey:(NSString *)key setReverse:(BOOL)setrev
 {
-	NSLog(@"DEBUG: addToManyTarget value class: %@, key: %@, setrev: %@", [value class], key,
setrev ? @"YES" : @"NO");
+	// NSLog(@"DEBUG: addToManyTarget value class: %@, key: %@, setrev: %@", [value class],
key, setrev ? @"YES" : @"NO");
 
+	[self willConnect:value forKey:key];
+	
 	if(!value)
 	{
 		// TODO: throw
@@ -241,7 +264,7 @@
 
 -(void)removeToManyTarget:(CAYPersistentObject *)value forKey:(NSString *)key setReverse:(BOOL)setrev
 {
-	NSLog(@"DEBUG: removeToManyTarget value class: %@, key: %@, setrev: %@", [value class],
key, setrev ? @"YES" : @"NO");
+	// NSLog(@"DEBUG: removeToManyTarget value class: %@, key: %@, setrev: %@", [value class],
key, setrev ? @"YES" : @"NO");
 
 	if(!value)
 	{
@@ -257,7 +280,7 @@
 		[self setPersistenceState:PSTATE_MODIFIED];
 	}
 	
-	NSLog(@"DEBUG: value for %@.%@ is of type %@", [self class], key, [list class]);
+	// NSLog(@"DEBUG: value for %@.%@ is of type %@", [self class], key, [list class]);
 	
 	[list removeObject:value];
 	
@@ -270,7 +293,7 @@
 
 -(void)setReverseRelationship:(CAYPersistentObject *)value forKey:(NSString *)key
 {
-	NSLog(@"DEBUG: setReverseRelationship value class: %@, key: %@", [value class], key);
+	// NSLog(@"DEBUG: setReverseRelationship value class: %@, key: %@", [value class], key);
 	
 	// find relationship
     CAYObjEntity *objEntity = [[[self objectContext] entityResolver] lookupObjEntity:self];
@@ -295,7 +318,7 @@
 
 -(void)unsetReverseRelationship:(CAYPersistentObject *)value forKey:(NSString *)key
 {
-	NSLog(@"DEBUG: unsetReverseRelationship value class: %@, key: %@", [value class], key);
+	// NSLog(@"DEBUG: unsetReverseRelationship value class: %@, key: %@", [value class], key);
 	
 	// find relationship
     CAYObjEntity *objEntity = [[[self objectContext] entityResolver] lookupObjEntity:self];
@@ -316,6 +339,25 @@
 			[value setToOneTarget:nil forKey:[revRelationship name] setReverse:NO];
 		}
 	}
+}
+
+-(void)willConnect:(CAYPersistentObject *)value forKey:(NSString *)key
+{	
+	if(!value)
+	{
+		return;
+	}
+	
+	if([self objectContext] && ![value objectContext])
+	{
+		[[self objectContext] registerNewObject:value];
+	} 
+    else if([value objectContext] && ![self objectContext])
+	{
+		[[value objectContext] registerNewObject:self];
+	}
+	
+    // TODO: give error if different data contexts?
 }
 
 -(void)dealloc

Modified: incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYRelationshipQuery.h
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYRelationshipQuery.h?view=diff&rev=442865&r1=442864&r2=442865
==============================================================================
--- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYRelationshipQuery.h (original)
+++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYRelationshipQuery.h Tue Sep 12
23:31:20 2006
@@ -31,5 +31,7 @@
 -(CAYObjectId *)objectId;
 -(void)setRelationshipName:(NSString *)reln;
 -(NSString *)relationshipName;
+-(void)setRefreshing:(BOOL)r;
+-(BOOL)refreshing;
 
 @end

Modified: incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYRelationshipQuery.m
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYRelationshipQuery.m?view=diff&rev=442865&r1=442864&r2=442865
==============================================================================
--- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYRelationshipQuery.m (original)
+++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYRelationshipQuery.m Tue Sep 12
23:31:20 2006
@@ -22,10 +22,20 @@
 
 @implementation CAYRelationshipQuery
 
+-(id)init
+{
+    self = [super init];
+    if(self)
+    {
+        [self setRefreshing:YES];
+    }
+    return self;
+}
+
 -(id)initWithCoder:(NSCoder*)coder
 {
-	[super init];
-	refreshing = [coder decodeBoolForKey:@"refreshing"];
+	[self init];
+	[self setRefreshing:[coder decodeBoolForKey:@"refreshing"]];
 	[self setObjectId:[coder decodeObjectForKey:@"objectId"]];
 	[self setRelationshipName:[coder decodeObjectForKey:@"relationshipName"]];
 	return self;
@@ -62,10 +72,20 @@
 	return relationshipName;
 }
 
+-(void)setRefreshing:(BOOL)r
+{
+    refreshing = r;
+}
+
+-(BOOL)refreshing
+{
+    return refreshing;
+}
+
 -(void)dealloc
 {
-	[relationshipName release];
-	[objectId release];
+	[self setRelationshipName:nil];
+	[self setObjectId:nil];
 	[super dealloc];
 }
 

Modified: incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYToOneFault.m
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYToOneFault.m?view=diff&rev=442865&r1=442864&r2=442865
==============================================================================
--- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYToOneFault.m (original)
+++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYToOneFault.m Tue Sep 12 23:31:20
2006
@@ -31,6 +31,7 @@
     CAYRelationshipQuery *q = [[CAYRelationshipQuery alloc] init];
     [q setObjectId:[[self sourceObject] objectId]];
     [q setRelationshipName:[self relationshipName]];
+    [q setRefreshing:NO];
     
     CAYObjectContext *ctxt = [[self sourceObject] objectContext];
     NSArray *rows = (NSArray *)[ctxt performQuery:q];
@@ -43,7 +44,6 @@
     {
         NSLog(@"ERROR: toOne fault resolve returned %i rows", [rows count]);
     }
-    // TODO: check in local ObjectContext first
     
     [q release];
     return row;



Mime
View raw message