This repository has been archived by the owner on May 6, 2024. It is now read-only.
/
OEXDBManager.m
931 lines (760 loc) · 33.5 KB
/
OEXDBManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
//
// OEXDBManager.m
// edXLibrary
//
// Created by Rahul Varma on 07/11/14.
// Copyright (c) 2014-2016 edX, Inc. All rights reserved.
//
#import "OEXDBManager.h"
#import "edX-Swift.h"
#import <Crashlytics/Crashlytics.h>
#import "LastAccessed.h"
#import "Logger+OEXObjC.h"
#import "OEXFileUtility.h"
#import "OEXUserDetails.h"
#import "OEXSession.h"
#import "VideoData.h"
static OEXDBManager* _sharedManager = nil;
@interface OEXDBManager ()
@property (nonatomic, strong) NSManagedObjectModel* managedObjectModel;
@property (nonatomic, strong) NSManagedObjectContext* masterManagedObjectContext;
@property (nonatomic, strong) NSPersistentStoreCoordinator* persistentStoreCoordinator;
@property(nonatomic, strong) NSOperationQueue* operationQueue;
@property(nonatomic, strong) NSMutableDictionary* dictFetchedRecords;
@property(nonatomic, strong) NSManagedObjectContext* backGroundContext;
@property(nonatomic, strong) NSString* userName;
- (NSManagedObjectContext*)masterManagedObjectContext;
@end
@implementation OEXDBManager
#pragma appdelegate code
- (void)createDatabaseDirectory {
//File URL
NSString* basePath = [OEXFileUtility userDirectory];
NSString* videosDirectory = [basePath stringByAppendingPathComponent:@"Database"];
if(![[NSFileManager defaultManager] fileExistsAtPath:videosDirectory]) {
NSError* error;
if(![[NSFileManager defaultManager] createDirectoryAtPath:videosDirectory
withIntermediateDirectories:NO
attributes:nil
error:&error]) {
OEXLogError(@"STORAGE", @"Failed to create database directory at %@", basePath);
}
OEXLogInfo(@"STORAGE", @"Database directory created at %@", basePath);
}
}
- (void)openDatabaseForUser:(NSString*)userName {
if(![self.userName isEqualToString:userName]) {
[self createDatabaseDirectory];
_userName = userName;
if(_masterManagedObjectContext != nil) {
[_backGroundContext save:nil];
[_masterManagedObjectContext save:nil];
_backGroundContext = nil;
_masterManagedObjectContext = nil;
}
if([self masterManagedObjectContext]) {
NSString* basePath = [OEXFileUtility userDirectory];
NSString* databasePath = [basePath stringByAppendingPathComponent:@"Database"];
OEXLogInfo(@"STORAGE", @"Database opened Sucessfully %@ ", databasePath);
_backGroundContext = [self newManagedObjectContext];
}
}
}
- (void)closeDatabse {
[self deactivate];
}
- (void)deactivate {
OEXLogInfo(@"STORAGE", @"Deactivating database");
[self saveCurrentStateToDB];
[self.backGroundContext reset];
[self.masterManagedObjectContext reset];
self.managedObjectModel = nil;
self.persistentStoreCoordinator = nil;
self.backGroundContext = nil;
self.userName = nil;
_sharedManager = nil;
[_dictFetchedRecords removeAllObjects];
}
- (NSManagedObjectContext*)masterManagedObjectContext {
if(_masterManagedObjectContext != nil) {
return _masterManagedObjectContext;
}
NSPersistentStoreCoordinator* coordinator = [self persistentStoreCoordinator];
if(coordinator != nil) {
self.masterManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_masterManagedObjectContext performBlockAndWait:^{
[_masterManagedObjectContext setPersistentStoreCoordinator:coordinator];
}];
}
return _masterManagedObjectContext;
}
- (NSManagedObjectModel*)managedObjectModel {
if(_managedObjectModel != nil) {
return _managedObjectModel;
}
_managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator*)persistentStoreCoordinator {
if(_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSString* storePath = [[OEXFileUtility userDirectory] stringByAppendingPathComponent:@"Database/edXDB.sqlite"];
NSURL* storeURL = [NSURL fileURLWithPath:storePath];
OEXLogInfo(@"STORAGE", @"DB path %@", storeURL);
NSError* error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if(![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
OEXLogInfo(@"STORAGE", @"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
- (NSManagedObjectContext*)newManagedObjectContext {
NSManagedObjectContext* newContext = nil;
NSManagedObjectContext* masterContext = _masterManagedObjectContext;
if(masterContext != nil) {
newContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[newContext performBlockAndWait:^{
[newContext setParentContext:masterContext];
}];
}
return newContext;
}
- (NSManagedObjectContext*)backGroundContext {
if(_backGroundContext) {
return _backGroundContext;
}
else if(_masterManagedObjectContext) {
return [self newManagedObjectContext];
}
else {
return nil;
}
}
//===================================================================================================================//
#pragma mark - Singleton method
+ (OEXDBManager*)sharedManager {
if(!_sharedManager) {
OEXUserDetails* user = [[OEXSession sharedSession] currentUser];
if(user) {
_sharedManager = [[OEXDBManager alloc] init];
[_sharedManager openDatabaseForUser:user.username];
}
else {
_sharedManager = nil;
}
}
return _sharedManager;
}
- (void)initializeDB {
dispatch_async(dispatch_get_main_queue(), ^{
if(!_operationQueue) {
_operationQueue = [[NSOperationQueue alloc] init];
}
});
_dictFetchedRecords = [[NSMutableDictionary alloc] init];
}
- (void)activate {
}
// Save all table data at a time
- (void)saveCurrentStateToDB {
OEXLogInfo(@"STORAGE", @"Save database context on main thread");
CLS_LOG(@"saveCurrentStateToDB");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
@synchronized(_masterManagedObjectContext){
[self.backGroundContext save:nil];
CLS_LOG(@"saveCurrentStateToDB: backGroundContext... %@", self.backGroundContext);
CLS_LOG(@"saveCurrentStateToDB: masterManagedObjectContext... %@", self.masterManagedObjectContext);
[self.masterManagedObjectContext save:nil];
NSError* error = nil;
if(_masterManagedObjectContext != nil) {
if([_masterManagedObjectContext hasChanges] && ![_masterManagedObjectContext save:&error]) {
OEXLogInfo(@"STORAGE", @"Could not save changes to database");
}
}
}
}];
}
- (NSEntityDescription*)getEntityByName:(NSString*)entityName {
if(self.backGroundContext) {
@synchronized(_backGroundContext)
{
NSEntityDescription* videoEntity = [NSEntityDescription entityForName:entityName inManagedObjectContext:_backGroundContext];
return videoEntity;
}
}
return nil;
}
- (NSArray*)executeFetchRequest:(NSFetchRequest*)fetchRequest {
CLS_LOG(@"executeFetchRequest");
if([self masterManagedObjectContext]) {
CLS_LOG(@"executeFetchRequest: masterManagedObjectContext exist...%@", _masterManagedObjectContext);
__block NSArray* resultArray;
if([NSThread isMainThread]) {
@synchronized(_masterManagedObjectContext)
{
CLS_LOG(@"executeFetchRequest: executeFetchRequest in main thread");
resultArray = [[self masterManagedObjectContext] executeFetchRequest:fetchRequest error:nil];
}
}
else {
[_backGroundContext performBlockAndWait:^{
CLS_LOG(@"executeFetchRequest: executeFetchRequest in performBlockAndWait with %@", _backGroundContext);
resultArray = [self.backGroundContext executeFetchRequest:fetchRequest error:nil];
}];
}
if(!resultArray) {
return [NSArray array];
}
else {
return resultArray;
}
}
else {
return nil;
}
}
#pragma mark - Existing methods refactored with new DB
#pragma mark - ResourceData Method
- (void)updateData:(NSData*)data ForURLString:(NSString*)URLString {
//File path
NSString* filePath = [OEXFileUtility filePathForRequestKey:URLString];
//check if file already exists, delete it
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSError* error;
if([[NSFileManager defaultManager] isDeletableFileAtPath:filePath]) {
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
if(!success) {
OEXLogError(@"STORAGE", @"Error removing file at path: %@", error.localizedDescription);
}
}
}
//write new file
if(![data writeToFile:filePath atomically:YES]) {
OEXLogError(@"STORAGE", @"There was a problem saving json to file");
}
}
// Insert Resource data
- (void)insertResourceDataForURL:(NSString*)resourceDownloadURL {
if([self resourceDataForURL:resourceDownloadURL]) {
return;
}
ResourceData* resourceObj = [NSEntityDescription insertNewObjectForEntityForName:@"ResourceData"
inManagedObjectContext:_backGroundContext];
resourceObj.resourceDownloadURL = resourceDownloadURL;
[self saveCurrentStateToDB];
}
// Get the resource data (JSON/image,etc.) for a URL
- (ResourceData*)resourceDataForURL:(NSString*)URL {
// ResourceData *result;
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription* resourceEntity = [self getEntityByName:@"ResourceData"];
[fetchRequest setEntity:resourceEntity];
NSPredicate* query = [NSPredicate predicateWithFormat:@"resourceDownloadURL==%@", URL];
//setting the predicate to the fetch request
[fetchRequest setPredicate:query];
NSArray* resultArray = [self executeFetchRequest:fetchRequest];
// ELog(@"resourceDataForURL : %@",resultArray);
return (ResourceData*)[resultArray firstObject];
}
// Set if the resource is completed
- (void)completedDownloadForResourceURL:(NSString*)URL {
ResourceData* resourceData = [self resourceDataForURL:URL];
if(resourceData) {
resourceData.downloadState = [NSNumber numberWithFloat: OEXDownloadStateComplete];
resourceData.downloadCompleteDate = [NSDate date];
[self saveCurrentStateToDB];
}
}
// Set if the resource is started
- (void)startedDownloadForResourceURL:(NSString*)URL {
ResourceData* resourceData = [self resourceDataForURL:URL];
if(resourceData) {
resourceData.downloadState = [NSNumber numberWithFloat: OEXDownloadStatePartial];
//[self saveCurrentStateToDB];
}
else {
[self insertResourceDataForURL:URL];
}
}
- (void)cancelledResourceDownloadForURL:(NSString*)url {
ResourceData* newVideoData = [self resourceDataForURL:url];
//Download State
newVideoData.downloadState = [NSNumber numberWithInt: OEXDownloadStateNew];
[self saveCurrentStateToDB];
}
- (void)deleteResourceDataForURL:(NSString*)url {
ResourceData* dataToDelete = [self resourceDataForURL:url];
NSString* filePath = [OEXFileUtility filePathForRequestKey:url];
if(dataToDelete) {
//Mark new in DB
[self cancelledResourceDownloadForURL:url];
//Delete file
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
[[NSFileManager defaultManager] removeItemAtPath:filePath
error:nil];
}
[self saveCurrentStateToDB];
}
}
// Get the download state for resource
- (OEXDownloadState)downloadStateForResourceURL:(NSString*)URL {
ResourceData* resourceData = [self resourceDataForURL:URL];
return [resourceData.downloadState intValue];
}
- (NSData*)dataForURLString:(NSString*)URLString {
NSString* filePath = [OEXFileUtility filePathForRequestKey:URLString];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSData* data = [NSData dataWithContentsOfFile:filePath];
return data;
}
return nil;
}
#pragma mark - LastAccessed table methods
- (LastAccessed*)lastAccessedDataForCourseID:(NSString*)courseID {
if(_masterManagedObjectContext) {
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"LastAccessed" inManagedObjectContext:_masterManagedObjectContext]];
NSPredicate* query = [NSPredicate predicateWithFormat:@"course_id==%@", courseID];
//setting the predicate to the fetch request
[fetchRequest setPredicate:query];
NSArray* resultArray = [self executeFetchRequest:fetchRequest];
OEXLogInfo(@"STORAGE", @"loaded lastAccessedDataForCourseID : %@", resultArray);
return [resultArray firstObject];
}
return nil;
}
- (void)setLastAccessedSubsection:(NSString*)subsectionID andSubsectionName:(NSString*)subsectionName forCourseID:(nullable NSString*)courseID OnTimeStamp:(NSString*)timestamp {
if(_backGroundContext) {
LastAccessed* lastAcc = [self lastAccessedDataForCourseID:courseID];
if(!lastAcc) {
LastAccessed* lastAcc = [NSEntityDescription insertNewObjectForEntityForName:@"LastAccessed"
inManagedObjectContext:_backGroundContext];
lastAcc.course_id = courseID;
lastAcc.subsection_id = subsectionID;
lastAcc.subsection_name = subsectionName;
lastAcc.timestamp = timestamp;
}
else {
lastAcc.subsection_id = subsectionID;
lastAcc.subsection_name = subsectionName;
lastAcc.timestamp = timestamp;
}
[self saveCurrentStateToDB];
}
}
#pragma mark - VideoData Table Methods
- (NSArray*)getAllLocalVideoData {
CLS_LOG(@"getAllLocalVideoData");
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
CLS_LOG(@"getAllLocalVideoData: backGroundContext...%@", _backGroundContext);
NSEntityDescription* videoEntity = [NSEntityDescription entityForName:@"VideoData" inManagedObjectContext:_backGroundContext];
CLS_LOG(@"getAllLocalVideoData: getVideoEntity from manageObjectContect");
[fetchRequest setEntity:videoEntity];
return [self executeFetchRequest:fetchRequest];
}
- (VideoData*)getVideoDataForVideoID:(NSString*)videoId {
return [self videoDataForVideoID:videoId];
}
- (VideoData*)videoDataForVideoID:(NSString*)video_id {
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription* videoEntity = [NSEntityDescription entityForName:@"VideoData" inManagedObjectContext:self.backGroundContext];
[fetchRequest setEntity:videoEntity];
NSPredicate* query = [NSPredicate predicateWithFormat:@"video_id==%@", video_id];
//setting the predicate to the fetch request
[fetchRequest setPredicate:query];
NSArray* resultArray = [self executeFetchRequest:fetchRequest];
// ELog(@"getVideoDataFor VideoID : %@",resultArray);
return (VideoData*)[resultArray firstObject];
}
- (void)startedDownloadForURL:(NSString*)downloadUrl andVideoId:(NSString*)videoId {
VideoData* newVideoData;
newVideoData = [self videoDataForVideoID:videoId];
if(!newVideoData) {
newVideoData = [NSEntityDescription insertNewObjectForEntityForName:@"VideoData"
inManagedObjectContext:_backGroundContext];
newVideoData.video_id = videoId;
}
//Download State
newVideoData.download_state = [NSNumber numberWithInt: OEXDownloadStatePartial];
[self saveCurrentStateToDB];
}
- (OEXDownloadState)videoStateForVideoID:(NSString*)video_id {
VideoData* videoData = [self getVideoDataForVideoID:video_id];
if(videoData) {
return [videoData.download_state intValue];
}
return OEXDownloadStateNew;
}
- (OEXPlayedState)watchedStateForVideoID:(NSString*)video_id {
VideoData* videoData = [self getVideoDataForVideoID:video_id];
if(videoData) {
return [videoData.played_state intValue];
}
return OEXPlayedStateUnwatched;
}
- (float)lastPlayedIntervalForVideoID:(NSString*)video_id {
VideoData* videoData = [self getVideoDataForVideoID:video_id];
if(videoData) {
return [videoData.last_played_offset floatValue];
}
return 0.0;
}
- (void)markLastPlayedInterval:(float)playedInterval forVideoID:(NSString*)video_id {
VideoData* videoData = [self getVideoDataForVideoID:video_id];
if(videoData) {
videoData.last_played_offset = [NSNumber numberWithFloat: playedInterval];
[self saveCurrentStateToDB];
}
}
- (void)markPlayedState:(OEXPlayedState)state forVideoID:(NSString*)video_id {
VideoData* videoData = [self getVideoDataForVideoID:video_id];
if(videoData) {
videoData.played_state = [NSNumber numberWithFloat: state];
[self saveCurrentStateToDB];
}
}
- (NSData*)resumeDataForVideoID:(NSString*)video_id {
NSData* data = nil;
VideoData* videoData = [self getVideoDataForVideoID:video_id];
if(!videoData) {
return nil;
}
data = [[NSFileManager defaultManager] contentsAtPath:[OEXFileUtility filePathForRequestKey:videoData.video_url]];
return data;
}
- (void)startedDownloadForVideo:(VideoData*)videoData {
if(videoData) {
videoData.download_state = [NSNumber numberWithFloat:OEXDownloadStatePartial];
[self saveCurrentStateToDB];
}
}
- (void)onlineEntryForVideo:(VideoData*)videoData {
if(videoData) {
videoData.download_state = [NSNumber numberWithFloat:OEXDownloadStateNew];
[self saveCurrentStateToDB];
}
}
- (void)completedDownloadForVideo:(VideoData*)videoData {
if(videoData) {
videoData.download_state = [NSNumber numberWithFloat:OEXDownloadStateComplete];
videoData.downloadCompleteDate = [NSDate date];
videoData.dm_id = [NSNumber numberWithInt:0];
[self saveCurrentStateToDB];
}
}
- (void)cancelledDownloadForVideo:(VideoData*)videoData {
if(videoData) {
videoData.download_state = [NSNumber numberWithFloat:OEXDownloadStateNew];
videoData.dm_id = [NSNumber numberWithInt:0];
[self deleteDataForVideoID:videoData.video_id];
[self saveCurrentStateToDB];
}
}
- (void)pausedAllDownloads {
NSArray* array = [self getAllLocalVideoData];
CLS_LOG(@"pausedAllDownloads: localVideoData...%@", array);
for(VideoData* video in array) {
CLS_LOG(@"pausedAllDownloads: VideoData...%@", video);
if((video && [video isKindOfClass:[VideoData class]]) && ([video.download_state intValue] == OEXDownloadStatePartial || [video.dm_id intValue] != 0)) {
video.dm_id = [NSNumber numberWithInt:0];
}
}
[self saveCurrentStateToDB];
}
- (void)deleteDataForVideoID:(NSString*)video_id {
VideoData* videoData = [self getVideoDataForVideoID:video_id];
NSArray* arrVideo = [self getVideosForDownloadUrl:videoData.video_url];
int referenceCount = 0;
if([arrVideo count] > 1) {
for(VideoData* video in arrVideo) {
if([video.download_state intValue] == OEXDownloadStateComplete) {
referenceCount++;
if(referenceCount >= 2) {
break;
}
}
}
}
//NSString * filePath = [NSString stringWithFormat:@"%@", videoData.filepath];
if(videoData) {
//Mark new in DB
videoData.download_state = @(OEXDownloadStateNew);
videoData.dm_id = @(0);
[self saveCurrentStateToDB];
//Delete file
if(referenceCount <= 1) {
[self deleteFileForURL:videoData.video_url];
}
}
}
- (void)deleteFileForURL:(NSString*)URL {
[[NSFileManager defaultManager] removeItemAtPath:[OEXFileUtility filePathForVideoURL:URL username:[OEXSession sharedSession].currentUser.username]
error:nil];
[[NSFileManager defaultManager] removeItemAtPath:[OEXFileUtility filePathForRequestKey:URL]
error:nil];
}
- (NSArray*)getVideosForDownloadUrl:(NSString*)downloadUrl {
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"VideoData" inManagedObjectContext:_backGroundContext]];
NSPredicate* query = [NSPredicate predicateWithFormat:@"video_url==%@", downloadUrl];
[fetchRequest setPredicate:query];
return [self executeFetchRequest:fetchRequest];
}
- (NSArray*)getVideosForDownloadState:(OEXDownloadState)state {
CLS_LOG(@"getVideosForDownloadState");
NSArray* allVideos = [self getAllLocalVideoData];
NSMutableArray* filteredArray = [[NSMutableArray alloc] init];
CLS_LOG(@"getVideosForDownloadState: videosForDownloadState...%@", allVideos);
for(VideoData* data in allVideos) {
if(data && [data isKindOfClass:[VideoData class]] && ([data.download_state intValue] == state)) {
[filteredArray addObject:data];
}
}
return filteredArray;
}
- (NSArray*)getAllDownloadingVideosForURL:(NSString*)url {
NSArray* allVideos = [self getVideosForDownloadUrl:url];
NSMutableArray* filteredArray = [[NSMutableArray alloc] init];
for(VideoData* data in allVideos) {
if([data.download_state intValue] == OEXDownloadStatePartial || [data.dm_id intValue] != 0) {
[filteredArray addObject:data];
}
}
return filteredArray;
}
- (NSArray*)getAllDownloadingVideos {
NSArray* allVideos = [self getAllLocalVideoData];
NSMutableArray* filteredArray = [[NSMutableArray alloc] init];
for(VideoData* data in allVideos) {
if([data.download_state intValue] == OEXDownloadStatePartial || [data.dm_id intValue] != 0) {
[filteredArray addObject:data];
}
}
return filteredArray;
}
- (VideoData*)videoDataForTaskIdentifier:(NSUInteger)dTaskId {
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"VideoData" inManagedObjectContext:_backGroundContext]];
NSPredicate* query = [NSPredicate predicateWithFormat:@"dm_id==%lu", dTaskId];
//setting the predicate to the fetch request
[fetchRequest setPredicate:query];
NSArray* resultArray = [self executeFetchRequest:fetchRequest];
return (VideoData*)[resultArray firstObject];
}
- (NSArray*)videosForTaskIdentifier:(NSUInteger)dTaskId {
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"VideoData" inManagedObjectContext:_backGroundContext]];
NSPredicate* query = [NSPredicate predicateWithFormat:@"dm_id==%lu", dTaskId];
//setting the predicate to the fetch request
[fetchRequest setPredicate:query];
NSArray* resultArray = [self executeFetchRequest:fetchRequest];
return resultArray;
}
// Update the is_active column on refresh
- (void)unregisterAllEntries {
NSArray* array = [self getAllLocalVideoData];
for(VideoData* video in array) {
video.is_registered = [NSNumber numberWithBool:NO];
}
[self saveCurrentStateToDB];
}
// Get all videos for enrollmentID
- (NSArray*)videosWithCourseID:(NSString*)courseid {
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"VideoData" inManagedObjectContext:_backGroundContext]];
NSPredicate* query = [NSPredicate predicateWithFormat:@"enrollment_id==%@", courseid];
//setting the predicate to the fetch request
[fetchRequest setPredicate:query];
NSArray* resultArray = [self executeFetchRequest:fetchRequest];
return resultArray;
}
// get all unregistered videos
- (void)deleteUnregisteredItems {
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"VideoData" inManagedObjectContext:_backGroundContext]];
NSPredicate* query = [NSPredicate predicateWithFormat:@"is_registered==%@", [NSNumber numberWithBool:NO]];
//setting the predicate to the fetch request
[fetchRequest setPredicate:query];
NSArray* resultArray = [self executeFetchRequest:fetchRequest];
for(VideoData* video in resultArray) {
[self deleteFileForURL:video.video_url];
}
// deleting the video data only if the video is deleted in online of offline mode.
for(NSManagedObject* managedObject in resultArray) {
[[managedObject managedObjectContext] deleteObject:managedObject];
}
[self saveCurrentStateToDB];
}
- (void)setRegisteredCoursesAndDeleteUnregisteredData:(NSString*)courseid {
NSArray* arrVideos = [self videosWithCourseID:courseid];
for(VideoData* video in arrVideos) {
video.is_registered = [NSNumber numberWithBool:YES];
}
[self saveCurrentStateToDB];
}
#pragma mark - POST GA DB Interface/Protocol Implementation
#pragma - insertion query
// All the operations will have a "where" clause to filter data as per the logged-in User.
//inserting the video data only if the video is played online or started downloading.
- (VideoData*)insertVideoData:(NSString*)username
Title:(NSString*)title
Size:(NSString*)size
Duration:(NSString*)duration
DownloadState:(OEXDownloadState)download_state
VideoURL:(NSString*)video_url
VideoID:(NSString*)video_id
UnitURL:(NSString*)unit_url
CourseID:(NSString*)enrollment_id
DMID:(int)dm_id
ChapterName:(NSString*)chapter_name
SectionName:(NSString*)section_name
TimeStamp:(NSDate*)downloadCompleteDate
LastPlayedTime:(float)last_played_offset
is_Reg:(BOOL)is_registered
PlayedState:(OEXPlayedState)played_state {
VideoData* Checkdata = [self getVideoDataForVideoID:video_id];
if(Checkdata && video_id != nil) {
return Checkdata;
}
VideoData* videoObj = [NSEntityDescription insertNewObjectForEntityForName:@"VideoData"
inManagedObjectContext:_backGroundContext];
videoObj.username = username;
videoObj.title = title;
videoObj.size = size;
videoObj.duration = duration;
videoObj.download_state = [NSNumber numberWithInt:download_state];
videoObj.video_url = video_url;
videoObj.video_id = video_id;
videoObj.unit_url = unit_url;
videoObj.enrollment_id = enrollment_id;
videoObj.dm_id = [NSNumber numberWithUnsignedInteger:dm_id];
videoObj.chapter_name = chapter_name;
videoObj.section_name = section_name;
videoObj.downloadCompleteDate = downloadCompleteDate;
videoObj.last_played_offset = [NSNumber numberWithFloat:last_played_offset];
videoObj.is_registered = [NSNumber numberWithBool:is_registered];
videoObj.played_state = [NSNumber numberWithInt:played_state];
[self saveCurrentStateToDB];
return videoObj;
}
#pragma - deletion query
//deleting the video data only if the video is deleted in online of offline mode.
- (void)deleteVideoData:(NSString*)username
:(NSString*)video_id {
NSArray* resultArray = [self getRecordsForOperation:username VideoID:video_id];
for(NSManagedObject* managedObject in resultArray) {
[_backGroundContext deleteObject:managedObject];
}
}
#pragma - Fetch / selection query
//select the video data to show up for a user
- (NSArray*)getAllVideoDataFor:(NSString*)username {
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"VideoData" inManagedObjectContext:_backGroundContext]];
NSPredicate* query = [NSPredicate predicateWithFormat:@"username==%@", username];
//setting the predicate to the fetch request
[fetchRequest setPredicate:query];
NSArray* resultArray = [self executeFetchRequest:fetchRequest];
// ELog(@"getAllVideoData FOr Username : %@",resultArray);
return resultArray;
}
- (NSArray*)getVideoDataFor:(NSString*)username
VideoID:(NSString*)video_id {
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"VideoData" inManagedObjectContext:_backGroundContext]];
NSPredicate* query = [NSPredicate predicateWithFormat:@"username==%@", username];
NSPredicate* query1 = [NSPredicate predicateWithFormat:@"video_id==%@", video_id];
NSPredicate* compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[query, query1]];
//setting the predicate to the fetch request
[fetchRequest setPredicate:compoundPredicate];
NSArray* resultArray = [self executeFetchRequest:fetchRequest];
OEXLogInfo(@"STORAGE", @"getVideoDataFor VideoID : %@", resultArray);
return resultArray;
}
- (NSArray*)getVideoDataFor:(NSString*)username
EnrollmentID:(NSString*)enrollment_id {
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"VideoData" inManagedObjectContext:_backGroundContext]];
NSPredicate* query = [NSPredicate predicateWithFormat:@"username==%@", username];
NSPredicate* query1 = [NSPredicate predicateWithFormat:@"enrollment_id==%@", enrollment_id];
NSPredicate* compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[query, query1]];
//setting the predicate to the fetch request
[fetchRequest setPredicate:compoundPredicate];
NSArray* resultArray = [self executeFetchRequest:fetchRequest];
OEXLogInfo(@"STORAGE", @"getVideoDataFor EnrollmentID : %@", resultArray);
return resultArray;
}
#pragma - update query
- (NSArray*)getRecordsForOperation:(NSString*)username
VideoID:(NSString*)video_id {
NSPredicate* query = [NSPredicate predicateWithFormat:@"username==%@ and video_id==%@", username, video_id];
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"VideoData" inManagedObjectContext:_backGroundContext]];
//setting the predicate to the fetch request
[fetchRequest setPredicate:query];
NSArray* resultArray = [self executeFetchRequest:fetchRequest];
return resultArray;
}
// Update the video data with last played time when playing is paused
- (void)updateLastPlayedTime:(NSString*)username
VideoID:(NSString*)video_id
WithLastPlayedTime:(float)last_played_offset {
NSArray* resultArray = [self getRecordsForOperation:username VideoID:video_id];
if([resultArray count] > 0) {
VideoData* videoObj = [resultArray objectAtIndex:0];
videoObj.last_played_offset = [NSNumber numberWithFloat: last_played_offset];
[self saveCurrentStateToDB];
}
}
// Update the video data with download state
- (void)updateDownloadState:(NSString*)username
VideoID:(NSString*)video_id
WithDownloadState:(int)download_state {
NSArray* resultArray = [self getRecordsForOperation:username VideoID:video_id];
if([resultArray count] > 0) {
VideoData* videoObj = [resultArray objectAtIndex:0];
videoObj.download_state = [NSNumber numberWithInt: download_state];
[self saveCurrentStateToDB];
}
}
// Update the video data with played state
- (void)updatePlayedState:(NSString*)username
VideoID:(NSString*)video_id
WithPlayedState:(int)played_state {
NSArray* resultArray = [self getRecordsForOperation:username VideoID:video_id];
if([resultArray count] > 0) {
VideoData* videoObj = [resultArray objectAtIndex:0];
videoObj.played_state = [NSNumber numberWithInt: played_state];
[self saveCurrentStateToDB];
}
}
// Update the video downloaded timestamp
- (void)updateDownloadTimestamp:(NSString*)username
VideoID:(NSString*)video_id
WithTimeStamp:(NSDate*)downloadCompleteDate {
NSArray* resultArray = [self getRecordsForOperation:username VideoID:video_id];
if([resultArray count] > 0) {
VideoData* videoObj = [resultArray objectAtIndex:0];
videoObj.downloadCompleteDate = downloadCompleteDate;
[self saveCurrentStateToDB];
}
}
// Update the course state if it is registered or no
- (void)updateCourseRegisterState:(NSString*)username
CourseID:(NSString*)enrollment_id
Withis_Reg:(BOOL)is_registered {
NSPredicate* query = [NSPredicate predicateWithFormat:@"username==%@ and enrollment_id==%@", username, enrollment_id];
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"VideoData" inManagedObjectContext:_backGroundContext]];
//setting the predicate to the fetch request
[fetchRequest setPredicate:query];
NSArray* resultArray = [self executeFetchRequest:fetchRequest];
if([resultArray count] > 0) {
VideoData* videoObj = [resultArray objectAtIndex:0];
videoObj.is_registered = [NSNumber numberWithBool:is_registered];
[self saveCurrentStateToDB];
}
}
@end