source: trunk/SapphireFrappliance/MetaData/SapphireMObjects/SapphireTVShow.m @ 1251

Revision 1251, 4.4 KB checked in by gbooker, 4 years ago (diff)

Merged over changes from V3 schema branch

RevLine 
[540]1#import "SapphireTVShow.h"
[622]2#import "SapphireSeason.h"
3#import "SapphireEpisode.h"
4#import "SapphireSubEpisode.h"
[540]5#import "CoreDataSupportFunctions.h"
[544]6#import "SapphireMetaDataSupport.h"
[598]7#import "NSString-Extensions.h"
[599]8#import "SapphireFileSorter.h"
[734]9#import "SapphireTVTranslation.h"
[540]10
11@implementation SapphireTVShow
12
[599]13static NSArray *allowedSorts = nil;
14
15+ (void)load
16{
17        allowedSorts = [[NSArray alloc] initWithObjects:[SapphireTVEpisodeSorter sharedInstance], [SapphireDateSorter sharedInstance], nil];
18}
19
[1251]20+ (SapphireTVShow *)show:(NSString *)show inContext:(NSManagedObjectContext *)moc
[540]21{
[573]22        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", show];
[578]23        SapphireTVShow *ret = (SapphireTVShow *)doSingleFetchRequest(SapphireTVShowName, moc, predicate);
24        if(ret != nil)
[540]25                return ret;
26       
[578]27        ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireTVShowName inManagedObjectContext:moc];
[540]28        ret.name = show;
29        return ret;
30}
31
[1251]32+ (void)upgradeShowsVersion:(int)version fromContext:(NSManagedObjectContext *)oldMoc toContext:(NSManagedObjectContext *)newMoc
[540]33{
[1251]34        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[734]35        NSArray *oldShows = doFetchRequest(SapphireTVShowName, oldMoc, nil);
36        NSEnumerator *showEnum = [oldShows objectEnumerator];
37        NSManagedObject *oldShow;
38        while((oldShow = [showEnum nextObject]) != nil)
39        {
40                SapphireTVShow *newShow = [NSEntityDescription insertNewObjectForEntityForName:SapphireTVShowName inManagedObjectContext:newMoc];
41                newShow.name = [oldShow valueForKey:@"name"];
42                newShow.showDescription = [oldShow valueForKey:@"showDescription"];
43               
44                NSEnumerator *translationEnum = [[oldShow valueForKey:@"translations"] objectEnumerator];
45                NSManagedObject *translation;
46                while((translation = [translationEnum nextObject]) != nil)
47                {
[1251]48                        [SapphireTVTranslation upgradeTVTranslationVersion:version from:translation toShow:newShow];
[734]49                }
50               
51                NSEnumerator *seasonEnum = [[oldShow valueForKey:@"seasons"] objectEnumerator];
52                NSManagedObject *season;
53                while((season = [seasonEnum nextObject]) != nil)
54                {
[1251]55                        [SapphireSeason upgradeSeasonVersion:version from:season toShow:newShow];
[734]56                }
57        }
[1251]58        [SapphireTVTranslation upgradeShowLessTVTranslationsVersion:version fromContext:oldMoc toContext:newMoc];
59        [pool drain];
[734]60}
61
[966]62+ (NSArray *)sortMethods
63{
64        return allowedSorts;
65}
66
[540]67- (NSComparisonResult)compare:(SapphireTVShow *)other
68{
[598]69        return [self.name nameCompare:other.name];
[540]70}
71
72- (NSString *)dirNameValue
73{
74        return @"seasonName";
75}
76
[573]77- (NSPredicate *)metaFileFetchPredicate
78{
79        return [NSPredicate predicateWithFormat:@"tvEpisode.tvShow == %@", self];
80}
81
[599]82- (NSArray *)fileSorters
[540]83{
[599]84        return allowedSorts;
[540]85}
86
[603]87- (void)defaultDirectorySort:(NSMutableArray *)dirs
88{
89        [dirs sortUsingSelector:@selector(compare:)];
90}
91
[540]92- (NSString *)path
93{
94        return [NSString stringWithFormat:@"@TV/%@", self.name];
95}
96
[849]97- (NSString *)classDefaultCoverPath
98{
99        return [[NSBundle bundleForClass:[self class]] pathForResource:@"TV" ofType:@"png"];
100}
101
[622]102static inline NSArray *getEpsFromFiles(NSManagedObjectContext *moc, NSArray *files)
103{
104        NSSet *epIds = [NSSet setWithArray:[files valueForKeyPath:@"tvEpisode.objectID"]];
105       
106        NSPredicate *epPredicate = [NSPredicate predicateWithFormat:@"SELF IN %@", epIds];
107        return doFetchRequest(SapphireEpisodeName, moc, epPredicate);
108}
109
110- (NSMutableArray *)internalDirectoryFetchFromFiles:(NSArray *)files
111{
112        NSManagedObjectContext *moc = [self managedObjectContext];
113        NSArray *eps = getEpsFromFiles(moc, files);
114       
115        NSSet *seasonIds = [NSSet setWithArray:[eps valueForKeyPath:@"season.objectID"]];
116        NSPredicate *seasonPred = [NSPredicate predicateWithFormat:@"SELF IN %@", seasonIds];
117        NSMutableArray *ret = [doFetchRequest(SapphireSeasonName, moc, seasonPred) mutableCopy];
118        return [ret autorelease];
119}
120
121- (void)prefetch:(NSArray *)files
122{
123        NSManagedObjectContext *moc = [self managedObjectContext];
124        NSArray *eps = getEpsFromFiles(moc, files);
125       
126        NSPredicate *subEpsPred = [NSPredicate predicateWithFormat:@"episode IN %@", eps];
127        doFetchRequest(SapphireSubEpisodeName, moc, subEpsPred);
128}
129
[840]130- (BOOL)shouldDelete
131{
132        return [self.episodesSet count] == 0;
133}
134
135-(void)removeEpisodes:(NSSet*)removedEpisodes
136{
137        [super removeEpisodes:removedEpisodes];
138        if([self.episodesSet count] == 0)
139                [SapphireMetaDataSupport setObjectForPendingDelete:self];
140}
141
142- (void)removeEpisodesObject:(SapphireEpisode*)removedEpisode
143{
144        [super removeEpisodesObject:removedEpisode];
145        if([self.episodesSet count] == 0)
146                [SapphireMetaDataSupport setObjectForPendingDelete:self];
147}
148
[540]149@end
Note: See TracBrowser for help on using the repository browser.