/***************************************************************** * 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 "CAYObjectId.h" @implementation CAYObjectId -(id)initWithCoder:(NSCoder*)coder { [super init]; [self setEntityName:[coder decodeObjectForKey:@"entityName"]]; [self setObjectIdKeys:[coder decodeObjectForKey:@"objectIdKeys"]]; [self setSingleKey:[coder decodeObjectForKey:@"singleKey"]]; [self setSingleValue:[coder decodeObjectForKey:@"singleValue"]]; [self setTempKey:[coder decodeObjectForKey:@"key"]]; // TODO: should not need to convert NSNull to nil. create // a nicer solution // check/repair tempKey if([[self tempKey] isKindOfClass:[NSNull class]]) { [self setTempKey:nil]; } // repair singleKey if([[self singleKey] isKindOfClass:[NSNull class]]) { [self setSingleKey:nil]; } // repair objectIdKeys if([[self objectIdKeys] isKindOfClass:[NSNull class]]) { [self setObjectIdKeys:nil]; } return self; } -(void)encodeWithCoder:(NSCoder*)coder { [coder encodeObject:[self entityName] forKey:@"entityName"]; [coder encodeObject:[self singleKey] forKey:@"singleKey"]; [coder encodeObject:[self singleValue] forKey:@"singleValue"]; [coder encodeObject:[self objectIdKeys] forKey:@"objectIdKeys"]; // TODO: fix problem: the key "key" result in a ugly unserialize exception at the server side. if([self tempKey]) { [coder encodeObject:[self tempKey] forKey:@"key"]; } } - (id)copyWithZone:(NSZone *)zone { id copy = [[[self class] allocWithZone:zone] init]; [copy setEntityName:[self entityName]]; [copy setObjectIdKeys:[self objectIdKeys]]; [copy setSingleKey:[self singleKey]]; [copy setSingleValue:[self singleValue]]; [copy setTempKey:[self tempKey]]; return copy; } -(void)setEntityName:(NSString *)en { [en retain]; [entityName release]; entityName = en; } -(NSString *)entityName { return entityName; } -(void)setObjectIdKeys:(NSDictionary *)oidks { [oidks retain]; [objectIdKeys release]; objectIdKeys = oidks; } -(NSDictionary *)objectIdKeys { return objectIdKeys; } -(void)setSingleKey:(NSString *)sk { [sk retain]; [singleKey release]; singleKey = sk; } -(NSString *)singleKey { return singleKey; } -(void)setSingleValue:(NSObject *)sv { [sv retain]; [singleValue release]; singleValue = sv; } -(NSObject *)singleValue { return singleValue; } -(void)setTempKey:(NSData *)k { [k retain]; [tempKey release]; tempKey = k; } -(NSData *)tempKey { return tempKey; } - (unsigned)hash { /* file:///Developer/ADC%20Reference%20Library/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSObject/hash http://www.mulle-kybernetik.com/artikel/Optimization/opti-7.html http://www.cocoabuilder.com/archive/message/cocoa/2003/12/18/546 */ if([self tempKey]) { return ([entityName hash] ^ [[self tempKey] hash]); } if([self singleValue]) { return ([entityName hash] ^ [[self singleValue] hash]); } // TODO: include other parameters in hash code return [entityName hash]; } - (BOOL)isEqual:(id)anObject { if(![[self entityName] isEqualToString:[anObject entityName]]) { return NO; } if([self singleKey]) { if(![[self singleKey] isEqualToString:[anObject singleKey]]) { return NO; } if(![[self singleValue] isEqual:[anObject singleValue]]) { return NO; } } if([self objectIdKeys]) { if(![[self objectIdKeys] isEqualToDictionary:[anObject objectIdKeys]]) { return NO; } } if([self tempKey]) { if(![[self tempKey] isEqualToData:[anObject tempKey]]) { return NO; } } // TODO: check all values return YES; } -(NSString *)description { NSString *result; if([self tempKey]) { result = [[NSString alloc] initWithFormat:@"%@ {%@; TEMP: %@}", [self class], [self entityName], [self tempKey]]; } else if ([self singleKey]) { result = [[NSString alloc] initWithFormat:@"%@ {%@; %@ = %@}", [self class], [self entityName], [self singleKey], [self singleValue]]; } else if ([self objectIdKeys]) { result = [[NSString alloc] initWithFormat:@"%@ {%@; objectIdKeys = %@}", [self class], [self entityName], [self objectIdKeys]]; } else { // should match one of the above. if not, output all values result = [[NSString alloc] initWithFormat:@"%@ {%@; objectIdKeys = %@; singleKey = %@; singleValue = %@}", [self class], [self entityName], [self objectIdKeys], [self singleKey], [self singleValue]]; } [result autorelease]; return result; } -(void)dealloc { [self setEntityName:nil]; [self setObjectIdKeys:nil]; [self setSingleKey:nil]; [self setSingleValue:nil]; [self setTempKey:nil]; [super dealloc]; } @end