summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/access/EOModelGroup.java
blob: dbab09d1d8091d0c79966a4995d50c4adf5c2e68 (plain)
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
/*
 Wotonomy: OpenStep design patterns for pure Java applications.
 Copyright (C) 2001 Michael Powers

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, see http://www.gnu.org
 */
package net.wotonomy.access;

import net.wotonomy.control.EOFetchSpecification;
import net.wotonomy.control.EOObjectStoreCoordinator;
import net.wotonomy.foundation.NSArray;
import net.wotonomy.foundation.NSDictionary;
import net.wotonomy.foundation.NSMutableDictionary;

/**
* A group of models that connect to the same database. Entities in
* these models can have relationships that point to other entities
* in any model of the same group.
*
* @author ezamudio@nasoft.com
* @author $Author: cgruber $
* @version $Revision: 894 $
*/
public class EOModelGroup {

	private static EOModelGroup _defaultGroup;
	private static EOModelGroup _globalGroup;
	protected NSMutableDictionary _models;

	public EOModelGroup() {
		super();
	}

	public void addModel(EOModel model) {
		if (model.name() == null)
			throw new IllegalArgumentException("Cannot add an unnamed model to a group.");
		if (_models.objectForKey(model.name()) != null)
			throw new IllegalArgumentException("Cannot add model " + model.name() +
				" to group because it already contains a model with the same name.");
		NSArray ents = model.entityNames();
		for (int i = 0; i < ents.count(); i++) {
			String ename = (String)ents.objectAtIndex(i);
			if (entityNamed(ename) != null)
				throw new IllegalArgumentException("Cannot add model " + model.name() +
					" to group because it contains entity named " + ename);
		}
		_models.setObjectForKey(model, model.name());
	}

	public void removeModel(EOModel model) {
		_models.removeObjectForKey(model.name());
	}

	public void addModelWithPath(String path) {
		EOModel model = new EOModel(path);
		addModel(model);
	}

	public void addModelsFromDirectory(String dir) {
	}

	public static void setDefaultGroup(EOModelGroup group) {
		_defaultGroup = group;
	}
	public static EOModelGroup defaultGroup() {
		if (_defaultGroup == null) {
			_defaultGroup = globalModelGroup();
		}
		return _defaultGroup;
	}

	public static EOModelGroup globalModelGroup() {
		if (_globalGroup == null) {
			_globalGroup = new EOModelGroup();
			//TODO: read all frameworks and get models from them
		}
		return _globalGroup;
	}

	public EOEntity entityForObject(net.wotonomy.control.EOEnterpriseObject eo) {
		return null;
	}

	public EOEntity entityNamed(String name) {
		java.util.Enumeration enumeration = _models.objectEnumerator();
		while (enumeration.hasMoreElements()) {
			EOModel m = (EOModel)enumeration.nextElement();
			if (m.entityNamed(name) != null)
				return m.entityNamed(name);
		}
		return null;
	}

	public EOModel modelNamed(String name) {
		return (EOModel)_models.objectForKey(name);
	}

	public NSArray modelNames() {
		return _models.allKeys();
	}

	public NSArray models() {
		return _models.allValues();
	}

	public EOModel modelWithPath(String path) {
		java.util.Enumeration enumeration = _models.objectEnumerator();
		while (enumeration.hasMoreElements()) {
			EOModel m = (EOModel)enumeration.nextElement();
			if (m.path() != null && m.path().equals(path))
				return m;
		}
		return null;
	}

	public EOStoredProcedure storedProcedureNamed(String name) {
		java.util.Enumeration enumeration = _models.objectEnumerator();
		while (enumeration.hasMoreElements()) {
			EOModel m = (EOModel)enumeration.nextElement();
			if (m.storedProcedureNamed(name) != null)
				return m.storedProcedureNamed(name);
		}
		return null;
	}

	public EOFetchSpecification fetchSpecificationNamed(String fetchSpecName, String entityName) {
		EOEntity e = entityNamed(entityName);
		if (e == null)
			return null;
		return e.fetchSpecificationNamed(fetchSpecName);
	}

	public void loadAllModelObjects() {
		java.util.Enumeration enumeration = _models.objectEnumerator();
		while (enumeration.hasMoreElements()) {
			EOModel m = (EOModel)enumeration.nextElement();
			//this causes all entities to be loaded
			NSArray ents = m.entities();
			for (int i=0; i<ents.count(); i++) {
				EOEntity e = (EOEntity)ents.objectAtIndex(i);
				//this loads all the fetch specifications
				e.fetchSpecificationNamed("whatever");
			}
		}
	}

	public static void setModelGroupForObjectStoreCoordinator(EOObjectStoreCoordinator coord, EOModelGroup group) {
		NSMutableDictionary d = new NSMutableDictionary(coord.userInfo());
		d.setObjectForKey(group, "ModelGroup");
		coord.setUserInfo(d);
	}
	public static EOModelGroup modelGroupForObjectStoreCoordinator(EOObjectStoreCoordinator coord) {
		NSDictionary d = coord.userInfo();
		if (d == null)
			return defaultGroup();
		Object g = d.objectForKey("ModelGroup");
		if (g != null && g instanceof EOModelGroup)
			return (EOModelGroup)g;
		return defaultGroup();
	}

}
/*
 * $Log$
 * Revision 1.2  2006/02/16 16:47:14  cgruber
 * Move some classes in to "internal" packages and re-work imports, etc.
 *
 * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
 *
 * Revision 1.1  2006/02/16 13:19:57  cgruber
 * Check in all sources in eclipse-friendly maven-enabled packages.
 *
 * Revision 1.4  2005/05/11 15:21:53  cgruber
 * Change enum to enumeration, since enum is now a keyword as of Java 5.0
 *
 * A few other comments in the code.
 *
 * Revision 1.3  2003/08/08 00:44:04  chochos
 * manage model groups for object store coordinators.
 *
 * Revision 1.2  2003/08/08 00:36:41  chochos
 * add a little more functionality
 *
 * Revision 1.1  2003/08/07 02:42:28  chochos
 * EOModel can read an .eomodeld file. EOModelGroup doesn't do much for now.
 *
*/