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.foundation.NSDictionary;
21  import net.wotonomy.foundation.NSMutableDictionary;
22  import net.wotonomy.foundation.NSSelector;
23  
24  /***
25  * Represents an attribute inside an entity. Contains mapping data for
26  * the attribute's external name, external and internal datatypes, etc.
27  * It can also represent a flattened or derived attribute, or a prototype;
28  * and they are also used to represent parameters in a stored procedure.
29  *
30  * @author ezamudio@nasoft.com
31  * @author $Author: cgruber $
32  * @version $Revision: 894 $
33  */
34  public class EOAttribute extends EOProperty implements EOPropertyListEncoding {
35  
36  	//These are used for stored procedure parameters.
37  	public static final int Void = 0;
38  	public static final int InParameter = 1;
39  	public static final int OutParameter = 2;
40  	public static final int InOutParameter = 3;
41  
42  	protected EOEntity _entity;
43  	protected String _name;
44  	protected String _columnName;
45  	protected String _definition;
46  	protected String _className;
47  	protected String _externalType;
48  	protected Class _valueClass;
49  	protected String _valueClassName;
50  	protected String _valueType;
51  	protected String _valueFactoryMethodName;
52  	protected String _readFormat;
53  	protected String _writeFormat;
54  	protected String _prototypeName;
55  	protected EOAttribute _prototype;
56  	protected NSSelector _valueFactoryMethod;
57  	protected boolean _allowsNull;
58  	protected boolean _readOnly;
59  	protected boolean _isFlattened;
60  	protected boolean _knowsIfFlattened;
61  	protected int _precision;
62  	protected int _scale;
63  	protected int _width;
64  	protected int _parameterDirection;
65  	protected NSDictionary _internalInfo;
66  	protected NSDictionary _userInfo;
67  
68  	protected boolean _has_allowsNull;
69  
70  	public EOAttribute() {
71  		super();
72  		_allowsNull = true;
73  	}
74  
75  	public EOAttribute(NSDictionary dict, Object obj) {
76  		super();
77  		if (obj instanceof EOEntity)
78  			_entity = (EOEntity)obj;
79  		setName((String)dict.objectForKey("name"));
80  		if (dict.objectForKey("columnName") != null)
81  			setColumnName((String)dict.objectForKey("columnName"));
82  		if (dict.objectForKey("definition") != null)
83  			setDefinition((String)dict.objectForKey("definition"));
84  		_prototypeName = (String)dict.objectForKey("prototypeName");
85  		setExternalType((String)dict.objectForKey("externalType"));
86  		setClassName((String)dict.objectForKey("valueClassName"));
87  		setValueType((String)dict.objectForKey("valueType"));
88  		_writeFormat = (String)dict.objectForKey("writeFormat");
89  		_readFormat = (String)dict.objectForKey("readFormat");
90  		if (dict.objectForKey("precision") != null)
91  			setPrecision(Integer.parseInt((String)dict.objectForKey("precision")));
92  		if (dict.objectForKey("scale") != null)
93  			setScale(Integer.parseInt((String)dict.objectForKey("scale")));
94  		if (dict.objectForKey("width") != null)
95  			setWidth(Integer.parseInt((String)dict.objectForKey("width")));
96  		if (dict.objectForKey("parameterDirection") != null)
97  			setParameterDirection(Integer.parseInt((String)dict.objectForKey("parameterDirection")));
98  		setAllowsNull("Y".equals(dict.objectForKey("allowsNull")));
99  	}
100 
101 	void setEntity(EOEntity value) {
102 		_entity = value;
103 	}
104 
105 	public void setName(String name) {
106 		_name = name;
107 	}
108 	public String name() {
109 		return _name;
110 	}
111 
112 	public void setColumnName(String name) {
113 		_columnName = name;
114 	}
115 	public String columnName() {
116 		if (_columnName != null)
117 			return _columnName;
118 		if (prototype() != null)
119 			if (_prototype.columnName() != null)
120 				return _prototype.columnName();
121 		return null;
122 	}
123 
124 	public void setClassName(String name) {
125 		_className = name;
126 	}
127 	public String className() {
128 		if (_className != null)
129 			return _className;
130 		if (prototype() != null)
131 			if (_prototype.className() != null)
132 				return _prototype.className();
133 		return null;
134 	}
135 
136 	public void setDefinition(String def) {
137 		_definition = def;
138 		_columnName = null;
139 	}
140 	public String definition() {
141 		if (_definition != null)
142 			return _definition;
143 		if (prototype() != null)
144 			if (_prototype.definition() != null)
145 				return _prototype.definition();
146 		return null;
147 	}
148 
149 	public void setExternalType(String type) {
150 		_externalType = type;
151 	}
152 	public String externalType() {
153 		if (_externalType != null)
154 			return _externalType;
155 		if (prototype() != null)
156 			if (_prototype.externalType() != null)
157 				return _prototype.externalType();
158 		return null;
159 	}
160 
161 	public void setAllowsNull(boolean flag) {
162 		_allowsNull = flag;
163 		_has_allowsNull = true;
164 	}
165 	public boolean allowsNull() {
166 		if (_has_allowsNull)
167 			return _allowsNull;
168 		if (prototype() != null)
169 			return _prototype.allowsNull();
170 		return _allowsNull;
171 	}
172 
173 	public void setReadOnly(boolean flag) {
174 		_readOnly = flag;
175 	}
176 	public boolean readOnly() {
177 		return _readOnly;
178 	}
179 
180 	public void setPrototype(EOAttribute proto) {
181 		_prototype = proto;
182 		if (proto != null)
183 			_prototypeName = proto.name();
184 		else
185 			_prototypeName = null;
186 	}
187 	public EOAttribute prototype() {
188 		if (_prototypeName != null && _prototype == null) {
189 			try {
190 				EOModel m = _entity.model();
191 				EOModelGroup g = m.modelGroup();
192 				EOEntity protos = g.entityNamed("EO" + m.adaptorName() + "Prototypes");
193 				if (protos == null)
194 					protos = g.entityNamed("EOPrototypes");
195 				_prototype = protos.attributeNamed(_prototypeName);
196 			} catch (NullPointerException e) {
197 			}
198 		}
199 		return _prototype;
200 	}
201 
202 	public void setPrecision(int value) {
203 		_precision = value;
204 	}
205 	public int precision() {
206 		if (_precision > 0)
207 			return _precision;
208 		if (prototype() != null)
209 			return _prototype.precision();
210 		return _precision;
211 	}
212 
213 	public void setScale(int value) {
214 		_scale = value;
215 	}
216 	public int scale() {
217 		if (_scale > 0)
218 			return _scale;
219 		if (prototype() != null)
220 			return _prototype.scale();
221 		return _scale;
222 	}
223 
224 	public void setWidth(int value) {
225 		_width = value;
226 	}
227 	public int width() {
228 		if (_width > 0)
229 			return _width;
230 		if  (prototype() != null)
231 			return _prototype.width();
232 		return _width;
233 	}
234 
235 	/*** @deprecated Use setClassName instead. */
236 	public void setValueClassName(String name) {
237 		setClassName(name);
238 	}
239 	/*** @deprecated Use className() instead. */
240 	public String valueClassName() {
241 		return className();
242 	}
243 
244 	public void setValueType(String type) {
245 		_valueType = type;
246 	}
247 	public String valueType() {
248 		if (_valueType != null)
249 			return _valueType;
250 		if (prototype() != null)
251 			return _prototype.valueType();
252 		return null;
253 	}
254 
255 	public void setReadFormat(String value) {
256 		_readFormat = value;
257 	}
258 	public String readFormat() {
259 		return _readFormat;
260 	}
261 
262 	public void setWriteFormat(String value) {
263 		_writeFormat = value;
264 	}
265 	public String writeFormat() {
266 		return _writeFormat;
267 	}
268 
269 	public boolean isDerived() {
270 		return (definition() != null);
271 	}
272 
273 	/*** Determines whether the receiver is a flattened attribute.
274 	 * A flattened attribute has as its definition a relationship
275 	 * path that can be resolved to an attribute.
276 	 * @return true if the receiver is flattened.
277 	 */
278 	public boolean isFlattened() {
279 		if (_knowsIfFlattened)
280 			return _isFlattened;
281 		_knowsIfFlattened = true;
282 		if (definition() == null)
283 			return false;
284 		_isFlattened = (entity()._attributeForPath(definition()) != null);
285 		return _isFlattened;
286 	}
287 
288 	public EOEntity entity() {
289 		return _entity;
290 	}
291 
292 	public void setParameterDirection(int dir) {
293 		_parameterDirection = dir;
294 	}
295 	public int parameterDirection() {
296 		return _parameterDirection;
297 	}
298 
299 	public String relationshipPath() {
300 		if (isFlattened())
301 			return definition();
302 		return null;
303 	}
304 
305 	public void setUserInfo(NSDictionary value) {
306 		_userInfo = value;
307 	}
308 	public NSDictionary userInfo() {
309 		return _userInfo;
310 	}
311 
312 	public void awakeWithPropertyList(NSDictionary plist) {
313 	}
314 
315 	public void encodeIntoPropertyList(NSMutableDictionary dict) {
316 		dict.setObjectForKey(name(), "name");
317 		if (_prototypeName != null)
318 			dict.setObjectForKey(_prototypeName, "prototypeName");
319 		if (_columnName != null)
320 			dict.setObjectForKey(_columnName, "columnName");
321 		if (_definition != null)
322 			dict.setObjectForKey(_definition, "definition");
323 		if (_className != null)
324 			dict.setObjectForKey(_className, "valueClassName");
325 		if (_valueType != null)
326 			dict.setObjectForKey(_valueType, "valueType");
327 		if (_precision > 0)
328 			dict.setObjectForKey(new Integer(_precision), "precision");
329 		if (_scale > 0)
330 			dict.setObjectForKey(new Integer(_scale), "scale");
331 		if (_width > 0)
332 			dict.setObjectForKey(new Integer(_width), "width");
333 		if (_externalType != null)
334 			dict.setObjectForKey(_externalType, "externalType");
335 		if (_readFormat != null)
336 			dict.setObjectForKey(_readFormat, "readFormat");
337 		if (_writeFormat != null)
338 			dict.setObjectForKey(_writeFormat, "writeFormat");
339 		if (_allowsNull)
340 			dict.setObjectForKey("Y", "allowsNull");
341 		if (_entity == null)
342 			dict.setObjectForKey(new Integer(parameterDirection()), "parameterDirection");
343 		if (userInfo() != null && userInfo().count() > 0)
344 			dict.setObjectForKey(userInfo(), "userInfo");
345 		if (_internalInfo != null && _internalInfo.count() > 0)
346 			dict.setObjectForKey(_internalInfo, "internalInfo");
347 	}
348 
349 }
350 /*
351  * $Log$
352  * Revision 1.2  2006/02/16 16:47:13  cgruber
353  * Move some classes in to "internal" packages and re-work imports, etc.
354  *
355  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
356  *
357  * Revision 1.1  2006/02/16 13:19:57  cgruber
358  * Check in all sources in eclipse-friendly maven-enabled packages.
359  *
360  * Revision 1.7  2003/08/14 02:13:56  chochos
361  * implement and use _attributeForPath()
362  *
363  * Revision 1.6  2003/08/12 01:45:04  chochos
364  * added some code to handle prototypes
365  *
366  * Revision 1.5  2003/08/11 19:38:27  chochos
367  * Can now read from a file and re-write to another file.
368  *
369  * Revision 1.4  2003/08/09 01:35:35  chochos
370  * implement EOPropertyListEncoding
371  *
372  * Revision 1.3  2003/08/08 06:52:09  chochos
373  * isFlattened() works
374  *
375  * Revision 1.2  2003/08/08 02:15:03  chochos
376  * added parameterDirection (for use with stored procedures)
377  *
378  * Revision 1.1  2003/08/07 02:39:45  chochos
379  * EOAttribute. Can be initialized from a property list.
380  *
381 */