1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package net.wotonomy.access; |
19 |
|
|
20 |
|
import java.io.File; |
21 |
|
import java.io.FileInputStream; |
22 |
|
import java.io.FileOutputStream; |
23 |
|
import java.io.IOException; |
24 |
|
import java.io.PrintStream; |
25 |
|
import java.util.Enumeration; |
26 |
|
|
27 |
|
import net.wotonomy.foundation.NSArray; |
28 |
|
import net.wotonomy.foundation.NSDictionary; |
29 |
|
import net.wotonomy.foundation.NSMutableArray; |
30 |
|
import net.wotonomy.foundation.NSMutableDictionary; |
31 |
|
import net.wotonomy.foundation.NSPropertyListSerialization; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
public class EOModel { |
42 |
|
|
43 |
|
protected static final String IDX_NAME = "index.eomodeld"; |
44 |
|
|
45 |
|
|
46 |
0 |
protected NSMutableArray _entities = new NSMutableArray(); |
47 |
0 |
protected NSMutableDictionary _entitiesByName = new NSMutableDictionary(); |
48 |
0 |
protected NSMutableDictionary _entitiesByClass = new NSMutableDictionary(); |
49 |
0 |
protected NSDictionary _connectionDictionary = NSDictionary.EmptyDictionary; |
50 |
0 |
protected String _adaptorName = "JDBC"; |
51 |
0 |
protected NSMutableDictionary _prototypesByName = new NSMutableDictionary(); |
52 |
0 |
protected NSMutableDictionary _storedProcedures = new NSMutableDictionary(); |
53 |
0 |
protected NSMutableArray _storedProcedureNames = new NSMutableArray(); |
54 |
0 |
protected NSDictionary _userInfo = NSDictionary.EmptyDictionary; |
55 |
|
protected String _name; |
56 |
|
protected String _path; |
57 |
0 |
protected NSDictionary _internalInfo = NSDictionary.EmptyDictionary; |
58 |
|
protected EOModelGroup _group; |
59 |
|
|
60 |
|
public EOModel() { |
61 |
0 |
super(); |
62 |
0 |
} |
63 |
|
|
64 |
|
public EOModel(NSDictionary dict, Object o) { |
65 |
0 |
this(); |
66 |
0 |
} |
67 |
|
|
68 |
|
public EOModel(String path) { |
69 |
0 |
this(); |
70 |
0 |
File f = new File(path); |
71 |
0 |
_path = f.getAbsolutePath(); |
72 |
0 |
if (!(f.exists() && f.isDirectory())) |
73 |
0 |
throw new IllegalArgumentException("The path is invalid (" + f + ")"); |
74 |
0 |
_name = f.getName(); |
75 |
0 |
f = new File(f, "index.eomodeld"); |
76 |
0 |
if (!(f.exists() && f.isFile())) |
77 |
0 |
throw new IllegalArgumentException("Cannot find " + f); |
78 |
0 |
String x = null; |
79 |
|
try { |
80 |
0 |
FileInputStream in = new FileInputStream(f); |
81 |
0 |
byte[] b = new byte[in.available()]; |
82 |
0 |
in.read(b); |
83 |
0 |
in.close(); |
84 |
0 |
x = new String(b); |
85 |
0 |
} catch (IOException e) { |
86 |
0 |
throw new IllegalArgumentException("Cannot read index.eomodeld"); |
87 |
0 |
} |
88 |
0 |
NSDictionary d = NSPropertyListSerialization.dictionaryForString(x); |
89 |
0 |
String version = (String)d.objectForKey("EOModelVersion"); |
90 |
0 |
if (version == null || !version.startsWith("2.")) |
91 |
0 |
throw new IllegalArgumentException("Invalid eomodel version: " + version); |
92 |
0 |
setAdaptorName((String)d.objectForKey("adaptorName")); |
93 |
0 |
setConnectionDictionary((NSDictionary)d.objectForKey("connectionDictionary")); |
94 |
0 |
_entities = ((NSArray)d.objectForKey("entities")).mutableClone(); |
95 |
0 |
if (d.objectForKey("storedProcedures") != null) |
96 |
0 |
_storedProcedureNames.addObjectsFromArray((NSArray)d.objectForKey("storedProcedures")); |
97 |
0 |
if (d.objectForKey("internalInfo") != null) |
98 |
0 |
_internalInfo = (NSDictionary)d.objectForKey("internalInfo"); |
99 |
0 |
if (d.objectForKey("userInfo") != null) |
100 |
0 |
_userInfo = (NSDictionary)d.objectForKey("userInfo"); |
101 |
0 |
entityNamed("EOPrototypes"); |
102 |
0 |
} |
103 |
|
|
104 |
|
public void setConnectionDictionary(NSDictionary dict) { |
105 |
0 |
_connectionDictionary = dict; |
106 |
0 |
} |
107 |
|
public NSDictionary connectionDictionary() { |
108 |
0 |
return _connectionDictionary; |
109 |
|
} |
110 |
|
|
111 |
|
public void addEntity(EOEntity ent) { |
112 |
0 |
_entitiesByName.setObjectForKey(ent, ent.name()); |
113 |
0 |
_entitiesByClass.setObjectForKey(ent, ent.className()); |
114 |
0 |
} |
115 |
|
|
116 |
|
public void removeEntity(EOEntity ent) { |
117 |
0 |
_entitiesByName.removeObjectForKey(ent.name()); |
118 |
0 |
_entitiesByClass.removeObjectForKey(ent.className()); |
119 |
0 |
} |
120 |
|
|
121 |
|
public void addStoredProcedure(EOStoredProcedure proc) { |
122 |
0 |
if (!_storedProcedureNames.containsObject(proc)) |
123 |
0 |
_storedProcedureNames.addObject(proc); |
124 |
0 |
_storedProcedures.setObjectForKey(proc, proc.name()); |
125 |
0 |
} |
126 |
|
|
127 |
|
public void removeStoredProcedure(EOStoredProcedure proc) { |
128 |
0 |
_storedProcedureNames.removeObject(proc.name()); |
129 |
0 |
_storedProcedures.removeObjectForKey(proc.name()); |
130 |
0 |
} |
131 |
|
|
132 |
|
public EOStoredProcedure storedProcedureNamed(String name) { |
133 |
0 |
EOStoredProcedure proc = (EOStoredProcedure)_storedProcedures.objectForKey(name); |
134 |
|
|
135 |
0 |
if (proc == null && _path != null && _storedProcedureNames.containsObject(name)) { |
136 |
|
|
137 |
0 |
File f = new File(new File(_path), name + ".storedProcedure"); |
138 |
0 |
if (!f.exists()) |
139 |
0 |
return null; |
140 |
0 |
String sdict = null; |
141 |
|
try { |
142 |
0 |
FileInputStream fin = new FileInputStream(f); |
143 |
0 |
byte[] b = new byte[fin.available()]; |
144 |
0 |
fin.read(b); |
145 |
0 |
fin.close(); |
146 |
0 |
sdict = new String(b); |
147 |
0 |
} catch (IOException ex) { |
148 |
0 |
return null; |
149 |
0 |
} |
150 |
0 |
NSDictionary plist = NSPropertyListSerialization.dictionaryForString(sdict); |
151 |
0 |
if (plist == null) |
152 |
0 |
throw new IllegalArgumentException("File " + f + " does not contain a valid stored procedure property list."); |
153 |
0 |
proc = new EOStoredProcedure(plist, this); |
154 |
|
|
155 |
0 |
_storedProcedures.setObjectForKey(proc, proc.name()); |
156 |
|
} |
157 |
0 |
return proc; |
158 |
|
} |
159 |
|
|
160 |
|
public NSArray storedProcedures() { |
161 |
0 |
if (_storedProcedures.count() < _storedProcedureNames.count()) { |
162 |
0 |
for (int i=0; i<_storedProcedureNames.count(); i++) |
163 |
0 |
storedProcedureNamed((String)_storedProcedureNames.objectAtIndex(i)); |
164 |
|
} |
165 |
0 |
return _storedProcedures.allValues(); |
166 |
|
} |
167 |
|
|
168 |
|
public NSArray storedProcedureNames() { |
169 |
0 |
return _storedProcedures.allKeys(); |
170 |
|
} |
171 |
|
|
172 |
|
public void setAdaptorName(String value) { |
173 |
0 |
_adaptorName = value; |
174 |
0 |
} |
175 |
|
public String adaptorName() { |
176 |
0 |
return _adaptorName; |
177 |
|
} |
178 |
|
|
179 |
|
public NSArray entities() { |
180 |
0 |
if (_entitiesByName.count() >= _entities.count()) |
181 |
0 |
return _entitiesByName.allValues(); |
182 |
0 |
NSMutableArray es = new NSMutableArray(); |
183 |
0 |
for (int i = 0; i < _entities.count(); i++) { |
184 |
0 |
NSDictionary d = (NSDictionary)_entities.objectAtIndex(i); |
185 |
0 |
es.addObject(entityNamed((String)d.objectForKey("name"))); |
186 |
|
} |
187 |
0 |
return es; |
188 |
|
} |
189 |
|
|
190 |
|
public NSArray entityNames() { |
191 |
0 |
if (_entitiesByName.count() >= _entities.count()) |
192 |
0 |
return _entitiesByName.allKeys(); |
193 |
0 |
NSMutableArray names = new NSMutableArray(); |
194 |
0 |
for (int i = 0; i < _entities.count(); i++) { |
195 |
0 |
NSDictionary d = (NSDictionary)_entities.objectAtIndex(i); |
196 |
0 |
names.addObject(d.objectForKey("name")); |
197 |
|
} |
198 |
0 |
return names; |
199 |
|
} |
200 |
|
|
201 |
|
public EOEntity entityNamed(String name) { |
202 |
0 |
EOEntity e = (EOEntity)_entitiesByName.objectForKey(name); |
203 |
0 |
if (e == null && path() != null) { |
204 |
0 |
boolean exists = false; |
205 |
0 |
for (int i = 0; i < _entities.count(); i++) { |
206 |
0 |
NSDictionary d = (NSDictionary)_entities.objectAtIndex(i); |
207 |
0 |
if (d.objectForKey("name").equals(name)) |
208 |
0 |
exists = true; |
209 |
|
} |
210 |
0 |
if (!exists) |
211 |
0 |
return null; |
212 |
0 |
File f = new File(new File(path()), name + ".plist"); |
213 |
0 |
if (!(f.exists() && f.isFile())) |
214 |
0 |
throw new IllegalArgumentException("Cannot find " + name + ".plist"); |
215 |
0 |
String s = null; |
216 |
|
try { |
217 |
0 |
FileInputStream fin = new FileInputStream(f); |
218 |
0 |
byte[] b = new byte[fin.available()]; |
219 |
0 |
fin.read(b); |
220 |
0 |
fin.close(); |
221 |
0 |
s = new String(b); |
222 |
0 |
} catch (IOException ex) { |
223 |
0 |
throw new IllegalArgumentException("Cannot read " + f); |
224 |
0 |
} |
225 |
0 |
NSDictionary d = NSPropertyListSerialization.dictionaryForString(s); |
226 |
0 |
if (d == null) |
227 |
0 |
throw new IllegalArgumentException("Cannot parse dictionary for " + f); |
228 |
0 |
e = new EOEntity(d, this); |
229 |
0 |
_entitiesByName.setObjectForKey(e, e.name()); |
230 |
|
} |
231 |
0 |
return e; |
232 |
|
} |
233 |
|
|
234 |
|
public String name() { |
235 |
0 |
return _name; |
236 |
|
} |
237 |
|
|
238 |
|
public String path() { |
239 |
0 |
return _path; |
240 |
|
} |
241 |
|
|
242 |
|
public void setModelGroup(EOModelGroup group) { |
243 |
0 |
_group = group; |
244 |
0 |
} |
245 |
|
public EOModelGroup modelGroup() { |
246 |
0 |
return _group; |
247 |
|
} |
248 |
|
|
249 |
|
public void setUserInfo(NSDictionary dict) { |
250 |
0 |
_userInfo = dict; |
251 |
0 |
} |
252 |
|
|
253 |
|
public NSDictionary userInfo() { |
254 |
0 |
return _userInfo; |
255 |
|
} |
256 |
|
|
257 |
|
public void write() { |
258 |
0 |
} |
259 |
|
|
260 |
|
public void writeToFile(String path) { |
261 |
0 |
if (!path.endsWith(".eomodeld")) |
262 |
0 |
path += ".eomodeld"; |
263 |
0 |
File f = new File(path); |
264 |
0 |
if (f.exists()) { |
265 |
0 |
if (f.isDirectory()) { |
266 |
0 |
File[] kids = f.listFiles(); |
267 |
0 |
for (int i = 0; i < kids.length; i++) { |
268 |
0 |
String fname = kids[i].getName(); |
269 |
0 |
if (kids[i].isFile() && (fname.endsWith(".plist") || fname.endsWith(".eomodeld") || fname.endsWith(".fspec") || fname.endsWith(".storedProcedure"))) |
270 |
0 |
kids[i].delete(); |
271 |
|
} |
272 |
0 |
} else |
273 |
0 |
f.delete(); |
274 |
0 |
} else |
275 |
0 |
f.mkdirs(); |
276 |
0 |
File kid = new File(f, "index.eomodeld"); |
277 |
|
|
278 |
|
|
279 |
0 |
NSMutableDictionary d = new NSMutableDictionary(); |
280 |
0 |
d.setObjectForKey(_adaptorName, "adaptorName"); |
281 |
0 |
d.setObjectForKey("2.1", "EOModelVersion"); |
282 |
0 |
d.setObjectForKey(_connectionDictionary, "connectionDictionary"); |
283 |
0 |
if (_internalInfo != null && _internalInfo.count() > 0) |
284 |
0 |
d.setObjectForKey(_internalInfo, "internalInfo"); |
285 |
0 |
if (_userInfo != null && _userInfo.count() > 0) |
286 |
0 |
d.setObjectForKey(_userInfo, "userInfo"); |
287 |
0 |
if (_storedProcedureNames.count() > 0) |
288 |
0 |
d.setObjectForKey(_storedProcedureNames, "storedProcedures"); |
289 |
|
|
290 |
|
|
291 |
0 |
entities(); |
292 |
0 |
storedProcedures(); |
293 |
0 |
NSMutableArray arr = new NSMutableArray(_entitiesByName.count()); |
294 |
0 |
Enumeration enumeration = _entitiesByName.keyEnumerator(); |
295 |
0 |
NSMutableDictionary md = new NSMutableDictionary(); |
296 |
0 |
while (enumeration.hasMoreElements()) { |
297 |
0 |
String key = (String)enumeration.nextElement(); |
298 |
0 |
EOEntity ent = (EOEntity)_entitiesByName.objectForKey(key); |
299 |
0 |
md.removeAllObjects(); |
300 |
0 |
ent.encodeIntoPropertyList(md); |
301 |
|
File plist; |
302 |
0 |
NSDictionary fetchSpecs = (NSDictionary)md.objectForKey("fetchSpecificationDictionary"); |
303 |
0 |
if (fetchSpecs != null) { |
304 |
0 |
if (fetchSpecs.count() > 0) { |
305 |
0 |
plist = new File(f, key + ".fspec"); |
306 |
0 |
String ps = NSPropertyListSerialization.stringForPropertyList(md.objectForKey("fetchSpecificationDictionary")); |
307 |
|
try { |
308 |
0 |
PrintStream stream = new PrintStream(new FileOutputStream(plist)); |
309 |
0 |
stream.println(ps); |
310 |
0 |
stream.close(); |
311 |
0 |
} catch (IOException ex) { |
312 |
0 |
} |
313 |
|
} |
314 |
0 |
md.removeObjectForKey("fetchSpecificationDictionary"); |
315 |
|
} |
316 |
0 |
plist = new File(f, key + ".plist"); |
317 |
0 |
String ps = NSPropertyListSerialization.stringForPropertyList(md); |
318 |
|
try { |
319 |
0 |
PrintStream stream = new PrintStream(new FileOutputStream(plist)); |
320 |
0 |
stream.println(ps); |
321 |
0 |
stream.close(); |
322 |
0 |
} catch (IOException ex) { |
323 |
0 |
} |
324 |
|
|
325 |
0 |
arr.addObject(new NSDictionary( |
326 |
0 |
new Object[]{ ent.name(), ent.className() }, |
327 |
0 |
new Object[]{ "name", "className" })); |
328 |
0 |
} |
329 |
0 |
d.setObjectForKey(arr, "entities"); |
330 |
|
|
331 |
|
|
332 |
0 |
String s = NSPropertyListSerialization.stringForPropertyList(d); |
333 |
|
try { |
334 |
0 |
PrintStream stream = new PrintStream(new FileOutputStream(kid)); |
335 |
0 |
stream.println(s); |
336 |
0 |
stream.close(); |
337 |
0 |
} catch (IOException ex) { |
338 |
0 |
throw new RuntimeException("Cannot write index.eomodeld"); |
339 |
0 |
} |
340 |
|
|
341 |
|
|
342 |
0 |
for (int i = 0; i < _storedProcedureNames.count(); i++) { |
343 |
0 |
EOStoredProcedure proc = (EOStoredProcedure)_storedProcedures.objectForKey(_storedProcedureNames.objectAtIndex(i)); |
344 |
0 |
kid = new File(f, proc.name() + ".storedProcedure"); |
345 |
0 |
d.removeAllObjects(); |
346 |
0 |
proc.encodeIntoPropertyList(d); |
347 |
0 |
s = NSPropertyListSerialization.stringForPropertyList(d); |
348 |
|
try { |
349 |
0 |
PrintStream stream = new PrintStream(new FileOutputStream(kid)); |
350 |
0 |
stream.println(s); |
351 |
0 |
stream.close(); |
352 |
0 |
} catch (IOException ex) { |
353 |
0 |
throw new RuntimeException("Cannot write " + f); |
354 |
0 |
} |
355 |
|
} |
356 |
0 |
_path = f.getAbsolutePath(); |
357 |
0 |
} |
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 |
|
|