-
Notifications
You must be signed in to change notification settings - Fork 5
/
CPEntityDescription.j
240 lines (193 loc) · 5.32 KB
/
CPEntityDescription.j
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
//
// CPEntityDescription.j
//
// Created by Raphael Bartolome on 15.10.09.
//
@import <Foundation/Foundation.j>
@import "CPPropertyDescription.j"
@import "CPAttributeDescription.j"
@import "CPRelationshipDescription.j"
@class CPManagedObjectModel;
@implementation CPEntityDescription : CPObject
{
CPManagedObjectModel _model @accessors(property=model);
CPString _name @accessors(property=name);
CPString _externalName @accessors(property=externalName);
BOOL _isAbstract;
CPMutableSet _properties @accessors(property=properties);
CPDictionary _userInfo @accessors(property=userInfo);
}
- (id)init
{
if(self = [super init])
{
_properties = [[CPMutableSet alloc] init];
_isAbstract = false;
}
return self;
}
/*+ (CPManagedObject)insertNewObjectForEntityForName:(CPString)aEntityName inManagedObjectContext:(CPManagedObjectContext) aContext
{
var result = [aContext insertNewObjectForEntityForName:aEntityName];
return result;
}
- (CPManagedObject)createObject
{
var newObject;
var objectClassWithName = CPClassFromString(_name);
var objectClassWithExternaName = CPClassFromString(_externalName);
if(objectClassWithExternaName != nil)
{
newObject = [[objectClassWithExternaName alloc] initWithEntity:self]
}
else if(objectClassWithName != nil)
{
newObject = [[objectClassWithName alloc] initWithEntity:self];
}
else
{
newObject = [[CPManagedObject alloc] initWithEntity:self];
}
return newObject;
}*/
- (BOOL)isAbstract
{
return _isAbstract;
}
- (void)setAbstract:(BOOL)isAbstract
{
if(isAbstract == null)
{
_isAbstract = false;
}
else
{
_isAbstract = isAbstract;
}
}
- (void)addRelationshipWithName:(CPString)name toMany:(BOOL)toMany optional:(BOOL)isOptional deleteRule:(int) aDeleteRule destination:(CPString)destinationEntityName
{
var tmp = [[CPRelationshipDescription alloc] init];
[tmp setName:name];
[tmp setEntity:self];
[tmp setIsToMany:toMany];
[tmp setOptional:isOptional];
[tmp setDeleteRule:aDeleteRule];
[tmp setDestinationEntityName:destinationEntityName];
[self addProperty:tmp];
}
- (void)addAttributeWithName:(CPString)name classValue:(CPString)aClassValue typeValue:(int)aAttributeType optional:(BOOL)isOptional
{
var tmp = [[CPAttributeDescription alloc] init];
[tmp setName:name];
[tmp setEntity:self];
[tmp setTypeValue:aAttributeType];
[tmp setClassValue:aClassValue];
[tmp setOptional:isOptional];
[self addProperty:[tmp copy]];
}
- (void)addProperty:(CPPropertyDescription)property
{
[_properties addObject:property];
}
- (CPDictionary)attributesByName
{
return [self _filteredPropertiesOfClass: [CPAttributeDescription class]];
}
- (CPDictionary)relationshipsByName
{
return [self _filteredPropertiesOfClass: [CPRelationshipDescription class]];
}
- (CPDictionary)propertiesByName
{
return [self _filteredPropertiesOfClass: Nil];
}
- (CPArray)propertyNames
{
return [[self _filteredPropertiesOfClass: Nil] allKeys];
}
- (CPArray)mandatoryAttributes
{
var result = [[CPMutableArray alloc] init];
var allAttributes = [self attributesByName];
var allKeys = [allAttributes allKeys];
var i = 0;
for(i=0;i<[allKeys count];i++)
{
var aKey = [allKeys objectAtIndex:i];
var attribute = [allAttributes objectForKey:aKey];
if(attribute != nil && ![attribute isOptional])
{
[result addObject:aKey];
}
}
return result
}
- (CPArray)mandatoryRelationships
{
var result = [[CPMutableArray alloc] init];
var allRC = [self relationshipsByName];
var allKeys = [allRC allKeys];
var i = 0;
for(i=0;i<[allKeys count];i++)
{
var aKey = [allKeys objectAtIndex:i];
var property = [allRC objectForKey:aKey];
if(property != nil && ![property isOptional])
{
[result addObject:aKey];
}
}
return result
}
- (CPDictionary)_filteredPropertiesOfClass: (Class) aClass
{
var dict;
var e;
var property;
dict = [[CPMutableDictionary alloc] init];
e = [_properties objectEnumerator];
while ((property = [e nextObject]) != nil)
{
if (aClass == Nil || [property isKindOfClass: aClass])
{
[dict setObject: property forKey: [property name]];
}
}
return dict;
}
- (BOOL)acceptValue:(id) aValue forProperty:(CPString) aKey
{
var result = NO;
var theProperty = [[self propertiesByName] objectForKey:aKey]
result = [theProperty acceptValue:aValue];
return result;
}
- (BOOL)isEqual:(CPEntityDescription)aEntity
{
if([[aEntity name] isEqualToString:_name])
{
return YES;
}
return NO;
}
- (CPString)stringRepresentation
{
var result = "\n";
result = result + "\n";
result = result + "-CPEntityDescription-";
result = result + "\n***********";
result = result + "\n";
result = result + "name:" + [self name] + ";";
result = result + "\n";
result = result + "externalName:" + [self externalName] + ";";
var propertiesE = [_properties objectEnumerator];
var aProperty;
while((aProperty = [propertiesE nextObject]))
{
result = result + "\n";
result = result + [aProperty stringRepresentation];
}
return result;
}
@end