-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJxCoreDataPredicateBuilder.m
More file actions
412 lines (311 loc) · 16.5 KB
/
JxCoreDataPredicateBuilder.m
File metadata and controls
412 lines (311 loc) · 16.5 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//
// JxCoreDataPredicateBuilder.m
// autokauf
//
// Created by Jeanette Müller on 13.11.13.
// Copyright (c) 2013 Motorpresse. All rights reserved.
//
#import "JxCoreDataPredicateBuilder.h"
#import "Logging.h"
@interface JxCoreDataPredicateBuilder ()
@property (strong, nonatomic) NSString *namespace;
@property (strong, nonatomic) NSDictionary *config;
@property (strong, nonatomic) NSMutableArray *dataFilter;
@property (strong, nonatomic) NSMutableDictionary *dataFiltervalues;
@end
@implementation JxCoreDataPredicateBuilder
#pragma mark init
+ (JxCoreDataPredicateBuilder *)sharedManager __deprecated_msg("Use Singleton with Namespace"){
return [self sharedManagerInNamespace:@""];
}
+ (JxCoreDataPredicateBuilder *)sharedManagerInNamespace:(NSString *)newNamespace{
static JxCoreDataPredicateBuilder *sharedInstance = nil;
static dispatch_once_t pred;
if (sharedInstance){
[sharedInstance loadNamespace:newNamespace];
return sharedInstance;
}
dispatch_once(&pred, ^{
sharedInstance = [[JxCoreDataPredicateBuilder alloc] init];
});
[sharedInstance loadNamespace:newNamespace];
return sharedInstance;
}
- (id)init{
self = [super init];
if (self) {
[self loadPropertyConfig];
}
return self;
}
- (void)loadNamespace:(NSString *)namespace{
DLog(@"namespace: %@", namespace);
if (![_namespace isEqualToString:namespace]) {
_namespace = namespace;
_dataFilter = [NSMutableArray array];
_dataFiltervalues = [NSMutableDictionary dictionary];
[self loadFilter];
}
}
- (void)loadPropertyConfig{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"CoreDataPredicateConfig.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:@"CoreDataPredicateConfig" ofType:@"plist"];
}
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
_config = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!_config)
{
DLog(@"Error reading plist: %@, format: %lu", errorDesc, format);
}else{
//DLog(@"Config Load: %@", _config);
}
}
#pragma mark Config
- (JxCoreDataPredicateConfig *)getConfigForKey:(NSString *)propKey{
//DLog(@"propKey %@", propKey);
if (!_config) {
[self loadPropertyConfig];
}
NSDictionary *dict = [_config objectForKey:propKey];
JxCoreDataPredicateConfig *config = [[JxCoreDataPredicateConfig alloc] init];
config.filterKey = propKey;
[config setValuesForKeysWithDictionary:dict];
return config;
}
#pragma mark load/save Filters
- (void)loadFilter{
DLog(@"namespace: %@", _namespace);
if ([[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"filter%@", _namespace]] != nil) {
_dataFilter = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"filter%@", _namespace]]];
}
if ([[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"filtervalues%@", _namespace]] != nil) {
NSData *dataFilterValuesData = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"filtervalues%@", _namespace]];
if (dataFilterValuesData != nil && [dataFilterValuesData isKindOfClass:[NSData class]]) {
_dataFiltervalues = [NSMutableDictionary dictionaryWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithData:dataFilterValuesData]];
}
}
if (!_dataFilter || [_dataFilter isKindOfClass:[NSNull class]]) {
_dataFilter = [NSMutableArray array];
}
if (!_dataFiltervalues || [_dataFiltervalues isKindOfClass:[NSNull class]]) {
_dataFiltervalues = [NSMutableDictionary dictionary];
}
// NSLog(@"load filter %@", _dataFilter);
// NSLog(@"load values %@", _dataFiltervalues);
}
- (void)saveFilter{
LLog();
// NSLog(@"save filter %@", _dataFilter);
// NSLog(@"save values %@", _dataFiltervalues);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:_dataFilter forKey:[NSString stringWithFormat:@"filter%@", _namespace]];
[defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:_dataFiltervalues] forKey:[NSString stringWithFormat:@"filtervalues%@", _namespace]];
[defaults synchronize];
NSLog(@"saved filter %@", [defaults objectForKey:[NSString stringWithFormat:@"filter%@", _namespace]]);
NSLog(@"saved values %@", [NSKeyedUnarchiver unarchiveObjectWithData:[defaults objectForKey:[NSString stringWithFormat:@"filtervalues%@", _namespace]]]);
[[NSNotificationCenter defaultCenter] postNotificationName:kJxCoreDataPredicateDidChange object:nil];
}
#pragma mark Create Filter
- (void)resetFilterAllNamespaces{
LLog();
_dataFilter = [NSMutableArray array];
_dataFiltervalues = [NSMutableDictionary dictionary];
[self saveFilter];
}
- (void)resetFilter{
[self resetFilterSilent:NO];
}
- (void)resetFilterSilent:(BOOL)silent{
DLog(@"silent %d", silent);
[_dataFilter removeAllObjects];
[_dataFiltervalues removeAllObjects];
if (!silent) {
[self saveFilter];
}
}
- (NSMutableArray *)filter{
return _dataFilter;
}
- (NSMutableDictionary *)filtervalues{
return _dataFiltervalues;
}
- (void)addFilterType:(NSString *)filterType{
[_dataFilter addObject:filterType];
[self saveFilter];
}
- (void)addFilter:(id)newFilterValues forType:(NSString *)filterType{
DLog(@"typ %@\nvalues %@", filterType, newFilterValues);
if (![_dataFilter containsObject:filterType]) {
[_dataFilter addObject:filterType];
}
[_dataFiltervalues setObject:newFilterValues forKey:filterType];
[self saveFilter];
}
- (void)removeFilterByType:(NSString *)filterType{
[self removeFilterByType:filterType inSilence:NO];
}
- (void)removeFilterByType:(NSString *)filterType inSilence:(BOOL)silent{
DLog(@"remove %@", filterType);
if (_dataFilter) {
[_dataFilter removeObject:filterType];
}
if (_dataFiltervalues) {
[_dataFiltervalues removeObjectForKey:filterType];
}
if (!silent) {
[self saveFilter];
}
}
- (JxCoreDataPredicateFilter *)getFilterValueFromFilter:(NSString *)filterName{
DLog(@"_dataFiltervalues: %@", _dataFiltervalues);
return [_dataFiltervalues objectForKey:filterName];
}
#pragma mark Get Filter
- (NSPredicate *)getPredicate{
return [self getPredicateUseSubquerys:YES];
}
- (NSPredicate *)getPredicateUseSubquerys:(BOOL)useSubQuerys{
LLog();
NSMutableArray *predicates = [NSMutableArray array];
if (_dataFiltervalues && [_dataFiltervalues isKindOfClass:[NSMutableDictionary class]]) {
for (NSString *filter in [_dataFiltervalues allKeys]) {
NSLog(@"Filter: %@", filter);
id filterv = [_dataFiltervalues objectForKey:filter];
//NSLog(@"filterv %@", filterv);
if ([[filterv class] isSubclassOfClass:[JxCoreDataPredicateFilter class]]) {
if ([filterv isKindOfClass:[JxCoreDataPredicateFilterContains class]]) {
JxCoreDataPredicateFilterContains *filterObject = (JxCoreDataPredicateFilterContains *)filterv;
NSString *compare = @"LIKE";
if (filterObject != nil && [filterObject isKindOfClass:[JxCoreDataPredicateFilterContains class]]) {
if ([[filterObject exclude] count] > 0) {
NSMutableArray *excludePredicates = [NSMutableArray array];
for (NSString *v in filterObject.exclude) {
if ([v isKindOfClass:[NSNumber class]]) {
compare = @"=";
}
NSString *format = [NSString stringWithFormat:@"NOT ( %@ %@ %%@ ) ", filter, compare];
[excludePredicates addObject:[NSPredicate predicateWithFormat:format, v]];
}
[predicates addObject:[NSCompoundPredicate andPredicateWithSubpredicates:excludePredicates]];
}else if ([[filterObject contains] count] > 0) {
NSMutableArray *containsPredicates = [NSMutableArray array];
for (id v in filterObject.contains) {
if ([v isKindOfClass:[NSNumber class]]) {
compare = @"=";
}
NSString *format = [NSString stringWithFormat:@"%@ %@ %%@", filter, compare];
[containsPredicates addObject:[NSPredicate predicateWithFormat:format, v]];
}
[predicates addObject:[NSCompoundPredicate orPredicateWithSubpredicates:containsPredicates]];
}
}
}else if ([filterv isKindOfClass:[JxCoreDataPredicateFilterRange class]]) {
JxCoreDataPredicateFilterRange *filterObject = (JxCoreDataPredicateFilterRange *)filterv;
float from = [filterObject.from floatValue];
float to = [filterObject.to floatValue];
NSArray *filterParts = [filter componentsSeparatedByString:@"."];
if ([filterParts count] > 1 && useSubQuerys) {
NSString *filterSubKeyPath = @"";
NSString *filterMainKey = @"";
int count = 0;
for (NSString *part in filterParts) {
if (count == 0) {
filterMainKey = part;
}else{
filterSubKeyPath = [filterSubKeyPath stringByAppendingFormat:@".%@", part];
}
count++;
}
NSString *filterFormat = [NSString stringWithFormat:@" SUBQUERY(%@, $p, $p%@ >= %%f AND $p%@ <= %%f).@count > 0 ", filterMainKey, filterSubKeyPath, filterSubKeyPath];
[predicates addObject:[NSPredicate predicateWithFormat:filterFormat, from, to]];
}else{
NSString *filterFormat = [NSString stringWithFormat:@" %@ BETWEEN {%%f, %%f} ", filter];
[predicates addObject:[NSPredicate predicateWithFormat:filterFormat, from, to]];
}
}else if ([filterv isKindOfClass:[JxCoreDataPredicateFilterSmaller class]]){
JxCoreDataPredicateFilterSmaller *filterObject = (JxCoreDataPredicateFilterSmaller *)filterv;
float smaller = [filterObject.smaller floatValue];
NSArray *filterParts = [filter componentsSeparatedByString:@"."];
if ([filterParts count] > 1) {
NSString *filterSubKeyPath = @"";
NSString *filterMainKey = @"";
int count = 0;
for (NSString *part in filterParts) {
if (count == 0) {
filterMainKey = part;
}else{
filterSubKeyPath = [filterSubKeyPath stringByAppendingFormat:@".%@", part];
}
count++;
}
NSString *filterFormat = [NSString stringWithFormat:@" SUBQUERY(%@, $p, $p%@ <= %%f).@count > 0 ", filterMainKey, filterSubKeyPath];
[predicates addObject:[NSPredicate predicateWithFormat:filterFormat, smaller]];
}else{
NSString *filterFormat = [NSString stringWithFormat:@" %@ <= %%f ", filter];
[predicates addObject:[NSPredicate predicateWithFormat:filterFormat, smaller]];
}
}else if ([filterv isKindOfClass:[JxCoreDataPredicateFilterLarger class]]){
JxCoreDataPredicateFilterLarger *filterObject = (JxCoreDataPredicateFilterLarger *)filterv;
float larger = [filterObject.larger floatValue];
NSArray *filterParts = [filter componentsSeparatedByString:@"."];
if ([filterParts count] > 1) {
NSString *filterSubKeyPath = @"";
NSString *filterMainKey = @"";
int count = 0;
for (NSString *part in filterParts) {
if (count == 0) {
filterMainKey = part;
}else{
filterSubKeyPath = [filterSubKeyPath stringByAppendingFormat:@".%@", part];
}
count++;
}
NSString *filterFormat = [NSString stringWithFormat:@" SUBQUERY(%@, $p, $p%@ >= %%f).@count > 0 ", filterMainKey, filterSubKeyPath];
[predicates addObject:[NSPredicate predicateWithFormat:filterFormat, larger]];
}else{
NSString *filterFormat = [NSString stringWithFormat:@" %@ >= %%f ", filter];
[predicates addObject:[NSPredicate predicateWithFormat:filterFormat, larger]];
}
}else if([filterv isKindOfClass:[JxCoreDataPredicateFilterBool class]]){
JxCoreDataPredicateFilterBool *filterObject = (JxCoreDataPredicateFilterBool *)filterv;
BOOL yesOrNo = [filterObject.yesOrNo boolValue];
NSArray *filterParts = [filter componentsSeparatedByString:@"."];
if ([filterParts count] > 1) {
NSString *filterSubKeyPath = @"";
NSString *filterMainKey = @"";
int count = 0;
for (NSString *part in filterParts) {
if (count == 0) {
filterMainKey = part;
}else{
filterSubKeyPath = [filterSubKeyPath stringByAppendingFormat:@".%@", part];
}
count++;
}
NSString *filterFormat = [NSString stringWithFormat:@" SUBQUERY(%@, $p, $p%@ = %%@).@count > 0 ", filterMainKey, filterSubKeyPath];
[predicates addObject:[NSPredicate predicateWithFormat:filterFormat, yesOrNo]];
}else{
NSString *filterFormat = [NSString stringWithFormat:@" %@ = %%d ", filter];
[predicates addObject:[NSPredicate predicateWithFormat:filterFormat, yesOrNo]];
}
}
}
}
}
if ([predicates count] > 0) {
NSPredicate *resultPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
NSLog(@"\n\n\npredicateString: %@\n\n\n", resultPredicate.predicateFormat);
return resultPredicate;
}
NSLog(@"return nil");
return nil;
}
@end