View Javadoc

1   /*
2    Wotonomy: OpenStep design patterns for pure Java applications.
3    Copyright (C) 2001 Michael Powers
4   
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14  
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, see http://www.gnu.org
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  			//TODO: read all frameworks and get models from them
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 			//this causes all entities to be loaded
149 			NSArray ents = m.entities();
150 			for (int i=0; i<ents.count(); i++) {
151 				EOEntity e = (EOEntity)ents.objectAtIndex(i);
152 				//this loads all the fetch specifications
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  * $Log$
176  * Revision 1.2  2006/02/16 16:47:14  cgruber
177  * Move some classes in to "internal" packages and re-work imports, etc.
178  *
179  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
180  *
181  * Revision 1.1  2006/02/16 13:19:57  cgruber
182  * Check in all sources in eclipse-friendly maven-enabled packages.
183  *
184  * Revision 1.4  2005/05/11 15:21:53  cgruber
185  * Change enum to enumeration, since enum is now a keyword as of Java 5.0
186  *
187  * A few other comments in the code.
188  *
189  * Revision 1.3  2003/08/08 00:44:04  chochos
190  * manage model groups for object store coordinators.
191  *
192  * Revision 1.2  2003/08/08 00:36:41  chochos
193  * add a little more functionality
194  *
195  * Revision 1.1  2003/08/07 02:42:28  chochos
196  * EOModel can read an .eomodeld file. EOModelGroup doesn't do much for now.
197  *
198 */