View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2001 Intersect Software Corporation
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.EOClassDescription;
21  import net.wotonomy.control.EOEditingContext;
22  import net.wotonomy.control.EOFetchSpecification;
23  import net.wotonomy.control.EOGenericRecord;
24  import net.wotonomy.control.EOGlobalID;
25  import net.wotonomy.foundation.NSArray;
26  import net.wotonomy.foundation.NSMutableArray;
27  
28  /***
29  * @author ezamudio@nasoft.com
30  * @author $Author: cgruber $
31  * @version $Revision: 894 $
32  */
33  public class EOEntityClassDescription extends EOClassDescription {
34  
35  	protected EOEntity _entity;
36  
37  	public EOEntityClassDescription() {
38  		super();
39  	}
40  
41  	public EOEntityClassDescription(EOEntity entity) {
42  		this();
43  		_entity = entity;
44  	}
45  
46  	public NSArray allAttributeKeys() {
47  		NSArray arr = entity().attributes();
48  		NSMutableArray a = new NSMutableArray(arr.count());
49  		for (int i = 0; i < arr.count(); i++) {
50  			EOAttribute atrib = (EOAttribute)arr.objectAtIndex(i);
51  			a.addObject(atrib);
52  		}
53  		return a;
54  	}
55  
56  	public NSArray allPropertyKeys() {
57  		return entity().classPropertyNames();
58  	}
59  
60  	public NSArray allToManyRelationshipKeys() {
61  		NSArray arr = entity().relationships();
62  		NSMutableArray a = new NSMutableArray(arr.count());
63  		for (int i = 0; i < arr.count(); i++) {
64  			EORelationship r = (EORelationship)arr.objectAtIndex(i);
65  			if (r.isToMany())
66  				a.addObject(r);
67  		}
68  		return a;
69  	}
70  
71  	public NSArray allToOneRelationshipKeys() {
72  		NSArray arr = entity().relationships();
73  		NSMutableArray a = new NSMutableArray(arr.count());
74  		for (int i = 0; i < arr.count(); i++) {
75  			EORelationship r = (EORelationship)arr.objectAtIndex(i);
76  			if (!r.isToMany())
77  				a.addObject(r);
78  		}
79  		return a;
80  	}
81  
82  	/*** Returns all attributes that correspond to columns
83  	 * in a database table.
84  	 */
85  	public NSArray attributeKeys() {
86  		NSArray arr = entity().attributes();
87  		NSMutableArray a = new NSMutableArray(arr.count());
88  		for (int i = 0; i < arr.count(); i++) {
89  			EOAttribute atrib = (EOAttribute)arr.objectAtIndex(i);
90  			if (!atrib.isDerived())
91  				a.addObject(atrib);
92  		}
93  		return a;
94  	}
95  
96  	public EOClassDescription classDescriptionForDestinationKey(String key) {
97  		EORelationship r = entity().relationshipNamed(key);
98  		if (r == null)
99  			return null;
100 		return r.destinationEntity().classDescriptionForInstances();
101 	}
102 
103 	public NSArray clientAttributeKeys() {
104 		return null;
105 	}
106 
107 	public NSArray clientToManyRelationshipKeys() {
108 		return null;
109 	}
110 
111 	public NSArray clientToOneRelationshipKeys() {
112 		return null;
113 	}
114 
115 	public EOEntity entity() {
116 		return _entity;
117 	}
118 
119 	public String entityName() {
120 		return _entity.name();
121 	}
122 
123 	public EOFetchSpecification fetchSpecificationNamed(String name) {
124 		return entity().fetchSpecificationNamed(name);
125 	}
126 
127 	public NSArray toManyRelationshipKeys() {
128 		NSArray arr = entity().relationships();
129 		NSMutableArray a = new NSMutableArray(arr.count());
130 		for (int i = 0; i < arr.count(); i++) {
131 			EORelationship r = (EORelationship)arr.objectAtIndex(i);
132 			if (r.isToMany() && !r.isFlattened())
133 				a.addObject(r);
134 		}
135 		return a;
136 	}
137 
138 	public NSArray toOneRelationshipKeys() {
139 		NSArray arr = entity().relationships();
140 		NSMutableArray a = new NSMutableArray(arr.count());
141 		for (int i = 0; i < arr.count(); i++) {
142 			EORelationship r = (EORelationship)arr.objectAtIndex(i);
143 			if (!r.isToMany() && !r.isFlattened())
144 				a.addObject(r);
145 		}
146 		return a;
147 	}
148 
149 	public Object createInstanceWithEditingContext(EOEditingContext ec, EOGlobalID gid) {
150 		if  (theClass == null) {
151 			try {
152 				theClass = Class.forName(entity().className());
153 			} catch (ClassNotFoundException ex) {
154 				if (entity().className().equals("net.wotonomy.control.EOGenericRecord"))
155 					throw new IllegalArgumentException("Cannot find class " + entity().className());
156 				theClass = EOGenericRecord.class;
157 			}
158 		}
159 		return super.createInstanceWithEditingContext(ec, gid);
160 	}
161 
162 }
163 /*
164  * $Log$
165  * Revision 1.2  2006/02/16 16:47:13  cgruber
166  * Move some classes in to "internal" packages and re-work imports, etc.
167  *
168  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
169  *
170  * Revision 1.1  2006/02/16 13:19:57  cgruber
171  * Check in all sources in eclipse-friendly maven-enabled packages.
172  *
173  * Revision 1.4  2003/08/19 01:55:54  chochos
174  * the behavior is now more consistent with its Apple counterpart.
175  *
176  * Revision 1.3  2003/08/09 01:40:31  chochos
177  * use EOClassDescription's methods to get a destination entity's class description.
178  *
179  * Revision 1.2  2003/08/08 05:51:59  chochos
180  * createInstanceWithEditingContext now works. It sets theClass with the class from the entity className() before calling super.
181  *
182  * Revision 1.1  2003/08/08 00:36:19  chochos
183  * concrete implementation of EOClassDescription that uses an EOEntity
184  *
185  */