Advertisement
Badal_hs_shah

Untitled

Feb 23rd, 2023
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (RW3StoreCallOperation *)uploadStoreCall:(NSArray *)items
  2.                                 categories:(NSArray *)categories
  3.                              subcategories:(NSArray *)subcategories
  4.                           spentTimeRecords:(NSArray *)spentTimeRecords
  5.                           itemScans:(NSArray *)itemScans
  6.                               ofPriceCheck:(RW3PriceCheck *)priceCheck
  7.                               withLatitude:(CLLocationDegrees)latitude withLongitude:(CLLocationDegrees)longitude
  8.                                withSuccess:(void (^)(NSString* storeCallError))success failure:(void (^)(NSError *error))failure
  9. {
  10.    NSParameterAssert(self.peopleID);
  11.    NSParameterAssert(self.syncGUID);
  12.    NSParameterAssert(items);
  13.    NSParameterAssert(categories);
  14.    NSParameterAssert(subcategories);
  15.    NSParameterAssert(priceCheck);
  16.    NSParameterAssert(latitude);
  17.    NSParameterAssert(longitude);
  18.    NSParameterAssert(priceCheck.store);
  19.    
  20.    self.uploadingInProgress = YES;
  21.    
  22.    NSMutableArray *itemsJSON = [NSMutableArray arrayWithCapacity:[items count]];
  23.    for (RW3Item *item in items)
  24.    {
  25.       NSMutableDictionary *itemJSON = [[NSMutableDictionary alloc] initWithDictionary:[item JSON]];
  26.       itemJSON[@"isDeleted"] = @(NO);
  27.       [itemsJSON addObject:[itemJSON copy]];
  28.    }
  29.  
  30.    NSMutableArray *categoryDateJSON = [NSMutableArray arrayWithCapacity:[categories count]];
  31.    for (RW3Category *category in categories) {
  32.       if (category.note.changed) {
  33.          NSDictionary *categoryJSON = [category JSON];
  34.          [categoryDateJSON addObject:categoryJSON];
  35.       }
  36.    }
  37.  
  38.    NSMutableArray *subcategoryDateJSON = [NSMutableArray arrayWithCapacity:[subcategories count]];
  39.    for (RW3Subcategory *subcategory in subcategories) {
  40.       if (subcategory.note.changed) {
  41.          NSDictionary *subcategoryJSON = [subcategory JSON];
  42.          [subcategoryDateJSON addObject:subcategoryJSON];
  43.       }
  44.    }
  45.    
  46.    NSMutableArray *spentTimeRecordsJSON = [NSMutableArray arrayWithCapacity:[items count]];
  47.    for (RW3SpentTimeRecord *record in spentTimeRecords) {
  48.       NSDictionary *recordJSON = [record JSON];
  49.       [spentTimeRecordsJSON addObject:recordJSON];
  50.    }
  51.    
  52.    NSMutableArray *itemScansJSON = [NSMutableArray arrayWithCapacity:[itemScans count]];
  53.    for (RW3ItemScan *record in itemScans) {
  54.       NSDictionary *recordJSON = [record JSON];
  55.       [itemScansJSON addObject:recordJSON];
  56.    }
  57.    
  58.    NSString *apnDeviceToken = [RW3Config getApnDeviceToken];
  59.    
  60.    NSDictionary *location = @{@"Latitude": @(latitude),
  61.                               @"Longitude": @(longitude)};
  62.  
  63. #ifdef DEBUG
  64.    BOOL apnIsProduction = NO;
  65. #else
  66.    BOOL apnIsProduction = YES;
  67. #endif
  68.    
  69.    NSDictionary *parameters = @{@"PeopleId": self.peopleID,
  70.                                 @"SyncGuid": self.syncGUID,
  71.                                 @"PriceCheckId" : priceCheck.id,
  72.                                 @"StoreId": priceCheck.store.id,
  73.                                 @"IsPriceCheckExpired": @(priceCheck.isExpired),
  74. //                                @"ApnDeviceToken": (!apnDeviceToken ? [NSNull null] : apnDeviceToken),
  75. //                                @"ApnIsProduction": @(apnIsProduction),
  76.                                 @"Items": itemsJSON,
  77.                                 @"CategoryNotes": categoryDateJSON,
  78.                                 @"SubcategoryNotes": subcategoryDateJSON,
  79.                                 @"ItemScans": itemScansJSON,
  80.                                 @"TimeCaptureRecords": spentTimeRecordsJSON,
  81.                                 @"Location": location};
  82.    
  83.    parameters = [self requestParametersWithParameters:parameters];
  84.    
  85.    NSString *boundary = @"--StoreCallBoundary";
  86.    
  87.    NSMutableData *body = [[NSMutableData alloc] init];
  88.    NSMutableArray *arr = [[NSMutableArray alloc]init];
  89.  
  90.    NSString *boundarySeparator = [NSString stringWithFormat:@"--%@\r\n", boundary];
  91.    NSString *JSONContentType = @"Content-Type: application/json\r\n";
  92.    NSString *JSONContentDisposition = @"Content-Disposition: form-data\r\n\r\n";
  93.    
  94.    NSError *serializationError = nil;
  95.    NSData *JSONData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&serializationError];
  96.    NSAssert(serializationError == nil, [serializationError localizedDescription]);
  97.    
  98.    [body appendData:[boundarySeparator dataUsingEncoding:NSUTF8StringEncoding]];
  99.    [body appendData:[JSONContentType dataUsingEncoding:NSUTF8StringEncoding]];
  100.    [body appendData:[JSONContentDisposition dataUsingEncoding:NSUTF8StringEncoding]];
  101.    [body appendData:JSONData];
  102.    
  103.    [arr addObject:[boundarySeparator dataUsingEncoding:NSUTF8StringEncoding]];
  104.    [arr addObject:[JSONContentType dataUsingEncoding:NSUTF8StringEncoding]];
  105.    [arr addObject:[JSONContentDisposition dataUsingEncoding:NSUTF8StringEncoding]];
  106.    [arr addObject:JSONData];
  107.    
  108.    NSString *separator = @"\r\n";
  109.  
  110.    NSInteger numberOfItemPhotosToUpload = 0;
  111.    float byteSizeItemPhotosToUpload = 0.0f;
  112.    
  113.    for (RW3Item *item in items)
  114.    {
  115.       @autoreleasepool
  116.       {
  117.          RW3LogNetwork(@"There are %lu photos to be uploaded for item UPC = '%@' InputUPC = '%@'.", (unsigned long)[item.photos count], item.upcPadded, item.upcUserInputted);
  118.          int i = 0;
  119.          for (RW3Photo *photo in item.photos)
  120.          {
  121.             NSString *name = [NSString stringWithFormat:@"%@_%d", item.upcPadded, i];
  122.             name = [name stringByReplacingOccurrencesOfString:@" " withString:@""];
  123.             [body appendData:[separator dataUsingEncoding:NSUTF8StringEncoding]];
  124.             [body appendData:[boundarySeparator dataUsingEncoding:NSUTF8StringEncoding]];
  125.             [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@.jpg\r\n", name, name] dataUsingEncoding:NSUTF8StringEncoding]];
  126.             [body appendData:[@"OwnerType: Item\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  127.             [body appendData:[[NSString stringWithFormat:@"OwnerId: %@\r\n", item.upcPadded] dataUsingEncoding:NSUTF8StringEncoding]];
  128.             [body appendData:[[NSString stringWithFormat:@"DateTaken: %@\r\n", [photo.timestamp toDateTimeString]] dataUsingEncoding:NSUTF8StringEncoding]];
  129.             [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  130.             [body appendData:photo.image];
  131.            
  132.             [arr addObject:[separator dataUsingEncoding:NSUTF8StringEncoding]];
  133.             [arr addObject:[boundarySeparator dataUsingEncoding:NSUTF8StringEncoding]];
  134.             [arr addObject:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@.jpg\r\n", name, name] dataUsingEncoding:NSUTF8StringEncoding]];
  135.             [arr addObject:[@"OwnerType: Item\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  136.             [arr addObject:[[NSString stringWithFormat:@"OwnerId: %@\r\n", item.upcPadded] dataUsingEncoding:NSUTF8StringEncoding]];
  137.             [arr addObject:[[NSString stringWithFormat:@"DateTaken: %@\r\n", [photo.timestamp toDateTimeString]] dataUsingEncoding:NSUTF8StringEncoding]];
  138.             [arr addObject:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  139.             [arr addObject:photo.image];
  140.  
  141.            
  142.             numberOfItemPhotosToUpload++;
  143.             byteSizeItemPhotosToUpload += (float)photo.image.length;
  144.             i++;
  145.          }
  146.       }
  147.    }
  148.    
  149.    RW3LogNetwork(@"There are %ld item photos to upload. The total size is %ld bytes, %0.2f KB, or %0.2f MB.", (long)numberOfItemPhotosToUpload,
  150.                  (long)byteSizeItemPhotosToUpload, byteSizeItemPhotosToUpload / 1024.0f, byteSizeItemPhotosToUpload / 1024.0f / 1024.0f);
  151.    
  152.    NSInteger numberOfCategoryPhotosToUpload = 0;
  153.    float byteSizeCategoryPhotosToUpload = 0.0f;
  154.    
  155.    for (RW3Category *category in categories)
  156.    {
  157.       @autoreleasepool
  158.       {
  159.          RW3LogNetwork(@"There are %lu photos to be uploaded for note for category: %@ (%@)", (unsigned long)[category.note.photos count], category.id, category.name);
  160.          int i = 0;
  161.          for (RW3Photo *photo in category.note.photos)
  162.          {
  163.             NSString *name = [NSString stringWithFormat:@"category_%@_%d", category.id, i];
  164.             [body appendData:[separator dataUsingEncoding:NSUTF8StringEncoding]];
  165.             [body appendData:[boundarySeparator dataUsingEncoding:NSUTF8StringEncoding]];
  166.             [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@.jpg\r\n", name, name] dataUsingEncoding:NSUTF8StringEncoding]];
  167.             [body appendData:[@"OwnerType: Category\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  168.             [body appendData:[[NSString stringWithFormat:@"OwnerId: %@\r\n", category.id] dataUsingEncoding:NSUTF8StringEncoding]];
  169.             [body appendData:[[NSString stringWithFormat:@"DateTaken: %@\r\n", [photo.timestamp toDateTimeString]] dataUsingEncoding:NSUTF8StringEncoding]];
  170.             [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  171.             [body appendData:photo.image];
  172.            
  173.             numberOfCategoryPhotosToUpload++;
  174.             byteSizeCategoryPhotosToUpload += (float)photo.image.length;
  175.            
  176.             i++;
  177.          }
  178.       }
  179.    }
  180.    
  181.    RW3LogNetwork(@"There are %ld category photos to upload. The total size is %ld bytes, %0.2f KB, or %0.2f MB.", (long)numberOfCategoryPhotosToUpload,
  182.                  (long)byteSizeCategoryPhotosToUpload, byteSizeCategoryPhotosToUpload / 1024.0f, byteSizeCategoryPhotosToUpload / 1024.0f / 1024.0f);
  183.    
  184.    NSInteger numberOfSubategoryPhotosToUpload = 0;
  185.    float byteSizeSubategoryPhotosToUpload = 0.0f;
  186.  
  187.    for (RW3Subcategory *subcategory in subcategories)
  188.    {
  189.       @autoreleasepool
  190.       {
  191.          RW3LogNetwork(@"There are %lu photos to be uploaded for note for subcategory: %@ (%@)", (unsigned long)[subcategory.note.photos count], subcategory.id, subcategory.name);
  192.          for (RW3Photo *photo in subcategory.note.photos)
  193.          {
  194.             int i = 0;
  195.             if (photo.changed)
  196.             {
  197.                NSString *name = [NSString stringWithFormat:@"subcategory_%@_%d", subcategory.id, i];
  198.                [body appendData:[separator dataUsingEncoding:NSUTF8StringEncoding]];
  199.                [body appendData:[boundarySeparator dataUsingEncoding:NSUTF8StringEncoding]];
  200.                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@.jpg\r\n", name, name] dataUsingEncoding:NSUTF8StringEncoding]];
  201.                [body appendData:[@"OwnerType: Subcategory\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  202.                [body appendData:[[NSString stringWithFormat:@"OwnerId: %@\r\n", subcategory.id] dataUsingEncoding:NSUTF8StringEncoding]];
  203.                [body appendData:[[NSString stringWithFormat:@"DateTaken: %@\r\n", [photo.timestamp toDateTimeString]] dataUsingEncoding:NSUTF8StringEncoding]];
  204.                [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  205.                [body appendData:photo.image];
  206.                
  207.                numberOfSubategoryPhotosToUpload++;
  208.                byteSizeSubategoryPhotosToUpload += (float)photo.image.length;
  209.                
  210.                i++;
  211.             }
  212.          }
  213.       }
  214.    }
  215.    
  216.    RW3LogNetwork(@"There are %ld subcategory photos to upload. The total size is %ld bytes, %0.2f KB, or %0.2f MB.", (long)numberOfCategoryPhotosToUpload,
  217.                  (long)byteSizeCategoryPhotosToUpload, byteSizeCategoryPhotosToUpload / 1024.0f, byteSizeCategoryPhotosToUpload / 1024.0f / 1024.0f);
  218.  
  219.    NSString *endBoundary = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary];
  220.    [body appendData:[endBoundary dataUsingEncoding:NSUTF8StringEncoding]];
  221.    
  222.    NSURL *URL = [[NSURL URLWithString:@"StoreCall" relativeToURL:self.baseURL] absoluteURL];
  223.    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
  224.    request.HTTPMethod = @"POST";
  225.    request.HTTPBody = body;
  226.    
  227.    NSString *header = [NSString stringWithFormat:@"multipart/form-data; boundary=\"%@\"", boundary];
  228.    [request setValue:header forHTTPHeaderField:@"Content-Type"];
  229.    
  230.    float requestBytes = request.HTTPBody.length;
  231.    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.",
  232.                  (unsigned long)items.count, (unsigned long)request.HTTPBody.length, requestBytes / 1024.0f, requestBytes / 1024.0f / 1024.0f);
  233.    
  234.    RW3StoreCallOperation *undirectedStoreCall = [[RW3StoreCallOperation alloc] initWithRequest:request];
  235.    [undirectedStoreCall setCompletionBlockWithSuccess:^(NSString *storeCallError, NSInteger uploadLogID) {
  236.       [RW3Config setLastUploadLogID:uploadLogID];
  237.       success(storeCallError);
  238.    } failure:^(NSError *error) {
  239.       [self cancelAllOperations];
  240.      
  241.       if (failure)
  242.       {
  243.          failure(error);
  244.       }
  245.       self.uploadingInProgress = NO;
  246.    }];
  247.    
  248.    return undirectedStoreCall;
  249. }
  250.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement