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

Line 
1#import "SapphireTVShow.h"
2#import "SapphireSeason.h"
3#import "SapphireEpisode.h"
4#import "SapphireSubEpisode.h"
5#import "CoreDataSupportFunctions.h"
6#import "SapphireMetaDataSupport.h"
7#import "NSString-Extensions.h"
8#import "SapphireFileSorter.h"
9#import "SapphireTVTranslation.h"
10
11@implementation SapphireTVShow
12
13static NSArray *allowedSorts = nil;
14
15+ (void)load
16{
17        allowedSorts = [[NSArray alloc] initWithObjects:[SapphireTVEpisodeSorter sharedInstance], [SapphireDateSorter sharedInstance], nil];
18}
19
20+ (SapphireTVShow *)show:(NSString *)show inContext:(NSManagedObjectContext *)moc
21{
22        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", show];
23        SapphireTVShow *ret = (SapphireTVShow *)doSingleFetchRequest(SapphireTVShowName, moc, predicate);
24        if(ret != nil)
25                return ret;
26       
27        ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireTVShowName inManagedObjectContext:moc];
28        ret.name = show;
29        return ret;
30}
31
32+ (void)upgradeShowsVersion:(int)version fromContext:(NSManagedObjectContext *)oldMoc toContext:(NSManagedObjectContext *)newMoc
33{
34        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
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                {
48                        [SapphireTVTranslation upgradeTVTranslationVersion:version from:translation toShow:newShow];
49                }
50               
51                NSEnumerator *seasonEnum = [[oldShow valueForKey:@"seasons"] objectEnumerator];
52                NSManagedObject *season;
53                while((season = [seasonEnum nextObject]) != nil)
54                {
55                        [SapphireSeason upgradeSeasonVersion:version from:season toShow:newShow];
56                }
57        }
58        [SapphireTVTranslation upgradeShowLessTVTranslationsVersion:version fromContext:oldMoc toContext:newMoc];
59        [pool drain];
60}
61
62+ (NSArray *)sortMethods
63{
64        return allowedSorts;
65}
66
67- (NSComparisonResult)compare:(SapphireTVShow *)other
68{
69        return [self.name nameCompare:other.name];
70}
71
72- (NSString *)dirNameValue
73{
74        return @"seasonName";
75}
76
77- (NSPredicate *)metaFileFetchPredicate
78{
79        return [NSPredicate predicateWithFormat:@"tvEpisode.tvShow == %@", self];
80}
81
82- (NSArray *)fileSorters
83{
84        return allowedSorts;
85}
86
87- (void)defaultDirectorySort:(NSMutableArray *)dirs
88{
89        [dirs sortUsingSelector:@selector(compare:)];
90}
91
92- (NSString *)path
93{
94        return [NSString stringWithFormat:@"@TV/%@", self.name];
95}
96
97- (NSString *)classDefaultCoverPath
98{
99        return [[NSBundle bundleForClass:[self class]] pathForResource:@"TV" ofType:@"png"];
100}
101
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
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
149@end
Note: See TracBrowser for help on using the repository browser.