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 net.wotonomy.control.EOFetchSpecification;
21 import net.wotonomy.control.EOObjectStoreCoordinator;
22 import net.wotonomy.foundation.NSArray;
23 import net.wotonomy.foundation.NSDictionary;
24 import net.wotonomy.foundation.NSMutableDictionary;
25
26 /***
27 * A group of models that connect to the same database. Entities in
28 * these models can have relationships that point to other entities
29 * in any model of the same group.
30 *
31 * @author ezamudio@nasoft.com
32 * @author $Author: cgruber $
33 * @version $Revision: 894 $
34 */
35 public class EOModelGroup {
36
37 private static EOModelGroup _defaultGroup;
38 private static EOModelGroup _globalGroup;
39 protected NSMutableDictionary _models;
40
41 public EOModelGroup() {
42 super();
43 }
44
45 public void addModel(EOModel model) {
46 if (model.name() == null)
47 throw new IllegalArgumentException("Cannot add an unnamed model to a group.");
48 if (_models.objectForKey(model.name()) != null)
49 throw new IllegalArgumentException("Cannot add model " + model.name() +
50 " to group because it already contains a model with the same name.");
51 NSArray ents = model.entityNames();
52 for (int i = 0; i < ents.count(); i++) {
53 String ename = (String)ents.objectAtIndex(i);
54 if (entityNamed(ename) != null)
55 throw new IllegalArgumentException("Cannot add model " + model.name() +
56 " to group because it contains entity named " + ename);
57 }
58 _models.setObjectForKey(model, model.name());
59 }
60
61 public void removeModel(EOModel model) {
62 _models.removeObjectForKey(model.name());
63 }
64
65 public void addModelWithPath(String path) {
66 EOModel model = new EOModel(path);
67 addModel(model);
68 }
69
70 public void addModelsFromDirectory(String dir) {
71 }
72
73 public static void setDefaultGroup(EOModelGroup group) {
74 _defaultGroup = group;
75 }
76 public static EOModelGroup defaultGroup() {
77 if (_defaultGroup == null) {
78 _defaultGroup = globalModelGroup();
79 }
80 return _defaultGroup;
81 }
82
83 public static EOModelGroup globalModelGroup() {
84 if (_globalGroup == null) {
85 _globalGroup = new EOModelGroup();
86
87 }
88 return _globalGroup;
89 }
90
91 public EOEntity entityForObject(net.wotonomy.control.EOEnterpriseObject eo) {
92 return null;
93 }
94
95 public EOEntity entityNamed(String name) {
96 java.util.Enumeration enumeration = _models.objectEnumerator();
97 while (enumeration.hasMoreElements()) {
98 EOModel m = (EOModel)enumeration.nextElement();
99 if (m.entityNamed(name) != null)
100 return m.entityNamed(name);
101 }
102 return null;
103 }
104
105 public EOModel modelNamed(String name) {
106 return (EOModel)_models.objectForKey(name);
107 }
108
109 public NSArray modelNames() {
110 return _models.allKeys();
111 }
112
113 public NSArray models() {
114 return _models.allValues();
115 }
116
117 public EOModel modelWithPath(String path) {
118 java.util.Enumeration enumeration = _models.objectEnumerator();
119 while (enumeration.hasMoreElements()) {
120 EOModel m = (EOModel)enumeration.nextElement();
121 if (m.path() != null && m.path().equals(path))
122 return m;
123 }
124 return null;
125 }
126
127 public EOStoredProcedure storedProcedureNamed(String name) {
128 java.util.Enumeration enumeration = _models.objectEnumerator();
129 while (enumeration.hasMoreElements()) {
130 EOModel m = (EOModel)enumeration.nextElement();
131 if (m.storedProcedureNamed(name) != null)
132 return m.storedProcedureNamed(name);
133 }
134 return null;
135 }
136
137 public EOFetchSpecification fetchSpecificationNamed(String fetchSpecName, String entityName) {
138 EOEntity e = entityNamed(entityName);
139 if (e == null)
140 return null;
141 return e.fetchSpecificationNamed(fetchSpecName);
142 }
143
144 public void loadAllModelObjects() {
145 java.util.Enumeration enumeration = _models.objectEnumerator();
146 while (enumeration.hasMoreElements()) {
147 EOModel m = (EOModel)enumeration.nextElement();
148
149 NSArray ents = m.entities();
150 for (int i=0; i<ents.count(); i++) {
151 EOEntity e = (EOEntity)ents.objectAtIndex(i);
152
153 e.fetchSpecificationNamed("whatever");
154 }
155 }
156 }
157
158 public static void setModelGroupForObjectStoreCoordinator(EOObjectStoreCoordinator coord, EOModelGroup group) {
159 NSMutableDictionary d = new NSMutableDictionary(coord.userInfo());
160 d.setObjectForKey(group, "ModelGroup");
161 coord.setUserInfo(d);
162 }
163 public static EOModelGroup modelGroupForObjectStoreCoordinator(EOObjectStoreCoordinator coord) {
164 NSDictionary d = coord.userInfo();
165 if (d == null)
166 return defaultGroup();
167 Object g = d.objectForKey("ModelGroup");
168 if (g != null && g instanceof EOModelGroup)
169 return (EOModelGroup)g;
170 return defaultGroup();
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