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

Revision 732, 12.6 KB checked in by gbooker, 6 years ago (diff)

Fixed relationships up a bit better; added all the abilities needed for new data model

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#import "NSString-Extensions.h"
12
13NSString *FILE_DID_CHANGE_MOVIE_NOTIFICATION = @"FileDidChangeMovie";
14
15#define ORDERED_CAST_KEY                        @"orderedCast"
16#define ORDERED_CAST_DATA                       @"orderedCastData"
17#define OVERRIDDEN_CAST_DATA            @"overriddenCastData"
18#define ORDERED_DIRECTOR_KEY            @"orderedDirectors"
19#define ORDERED_DIRECTOR_DATA           @"orderedDirectorsData"
20#define OVERRIDDEN_DIRECTOR_DATA        @"overriddenDirectorsData"
21#define ORDERED_GENRES_KEY                      @"orderedGenres"
22#define ORDERED_GENRES_DATA                     @"orderedGenresData"
23#define OVERRIDDEN_GENRES_DATA          @"overriddenGenresData"
24
25
26@implementation SapphireMovie
27
28+ (SapphireMovie *)movieWithIMDB:(int)imdbNumber inContext:(NSManagedObjectContext *)moc
29{
30        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"imdbNumber == %d", imdbNumber];
31        return (SapphireMovie *)doSingleFetchRequest(SapphireMovieName, moc, predicate);
32}
33       
34+ (SapphireMovie *)createMovieWithIMDB:(int)imdbNumber inContext:(NSManagedObjectContext *)moc
35{
36        SapphireMovie *ret = [SapphireMovie movieWithIMDB:imdbNumber inContext:moc];
37        if(ret != nil)
38                return ret;
39       
40        ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireMovieName inManagedObjectContext:moc];
41        ret.imdbNumber = [NSNumber numberWithInt:imdbNumber];
42        return ret;
43}
44
45+ (SapphireMovie *)movieWithDictionary:(NSDictionary *)dict inContext:(NSManagedObjectContext *)moc
46{
47        NSString *imdbStr = [dict objectForKey:META_MOVIE_IDENTIFIER_KEY];
48        int imdbNumber = [SapphireMovie imdbNumberFromString:imdbStr];
49        if(imdbNumber == 0)
50                return nil;
51       
52        SapphireMovie *ret = [SapphireMovie createMovieWithIMDB:imdbNumber inContext:moc];
53        ret.title = [dict objectForKey:META_MOVIE_TITLE_KEY];
54        ret.releaseDate = [dict objectForKey:META_MOVIE_RELEASE_DATE_KEY];
55        ret.MPAARating = [dict objectForKey:META_MOVIE_MPAA_RATING_KEY];
56        ret.imdbRating = [dict objectForKey:META_MOVIE_IMDB_RATING_KEY];
57        ret.plot = [dict objectForKey:META_MOVIE_PLOT_KEY];
58        id value = [dict objectForKey:META_MOVIE_IMDB_250_KEY];
59        int i;
60        if((i = [value intValue]) != 0)
61                ret.imdbTop250RankingValue = i;
62        value = [dict objectForKey:META_MOVIE_OSCAR_KEY];
63        if((i = [value intValue]) != 0)
64                ret.oscarsWonValue = i;
65        NSMutableArray *genreArray = [NSMutableArray array];
66        NSEnumerator *genreEnum = [[dict objectForKey:META_MOVIE_GENRES_KEY] objectEnumerator];
67        NSString *genre;
68        while((genre = [genreEnum nextObject]) != nil)
69                [genreArray addObject:[SapphireGenre createGenre:genre inContext:moc]];
70        ret.orderedGenres = genreArray;
71       
72        NSMutableArray *dirArray = [NSMutableArray array];
73        NSEnumerator *directorEnum = [[dict objectForKey:META_MOVIE_DIRECTOR_KEY] objectEnumerator];
74        NSString *director;
75        while((director = [directorEnum nextObject]) != nil)
76                [dirArray addObject:[SapphireDirector createDirector:director inContext:moc]];
77        ret.orderedDirectors = dirArray;
78
79        NSMutableArray *castArray = [NSMutableArray array];
80        NSEnumerator *castEnum = [[dict objectForKey:META_MOVIE_CAST_KEY] objectEnumerator];
81        NSString *cast;
82        while((cast = [castEnum nextObject]) != nil)
83                [castArray addObject:[SapphireCast createCast:cast inContext:moc]];
84        ret.orderedCast = castArray;
85        return ret;
86}
87
88+ (int)imdbNumberFromString:(NSString *)imdbStr
89{
90        int imdbNumber = 0;
91        if(imdbStr != nil)
92        {
93                NSScanner *scanner = [NSScanner scannerWithString:imdbStr];
94                [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
95                [scanner scanInt:&imdbNumber];
96        }
97        return imdbNumber;
98}
99
100- (NSComparisonResult)releaseDateCompare:(SapphireMovie *)other
101{
102        return [self.releaseDate compare:other.releaseDate];
103}
104
105- (NSComparisonResult)titleCompare:(SapphireMovie *)other
106{
107        return [self.title nameCompare:other.title];
108}
109
110- (NSComparisonResult)imdbTop250RankingCompare:(SapphireMovie *)other
111{
112        return [self.imdbTop250Ranking compare:other.imdbTop250Ranking];
113}
114
115- (NSComparisonResult)oscarsWonCompare:(SapphireMovie *)other
116{
117        return [self.oscarsWon compare:other.oscarsWon];
118}
119
120- (NSComparisonResult)imdbRatingCompare:(SapphireMovie *)other
121{
122        NSNumber *otherNum = other.imdbRating;
123        NSNumber *myNum = self.imdbRating;
124        if(myNum != nil)
125                if(otherNum != nil)
126                        return [myNum compare:otherNum];
127                else
128                        return NSOrderedDescending;
129        else if(otherNum != nil)
130                return NSOrderedAscending;
131        return NSOrderedSame;
132}
133
134- (NSArray *)orderedCast
135{
136        [self willAccessValueForKey:ORDERED_CAST_KEY];
137        NSArray *ret = [self primitiveValueForKey:ORDERED_CAST_KEY];
138        [self didAccessValueForKey:ORDERED_CAST_KEY];
139        if(ret == nil)
140        {
141                NSData *propData = [self valueForKey:OVERRIDDEN_CAST_DATA];
142                if(propData == nil)
143                        propData = [self valueForKey:ORDERED_CAST_DATA];
144                if(propData != nil)
145                {
146                        NSArray *names = [NSKeyedUnarchiver unarchiveObjectWithData:propData];
147                        NSMutableArray *mutRet = [NSMutableArray arrayWithCapacity:[names count]];
148                        NSManagedObjectContext *moc = [self managedObjectContext];
149                        NSEnumerator *nameEnum = [names objectEnumerator];
150                        NSString *name;
151                        while((name = [nameEnum nextObject]) != nil)
152                        {
153                                SapphireCast *aCast = [SapphireCast cast:name inContext:moc];
154                                if(aCast != nil)
155                                        [mutRet addObject:aCast];
156                        }
157                        ret = [NSArray arrayWithArray:mutRet];
158                        [self setPrimitiveValue:ret forKey:ORDERED_CAST_KEY];
159                }
160        }
161       
162        return ret;
163}
164
165- (void)setOrderedCast:(NSArray *)ordered
166{
167        [self willChangeValueForKey:ORDERED_CAST_KEY];
168        [self setPrimitiveValue:ordered forKey:ORDERED_CAST_KEY];
169        [self didChangeValueForKey:ORDERED_CAST_KEY];
170        NSArray *arc = [ordered valueForKey:@"name"];
171        [self setValue:[NSKeyedArchiver archivedDataWithRootObject:arc] forKey:ORDERED_CAST_DATA];
172        [self.castSet setSet:[NSSet setWithArray:ordered]];
173        int i, count=[ordered count];
174        if(count > 10)
175                count = 10;
176        for(i=0; i<count; i++)
177        {
178                ((SapphireCast *)[ordered objectAtIndex:i]).hasMajorRoleValue = YES;
179        }       
180}
181
182- (NSArray *)orderedGenres
183{
184        [self willAccessValueForKey:ORDERED_GENRES_KEY];
185        NSArray *ret = [self primitiveValueForKey:ORDERED_GENRES_KEY];
186        [self didAccessValueForKey:ORDERED_GENRES_KEY];
187        if(ret == nil)
188        {
189                NSData *propData = [self valueForKey:OVERRIDDEN_GENRES_DATA];
190                if(propData == nil)
191                        propData = [self valueForKey:ORDERED_GENRES_DATA];
192                if(propData != nil)
193                {
194                        NSArray *genres = [NSKeyedUnarchiver unarchiveObjectWithData:propData];
195                        NSMutableArray *mutRet = [NSMutableArray arrayWithCapacity:[genres count]];
196                        NSManagedObjectContext *moc = [self managedObjectContext];
197                        NSEnumerator *genreEnum = [genres objectEnumerator];
198                        NSString *genre;
199                        while((genre = [genreEnum nextObject]) != nil)
200                        {
201                                SapphireGenre *aGenre = [SapphireGenre genre:genre inContext:moc];
202                                if(aGenre != nil)
203                                        [mutRet addObject:aGenre];
204                        }
205                        ret = [NSArray arrayWithArray:mutRet];
206                        [self setPrimitiveValue:ret forKey:ORDERED_GENRES_KEY];
207                }
208        }
209       
210        return ret;
211}
212
213- (void)setOrderedGenres:(NSArray *)ordered
214{
215        [self willChangeValueForKey:ORDERED_GENRES_KEY];
216        [self setPrimitiveValue:ordered forKey:ORDERED_GENRES_KEY];
217        [self didChangeValueForKey:ORDERED_GENRES_KEY];
218        NSArray *arc = [ordered valueForKey:@"name"];
219        [self setValue:[NSKeyedArchiver archivedDataWithRootObject:arc] forKey:ORDERED_GENRES_DATA];
220        [self.genresSet addObjectsFromArray:ordered];
221}
222
223- (NSArray *)orderedDirectors
224{
225        [self willAccessValueForKey:ORDERED_DIRECTOR_KEY];
226        NSArray *ret = [self primitiveValueForKey:ORDERED_DIRECTOR_KEY];
227        [self didAccessValueForKey:ORDERED_DIRECTOR_KEY];
228        if(ret == nil)
229        {
230                NSData *propData = [self valueForKey:OVERRIDDEN_DIRECTOR_DATA];
231                if(propData == nil)
232                        propData = [self valueForKey:ORDERED_DIRECTOR_DATA];
233                if(propData != nil)
234                {
235                        NSArray *names = [NSKeyedUnarchiver unarchiveObjectWithData:propData];
236                        NSMutableArray *mutRet = [NSMutableArray arrayWithCapacity:[names count]];
237                        NSManagedObjectContext *moc = [self managedObjectContext];
238                        NSEnumerator *nameEnum = [names objectEnumerator];
239                        NSString *name;
240                        while((name = [nameEnum nextObject]) != nil)
241                        {
242                                SapphireDirector *aDir = [SapphireDirector director:name inContext:moc];
243                                if(aDir != nil)
244                                        [mutRet addObject:aDir];
245                        }
246                        ret = [NSArray arrayWithArray:mutRet];
247                        [self setPrimitiveValue:ret forKey:ORDERED_DIRECTOR_KEY];
248                }
249        }
250       
251        return ret;
252}
253
254- (void)setOrderedDirectors:(NSArray *)ordered
255{
256        [self willChangeValueForKey:ORDERED_DIRECTOR_KEY];
257        [self setPrimitiveValue:ordered forKey:ORDERED_DIRECTOR_KEY];
258        [self didChangeValueForKey:ORDERED_DIRECTOR_KEY];
259        NSArray *arc = [ordered valueForKey:@"name"];
260        [self setValue:[NSKeyedArchiver archivedDataWithRootObject:arc] forKey:ORDERED_DIRECTOR_DATA];
261        [self.directorsSet addObjectsFromArray:ordered];
262}
263
264#define checkOrderedData(property, overridden) \
265{ \
266        NSEnumerator *xmlEnum = [self.xmlSet objectEnumerator]; \
267        SapphireXMLMovieData *xml; \
268        while([overridden count] == 0 && (xml = [xmlEnum nextObject]) != nil) \
269                overridden = xml.property; \
270}
271
272- (void)checkOrderedCast
273{
274        NSArray *overridden = nil;
275        checkOrderedData(orderedCast, overridden);
276        if(![overridden count])
277        {
278                self.overriddenCastData = nil;
279        }
280        [self setPrimitiveValue:nil forKey:ORDERED_CAST_KEY];
281        [self.castSet setSet:[NSSet setWithArray:self.orderedCast]];
282}
283
284- (void)checkOrderedGenres
285{
286        NSArray *overridden = nil;
287        checkOrderedData(orderedGenres, overridden);
288        if(![overridden count])
289        {
290                self.overriddenGenresData = nil;
291        }
292        [self setPrimitiveValue:nil forKey:ORDERED_GENRES_KEY];
293        [self.genresSet setSet:[NSSet setWithArray:self.orderedGenres]];
294}
295
296- (void)checkOrderedDirectors
297{
298        NSArray *overridden = nil;
299        checkOrderedData(orderedDirectors, overridden);
300        if(![overridden count])
301        {
302                self.overriddenDirectorsData = nil;
303        }
304        [self setPrimitiveValue:nil forKey:ORDERED_DIRECTOR_KEY];
305        [self.directorsSet setSet:[NSSet setWithArray:self.orderedDirectors]];
306}
307
308- (NSString *)title
309{
310        overrideWithXMLForKey(SapphireXMLMovieData, NSString, xml.title);
311        return super.title;
312}
313
314- (NSString *)plot
315{
316        overrideWithXMLForKey(SapphireXMLMovieData, NSString, xml.contentDescription);
317        overrideWithXMLForKey(SapphireXMLMovieData, NSString, xml.summary);
318        return super.plot;
319}
320
321- (BOOL)castMemberHasMajorRoleStatus:(SapphireCast *)cast
322{
323        NSArray *ordered = self.orderedCast;
324        int i, count=[ordered count];
325        if(count > 10)
326                count = 10;
327        for(i=0; i<count; i++)
328        {
329                if([ordered objectAtIndex:i] == cast)
330                        return YES;
331        }
332        return NO;
333}
334
335- (NSString *)path
336{
337        return [@"@MOVIES/Movie" stringByAppendingPathComponent:[self.imdbNumber stringValue]];
338}
339
340- (NSString *)coverArtPath
341{
342        return [NSString stringWithFormat:@"%@/@MOVIES/%@", [SapphireMetaDataSupport collectionArtPath], self.imdbNumber];
343}
344
345- (void)insertDisplayMetaData:(NSMutableDictionary *)dict
346{
347        id value = [self title];
348        if(value != nil)
349                [dict setObject:value forKey:META_MOVIE_TITLE_KEY];
350        value = [self MPAARating];
351        if(value != nil)
352                [dict setObject:value forKey:META_MOVIE_MPAA_RATING_KEY];
353        value = [self imdbRating];
354        if(value != nil)
355                [dict setObject:value forKey:META_MOVIE_IMDB_RATING_KEY];
356        value = [self plot];
357        if(value != nil)
358                [dict setObject:value forKey:META_MOVIE_PLOT_KEY];
359        value = [self releaseDate];
360        if(value != nil)
361                [dict setObject:value forKey:META_MOVIE_RELEASE_DATE_KEY];
362        value = [self imdbTop250Ranking];
363        if(value != nil)
364                [dict setObject:value forKey:META_MOVIE_IMDB_250_KEY];
365        value = [self oscarsWon];
366        if(value != nil)
367                [dict setObject:value forKey:META_MOVIE_OSCAR_KEY];
368        value = [[self orderedDirectors] valueForKey:@"name"];
369        if(value != nil)
370                [dict setObject:value forKey:META_MOVIE_DIRECTOR_KEY];
371        value = [[self orderedCast] valueForKey:@"name"];
372        if(value != nil)
373                [dict setObject:value forKey:META_MOVIE_CAST_KEY];
374        value = [[self orderedGenres] valueForKey:@"name"];
375        if(value != nil)
376                [dict setObject:value forKey:META_MOVIE_GENRES_KEY];
377}
378
379- (NSNumber *)watched
380{
381        NSSet *files = self.filesSet;
382        if([files count] < 2)
383                return [[files anyObject] watched];
384       
385        NSArray *remain = [[files allObjects] filteredArrayUsingPredicate:[SapphireApplianceController unwatchedPredicate]];
386        if([remain count])
387                return [NSNumber numberWithBool:YES];
388        return [NSNumber numberWithBool:NO];
389}
390
391- (NSNumber *)favorite
392{
393        NSSet *files = self.filesSet;
394        if([files count] < 2)
395                return [[files anyObject] favorite];
396       
397        NSArray *remain = [[files allObjects] filteredArrayUsingPredicate:[SapphireApplianceController favoritePredicate]];
398        if([remain count])
399                return [NSNumber numberWithBool:YES];
400        return [NSNumber numberWithBool:NO];
401}
402
403- (void)clearPredicateCache
404{
405        [self.orderedGenres makeObjectsPerformSelector:@selector(clearPredicateCache)];
406        [self.orderedDirectors makeObjectsPerformSelector:@selector(clearPredicateCache)];
407        [self.orderedCast makeObjectsPerformSelector:@selector(clearPredicateCache)];
408}
409
410@end
Note: See TracBrowser for help on using the repository browser.