-
Notifications
You must be signed in to change notification settings - Fork 0
/
BPDataMergeImport.m
195 lines (154 loc) · 7.24 KB
/
BPDataMergeImport.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
//
// BPDataMergeImport.m
// BPTracker
//
// Created by Robert Saccone on 10/9/12.
// Copyright (c) 2017 Robert Saccone. All rights reserved.
//
#import "BPDataMergeImport.h"
#import <SLexUtil/NSErrorHelper.h>
#import "BPDataImportError.h"
#import "BPReadingUtil.h"
#import "BPFetchRequestBuilderHelper.h"
@implementation BPDataMergeImport
#pragma mark - BPDataImportBase contract implementation
- (void)beginImpl
{
[self.dateToReading removeAllObjects];
[self.importReadingDates removeAllObjects];
}
- (BOOL)importImpl:(BloodPressureReading *)reading error:(NSError * __autoreleasing *)error
{
if (reading == nil)
{
NSException* myException = [NSException
exceptionWithName:NSInvalidArgumentException
reason:@"reading is nil!"
userInfo:nil];
@throw myException;
}
if (error == nil)
{
NSException* myException = [NSException
exceptionWithName:NSInvalidArgumentException
reason:@"error is nil!"
userInfo:nil];
@throw myException;
}
// Make sure that a reading with this date/time hasn't already been imported.
BloodPressureReading *dupe = [self.dateToReading objectForKey:reading.readingDate];
if (dupe != nil)
{
NSString *reason = [NSString stringWithFormat:NSLocalizedString(@"IMPORT_READING_DUPLICATE_DATES_FAILURE_REASON", nil),
[self.dateFormatter stringFromDate:reading.readingDate]];
*error = makeNSErrorFromResources(BPDataImportErrorDomain,
BPIDuplicateReadingDateInSource,
@"IMPORT_READINGS_FAILURE_DESCRIPTION",
reason,
@"IMPORT_READING_DUPLICATE_DATES_RECOVERY_SUGGESTION",
*error,
nil,
nil,
FailureReasonIsAString);
// Extend the method to return an NSError with information for the user
// describing that a reading with the same date/time already exists.
return NO;
}
NSDate *readingDate = reading.readingDate;
[self.dateToReading setObject:reading forKey:readingDate];
[self.importReadingDates addObject:readingDate];
return YES;
}
- (BOOL)commitImpl:(NSError * __autoreleasing *)error
{
if (error == nil)
{
NSException* myException = [NSException
exceptionWithName:NSInvalidArgumentException
reason:@"error is nil!"
userInfo:nil];
@throw myException;
}
NSError *internalError = nil;
// Sort the reading dates.
NSArray *sortedReadingDates = [self.importReadingDates sortedArrayUsingSelector:@selector(compare:)];
// Create the fetch request to get all readings with matching dates.
NSFetchRequest *fetchRequest = [BPFetchRequestBuilderHelper makeFetchRequest:self.managedObjectContext matchReadingDates:sortedReadingDates];
NSArray *readingsMatchingDates = [self.managedObjectContext executeFetchRequest:fetchRequest
error:&internalError];
if (!readingsMatchingDates)
{
*error = makeNSErrorFromResources(BPDataImportErrorDomain,
BPIDuplicateReadingDetectionFailed,
@"IMPORT_READINGS_FAILURE_DESCRIPTION",
@"IMPORT_READINGS_DUPLICATE_DATE_DETECTION_FAILURE_REASON",
@"IMPORT_READINGS_RESTART_APPLICATION_RECOVERY_SUGGESTION",
internalError,
nil,
nil,
NoStringOverrides);
return NO;
}
NSUInteger numReadingsFetched = [readingsMatchingDates count];
NSUInteger currFetchedReadingIndex = 0;
NSUInteger newReadingsImportedCount = 0;
NSUInteger readingsUpdatedCount = 0;
/* For debugging.
for (BloodPressureReading *bpFetched in readingsMatchingDates)
{
NSLog(@"reading index %u = %@, systolic = %@, diastolic = %@, pulse = %@", currFetchedReadingIndex,
bpFetched.readingDate,
bpFetched.systolic, bpFetched.diastolic, bpFetched.pulse);
++currFetchedReadingIndex;
}
*/
currFetchedReadingIndex = 0;
for (NSDate *currImportDate in sortedReadingDates)
{
BloodPressureReading *currImportReading = [self.dateToReading objectForKey:currImportDate];
NSAssert(currImportReading != nil, @"currImportReading == nil");
if (currFetchedReadingIndex < numReadingsFetched)
{
BloodPressureReading *currFetchedReading = [readingsMatchingDates objectAtIndex:currFetchedReadingIndex];
if ([currFetchedReading.readingDate compare:currImportDate] == NSOrderedSame)
{
copyReading(currImportReading, currFetchedReading);
++currFetchedReadingIndex;
++readingsUpdatedCount;
continue;
}
}
// Reaching here means that a new BloodPressureReading should be created
// because the current import date wasn't found in the fetched results set.
BloodPressureReading *newReading
=[NSEntityDescription insertNewObjectForEntityForName:@"BloodPressureReading"
inManagedObjectContext:self.managedObjectContext];
copyReading(currImportReading, newReading);
++newReadingsImportedCount;
}
if (![self.managedObjectContext save:&internalError])
{
*error = makeNSErrorFromResources(BPDataImportErrorDomain,
BPIDuplicateReadingDetectionFailed,
@"IMPORT_READINGS_FAILURE_DESCRIPTION",
@"IMPORT_READINGS_SAVE_READINGS_FAILURE_REASON",
@"IMPORT_READINGS_RESTART_APPLICATION_RECOVERY_SUGGESTION",
internalError,
nil,
nil,
NoStringOverrides);
return NO;
}
[self.importReadingDates removeAllObjects];
[self.dateToReading removeAllObjects];
self.newReadingsImportedCount = newReadingsImportedCount;
self.readingsUpdatedCount = readingsUpdatedCount;
return YES;
}
- (void)rollbackImpl
{
[self.importReadingDates removeAllObjects];
[self.dateToReading removeAllObjects];
[self.managedObjectContext rollback];
}
@end