Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // FBDataAdapter.m
- // Yearbook
- //
- // Created by Mitch Robertson on 9/06/10.
- // Copyright 2010 __MyCompanyName__. All rights reserved.
- //
- #import "DataInputAdapter.h"
- #import "DictionaryToObject.h"
- #import "lazyjobdirective.h"
- #import "guroo_image.h"
- #import "Ref.h"
- #import "SyncedDate.h"
- @implementation DataInputAdapter
- @synthesize managedObjectContext;
- @synthesize dataOutputAdapter;
- @synthesize preferences;
- - (id)initWithManagedObjectContext:(NSManagedObjectContext *)managedObjectContext1 andPrefs:(Preferences *)prefs1 {
- if (!(self = [self init])) return nil;
- self.managedObjectContext = managedObjectContext1;
- self.dataOutputAdapter = [[DataOutputAdapter alloc] initWithManagedObjectContext:managedObjectContext1];
- self.preferences = prefs1;
- return self;
- }
- + (NSString *)getModifiedField:(NSString *)t {
- if ([t isEqualToString:@"stream"]) {
- return @"updated_time";
- } else if ([t isEqualToString:@"album"]) {
- return @"modified";
- } else if ([t isEqualToString:@"note"]) {
- return @"updated_time";
- } else if ([t isEqualToString:@"thread"]) {
- return @"updated_time";
- } else if ([t isEqualToString:@"photo"]) {
- return @"modified";
- } else if ([t isEqualToString:@"guroo_friends"]) {
- return @"profile_update_time";
- }
- if ([t isEqualToString:@"status"]) {
- return @"updated_time";
- }
- return nil;
- }
- - (void)commitTransaction {
- NSError *error;
- int insertedCount = [[managedObjectContext insertedObjects] count];
- if (![managedObjectContext save:&error]) {
- NSLog(@"ERROR SAVING STREAM:%@", [error localizedDescription]);
- } else {
- //NSLog(@"DataInputAdapter: inserted %i objects",insertedCount);
- }
- }
- - (BOOL)date:(NSDate *)d1 isGreaterThan:(NSDate *)d2 {
- double d1doub = [d1 timeIntervalSince1970];
- double d2doub = [d2 timeIntervalSince1970];
- //NSLog(@"%0.f-%0.f = %0.f :: %@",d1doub,d2doub,(d1doub-d2doub),(d1doub>d2doub) ? @"YES" : @"NO");
- return (d1doub > d2doub);
- }
- - (BOOL)dateInField:(NSString *)fieldKey inDict:(NSDictionary *)obj isGreaterThan:(NSDate *)date {
- NSDate *dictDate = [DictionaryToObject parseDate:[obj objectForKey:fieldKey]];
- return [self date:dictDate isGreaterThan:date];
- }
- // add entity returning a list of key/otherField pairs that have been modified
- - (NSMutableArray *)saveEntityType:(NSString *)t objs:(NSArray *)objs keyField:(NSString *)kf recordAuxField:(NSString *)otherField assocFeedIndex:(int)ft {
- // list of new or modified keys
- NSString *modifiedKey = [DataInputAdapter getModifiedField:t];
- NSMutableArray *modifiedEntityKeys = [[NSMutableArray alloc] init];
- BOOL contentToSave = NO;
- int addCount = 0;
- for (int i = 0; i < [objs count]; i++) {
- NSDictionary *obj = [objs objectAtIndex:i];
- NSString *idValue = [obj valueForKey:kf];
- if ([idValue isKindOfClass:[NSNumber class]]) {
- idValue = [((NSNumber *) idValue) stringValue];
- }
- NSManagedObject *mo = [self.dataOutputAdapter getDBRecordOnTable:t whereField:kf equals:idValue];
- if (mo == nil) {
- //NSLog(@"----------------------->ADD %@, id: %@",t,idValue);
- addCount++;
- mo = [NSEntityDescription
- insertNewObjectForEntityForName:t
- inManagedObjectContext:self.managedObjectContext];
- mo = [DictionaryToObject populateManagedObject:mo ofType:t withResult:obj];
- if (otherField == nil) {
- [modifiedEntityKeys addObject:idValue];
- } else {
- Ref *ref = [[Ref alloc] init];
- ref.string1 = idValue;
- NSArray *otherFields = [otherField componentsSeparatedByString:@","];
- ref.string2 = [obj valueForKey:[otherFields objectAtIndex:0]];
- if ([otherFields count] > 1) {
- ref.string3 = [obj valueForKey:[otherFields objectAtIndex:1]];
- }
- [modifiedEntityKeys addObject:ref];
- }
- contentToSave = YES;
- } else {
- if (modifiedKey != nil) {
- // edit existing if needed
- NSDate *oldDate = ((NSDate *) objc_msgSend(mo, NSSelectorFromString(modifiedKey)));
- if ([self dateInField:modifiedKey inDict:obj isGreaterThan:oldDate] == YES) {
- //NSLog(@"----------------------->EDIT %@, id: %@",t,idValue);
- // EDIT existing
- mo = [DictionaryToObject populateManagedObject:mo ofType:t withResult:obj];
- if (otherField == nil) {
- [modifiedEntityKeys addObject:idValue];
- } else {
- Ref *ref = [[Ref alloc] init];
- ref.string1 = idValue;
- NSArray *otherFields = [otherField componentsSeparatedByString:@","];
- ref.string2 = [obj valueForKey:[otherFields objectAtIndex:0]];
- if ([otherFields count] > 1) {
- ref.string3 = [obj valueForKey:[otherFields objectAtIndex:1]];
- }
- [modifiedEntityKeys addObject:ref];
- }
- contentToSave = YES;
- }
- }
- }
- }
- if (contentToSave) {
- if (ft != -1) {
- [self.preferences addToTotalForFeed:ft amount:addCount];
- }
- [self commitTransaction];
- }
- return modifiedEntityKeys;
- }
- // add entity returning a list of ids that have been modified
- - (NSMutableArray *)saveEntityType:(NSString *)t objs:(NSArray *)objs keyField:(NSString *)kf assocFeedIndex:(int)ft {
- return [self saveEntityType:t objs:objs keyField:kf recordAuxField:nil assocFeedIndex:ft];
- }
- - (void)saveChildEntityType:(NSString *)t objs:(NSArray *)objs parentRefField:(NSString *)prf {
- BOOL contentToSave = NO;
- // parent keys whose children have been deleted
- NSMutableDictionary *deletedChildren = [[NSMutableDictionary alloc] init];
- NSArray *parentRefFields = [prf componentsSeparatedByString:@","];
- prf = [parentRefFields objectAtIndex:0];
- for (int i = 0; i < [objs count]; i++) {
- NSDictionary *obj = [objs objectAtIndex:i];
- NSObject *parentIdVal = [obj valueForKey:prf];
- if ((parentIdVal == [NSNull null] || parentIdVal == nil) && [parentRefFields count] > 1) {
- prf = [parentRefFields objectAtIndex:1];
- parentIdVal = [obj valueForKey:prf];
- }
- NSString *parentId = [parentIdVal isKindOfClass:[NSNumber class]] ? [((NSNumber *) parentIdVal) stringValue] : parentIdVal;
- parentId = [DictionaryToObject extractID:parentId];
- // delete all children if haven't already
- if ([deletedChildren valueForKey:parentId] == nil) {
- // delete all children
- NSMutableArray *oldChildren = [self.dataOutputAdapter getDBRecordsOnTable:t whereField:[parentRefFields count] > 1 ? [parentRefFields objectAtIndex:0] : prf equals:parentId];
- //NSLog(@"DataInputAdapter: deleting %i old children before saving new ones",[oldChildren count]);
- for (NSManagedObject *oldManagedObj in oldChildren) {
- [self.managedObjectContext deleteObject:oldManagedObj];
- contentToSave = YES;
- }
- [deletedChildren setValue:@"YES" forKey:parentId];
- }
- NSManagedObject *mo = [NSEntityDescription
- insertNewObjectForEntityForName:t
- inManagedObjectContext:self.managedObjectContext];
- mo = [DictionaryToObject populateManagedObject:mo ofType:t withResult:obj];
- contentToSave = YES;
- }
- if (contentToSave) {
- [self commitTransaction];
- }
- }
- // private
- - (void)saveLazyJobDirectivesFor:(NSMutableArray *)changed assocFeedType:(FeedType *)feedType {
- NSString *feedTypeStr = [NSString stringWithFormat:@"%i", feedType];
- NSMutableArray *all = [self.dataOutputAdapter getDBRecordsOnTable:
- @"lazyjobdirective" whereField:@"feedType" equals:feedTypeStr];
- NSMutableSet *seen = [[NSMutableSet alloc] init];
- if (all != nil) {
- // build up list of seen
- for (lazyjobdirective *lazy in all) {
- /*if ([lazy.completed isEqualToString:@"YES"]){
- [self.managedObjectContext deleteObject:lazy];
- }else {*/
- [seen addObject:lazy.itemUID];
- //}
- }
- }
- for (NSObject *obj in changed) {
- NSString *key = [obj isKindOfClass:[Ref class]] ? ((Ref *) obj).string1 : (NSString *) obj;
- if (![seen containsObject:key]) {
- lazyjobdirective *lazy = [NSEntityDescription
- insertNewObjectForEntityForName:@"lazyjobdirective"
- inManagedObjectContext:self.managedObjectContext];
- lazy.completed = @"NO";
- lazy.feedType = feedTypeStr;
- lazy.itemUID = key;
- }
- }
- }
- // LEVEL 1 Entities
- - (Ref *)saveProfileInfo:(NSDictionary *)userInfo {
- NSString *idValue = [userInfo valueForKey:@"uid"];
- NSManagedObject *mo = [self.dataOutputAdapter getDBRecordOnTable:@"user" whereField:@"uid" equals:idValue];
- if (mo == nil) {
- mo = [NSEntityDescription
- insertNewObjectForEntityForName:@"user"
- inManagedObjectContext:self.managedObjectContext];
- mo = [DictionaryToObject populateManagedObject:mo ofType:@"user" withResult:userInfo];
- } else {
- // edit exisiting
- mo = [DictionaryToObject populateManagedObject:mo ofType:@"user" withResult:userInfo];
- }
- [self commitTransaction];
- // add fake friend
- guroo_friends *profileFriend = (guroo_friends *) [self.dataOutputAdapter getDBRecordOnTable:@"guroo_friends" whereField:@"uid" equals:idValue];
- Ref *ref = nil;
- if (profileFriend == nil) {
- // create new
- profileFriend = (guroo_friends *) [NSEntityDescription
- insertNewObjectForEntityForName:@"guroo_friends"
- inManagedObjectContext:self.managedObjectContext];
- profileFriend = [DictionaryToObject populateFriendObject:profileFriend withResult:userInfo];
- ref = [[Ref alloc] init];
- ref.string1 = profileFriend.uid;
- ref.string2 = @"pic_square";
- ref.string3 = profileFriend.pic_square;
- } else {
- NSLog(@"pic_big=@%", profileFriend.pic_big);
- NSLog(@"pic_square=@%", profileFriend.pic_square);
- NSLog(@"uid=@%", profileFriend.uid);
- NSLog(@"name=@%", profileFriend.name);
- NSLog(@"status=@%", profileFriend.status);
- NSLog(@"===================");
- NSString *oldURL = profileFriend.pic_square;
- NSString *newURL = [userInfo valueForKey:@"pic_square"];
- if ((oldURL == nil && newURL != nil) || (oldURL != nil && newURL != nil && ![oldURL isEqualToString:newURL])) {
- ref = [[Ref alloc] init];
- ref.string1 = profileFriend.uid;
- ref.string2 = @"pic_square";
- ref.string3 = profileFriend.pic_square;
- }
- // edit existing
- profileFriend = [DictionaryToObject populateFriendObject:profileFriend withResult:userInfo];
- }
- [self commitTransaction];
- return ref;
- }
- - (void)deleteObjects:(NSString *)objectType whereField:(NSString *)fieldKey equals:(NSString *)fieldValue {
- NSArray *results = [self.dataOutputAdapter getDBRecordsOnTable:objectType whereField:fieldKey equals:fieldValue];
- if (results != nil) {
- for (NSManagedObject *obj in results) {
- [self.managedObjectContext deleteObject:obj];
- }
- [self commitTransaction];
- }
- }
- - (BOOL)isNameForPerson:(NSString *)uid {
- if (uid != nil) {
- return [self.preferences getNameForUID:uid] != nil || [self.dataOutputAdapter getDBRecordOnTable:@"guroo_friends" whereField:@"uid" equals:uid] != nil;
- } else {
- return YES;
- }
- }
- - (NSString *)toString:(NSObject *)obj {
- if (obj == nil) {
- return @"";
- }
- if ([obj isKindOfClass:[NSNumber class]]) {
- return [((NSNumber *) obj) stringValue];
- }
- return obj;
- }
- - (NSMutableArray *)saveStatusResults:(NSArray *)objs {
- // list of new or modified keys
- NSString *modifiedKey = @"updated_time";
- NSMutableArray *modifiedEntityKeys = [[NSMutableArray alloc] init];
- BOOL contentToSave = NO;
- int addCount = 0;
- for (int i = 0; i < [objs count]; i++) {
- NSDictionary *obj = [objs objectAtIndex:i];
- NSString *idValue = [obj valueForKey:@"id"];
- if ([idValue isKindOfClass:[NSNumber class]]) {
- idValue = [((NSNumber *) idValue) stringValue];
- }
- NSManagedObject *mo = [self.dataOutputAdapter getDBRecordOnTable:@"status" whereField:@"status_id" equals:idValue];
- if (mo == nil) {
- //NSLog(@"----------------------->ADD %@, id: %@",t,idValue);
- addCount++;
- mo = [NSEntityDescription
- insertNewObjectForEntityForName:@"status"
- inManagedObjectContext:self.managedObjectContext];
- } else {
- // don't add again
- }
- mo = [DictionaryToObject populateManagedObject:mo ofType:@"status" withResult:obj graph:YES];
- [modifiedEntityKeys addObject:idValue];
- contentToSave = YES;
- [self saveGraphComments:[obj objectForKey:@"comments"] forObjectID:idValue];
- [self saveGraphLikes:[obj objectForKey:@"likes"] forObjectID:idValue];
- }
- if (contentToSave) {
- [self.preferences addToTotalForFeed:STATUSUPDATES amount:addCount];
- [self commitTransaction];
- }
- return modifiedEntityKeys;
- }
- - (NSMutableArray *)saveStreamResults:(NSArray *)objs {
- NSObject *paging = nil;
- if (objs != nil && [objs isKindOfClass:[NSDictionary class]]) {
- objs = [((NSDictionary *) objs) objectForKey:@"data"];
- }
- // list of new or modified keys
- NSString *modifiedKey = @"updated_time";
- NSMutableArray *modifiedEntityKeys = [[NSMutableArray alloc] init];
- BOOL contentToSave = NO;
- int addCount = 0;
- for (int i = 0; i < [objs count]; i++) {
- NSDictionary *obj = [objs objectAtIndex:i];
- NSString *idValue = [obj valueForKey:@"id"];
- /*for(NSString *aKey in obj){
- NSLog(aKey);
- }*/
- NSString *type = [obj valueForKey:@"type"];
- /*if (type != nil && [type isEqualToString:@"status"]){
- continue;
- }*/
- NSDictionary *from = [obj objectForKey:@"from"];
- NSString *fromID = [from objectForKey:@"id"];
- // filter out status updates
- if ([type isEqualToString:@"status"] && [fromID isEqualToString:[self.preferences getLoggedInUserID]]) {
- NSLog(@"IGNORED %@", [obj objectForKey:@"message"]);
- continue;
- }
- if ([idValue isKindOfClass:[NSNumber class]]) {
- idValue = [((NSNumber *) idValue) stringValue];
- }
- idValue = [DictionaryToObject extractID:idValue];
- NSManagedObject *mo = [self.dataOutputAdapter getDBRecordOnTable:@"stream" whereField:@"post_id" equals:idValue];
- if (mo == nil) {
- //NSLog(@"----------------------->ADD %@, id: %@",t,idValue);
- addCount++;
- mo = [NSEntityDescription
- insertNewObjectForEntityForName:@"stream"
- inManagedObjectContext:self.managedObjectContext];
- } else {
- // don't add again
- }
- mo = [DictionaryToObject populateManagedObject:mo ofType:@"stream" withResult:obj graph:YES];
- [modifiedEntityKeys addObject:idValue];
- contentToSave = YES;
- [self saveGraphComments:[obj objectForKey:@"comments"] forObjectID:idValue];
- [self saveGraphLikes:[obj objectForKey:@"likes"] forObjectID:idValue];
- }
- if (contentToSave) {
- [self.preferences addToTotalForFeed:WALLPOST amount:addCount];
- [self commitTransaction];
- }
- return modifiedEntityKeys;
- }
- - (NSMutableArray *)saveAlbumsList:(NSArray *)albums {
- NSMutableArray *newOrModified = [self saveEntityType:@"album" objs:albums keyField:@"aid" recordAuxField:@"object_id,cover_pid" assocFeedIndex:ALBUM];
- [self saveLazyJobDirectivesFor:newOrModified assocFeedType:ALBUM];
- return newOrModified;
- }
- - (NSMutableArray *)saveNoteResults:(NSArray *)noteResults {
- return [self saveEntityType:@"note" objs:noteResults keyField:@"note_id" assocFeedIndex:NOTE];
- }
- - (void)saveThreadResults:(NSArray *)threadResults {
- NSMutableArray *newOrModified = [self saveEntityType:@"thread" objs:threadResults keyField:@"thread_id" assocFeedIndex:INBOX];
- [self saveLazyJobDirectivesFor:newOrModified assocFeedType:INBOX];
- }
- /*
- -(NSMutableArray *) savePhotosList: (NSArray *) photos{
- return [self saveEntityType:@"photo" objs:albums keyField:@"pid" recordAuxField:@"object_id" assocFeedIndex:PHOTO];
- }*/
- // LEVEL 2 Entities
- - (void)saveComments:(NSArray *)comments {
- [self saveChildEntityType:@"comment" objs:comments parentRefField:@"object_id"];
- }
- - (void)saveStreamComments:(NSArray *)comments {
- [self saveChildEntityType:@"comment" objs:comments parentRefField:@"object_id,post_id"];
- }
- - (void)saveMessageResults:(NSArray *)messages {
- [self saveChildEntityType:@"message" objs:messages parentRefField:@"thread_id"];
- }
- - (void)saveLikeResults:(NSArray *)likes {
- [self saveChildEntityType:@"like" objs:likes parentRefField:@"object_id"];
- }
- - (void)savePhotoTagResults:(NSArray *)photoTags {
- [self saveChildEntityType:@"photo_tag" objs:photoTags parentRefField:@"pid"];
- }
- - (NSArray *)extractGraphData:(NSArray *)data {
- if (data != nil && [data isKindOfClass:[NSDictionary class]]) {
- return [((NSDictionary *) data) objectForKey:@"data"];
- }
- return data;
- }
- - (int)saveGraphComments:(NSArray *)newComments forObjectID:(NSString *)objID {
- newComments = [self extractGraphData:newComments];
- // delete old ones
- NSArray *cms = [self.dataOutputAdapter getDBRecordsOnTable:@"comment" whereField:@"object_id" equals:objID];
- if (cms != nil) {
- for (NSManagedObject *comment in cms) {
- [self.managedObjectContext deleteObject:comment];
- }
- }
- comment *c;
- NSDictionary *from;
- NSString *commentID;
- for (NSDictionary *dict in newComments) {
- c = [NSEntityDescription
- insertNewObjectForEntityForName:@"comment"
- inManagedObjectContext:self.managedObjectContext];
- from = [dict objectForKey:@"from"];
- commentID = [dict valueForKey:@"id"];
- c.text = [dict valueForKey:@"message"];
- c.time = [DictionaryToObject parseDate:[dict valueForKey:@"created_time"]];
- c.fromid = [DictionaryToObject parseString:[from valueForKey:@"id"]];
- c.object_id = objID;
- c.xid = [DictionaryToObject extractID:commentID];
- if (![self isNameForPerson:c.fromid]) {
- [self.preferences setNameForUID:[from valueForKey:@"name"] :c.fromid];
- }
- }
- [self commitTransaction];
- return newComments != nil ? [newComments count] : 0;
- }
- - (int)saveGraphLikes:(NSArray *)newLikes forObjectID:(NSString *)objID {
- newLikes = [self extractGraphData:newLikes];
- // delete old ones
- NSArray *likes = [self.dataOutputAdapter getDBRecordsOnTable:@"like" whereField:@"object_id" equals:objID];
- if (likes != nil) {
- for (NSManagedObject *like in likes) {
- [self.managedObjectContext deleteObject:like];
- }
- }
- like *l;
- for (NSDictionary *dict in newLikes) {
- l = [NSEntityDescription
- insertNewObjectForEntityForName:@"like"
- inManagedObjectContext:self.managedObjectContext];
- l.user_id = [dict valueForKey:@"id"];
- l.object_id = objID;
- if (![self isNameForPerson:[dict valueForKey:@"id"]]) {
- [self.preferences setNameForUID:[dict valueForKey:@"name"] :[dict valueForKey:@"id"]];
- }
- }
- [self commitTransaction];
- return newLikes != nil ? [newLikes count] : 0;
- }
- // exceptions
- - (NSMutableArray *)saveFriendListResults:(NSArray *)friendListConnections {
- BOOL contentToSave = NO;
- // delete old ones
- NSMutableArray *oldFriends = [self.dataOutputAdapter getDBRecordsOnTable:@"guroo_friends"];
- NSLog(@"DataInputAdapter: deleting %i old friends before saving %i ones", [oldFriends count], [friendListConnections count]);
- // while deleting, store all friend lists and their pic url for use later
- NSMutableDictionary *oldFriendListPhotoDetails = [[NSMutableDictionary alloc] init];
- for (NSManagedObject *oldManagedObj in oldFriends) {
- // save the pic_square against the uid - we will check later if the pic_square url has changed
- [oldFriendListPhotoDetails setValue:((guroo_friends *) oldManagedObj).pic_square forKey:((guroo_friends *) oldManagedObj).uid];
- [self.managedObjectContext deleteObject:oldManagedObj];
- }
- int addCount = 0;
- NSMutableArray *newOrUpdatedPhotoRefs = [[NSMutableArray alloc] init];
- for (int i = 0; i < [friendListConnections count]; i++) {
- addCount++;
- contentToSave = YES;
- guroo_friends *gf = (guroo_friends *) [NSEntityDescription
- insertNewObjectForEntityForName:@"guroo_friends"
- inManagedObjectContext:self.managedObjectContext];
- gf = [DictionaryToObject populateFriendObject:gf withResult:[friendListConnections objectAtIndex:i]];
- Ref *ref = [[Ref alloc] init];
- NSString *oldURL = [oldFriendListPhotoDetails valueForKey:gf.uid];
- NSString *newURL = gf.pic_square;
- ref.string1 = gf.uid;
- ref.string2 = @"pic_square";
- ref.string3 = gf.pic_square;
- if ((oldURL == nil && newURL != nil) || (oldURL != nil && newURL != nil && ![oldURL isEqualToString:newURL])) {
- // brand new photo
- [newOrUpdatedPhotoRefs addObject:ref];
- //NSLog(@"DataInputAdapter: will download pic_square for '%@' with uid=%@",gf.name,gf.uid);
- }
- }
- for (Ref *updatedPhoto in newOrUpdatedPhotoRefs) {
- NSManagedObject *oldPhoto = [self.dataOutputAdapter getDBRecordOnTable:@"guroo_image" whereField:@"pid" equals:updatedPhoto.string1 andOtherField:@"srcfield" equals:@"pic_big"];
- if (oldPhoto != nil)
- [self.managedObjectContext deleteObject:oldPhoto];
- }
- if (contentToSave) {
- if (addCount > 0) {
- [self.preferences setTotalForFeed:FRIEND amount:addCount];
- }
- NSLog(@"DataInputAdapter: saving %i friends", [friendListConnections count]);
- [self commitTransaction];
- }
- return newOrUpdatedPhotoRefs;
- }
- - (void)savePhotoBinary:(UIImage *)image forURL:(NSString *)url andPID:(NSString *)pid fromField:(NSString *)srcfield {
- if (image != nil && pid != nil) {
- NSData *imageData = UIImagePNGRepresentation(image);
- guroo_image *gi = (guroo_image *) [NSEntityDescription
- insertNewObjectForEntityForName:@"guroo_image"
- inManagedObjectContext:self.managedObjectContext];
- gi.url = url;
- gi.pid = pid;
- gi.binary = imageData;
- gi.srcfield = srcfield;
- //NSLog(@"DataInputAdapter: saving photo for id %@,%@,%@",srcfield,pid,url);
- [self commitTransaction];
- /*[NSThread detachNewThreadSelector:@selector(commitTransaction)
- toTarget:self
- withObject:nil];*/
- }
- }
- - (NSMutableArray *)saveAlbumCoverPhotos:(NSArray *)photos {
- NSMutableArray *modified = [[NSMutableArray alloc] init];
- BOOL contentToSave = NO;
- for (NSDictionary *photoObj in photos) {
- NSString *pid = [photoObj valueForKey:@"pid"];
- photo *photoDB = (photo *) [self.dataOutputAdapter getDBRecordOnTable:@"photo" whereField:@"pid" equals:pid];
- if (photoDB == nil) {
- // add new
- photoDB = (photo *) [NSEntityDescription
- insertNewObjectForEntityForName:@"photo"
- inManagedObjectContext:self.managedObjectContext];
- // add modified reference
- Ref *ref = [[Ref alloc] init];
- ref.string1 = pid;
- ref.string2 = @"src";
- ref.string3 = [photoObj valueForKey:@"src"];
- [modified addObject:ref];
- photoDB = [DictionaryToObject populatePhotoObject:photoDB withResult:photoObj];
- contentToSave = YES;
- } else {
- // one already - check modified
- NSDate *oldModified = photoDB.modified;
- if ([self dateInField:@"modified" inDict:photoObj isGreaterThan:oldModified]) {
- // add modified reference
- Ref *ref = [[Ref alloc] init];
- ref.string1 = pid;
- ref.string2 = @"src";
- ref.string3 = [photoObj valueForKey:@"src"];
- [modified addObject:ref];
- photoDB = [DictionaryToObject populatePhotoObject:photoDB withResult:photoObj];
- contentToSave = YES;
- }
- }
- }
- if (contentToSave == YES) {
- [self commitTransaction];
- }
- return modified;
- }
- - (NSArray *)savePhotos:(NSArray *)photos forAlbum:(NSString *)aid returnAllRefs:(BOOL)returnAllRefs {
- BOOL contentToSave = NO;
- // delete old ones
- NSMutableArray *oldPhotos = [self.dataOutputAdapter getDBRecordsOnTable:@"photo" whereField:@"aid" equals:aid];
- NSLog(@"DataInputAdapter: deleting %i old photos before saving new ones", [oldPhotos count]);
- // TODO DELETE BIG VERSIONS OF PICTURES IF CHANGED
- // while deleting, store all photos and their modified date for use later
- NSMutableDictionary *pidToModified = [[NSMutableDictionary alloc] init];
- for (NSManagedObject *oldManagedObj in oldPhotos) {
- [pidToModified setObject:((photo *) oldManagedObj).modified forKey:((photo *) oldManagedObj).pid];
- [self.managedObjectContext deleteObject:oldManagedObj];
- contentToSave = NO;
- }
- // tally up list of modified or new photos
- NSMutableArray *modifiedOrNewPhotos = [[NSMutableArray alloc] init];
- // save new batch
- NSDictionary *obj;
- photo *p;
- for (int i = 0; i < [photos count]; i++) {
- obj = [photos objectAtIndex:i];
- p = (photo *) [NSEntityDescription
- insertNewObjectForEntityForName:@"photo"
- inManagedObjectContext:self.managedObjectContext];
- p = [DictionaryToObject populatePhotoObject:p withResult:obj];
- NSDate *oldModified = ((NSDate *) [pidToModified objectForKey:p.pid]);
- Ref *ref = [[Ref alloc] init];
- ref.string1 = p.pid;
- ref.string2 = @"src";
- ref.string3 = p.src;
- ref.string4 = p.object_id;
- if (oldModified == nil || returnAllRefs) {
- // brand new photo
- [modifiedOrNewPhotos addObject:ref];
- } else if ([self date:p.modified isGreaterThan:oldModified]) {
- // updated photo
- [modifiedOrNewPhotos addObject:ref];
- }
- contentToSave = YES;
- }
- if (contentToSave == YES) {
- [self commitTransaction];
- }
- return modifiedOrNewPhotos;
- }
- - (NSArray *)savePhotos:(NSArray *)photos forAlbum:(NSString *)aid {
- return [self savePhotos:photos forAlbum:aid returnAllRefs:NO];
- }
- - (void)saveOverallSyncDate {
- SyncedDate *syncDate = ((SyncedDate *) [self.dataOutputAdapter getDBRecordOnTable:@"SyncedDate"
- whereField:@"key" equals:@"overall"]);
- if (syncDate == nil) {
- syncDate = (SyncedDate *) [NSEntityDescription
- insertNewObjectForEntityForName:@"SyncedDate"
- inManagedObjectContext:self.managedObjectContext];
- }
- syncDate.date = [[NSDate alloc] init];
- syncDate.key = @"overall";
- [self commitTransaction];
- }
- - (void)saveSyncDateForJob:(double)job {
- SyncedDate *syncDate = ((SyncedDate *) [self.dataOutputAdapter getDBRecordOnTable:@"SyncedDate"
- whereField:@"key" equals:[NSString stringWithFormat:@"%f", job]]);
- if (syncDate == nil) {
- syncDate = (SyncedDate *) [NSEntityDescription
- insertNewObjectForEntityForName:@"SyncedDate"
- inManagedObjectContext:self.managedObjectContext];
- }
- syncDate.date = [[NSDate alloc] init];
- syncDate.key = [NSString stringWithFormat:@"%f", job];
- [self commitTransaction];
- }
- - (void)saveSyncDateForFeedType:(FeedType)feedType {
- SyncedDate *syncDate = ((SyncedDate *) [self.dataOutputAdapter getDBRecordOnTable:@"SyncedDate"
- whereField:@"key" equals:[NSString stringWithFormat:@"%i", feedType]]);
- if (syncDate == nil) {
- syncDate = (SyncedDate *) [NSEntityDescription
- insertNewObjectForEntityForName:@"SyncedDate"
- inManagedObjectContext:self.managedObjectContext];
- }
- syncDate.date = [[NSDate alloc] init];
- syncDate.key = [NSString stringWithFormat:@"%i", feedType];
- [self commitTransaction];
- }
- - (void)saveStreamStartDate:(NSDate *)theDate {
- SyncedDate *syncDate = ((SyncedDate *) [self.dataOutputAdapter getDBRecordOnTable:@"SyncedDate"
- whereField:@"key" equals:@"streamstart"]);
- if (syncDate == nil) {
- syncDate = (SyncedDate *) [NSEntityDescription
- insertNewObjectForEntityForName:@"SyncedDate"
- inManagedObjectContext:self.managedObjectContext];
- }
- syncDate.date = theDate;
- syncDate.key = @"streamstart";
- [self commitTransaction];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement