cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From torehal...@apache.org
Subject svn commit: r442242 - in /incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne: CAYCocoaCayenne.m CAYObjectContext.m CAYPersistentObject.h CAYPersistentObject.m
Date Mon, 11 Sep 2006 15:49:53 GMT
Author: torehalset
Date: Mon Sep 11 08:49:52 2006
New Revision: 442242

URL: http://svn.apache.org/viewvc?view=rev&rev=442242
Log:
 * moved from cayenne-1.2 to cayenne-3.0
 * relationship add/remove improvement

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

Modified: incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYCocoaCayenne.m
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYCocoaCayenne.m?view=diff&rev=442242&r1=442241&r2=442242
==============================================================================
--- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYCocoaCayenne.m (original)
+++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYCocoaCayenne.m Mon Sep 11 08:49:52
2006
@@ -25,30 +25,30 @@
 +(NSMutableDictionary *)classMapping;
 {
     NSMutableDictionary *classMapping = [NSMutableDictionary dictionary];
-    //[classMapping setObject:@"CAYRemoteSession" forKey:@"org.objectstyle.cayenne.remote.RemoteSession"];
-    [classMapping setObject:@"CAYNamedQuery" forKey:@"org.objectstyle.cayenne.query.NamedQuery"];
-    [classMapping setObject:@"CAYRelationshipQuery" forKey:@"org.objectstyle.cayenne.query.RelationshipQuery"];
-    [classMapping setObject:@"CAYGenericResponse" forKey:@"org.objectstyle.cayenne.util.GenericResponse"];
-    [classMapping setObject:@"CAYObjectId" forKey:@"org.objectstyle.cayenne.ObjectId"];
-    [classMapping setObject:@"CAYObjEntity" forKey:@"org.objectstyle.cayenne.map.ObjEntity"];
-    [classMapping setObject:@"CAYClientObjRelationship" forKey:@"org.objectstyle.cayenne.map.ClientObjRelationship"];
-    [classMapping setObject:@"CAYEntityResolver" forKey:@"org.objectstyle.cayenne.map.EntityResolver"];
-    [classMapping setObject:@"CAYDataMap" forKey:@"org.objectstyle.cayenne.map.DataMap"];
+    //[classMapping setObject:@"CAYRemoteSession" forKey:@"org.apache.cayenne.remote.RemoteSession"];
+    [classMapping setObject:@"CAYNamedQuery" forKey:@"org.apache.cayenne.query.NamedQuery"];
+    [classMapping setObject:@"CAYRelationshipQuery" forKey:@"org.apache.cayenne.query.RelationshipQuery"];
+    [classMapping setObject:@"CAYGenericResponse" forKey:@"org.apache.cayenne.util.GenericResponse"];
+    [classMapping setObject:@"CAYObjectId" forKey:@"org.apache.cayenne.ObjectId"];
+    [classMapping setObject:@"CAYObjEntity" forKey:@"org.apache.cayenne.map.ObjEntity"];
+    [classMapping setObject:@"CAYClientObjRelationship" forKey:@"org.apache.cayenne.map.ClientObjRelationship"];
+    [classMapping setObject:@"CAYEntityResolver" forKey:@"org.apache.cayenne.map.EntityResolver"];
+    [classMapping setObject:@"CAYDataMap" forKey:@"org.apache.cayenne.map.DataMap"];
     
     // message
-    [classMapping setObject:@"CAYBootstrapMessage" forKey:@"org.objectstyle.cayenne.remote.BootstrapMessage"];
-    [classMapping setObject:@"CAYQueryMessage" forKey:@"org.objectstyle.cayenne.remote.QueryMessage"];
-    [classMapping setObject:@"CAYSyncMessage" forKey:@"org.objectstyle.cayenne.remote.SyncMessage"];
+    [classMapping setObject:@"CAYBootstrapMessage" forKey:@"org.apache.cayenne.remote.BootstrapMessage"];
+    [classMapping setObject:@"CAYQueryMessage" forKey:@"org.apache.cayenne.remote.QueryMessage"];
+    [classMapping setObject:@"CAYSyncMessage" forKey:@"org.apache.cayenne.remote.SyncMessage"];
     
     // graph
-    // abstract [classMapping setObject:@"CAYNodeDiff" forKey:@"org.objectstyle.cayenne.graph.NodeDiff"];
-    [classMapping setObject:@"CAYCompoundDiff" forKey:@"org.objectstyle.cayenne.graph.CompoundDiff"];
-    [classMapping setObject:@"CAYNodeCreateOperation" forKey:@"org.objectstyle.cayenne.graph.NodeCreateOperation"];
-    [classMapping setObject:@"CAYNodePropertyChangeOperation" forKey:@"org.objectstyle.cayenne.graph.NodePropertyChangeOperation"];
-    [classMapping setObject:@"CAYNodeDeleteOperation" forKey:@"org.objectstyle.cayenne.graph.NodeDeleteOperation"];
-    [classMapping setObject:@"CAYNodeIdChangeOperation" forKey:@"org.objectstyle.cayenne.graph.NodeIdChangeOperation"];
-    [classMapping setObject:@"CAYArcCreateOperation" forKey:@"org.objectstyle.cayenne.graph.ArcCreateOperation"];
-    [classMapping setObject:@"CAYArcDeleteOperation" forKey:@"org.objectstyle.cayenne.graph.ArcDeleteOperation"];
+    // abstract [classMapping setObject:@"CAYNodeDiff" forKey:@"org.apache.cayenne.graph.NodeDiff"];
+    [classMapping setObject:@"CAYCompoundDiff" forKey:@"org.apache.cayenne.graph.CompoundDiff"];
+    [classMapping setObject:@"CAYNodeCreateOperation" forKey:@"org.apache.cayenne.graph.NodeCreateOperation"];
+    [classMapping setObject:@"CAYNodePropertyChangeOperation" forKey:@"org.apache.cayenne.graph.NodePropertyChangeOperation"];
+    [classMapping setObject:@"CAYNodeDeleteOperation" forKey:@"org.apache.cayenne.graph.NodeDeleteOperation"];
+    [classMapping setObject:@"CAYNodeIdChangeOperation" forKey:@"org.apache.cayenne.graph.NodeIdChangeOperation"];
+    [classMapping setObject:@"CAYArcCreateOperation" forKey:@"org.apache.cayenne.graph.ArcCreateOperation"];
+    [classMapping setObject:@"CAYArcDeleteOperation" forKey:@"org.apache.cayenne.graph.ArcDeleteOperation"];
     
     return classMapping;
 }

Modified: incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjectContext.m
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjectContext.m?view=diff&rev=442242&r1=442241&r2=442242
==============================================================================
--- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjectContext.m (original)
+++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYObjectContext.m Mon Sep 11 08:49:52
2006
@@ -145,11 +145,11 @@
 
 -(void) arcPropertyChanged:(CAYPersistentObject *)object forProperty:(NSString *)property
fromOld:(NSObject *)oldValue toNew:(NSObject *)newValue
 {
-    NSLog(@"arc prop %@ changed from %@ to %@", property, oldValue, newValue);
+    // NSLog(@"DEBUG: arc prop %@ changed from %@ to %@", property, oldValue, newValue);
     
     if([oldValue isKindOfClass:[CAYPersistentObject class]])
     {
-        NSLog(@"add a arcDelete");
+        NSLog(@"add a arcDelete %@.%@", [object class], property);
         CAYPersistentObject *ov = (CAYPersistentObject *)oldValue;
         CAYArcDeleteOperation *diff = [[CAYArcDeleteOperation alloc] initWithNodeId:[object
objectId] targetNodeId:[ov objectId] arcId:property];
         [diffs addObject:diff];
@@ -158,7 +158,7 @@
 
     if([newValue isKindOfClass:[CAYPersistentObject class]])
     {
-        NSLog(@"add a arcCreate");
+        NSLog(@"add a arcCreate %@.%@", [object class], property);
         CAYPersistentObject *nv = (CAYPersistentObject *)newValue;
         CAYArcCreateOperation *diff = [[CAYArcCreateOperation alloc] initWithNodeId:[object
objectId] targetNodeId:[nv objectId] arcId:property];
         [diffs addObject:diff];
@@ -178,13 +178,13 @@
     id bootresult = [[self connection] sendMessage:bootstrapMsg];
     if([bootresult isKindOfClass:[CAYEntityResolver class]])
     {
-        NSLog(@" ****** got an CATEntityResolver");
+        NSLog(@" ****** got an CAYEntityResolver");
         CAYEntityResolver *resolver = (CAYEntityResolver *)bootresult;
         [self setEntityResolver:resolver];
     }
     else
     {
-        NSLog(@"ERROR: bootstrap answer not of type CATEntityResolver, but %@", [bootresult
class]);
+        NSLog(@"ERROR: bootstrap answer not of type CAYEntityResolver, but %@", [bootresult
class]);
     }
     [bootstrapMsg release];
     

Modified: incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.h
URL: http://svn.apache.org/viewvc/incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.h?view=diff&rev=442242&r1=442241&r2=442242
==============================================================================
--- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.h (original)
+++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.h Mon Sep 11 08:49:52
2006
@@ -42,4 +42,11 @@
 -(void)setValue:(id)value forKey:(NSString *)key;
 -(id)valueForKey:(NSString *)key;
 
+-(void)setToOneTarget:(CAYPersistentObject *)value forKey:(NSString *)key setReverse:(BOOL)setrev;
+-(void)addToManyTarget:(CAYPersistentObject *)value forKey:(NSString *)key setReverse:(BOOL)setrev;
+-(void)removeToManyTarget:(CAYPersistentObject *)value forKey:(NSString *)key setReverse:(BOOL)setrev;
+-(void)setReverseRelationship:(CAYPersistentObject *)value forKey:(NSString *)key;
+-(void)unsetReverseRelationship:(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=442242&r1=442241&r2=442242
==============================================================================
--- incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m (original)
+++ incubator/cayenne/sandbox/CocoaCayenne/CocoaCayenne/CAYPersistentObject.m Mon Sep 11 08:49:52
2006
@@ -86,7 +86,8 @@
 -(NSString *)description
 {
     NSString *result;
-    result = [[NSString alloc] initWithFormat:@"{objectContext = %@; objectId = %@; values
= %@}", [self objectContext], [self objectId], values];
+	// TODO: it is dangerous to include relational values as it can result in a neverending
loop..
+    result = [[NSString alloc] initWithFormat:@"{objectContext = %@; objectId = %@; values
= %@}", [self objectContext], [self objectId], /*values*/ @"...(TODO)...)"];
     [result autorelease];
     return result;
 }
@@ -98,12 +99,12 @@
 
 -(void)setValue:(id)value forKey:(NSString *)key
 {
-    NSLog(@"set value %@ for key %@ of type %@", value, key, [value class]);
+    // NSLog(@"DEBUG: set value %@ for key %@ of type %@", value, key, [value class]);
     
     // see if the key is a relationship
     CAYObjEntity *objEntity = [[[self objectContext] entityResolver] lookupObjEntity:self];
     CAYClientObjRelationship *relationship = [[objEntity relationships] valueForKey:key];
-    
+	
     if (relationship)
     {
         
@@ -114,29 +115,44 @@
             {
                 NSLog(@"ERROR: toMany argument should be of type NSArray, not %@", [value
class]);
             }
+			
+			NSArray *oldValue = [self valueForKey:key];
             
+			// register new objects in the context
+			// TODO: replace with autoregister stuff in willConnect
             NSEnumerator *enumerator = [value objectEnumerator];
-            // TODO: handle and give error if not CAYPersistentObject
             CAYPersistentObject *element;
             while(element = [enumerator nextObject])
             {
-                
-                // find reverse relationship. could be nil.
-                // CAYObjEntity *revObjEntity = [[[self objectContext] entityResolver] lookupObjEntity:element];
-                // CAYClientObjRelationship *revRelationship = [[revObjEntity relationships]
valueForKey:[relationship reverseRelationshipName]];
-                
                 if(![element objectContext])
                 {
                     [[self objectContext] registerNewObject:element];
-                    
-                    // TODO: set relation both ways
-                    [[self objectContext] arcPropertyChanged:self forProperty:key fromOld:nil
toNew:element];
-                    [[self objectContext] arcPropertyChanged:element forProperty:[relationship
reverseRelationshipName] fromOld:nil toNew:self];
-                    // TODO: or perhaps use 'element's setValue:forKey? could lead to infinite
loop..
                 }
             }
-            // TODO: check for new objects that have a objectContext
-            // TODO: check for objects that are not present any more
+
+			// handle new objects for the relationship
+			NSMutableArray *newElements = [[NSMutableArray alloc] init];
+			[newElements addObjectsFromArray:value];
+			[newElements removeObjectsInArray:oldValue];
+			enumerator = [newElements objectEnumerator];
+            while(element = [enumerator nextObject])
+            {
+				NSLog(@"new element for rel %@: %@", key, element);
+				[self addToManyTarget:element forKey:key setReverse:YES];
+			}
+			[newElements release];
+			
+			// handle objects that are not longer in the relationship
+			NSMutableArray *removedElements = [[NSMutableArray alloc] init];
+			[removedElements addObjectsFromArray:oldValue];
+			[removedElements removeObjectsInArray:value];
+			enumerator = [removedElements objectEnumerator];
+            while(element = [enumerator nextObject])
+            {
+				NSLog(@"removed element for rel %@: %@", key, element);
+				[self removeToManyTarget:element forKey:key setReverse:YES];
+			}
+			[removedElements release];
         }
         else 
         {
@@ -146,24 +162,15 @@
                 NSLog(@"ERROR: toMany argument should be of type CAYPersistentObject, not
%@", [value class]);
             }
             
-            // a to one relationship
-            [[self objectContext] arcPropertyChanged:self forProperty:key fromOld:[values
objectForKey:key] toNew:value];
-            
-            // find reverse relationship. could be nil.
-            // CAYObjEntity *revObjEntity = [[[self objectContext] entityResolver] lookupObjEntity:value];
-            // CAYClientObjRelationship *revRelationship = [[revObjEntity relationships]
valueForKey:[relationship reverseRelationshipName]];
+			[self setToOneTarget:value forKey:key setReverse:YES];
         }
-        
-        // handle reverse relationship
-        // TODO: handle reverse relationship without creating a neverending loop...
     }
     else
     {
         // a none-relationship property
         [[self objectContext] propertyChanged:self forProperty:key fromOld:[values objectForKey:key]
toNew:value];
+		[values setValue:value forKey:key];
     }
-    
-    [values setValue:value forKey:key];
 }
 
 -(id)valueForKey:(NSString *)key
@@ -177,6 +184,135 @@
         [values setValue:val forKey:key];
     }
 	return [values objectForKey:key];
+}
+
+-(void)setToOneTarget:(CAYPersistentObject *)value forKey:(NSString *)key setReverse:(BOOL)setrev
+{
+	NSLog(@"DEBUG: setToOneTarget value class: %@, key: %@, setrev: %@", [value class], key,
setrev ? @"YES" : @"NO");
+	id oldTarget = [self valueForKey:key];
+	if(oldTarget == value)
+	{
+		return;
+	}
+	
+	[[self objectContext] arcPropertyChanged:self forProperty:key fromOld:oldTarget toNew:value];
+	
+	if(setrev)
+	{
+		if([oldTarget isKindOfClass:[CAYPersistentObject class]])
+		{
+			// TODO: unset reverse
+			[self unsetReverseRelationship:oldTarget forKey:key];
+		}
+		
+		if(value)
+		{
+			[self setReverseRelationship:value forKey:key];
+		}
+	}
+	
+	[values setValue:value forKey:key];
+}
+
+-(void)addToManyTarget:(CAYPersistentObject *)value forKey:(NSString *)key setReverse:(BOOL)setrev
+{
+	NSLog(@"DEBUG: addToManyTarget value class: %@, key: %@, setrev: %@", [value class], key,
setrev ? @"YES" : @"NO");
+	if(!value)
+	{
+		// TODO: throw
+		NSLog(@"ERROR. can not add nil to %@.%@", [self class], key);
+		return;
+	}
+	NSMutableArray *list = [self valueForKey:key];
+	
+	[[self objectContext] arcPropertyChanged:self forProperty:key fromOld:nil toNew:value];
+	
+	[list addObject:value];
+	
+	if(setrev)
+	{
+		[self setReverseRelationship:value forKey:key];
+	}
+}
+
+-(void)removeToManyTarget:(CAYPersistentObject *)value forKey:(NSString *)key setReverse:(BOOL)setrev
+{
+	NSLog(@"DEBUG: removeToManyTarget value class: %@, key: %@, setrev: %@", [value class],
key, setrev ? @"YES" : @"NO");
+	if(!value)
+	{
+		// TODO: throw
+		NSLog(@"ERROR. can not remove nil to %@.%@", [self class], key);
+		return;
+	}
+	NSMutableArray *list = [self valueForKey:key];
+	
+	[[self objectContext] arcPropertyChanged:self forProperty:key fromOld:value toNew:nil];
+	// TODO:
+	/*
+	 if (persistenceState == PersistenceState.COMMITTED) {
+		 persistenceState = PersistenceState.MODIFIED;
+	 }
+	 */
+	
+	NSLog(@"DEBUG: value for %@.%@ is of type %@", [self class], key, [list class]);
+	
+	[list removeObject:value];
+	
+	if(setrev)
+	{
+		[self unsetReverseRelationship:value forKey:key];
+	}
+	
+}
+
+-(void)setReverseRelationship:(CAYPersistentObject *)value forKey:(NSString *)key
+{
+	NSLog(@"DEBUG: setReverseRelationship value class: %@, key: %@", [value class], key);
+	
+	// find relationship
+    CAYObjEntity *objEntity = [[[self objectContext] entityResolver] lookupObjEntity:self];
+    CAYClientObjRelationship *relationship = [[objEntity relationships] valueForKey:key];
+	
+	// find reverse relationship. could be nil.
+	CAYObjEntity *revObjEntity = [[[self objectContext] entityResolver] lookupObjEntity:value];
+	CAYClientObjRelationship *revRelationship = [[revObjEntity relationships] valueForKey:[relationship
reverseRelationshipName]];
+	
+	if(revRelationship)
+	{
+		if([revRelationship isToMany])
+		{
+			[value addToManyTarget:self forKey:[revRelationship name] setReverse:NO];
+		}
+		else
+		{
+			[value setToOneTarget:self forKey:[revRelationship name] setReverse:NO];
+		}
+	}
+}
+
+-(void)unsetReverseRelationship:(CAYPersistentObject *)value forKey:(NSString *)key
+{
+	NSLog(@"DEBUG: unsetReverseRelationship value class: %@, key: %@", [value class], key);
+	
+	// find relationship
+    CAYObjEntity *objEntity = [[[self objectContext] entityResolver] lookupObjEntity:self];
+    CAYClientObjRelationship *relationship = [[objEntity relationships] valueForKey:key];
+	
+	// find reverse relationship. could be nil.
+	CAYObjEntity *revObjEntity = [[[self objectContext] entityResolver] lookupObjEntity:value];
+	CAYClientObjRelationship *revRelationship = [[revObjEntity relationships] valueForKey:[relationship
reverseRelationshipName]];
+	
+	if(revRelationship)
+	{
+		if([revRelationship isToMany])
+		{
+			[value removeToManyTarget:self forKey:[revRelationship name] setReverse:NO];
+		}
+		else
+		{
+			[value setToOneTarget:nil forKey:[revRelationship name] setReverse:NO];
+		}
+	}
 }
 
 -(void)dealloc



Mime
View raw message