-
Notifications
You must be signed in to change notification settings - Fork 0
/
BPDataReplaceAllImport.m
214 lines (174 loc) · 7.46 KB
/
BPDataReplaceAllImport.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
//
// BPDataReplaceAllImport.m
// BPTracker
//
// Created by Robert Saccone on 10/9/12.
// Copyright (c) 2017 Robert Saccone. All rights reserved.
//
#import "BPDataReplaceAllImport.h"
#import <Foundation/NSBundle.h>
#import <SLexUtil/NSErrorHelper.h>
#import <SLexUtil/ViewHelper.h>
#import "BPDataImportError.h"
#import "BPFetchRequestBuilderHelper.h"
#import "BPReadingUtil.h"
@interface BPDataReplaceAllImport ()
- (void)deleteAllReadings;
@end
@implementation BPDataReplaceAllImport
{
@private
BOOL dbCleared_;
}
- (id)initWithManagedObjectContext:(NSManagedObjectContext *)moc
batchSizeHint:(NSUInteger)batchHint
{
self = [super initWithManagedObjectContext:moc batchSizeHint:batchHint];
if (self != nil)
{
dbCleared_ = NO;
}
return self;
}
- (void)deleteAllReadings
{
NSFetchRequest * allReadings = [[NSFetchRequest alloc] init];
[allReadings setEntity:[NSEntityDescription entityForName:@"Car" inManagedObjectContext:self.managedObjectContext]];
[allReadings setIncludesPropertyValues:NO]; //only fetch the managedObjectID
NSError * __autoreleasing error = nil;
NSArray *readings = [self.managedObjectContext executeFetchRequest:allReadings error:&error];
//error handling goes here
for (NSManagedObject * reading in readings)
{
[self.managedObjectContext deleteObject:reading];
}
}
- (void)beginImpl
{
if (!dbCleared_)
{
[self deleteAllReadings];
dbCleared_ = YES;
}
[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;
}
[self.dateToReading setObject:reading forKey:reading.readingDate];
[self.importReadingDates addObject:reading.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 *sortedDatesToMatch = [self.importReadingDates sortedArrayUsingSelector:@selector(compare:)];
NSFetchRequest *fetchRequest = [BPFetchRequestBuilderHelper makeFetchRequest:self.managedObjectContext matchReadingDates:sortedDatesToMatch];
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;
}
if ([readingsMatchingDates count] > 0)
{
// Duplicate dates in source.
*error = makeNSErrorFromResources(BPDataImportErrorDomain,
BPIDuplicateReadingDetectionFailed,
@"IMPORT_READINGS_FAILURE_DESCRIPTION",
@"IMPORT_READINGS_DUPLICATE_DATES_DETECTED_REASON",
@"IMPORT_READINGS_DUPLICATE_DATES_RECOVERY_SUGGESTION",
nil,
nil,
nil,
NoStringOverrides);
return NO;
}
NSEnumerator *objEnumerator = [self.dateToReading objectEnumerator];
BloodPressureReading *currReading;
NSUInteger numReadingsImported = 0;
while (currReading = [objEnumerator nextObject])
{
BloodPressureReading *destReading
=[NSEntityDescription insertNewObjectForEntityForName:@"BloodPressureReading"
inManagedObjectContext:self.managedObjectContext];
copyReading(currReading, destReading);
++numReadingsImported;
}
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.newReadingsImportedCount = numReadingsImported;
[self.dateToReading removeAllObjects];
[self.importReadingDates removeAllObjects];
return YES;
}
- (void)rollbackImpl
{
[self.managedObjectContext rollback];
[self.dateToReading removeAllObjects];
[self.importReadingDates removeAllObjects];
}
@end