// // Topic.m // acidcow // // Created by Matthew Handler on 4/16/11. // Copyright 2011 Earl Industries. All rights reserved. // #import "Topic.h" #import "FMDatabase.h" #import "FMResultSet.h" @implementation Topic @synthesize guid, title, iconUrl, description, releaseDate, category, picCount; + (Topic *) initFromDatabaseRow:(FMResultSet *)result { NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateStyle:NSDateFormatterLongStyle]; [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"US"] autorelease]]; Topic *topic = [[[Topic alloc] init] autorelease]; topic.guid = [result stringForColumn:@"link"]; topic.title = [result stringForColumn:@"title"]; topic.iconUrl = [result stringForColumn:@"iconUrl"]; topic.description = [result stringForColumn:@"description"]; topic.releaseDate = [Topic stringToDate:[result stringForColumn:@"releaseDate"]]; topic.category = [result stringForColumn:@"category"]; topic.picCount = [result intForColumn:@"picCount"]; return topic; } + (NSString *) dateToString:(NSDate *)date { NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateStyle:NSDateFormatterLongStyle]; [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"US"] autorelease]]; return [dateFormatter stringFromDate:date]; } + (NSDate *) stringToDate:(NSString *)string { NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateStyle:NSDateFormatterLongStyle]; [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"US"] autorelease]]; return [dateFormatter dateFromString:string]; } - (void)serializeToDatabase:(FMDatabase *)db { //NSLog(@"title: %@", title); [db executeUpdate:@"INSERT INTO picCasts \ (foreignId, link, title, description, releaseDate, category, iconUrl, picCount) \ VALUES \ (?, ?, ?, ?, ?, ?, ?, ?)", 12, guid, title, description, [Topic dateToString:releaseDate], category, picCount]; //NSLog(@"insert id:%d error: %@", [db lastInsertRowId], [db lastErrorMessage]); // [NSString stringWithFormat:@"number %d", i], // [NSNumber numberWithInt:i], // [NSDate date], // [NSNumber numberWithFloat:2.2f]]; } - (void)dealloc { [picCount release]; [guid release]; [title release]; [description release]; [releaseDate release]; [category release]; [iconUrl release]; [super dealloc]; } @end