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

Revision 578, 10.0 KB checked in by gbooker, 6 years ago (diff)

Many optimizations

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