source: branches/CoreData/SapphireFrappliance/MetaData/SapphireMObjects/SapphireMovie.m @ 576

Last change on this file since 576 was 576, checked in by gbooker, 10 years ago

Split off the tasks of the entity directory to two pieces, one for files, one for directories. This allows the files one to be a lot more efficient

File size: 10.2 KB
Line 
1#import "SapphireMovie.h"
2#import "SapphireGenre.h"
3#import "SapphireCast.h"
4#import "SapphireDirector.h"
5#import "CoreDataSupportFunctions.h"
6#import "SapphireMetaDataSupport.h"
7#import "SapphireFileMetaData.h"
8#import "SapphireApplianceController.h"
9#import "SapphireXMLMovieData.h"
10#import "SapphireXMLData.h"
11
12#define ORDERED_CAST_KEY                @"orderedCast"
13#define ORDERED_CAST_DATA               @"orderedCastData"
14#define ORDERED_DIRECTOR_KEY    @"orderedDirectors"
15#define ORDERED_DIRECTOR_DATA   @"orderedDirectorsData"
16#define ORDERED_GENRES_KEY              @"orderedGenres"
17#define ORDERED_GENRES_DATA             @"orderedGenresData"
18
19
20@implementation SapphireMovie
21
22+ (SapphireMovie *)movieWithIMDB:(int)imdbNumber inContext:(NSManagedObjectContext *)moc
23{
24        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"imdbNumber == %d", imdbNumber];
25        NSArray *array = doFetchRequest(SapphireMovieName, moc, predicate);
26       
27        if([array count])
28                return [array objectAtIndex:0];
29       
30        SapphireMovie *ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireMovieName inManagedObjectContext:moc];
31        ret.imdbNumber = [NSNumber numberWithInt:imdbNumber];
32        return ret;
33}
34
35+ (SapphireMovie *)movieWithDictionary:(NSDictionary *)dict inContext:(NSManagedObjectContext *)moc
36{
37        NSString *imdbStr = [dict objectForKey:META_MOVIE_IDENTIFIER_KEY];
38        int imdbNumber = [SapphireMovie imdbNumberFromString:imdbStr];
39        if(imdbNumber == 0)
40                return nil;
41       
42        SapphireMovie *ret = [SapphireMovie movieWithIMDB:imdbNumber inContext:moc];
43        ret.title = [dict objectForKey:META_MOVIE_TITLE_KEY];
44        ret.releaseDate = [dict objectForKey:META_MOVIE_RELEASE_DATE_KEY];
45        ret.MPAARating = [dict objectForKey:META_MOVIE_MPAA_RATING_KEY];
46        ret.imdbRating = [dict objectForKey:META_MOVIE_IMDB_RATING_KEY];
47        ret.plot = [dict objectForKey:META_MOVIE_PLOT_KEY];
48        id value = [dict objectForKey:META_MOVIE_IMDB_250_KEY];
49        int i;
50        if((i = [value intValue]) != 0)
51                ret.imdbTop250RankingValue = i;
52        value = [dict objectForKey:META_MOVIE_OSCAR_KEY];
53        if((i = [value intValue]) != 0)
54                ret.oscarsWonValue = i;
55        NSMutableArray *genreArray = [NSMutableArray array];
56        NSEnumerator *genreEnum = [[dict objectForKey:META_MOVIE_GENRES_KEY] objectEnumerator];
57        NSString *genre;
58        while((genre = [genreEnum nextObject]) != nil)
59                [genreArray addObject:[SapphireGenre createGenre:genre inContext:moc]];
60        ret.orderedGenres = genreArray;
61       
62        NSMutableArray *dirArray = [NSMutableArray array];
63        NSEnumerator *directorEnum = [[dict objectForKey:META_MOVIE_DIRECTOR_KEY] objectEnumerator];
64        NSString *director;
65        while((director = [directorEnum nextObject]) != nil)
66                [dirArray addObject:[SapphireDirector createDirector:director inContext:moc]];
67        ret.orderedDirectors = dirArray;
68
69        NSMutableArray *castArray = [NSMutableArray array];
70        NSEnumerator *castEnum = [[dict objectForKey:META_MOVIE_CAST_KEY] objectEnumerator];
71        NSString *cast;
72        while((cast = [castEnum nextObject]) != nil)
73                [castArray addObject:[SapphireCast createCast:cast inContext:moc]];
74        ret.orderedCast = castArray;
75        return ret;
76}
77
78+ (int)imdbNumberFromString:(NSString *)imdbStr
79{
80        int imdbNumber = 0;
81        if(imdbStr != nil)
82        {
83                NSScanner *scanner = [NSScanner scannerWithString:imdbStr];
84                [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
85                [scanner scanInt:&imdbNumber];
86        }
87        return imdbNumber;
88}
89
90- (NSComparisonResult)releaseDateCompare:(SapphireMovie *)other
91{
92        return [self.releaseDate compare:other.releaseDate];
93}
94
95- (NSComparisonResult)titleCompare:(SapphireMovie *)other
96{
97        return [self.title compare:other.title];
98}
99
100- (NSArray *)orderedCast
101{
102        NSArray *ret = self.xml.orderedCast;
103        if(ret != nil)
104                return ret;
105        [self willAccessValueForKey:ORDERED_CAST_KEY];
106        ret = [self primitiveValueForKey:ORDERED_CAST_KEY];
107        [self didAccessValueForKey:ORDERED_CAST_KEY];
108        if(ret == nil)
109        {
110                NSData *propData = [self valueForKey:ORDERED_CAST_DATA];
111                if(propData != nil)
112                {
113                        NSArray *names = [NSKeyedUnarchiver unarchiveObjectWithData:propData];
114                        NSMutableArray *mutRet = [NSMutableArray arrayWithCapacity:[names count]];
115                        NSManagedObjectContext *moc = [self managedObjectContext];
116                        NSEnumerator *nameEnum = [names objectEnumerator];
117                        NSString *name;
118                        while((name = [nameEnum nextObject]) != nil)
119                        {
120                                SapphireCast *aCast = [SapphireCast cast:name inContext:moc];
121                                if(aCast != nil)
122                                        [mutRet addObject:aCast];
123                        }
124                        ret = [NSArray arrayWithArray:mutRet];
125                        [self setPrimitiveValue:ret forKey:ORDERED_CAST_KEY];
126                }
127        }
128       
129        return ret;
130}
131
132- (void)setOrderedCast:(NSArray *)ordered
133{
134        [self willChangeValueForKey:ORDERED_CAST_KEY];
135        [self setPrimitiveValue:ordered forKey:ORDERED_CAST_KEY];
136        [self didChangeValueForKey:ORDERED_CAST_KEY];
137        NSArray *arc = [ordered valueForKey:@"name"];
138        [self setValue:[NSKeyedArchiver archivedDataWithRootObject:arc] forKey:ORDERED_CAST_DATA];
139        [self.castSet addObjectsFromArray:ordered];
140}
141
142- (NSArray *)orderedGenres
143{
144        NSArray *ret = self.xml.orderedGenres;
145        if(ret != nil)
146                return ret;
147        [self willAccessValueForKey:ORDERED_GENRES_KEY];
148        ret = [self primitiveValueForKey:ORDERED_GENRES_KEY];
149        [self didAccessValueForKey:ORDERED_GENRES_KEY];
150        if(ret == nil)
151        {
152                NSData *propData = [self valueForKey:ORDERED_GENRES_DATA];
153                if(propData != nil)
154                {
155                        NSArray *genres = [NSKeyedUnarchiver unarchiveObjectWithData:propData];
156                        NSMutableArray *mutRet = [NSMutableArray arrayWithCapacity:[genres count]];
157                        NSManagedObjectContext *moc = [self managedObjectContext];
158                        NSEnumerator *genreEnum = [genres objectEnumerator];
159                        NSString *genre;
160                        while((genre = [genreEnum nextObject]) != nil)
161                        {
162                                SapphireGenre *aGenre = [SapphireGenre genre:genre inContext:moc];
163                                if(aGenre != nil)
164                                        [mutRet addObject:aGenre];
165                        }
166                        ret = [NSArray arrayWithArray:mutRet];
167                        [self setPrimitiveValue:ret forKey:ORDERED_GENRES_KEY];
168                }
169        }
170       
171        return ret;
172}
173
174- (void)setOrderedGenres:(NSArray *)ordered
175{
176        [self willChangeValueForKey:ORDERED_GENRES_KEY];
177        [self setPrimitiveValue:ordered forKey:ORDERED_GENRES_KEY];
178        [self didChangeValueForKey:ORDERED_GENRES_KEY];
179        NSArray *arc = [ordered valueForKey:@"name"];
180        [self setValue:[NSKeyedArchiver archivedDataWithRootObject:arc] forKey:ORDERED_GENRES_DATA];
181        [self.genresSet addObjectsFromArray:ordered];
182}
183
184- (NSArray *)orderedDirectors
185{
186        NSArray *ret = self.xml.orderedDirectors;
187        if(ret != nil)
188                return ret;
189        [self willAccessValueForKey:ORDERED_DIRECTOR_KEY];
190        ret = [self primitiveValueForKey:ORDERED_DIRECTOR_KEY];
191        [self didAccessValueForKey:ORDERED_DIRECTOR_KEY];
192        if(ret == nil)
193        {
194                NSData *propData = [self valueForKey:ORDERED_DIRECTOR_DATA];
195                if(propData != nil)
196                {
197                        NSArray *names = [NSKeyedUnarchiver unarchiveObjectWithData:propData];
198                        NSMutableArray *mutRet = [NSMutableArray arrayWithCapacity:[names count]];
199                        NSManagedObjectContext *moc = [self managedObjectContext];
200                        NSEnumerator *nameEnum = [names objectEnumerator];
201                        NSString *name;
202                        while((name = [nameEnum nextObject]) != nil)
203                        {
204                                SapphireDirector *aDir = [SapphireDirector director:name inContext:moc];
205                                if(aDir != nil)
206                                        [mutRet addObject:aDir];
207                        }
208                        ret = [NSArray arrayWithArray:mutRet];
209                        [self setPrimitiveValue:ret forKey:ORDERED_DIRECTOR_KEY];
210                }
211        }
212       
213        return ret;
214}
215
216- (void)setOrderedDirectors:(NSArray *)ordered
217{
218        [self willChangeValueForKey:ORDERED_DIRECTOR_KEY];
219        [self setPrimitiveValue:ordered forKey:ORDERED_DIRECTOR_KEY];
220        [self didChangeValueForKey:ORDERED_DIRECTOR_KEY];
221        NSArray *arc = [ordered valueForKey:@"name"];
222        [self setValue:[NSKeyedArchiver archivedDataWithRootObject:arc] forKey:ORDERED_DIRECTOR_DATA];
223        [self.directorsSet addObjectsFromArray:ordered];
224}
225
226- (NSString *)title
227{
228        NSString *ret = self.xml.xml.title;
229        if(ret != nil)
230                return ret;
231        return super.title;
232}
233
234- (NSString *)plot
235{
236        NSString *ret = self.xml.xml.contentDescription;
237        if(ret != nil)
238                return ret;
239        ret = self.xml.xml.summary;
240        if(ret != nil)
241                return ret;
242        return super.plot;
243}
244
245- (NSString *)path
246{
247        return [@"@MOVIES/Movie" stringByAppendingPathComponent:[self.imdbNumber stringValue]];
248}
249
250- (NSString *)coverArtPath
251{
252        return [NSString stringWithFormat:@"%@/@MOVIES/%@", [SapphireMetaDataSupport collectionArtPath], self.imdbNumber];
253}
254
255- (void)insertDisplayMetaData:(NSMutableDictionary *)dict
256{
257        id value = [self title];
258        if(value != nil)
259                [dict setObject:value forKey:META_MOVIE_TITLE_KEY];
260        value = [self MPAARating];
261        if(value != nil)
262                [dict setObject:value forKey:META_MOVIE_MPAA_RATING_KEY];
263        value = [self imdbRating];
264        if(value != nil)
265                [dict setObject:value forKey:META_MOVIE_IMDB_RATING_KEY];
266        value = [self plot];
267        if(value != nil)
268                [dict setObject:value forKey:META_MOVIE_PLOT_KEY];
269        value = [self releaseDate];
270        if(value != nil)
271                [dict setObject:value forKey:META_MOVIE_RELEASE_DATE_KEY];
272        value = [self imdbTop250Ranking];
273        if(value != nil)
274                [dict setObject:value forKey:META_MOVIE_IMDB_250_KEY];
275        value = [self oscarsWon];
276        if(value != nil)
277                [dict setObject:value forKey:META_MOVIE_OSCAR_KEY];
278        value = [[self orderedDirectors] valueForKey:@"name"];
279        if(value != nil)
280                [dict setObject:value forKey:META_MOVIE_DIRECTOR_KEY];
281        value = [[self orderedCast] valueForKey:@"name"];
282        if(value != nil)
283                [dict setObject:value forKey:META_MOVIE_CAST_KEY];
284        value = [[self orderedGenres] valueForKey:@"name"];
285        if(value != nil)
286                [dict setObject:value forKey:META_MOVIE_GENRES_KEY];
287}
288
289- (NSNumber *)watched
290{
291        NSSet *files = self.filesSet;
292        if([files count] < 2)
293                return [[files anyObject] watched];
294       
295        NSArray *remain = [[files allObjects] filteredArrayUsingPredicate:[SapphireApplianceController unwatchedPredicate]];
296        if([remain count])
297                return [NSNumber numberWithBool:YES];
298        return [NSNumber numberWithBool:NO];
299}
300
301- (NSNumber *)favorite
302{
303        NSSet *files = self.filesSet;
304        if([files count] < 2)
305                return [[files anyObject] favorite];
306       
307        NSArray *remain = [[files allObjects] filteredArrayUsingPredicate:[SapphireApplianceController favoritePredicate]];
308        if([remain count])
309                return [NSNumber numberWithBool:YES];
310        return [NSNumber numberWithBool:NO];
311}
312
313- (void)clearPredicateCache
314{
315        [self.orderedGenres makeObjectsPerformSelector:@selector(clearPredicateCache)];
316        [self.orderedDirectors makeObjectsPerformSelector:@selector(clearPredicateCache)];
317        [self.orderedCast makeObjectsPerformSelector:@selector(clearPredicateCache)];
318        [[NSNotificationCenter defaultCenter] postNotificationName:MOVIE_DID_CHANGE_PREDICATE_NOTIFICATION object:self];
319}
320
321@end
Note: See TracBrowser for help on using the repository browser.