summaryrefslogtreecommitdiffstats
path: root/Classes/Topic.m
blob: 36be5f68ea06f21aba488eb81d10e098cdf65d30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
//  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