// // 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, foreignId, sourceId; + (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.sourceId = [NSNumber numberWithInt:[result intForColumn:@"sourceId"]]; topic.guid = [result stringForColumn:@"link"]; topic.title = [result stringForColumn:@"title"]; topic.iconUrl = [result stringForColumn:@"iconUrl"]; topic.description = [result stringForColumn:@"description"]; topic.releaseDate = [result dateForColumn:@"releaseDate"]; topic.category = [result stringForColumn:@"category"]; topic.picCount = [NSNumber numberWithInt:[result intForColumn:@"picCount"]]; topic.foreignId = [NSNumber numberWithInt:[result intForColumn:@"foreignId"]]; return topic; } + (Topic *) initWithJsonObject:(NSDictionary *)obj { Topic *topic = [[[Topic alloc] init] autorelease]; //NSDictionary *info = [obj objectForKey:@"fields"]; topic.title = [obj objectForKey:@"title"]; topic.foreignId = [obj objectForKey:@"id"]; topic.iconUrl = [self getImageUrl:[obj objectForKey:@"image"]]; topic.description = [obj objectForKey:@"description"]; topic.releaseDate = [NSDate dateWithTimeIntervalSince1970:[[obj objectForKey:@"created_unixtime"] doubleValue]]; topic.category = [obj objectForKey:@"category"]; topic.picCount = [obj objectForKey:@"pic_count"]; return topic; } - (Topic *) init { return self; } + (NSString *) getImageUrl:(NSDictionary *)obj { if (obj == nil || [obj class] == [NSNull class]) return nil; else { return [obj objectForKey:@"original_url"]; } } + (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]; } - (void)serializeToDatabase:(FMDatabase *)db { [db retain]; [db executeUpdate:@"INSERT INTO picCasts \ (foreignId, link, title, description, releaseDate, category, iconUrl, picCount, sourceId) \ VALUES \ (?, ?, ?, ?, ?, ?, ?, ?, ?)", foreignId, guid, title, description, releaseDate, category, iconUrl, picCount, sourceId]; [db release]; } - (void)dealloc { [picCount release]; [guid release]; [title release]; [description release]; [releaseDate release]; [category release]; [iconUrl release]; [sourceId release]; [super dealloc]; } @end