source: branches/CoreData/SapphireFrappliance/MetaData/SapphireMObjects/SapphireDirector.m @ 734

Revision 734, 2.0 KB checked in by gbooker, 6 years ago (diff)

Switched the the next version of the metadata.
Fixes #264

Line 
1#import "SapphireDirector.h"
2#import "CoreDataSupportFunctions.h"
3#import "SapphireFileSorter.h"
4
5@implementation SapphireDirector
6
7static NSArray *allowedSorts = nil;
8
9+ (void)load
10{
11        allowedSorts = [[NSArray alloc] initWithObjects:[SapphireMovieTitleSorter sharedInstance], [SapphireDateSorter sharedInstance], [SapphireMovieIMDBRatingSorter sharedInstance], nil];
12}
13
14+ (SapphireDirector *)director:(NSString *)director inContext:(NSManagedObjectContext *)moc
15{
16        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", director];
17        return (SapphireDirector *)doSingleFetchRequest(SapphireDirectorName, moc, predicate);
18}
19
20+ (SapphireDirector *)createDirector:(NSString *)director inContext:(NSManagedObjectContext *)moc
21{
22        SapphireDirector *ret = [SapphireDirector director:director inContext:moc];
23        if(ret != nil)
24                return ret;
25       
26        ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireDirectorName inManagedObjectContext:moc];
27        ret.name = director;
28        return ret;
29}
30
31+ (NSDictionary *)upgradeV1DirectorsFromContext:(NSManagedObjectContext *)oldMoc toContext:(NSManagedObjectContext *)newMoc
32{
33        NSMutableDictionary *lookup = [NSMutableDictionary dictionary];
34        NSArray *oldDirectors = doFetchRequest(SapphireDirectorName, oldMoc, nil);
35        NSEnumerator *directorEnum = [oldDirectors objectEnumerator];
36        NSManagedObject *oldDirector;
37        while((oldDirector = [directorEnum nextObject]) != nil)
38        {
39                SapphireDirector *newDirector = [NSEntityDescription insertNewObjectForEntityForName:SapphireDirectorName inManagedObjectContext:newMoc];
40                NSString *name = [oldDirector valueForKey:@"name"];
41                newDirector.name = name;
42                newDirector.sortMethod = [oldDirector valueForKey:@"sortMethod"];
43                [lookup setObject:newDirector forKey:name];
44        }
45        return lookup;
46}
47
48- (NSPredicate *)metaFileFetchPredicate
49{
50        NSArray *movieIds = [self.moviesSet valueForKey:@"objectID"];
51        return [NSPredicate predicateWithFormat:@"movie IN %@", movieIds];
52}
53
54- (NSArray *)fileSorters
55{
56        return allowedSorts;
57}
58
59- (NSString *)path
60{
61        return [super.path stringByAppendingPathComponent:self.name];
62}
63
64@end
Note: See TracBrowser for help on using the repository browser.