// // 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; + (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 = [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; } + (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]; } + (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 { [db retain]; NSLog(@"title: %d", [releaseDate timeIntervalSince1970]); [db executeUpdate:@"INSERT INTO picCasts \ (foreignId, link, title, description, releaseDate, category, iconUrl, picCount) \ VALUES \ (?, ?, ?, ?, ?, ?, ?, ?)", foreignId, guid, title, description, releaseDate, category, iconUrl, picCount]; //NSLog(@"insert id:%d error: %@", [db lastInsertRowId], [db lastErrorMessage]); // [NSString stringWithFormat:@"number %d", i], // [NSNumber numberWithInt:i], // [NSDate date], // [NSNumber numberWithFloat:2.2f]]; [db release]; } - (void)dealloc { [picCount release]; [guid release]; [title release]; [description release]; [releaseDate release]; [category release]; [iconUrl release]; [super dealloc]; } @end