Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (RW3StoreCallOperation *)uploadStoreCall:(NSArray *)items
- categories:(NSArray *)categories
- subcategories:(NSArray *)subcategories
- spentTimeRecords:(NSArray *)spentTimeRecords
- itemScans:(NSArray *)itemScans
- ofPriceCheck:(RW3PriceCheck *)priceCheck
- withLatitude:(CLLocationDegrees)latitude withLongitude:(CLLocationDegrees)longitude
- withSuccess:(void (^)(NSString* storeCallError))success failure:(void (^)(NSError *error))failure
- {
- NSParameterAssert(self.peopleID);
- NSParameterAssert(self.syncGUID);
- NSParameterAssert(items);
- NSParameterAssert(categories);
- NSParameterAssert(subcategories);
- NSParameterAssert(priceCheck);
- NSParameterAssert(latitude);
- NSParameterAssert(longitude);
- NSParameterAssert(priceCheck.store);
- self.uploadingInProgress = YES;
- NSMutableArray *itemsJSON = [NSMutableArray arrayWithCapacity:[items count]];
- for (RW3Item *item in items)
- {
- NSMutableDictionary *itemJSON = [[NSMutableDictionary alloc] initWithDictionary:[item JSON]];
- itemJSON[@"isDeleted"] = @(NO);
- [itemsJSON addObject:[itemJSON copy]];
- }
- NSMutableArray *categoryDateJSON = [NSMutableArray arrayWithCapacity:[categories count]];
- for (RW3Category *category in categories) {
- if (category.note.changed) {
- NSDictionary *categoryJSON = [category JSON];
- [categoryDateJSON addObject:categoryJSON];
- }
- }
- NSMutableArray *subcategoryDateJSON = [NSMutableArray arrayWithCapacity:[subcategories count]];
- for (RW3Subcategory *subcategory in subcategories) {
- if (subcategory.note.changed) {
- NSDictionary *subcategoryJSON = [subcategory JSON];
- [subcategoryDateJSON addObject:subcategoryJSON];
- }
- }
- NSMutableArray *spentTimeRecordsJSON = [NSMutableArray arrayWithCapacity:[items count]];
- for (RW3SpentTimeRecord *record in spentTimeRecords) {
- NSDictionary *recordJSON = [record JSON];
- [spentTimeRecordsJSON addObject:recordJSON];
- }
- NSMutableArray *itemScansJSON = [NSMutableArray arrayWithCapacity:[itemScans count]];
- for (RW3ItemScan *record in itemScans) {
- NSDictionary *recordJSON = [record JSON];
- [itemScansJSON addObject:recordJSON];
- }
- NSString *apnDeviceToken = [RW3Config getApnDeviceToken];
- NSDictionary *location = @{@"Latitude": @(latitude),
- @"Longitude": @(longitude)};
- #ifdef DEBUG
- BOOL apnIsProduction = NO;
- #else
- BOOL apnIsProduction = YES;
- #endif
- NSDictionary *parameters = @{@"PeopleId": self.peopleID,
- @"SyncGuid": self.syncGUID,
- @"PriceCheckId" : priceCheck.id,
- @"StoreId": priceCheck.store.id,
- @"IsPriceCheckExpired": @(priceCheck.isExpired),
- // @"ApnDeviceToken": (!apnDeviceToken ? [NSNull null] : apnDeviceToken),
- // @"ApnIsProduction": @(apnIsProduction),
- @"Items": itemsJSON,
- @"CategoryNotes": categoryDateJSON,
- @"SubcategoryNotes": subcategoryDateJSON,
- @"ItemScans": itemScansJSON,
- @"TimeCaptureRecords": spentTimeRecordsJSON,
- @"Location": location};
- parameters = [self requestParametersWithParameters:parameters];
- NSString *boundary = @"--StoreCallBoundary";
- NSMutableData *body = [[NSMutableData alloc] init];
- NSMutableArray *arr = [[NSMutableArray alloc]init];
- NSString *boundarySeparator = [NSString stringWithFormat:@"--%@\r\n", boundary];
- NSString *JSONContentType = @"Content-Type: application/json\r\n";
- NSString *JSONContentDisposition = @"Content-Disposition: form-data\r\n\r\n";
- NSError *serializationError = nil;
- NSData *JSONData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&serializationError];
- NSAssert(serializationError == nil, [serializationError localizedDescription]);
- [body appendData:[boundarySeparator dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[JSONContentType dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[JSONContentDisposition dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:JSONData];
- [arr addObject:[boundarySeparator dataUsingEncoding:NSUTF8StringEncoding]];
- [arr addObject:[JSONContentType dataUsingEncoding:NSUTF8StringEncoding]];
- [arr addObject:[JSONContentDisposition dataUsingEncoding:NSUTF8StringEncoding]];
- [arr addObject:JSONData];
- NSString *separator = @"\r\n";
- NSInteger numberOfItemPhotosToUpload = 0;
- float byteSizeItemPhotosToUpload = 0.0f;
- for (RW3Item *item in items)
- {
- @autoreleasepool
- {
- RW3LogNetwork(@"There are %lu photos to be uploaded for item UPC = '%@' InputUPC = '%@'.", (unsigned long)[item.photos count], item.upcPadded, item.upcUserInputted);
- int i = 0;
- for (RW3Photo *photo in item.photos)
- {
- NSString *name = [NSString stringWithFormat:@"%@_%d", item.upcPadded, i];
- name = [name stringByReplacingOccurrencesOfString:@" " withString:@""];
- [body appendData:[separator dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[boundarySeparator dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@.jpg\r\n", name, name] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[@"OwnerType: Item\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"OwnerId: %@\r\n", item.upcPadded] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"DateTaken: %@\r\n", [photo.timestamp toDateTimeString]] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:photo.image];
- [arr addObject:[separator dataUsingEncoding:NSUTF8StringEncoding]];
- [arr addObject:[boundarySeparator dataUsingEncoding:NSUTF8StringEncoding]];
- [arr addObject:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@.jpg\r\n", name, name] dataUsingEncoding:NSUTF8StringEncoding]];
- [arr addObject:[@"OwnerType: Item\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
- [arr addObject:[[NSString stringWithFormat:@"OwnerId: %@\r\n", item.upcPadded] dataUsingEncoding:NSUTF8StringEncoding]];
- [arr addObject:[[NSString stringWithFormat:@"DateTaken: %@\r\n", [photo.timestamp toDateTimeString]] dataUsingEncoding:NSUTF8StringEncoding]];
- [arr addObject:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
- [arr addObject:photo.image];
- numberOfItemPhotosToUpload++;
- byteSizeItemPhotosToUpload += (float)photo.image.length;
- i++;
- }
- }
- }
- RW3LogNetwork(@"There are %ld item photos to upload. The total size is %ld bytes, %0.2f KB, or %0.2f MB.", (long)numberOfItemPhotosToUpload,
- (long)byteSizeItemPhotosToUpload, byteSizeItemPhotosToUpload / 1024.0f, byteSizeItemPhotosToUpload / 1024.0f / 1024.0f);
- NSInteger numberOfCategoryPhotosToUpload = 0;
- float byteSizeCategoryPhotosToUpload = 0.0f;
- for (RW3Category *category in categories)
- {
- @autoreleasepool
- {
- RW3LogNetwork(@"There are %lu photos to be uploaded for note for category: %@ (%@)", (unsigned long)[category.note.photos count], category.id, category.name);
- int i = 0;
- for (RW3Photo *photo in category.note.photos)
- {
- NSString *name = [NSString stringWithFormat:@"category_%@_%d", category.id, i];
- [body appendData:[separator dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[boundarySeparator dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@.jpg\r\n", name, name] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[@"OwnerType: Category\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"OwnerId: %@\r\n", category.id] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"DateTaken: %@\r\n", [photo.timestamp toDateTimeString]] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:photo.image];
- numberOfCategoryPhotosToUpload++;
- byteSizeCategoryPhotosToUpload += (float)photo.image.length;
- i++;
- }
- }
- }
- RW3LogNetwork(@"There are %ld category photos to upload. The total size is %ld bytes, %0.2f KB, or %0.2f MB.", (long)numberOfCategoryPhotosToUpload,
- (long)byteSizeCategoryPhotosToUpload, byteSizeCategoryPhotosToUpload / 1024.0f, byteSizeCategoryPhotosToUpload / 1024.0f / 1024.0f);
- NSInteger numberOfSubategoryPhotosToUpload = 0;
- float byteSizeSubategoryPhotosToUpload = 0.0f;
- for (RW3Subcategory *subcategory in subcategories)
- {
- @autoreleasepool
- {
- RW3LogNetwork(@"There are %lu photos to be uploaded for note for subcategory: %@ (%@)", (unsigned long)[subcategory.note.photos count], subcategory.id, subcategory.name);
- for (RW3Photo *photo in subcategory.note.photos)
- {
- int i = 0;
- if (photo.changed)
- {
- NSString *name = [NSString stringWithFormat:@"subcategory_%@_%d", subcategory.id, i];
- [body appendData:[separator dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[boundarySeparator dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@.jpg\r\n", name, name] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[@"OwnerType: Subcategory\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"OwnerId: %@\r\n", subcategory.id] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"DateTaken: %@\r\n", [photo.timestamp toDateTimeString]] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:photo.image];
- numberOfSubategoryPhotosToUpload++;
- byteSizeSubategoryPhotosToUpload += (float)photo.image.length;
- i++;
- }
- }
- }
- }
- RW3LogNetwork(@"There are %ld subcategory photos to upload. The total size is %ld bytes, %0.2f KB, or %0.2f MB.", (long)numberOfCategoryPhotosToUpload,
- (long)byteSizeCategoryPhotosToUpload, byteSizeCategoryPhotosToUpload / 1024.0f, byteSizeCategoryPhotosToUpload / 1024.0f / 1024.0f);
- NSString *endBoundary = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary];
- [body appendData:[endBoundary dataUsingEncoding:NSUTF8StringEncoding]];
- NSURL *URL = [[NSURL URLWithString:@"StoreCall" relativeToURL:self.baseURL] absoluteURL];
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
- request.HTTPMethod = @"POST";
- request.HTTPBody = body;
- NSString *header = [NSString stringWithFormat:@"multipart/form-data; boundary=\"%@\"", boundary];
- [request setValue:header forHTTPHeaderField:@"Content-Type"];
- float requestBytes = request.HTTPBody.length;
- RW3LogNetwork(@"There are %lu items to upload. The upload body size for all data, item data and photos, is %lu bytes, %0.2f KB, or %0.2f MB.",
- (unsigned long)items.count, (unsigned long)request.HTTPBody.length, requestBytes / 1024.0f, requestBytes / 1024.0f / 1024.0f);
- RW3StoreCallOperation *undirectedStoreCall = [[RW3StoreCallOperation alloc] initWithRequest:request];
- [undirectedStoreCall setCompletionBlockWithSuccess:^(NSString *storeCallError, NSInteger uploadLogID) {
- [RW3Config setLastUploadLogID:uploadLogID];
- success(storeCallError);
- } failure:^(NSError *error) {
- [self cancelAllOperations];
- if (failure)
- {
- failure(error);
- }
- self.uploadingInProgress = NO;
- }];
- return undirectedStoreCall;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement