forked from AndrewSchenk/App-Scanner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ModelDataHelpers.m
122 lines (96 loc) · 3.99 KB
/
ModelDataHelpers.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
//
// ModelDataHelpers.m
// APIScanner
//
// Created by Andrew Schenk on 9/7/10.
// Copyright 2010 Chimp Studios. All rights reserved.
//
#import "ModelDataHelpers.h"
#import "Pweep.h"
#import "Constants.h"
@implementation ModelDataHelpers
+ (BOOL)stringIsInDatabase:(NSString*)string context:(NSManagedObjectContext*)context
{
BOOL wasFound = NO;
NSFetchRequest *req = [[NSFetchRequest alloc] init];
[req setEntity:[NSEntityDescription entityForName:@"Pweep" inManagedObjectContext:context]];
int percentage = 30;
if([[NSUserDefaults standardUserDefaults] boolForKey:kPrefFilterSet]) {
percentage = [[NSUserDefaults standardUserDefaults] integerForKey:kPrefFilter];
}
[req setPredicate:[NSPredicate predicateWithFormat:@"Signature == %@ AND Level >= %i", string, percentage]];
NSError *fetchError = nil;
NSArray *results = [context executeFetchRequest:req error:&fetchError];
[req release];
if (fetchError) {
NSLog(@"Error Fetching Method From Database... %@", [fetchError localizedDescription]);
} else {
if ([results count] > 0) {
wasFound = YES;
}
}
return wasFound;
}
+ (NSArray*)getPweepObjectsForSigs:(NSArray*)flagged context:(NSManagedObjectContext*)context
{
NSMutableArray *pweeps = [[[NSMutableArray alloc] initWithCapacity:[flagged count]] autorelease];
for(NSDictionary* dict in flagged)
{
NSFetchRequest *req = [[NSFetchRequest alloc] init];
[req setEntity:[NSEntityDescription entityForName:@"Pweep" inManagedObjectContext:context]];
[req setPredicate:[NSPredicate predicateWithFormat:@"Signature == %@", [dict objectForKey:@"Sig"]]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"Level" ascending:NO];
[req setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
NSError *fetchError = nil;
NSArray *results = [context executeFetchRequest:req error:&fetchError];
[req release];
if (fetchError) {
NSLog(@"Error Fetching Method From Database... %@", [fetchError localizedDescription]);
} else {
if ([results count] > 0) {
NSDictionary *newDict = [NSDictionary dictionaryWithObjectsAndKeys:[dict objectForKey:@"Class"],
@"Class", [dict objectForKey:@"Sig"], @"Sig", [results objectAtIndex:0], @"Pweep", nil];
[pweeps addObject:newDict];
}
}
}
return pweeps;
}
+ (NSDictionary*)getPweepObjectForSig:(NSDictionary*)flagged context:(NSManagedObjectContext*)context
{
NSFetchRequest *req = [[NSFetchRequest alloc] init];
[req setEntity:[NSEntityDescription entityForName:@"Pweep" inManagedObjectContext:context]];
[req setPredicate:[NSPredicate predicateWithFormat:@"Signature == %@", [flagged objectForKey:@"Sig"]]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"Level" ascending:NO];
[req setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
NSError *fetchError = nil;
NSArray *results = [context executeFetchRequest:req error:&fetchError];
[req release];
if (fetchError) {
NSLog(@"Error Fetching Method From Database... %@", [fetchError localizedDescription]);
} else {
if ([results count] > 0) {
return [NSDictionary dictionaryWithObjectsAndKeys:[flagged objectForKey:@"Class"],
@"Class", [flagged objectForKey:@"Sig"], @"Sig", [results objectAtIndex:0], @"Pweep", nil];
}
}
return nil;
}
+ (NSArray*)searchForPweepsLikeSig:(NSString*)searchSig context:(NSManagedObjectContext*)context
{
NSFetchRequest *req = [[NSFetchRequest alloc] init];
[req setEntity:[NSEntityDescription entityForName:@"Pweep" inManagedObjectContext:context]];
[req setPredicate:[NSPredicate predicateWithFormat:@"Signature CONTAINS[cd] %@", searchSig]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"Level" ascending:NO];
[req setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
NSError *fetchError = nil;
return [context executeFetchRequest:req error:&fetchError];
return [NSArray arrayWithObject:nil];
}
@end