-
Notifications
You must be signed in to change notification settings - Fork 5
/
CPManagedObjectModel.j
193 lines (154 loc) · 4.05 KB
/
CPManagedObjectModel.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
//
// CPManagedObjectModel.j
//
// Created by Raphael Bartolome on 15.10.09.
//
@import <Foundation/CPObject.j>
@import "CPPropertyDescription.j"
@import "CPAttributeDescription.j"
@import "CPRelationshipDescription.j"
@import "CPEntityDescription.j"
var EOMODEL_SUFFIX = "eomodeld";
var COREDATAMODEL_SUFFIX = "xcdatamodel";
var CPDCOREDATAMODEL_SUFFIX = "cpxcdatamodel";
@implementation CPManagedObjectModel : CPObject
{
CPString _name @accessors(property=name);
CPMutableSet _entities @accessors(property=entities);
JSObject _entitiesCache;
}
- (id)init
{
if(self = [super init])
{
_entities = [[CPMutableSet alloc] init];
}
return self;
}
+ (id)modelWithModelNamed:(CPString) aModelName bundle:(CPBundle)aBundle
{
var objectModel = [[CPManagedObjectModel alloc] init];
var modelURL = [aBundle || [CPBundle mainBundle] pathForResource:aModelName];
if(modelURL != nil)
{
//check what kind of model we have
if([aModelName hasSuffix:EOMODEL_SUFFIX])
{
[objectModel parseEOModel:modelURL];
}
else if([aModelName hasSuffix:COREDATAMODEL_SUFFIX]
|| [aModelName hasSuffix:CPDCOREDATAMODEL_SUFFIX])
{
objectModel = [CPManagedObjectModel parseCoreDataModel:modelURL];
}
}
CPLog.info("Data Model '" + [objectModel name] + "' loaded");
return objectModel;
}
+ (id)mergedModelFromBundles: (CPArray) bundles
{
//Unimplemented
return nil
}
+ (id)modelByMergingModels: (CPArray) models
{
//Unimplemented
return nil;
}
- (void)setNameFromFilePath:(CPString)aFilePath
{
var pathComponents = [aFilePath componentsSeparatedByString:@"/"];
[pathComponents removeObjectAtIndex:0];
var pathComponentsEnum = [pathComponents objectEnumerator];
var aPathComponent;
while((aPathComponent = [pathComponentsEnum nextObject]) != null)
{
if([CPManagedObjectModel hasSupportedModelSuffix:aPathComponent])
{
var componentSuffix = [aPathComponent componentsSeparatedByString:@"."];
if([componentSuffix count] > 1)
{
[self setName:[componentSuffix objectAtIndex:0]];
break;
}
}
}
}
+ (BOOL)hasSupportedModelSuffix:(CPString) aModelFile
{
var result = NO;
if([aModelFile hasSuffix:EOMODEL_SUFFIX])
{
result = YES;
}
else if([aModelFile hasSuffix:COREDATAMODEL_SUFFIX])
{
result = YES;
}
else if([aModelFile hasSuffix:CPDCOREDATAMODEL_SUFFIX])
{
result = YES;
}
return result;
}
- (BOOL)isModelAllowedForMergeByName:(CPString) aModelName
{
var result = NO;
if([[self name] isEqualToString:aModelName])
result = YES;
return result;
}
- (void)addEntity:(CPEntityDescription) entity
{
[entity setModel:self];
[_entities addObject:entity];
_entitiesCache = nil;
}
- (CPEntityDescription)entityWithName:(CPString)name
{
if (_entitiesCache)
return _entitiesCache[name];
var result;
var entity;
var e = [_entities objectEnumerator];
_entitiesCache = {};
while ((entity = [e nextObject]) != nil)
{
var entityName = [entity name];
_entitiesCache[entityName] = entity;
if([entityName isEqualToString:name])
{
result = entity;
}
}
return result;
}
- (CPArray)entitiesByName
{
var result = [[CPMutableArray alloc] init];
var entitiesE = [_entities objectEnumerator];
var aEntity;
while((aEntity = [entitiesE nextObject]))
{
[result addObject:[aEntity name]];
}
return result;
}
- (CPFetchRequest)fetchRequestFromTemplateWithName:(CPString) aTemplateName
{
//Unimplemented
return nil;
}
- (CPString)stringRepresentation
{
var result = "CPObjectModel";
var entitiesE = [_entities objectEnumerator];
var aEntity;
while((aEntity = [entitiesE nextObject]))
{
result = result + "\n";
result = result + [aEntity stringRepresentation];
}
return result;
}
@end