/***************************************************************** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. ****************************************************************/ #import "CAYEntityResolver.h" #import "CAYDataMap.h" #import "CAYObjEntity.h" #import "CAYCocoaCayenne.h" @implementation CAYEntityResolver -(id)init { self = [super init]; if(self) { [self setClassMapping:[CAYCocoaCayenne classMapping]]; } return self; } -(id)initWithCoder:(NSCoder*)coder { [self init]; [self setMaps:[coder decodeObjectForKey:@"maps"]]; return self; } -(void)encodeWithCoder:(NSCoder*)coder { [coder encodeObject:maps forKey:@"maps"]; } -(void)setMaps:(NSArray *)m { [m retain]; [maps release]; maps = m; } -(NSArray *)maps { return maps; } -(void)setClassMapping:(NSMutableDictionary *)cm { [cm retain]; [classMapping release]; classMapping = cm; } -(NSMutableDictionary *)classMapping { return classMapping; } -(void)updateClassMapping { //NSMutableDictionary *cm = [NSMutableDictionary dictionary]; NSMutableDictionary *cm = classMapping; // TODO: handle more than one map? CAYDataMap *dataMap = [[self maps] objectAtIndex:0]; //NSLog(@"DEBUG: datamap.name: %@", [dataMap valueForKey:@"name"]); NSMutableDictionary *objEntityMap = [dataMap objEntityMap]; NSEnumerator *enumerator = [objEntityMap keyEnumerator]; NSString *entityName; while(entityName = [enumerator nextObject]) { CAYObjEntity *entity = [objEntityMap objectForKey:entityName]; Class cl = NSClassFromString([entity name]); if(cl) { NSLog(@"INFO: mapping client %@ to server %@", [entity name], [entity serverClassName]); [cm setObject:[entity name] forKey:[entity serverClassName]]; } else { NSLog(@"ERROR: Could not find cocoa class %@ for server class %@", [entity name], [entity serverClassName]); } } } -(CAYObjEntity *)objEntityForClass:(Class)cl { // TODO: handle more than one map? CAYDataMap *dataMap = [[self maps] objectAtIndex:0]; //NSLog(@"DEBUG: datamap.name: %@", [dataMap valueForKey:@"name"]); NSMutableDictionary *objEntityMap = [dataMap objEntityMap]; // TODO: cache the lookup using a NSDictionary! NSEnumerator *enumerator = [objEntityMap keyEnumerator]; NSString *entityName; while(entityName = [enumerator nextObject]) { CAYObjEntity *entity = [objEntityMap objectForKey:entityName]; Class tc = NSClassFromString([entity name]); if([cl isEqualTo:tc]) { return entity; } } NSLog(@"ERROR: Could not find ObjEntity for class %@", cl); return nil; } -(CAYObjEntity *)lookupObjEntity:(CAYPersistentObject *)dataObject { // TODO: handle more than one map? CAYDataMap *dataMap = [[self maps] objectAtIndex:0]; //NSLog(@"DEBUG: datamap.name: %@", [dataMap valueForKey:@"name"]); NSMutableDictionary *objEntityMap = [dataMap objEntityMap]; CAYObjectId *oid = [dataObject objectId]; NSString *entityName = [oid entityName]; CAYObjEntity *entity = [objEntityMap valueForKey:entityName]; if(!entity) { NSLog(@"ERROR: Could not find ObjEntity for object %@", dataObject); } return entity; } -(NSString *)description { NSString *description = [[NSString alloc] initWithFormat:@"%@ {maps = %@}", [self class], [self maps]]; [description autorelease]; return description; } -(void)dealloc { [self setMaps:nil]; [self setClassMapping:nil]; [super dealloc]; } @end