//
//  TKMDManagedObject.m
//  ThemeKit
//
//  Created by Colin Cornaby on 4/19/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#import "TKMDManagedObject.h"


@implementation TKMDManagedObject

-(void)setMetadataForKey:(id)key forValue:(id)value
{
	NSManagedObject *metadataObject = [self valueForKey:@"metadata"];
	if(metadataObject==nil)
	{
		metadataObject = [NSEntityDescription insertNewObjectForEntityForName:@"TKResourceMetadata" inManagedObjectContext:[self managedObjectContext]];
		[self setValue:metadataObject forKey:@"metadata"];
	}
	NSMutableDictionary *metadataDict = [NSMutableDictionary dictionaryWithDictionary:[[[self valueForKey:@"metadata"] valueForKey:@"metadata"] propertyList]];
	if(metadataDict==nil)
		metadataDict = [[[NSMutableDictionary alloc] init] autorelease];
	[metadataDict setObject:value forKey:key];
	NSString *myError = @"";
	NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:metadataDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&myError];
	NSString *metadataAsString = [[NSString alloc] initWithData:plistData encoding:NSUTF8StringEncoding];
	[[self valueForKey:@"metadata"] setValue:metadataAsString forKey:@"metadata"];
}

-(id)metadataForKey:(id)key
{
	return [[[[self valueForKey:@"metadata"] valueForKey:@"metadata"] propertyList] objectForKey:key];
}

@end
