summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/access/EOAttribute.java
blob: d77a1629b56e4cfd0f39f92f5c70164373830f36 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 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.foundation.NSDictionary;
import net.wotonomy.foundation.NSMutableDictionary;
import net.wotonomy.foundation.NSSelector;

/**
 * Represents an attribute inside an entity. Contains mapping data for the
 * attribute's external name, external and internal datatypes, etc. It can also
 * represent a flattened or derived attribute, or a prototype; and they are also
 * used to represent parameters in a stored procedure.
 *
 * @author ezamudio@nasoft.com
 * @author $Author: cgruber $
 * @version $Revision: 894 $
 */
public class EOAttribute extends EOProperty implements EOPropertyListEncoding {

	// These are used for stored procedure parameters.
	public static final int Void = 0;
	public static final int InParameter = 1;
	public static final int OutParameter = 2;
	public static final int InOutParameter = 3;

	protected EOEntity _entity;
	protected String _name;
	protected String _columnName;
	protected String _definition;
	protected String _className;
	protected String _externalType;
	protected Class _valueClass;
	protected String _valueClassName;
	protected String _valueType;
	protected String _valueFactoryMethodName;
	protected String _readFormat;
	protected String _writeFormat;
	protected String _prototypeName;
	protected EOAttribute _prototype;
	protected NSSelector _valueFactoryMethod;
	protected boolean _allowsNull;
	protected boolean _readOnly;
	protected boolean _isFlattened;
	protected boolean _knowsIfFlattened;
	protected int _precision;
	protected int _scale;
	protected int _width;
	protected int _parameterDirection;
	protected NSDictionary _internalInfo;
	protected NSDictionary _userInfo;

	protected boolean _has_allowsNull;

	public EOAttribute() {
		super();
		_allowsNull = true;
	}

	public EOAttribute(NSDictionary dict, Object obj) {
		super();
		if (obj instanceof EOEntity)
			_entity = (EOEntity) obj;
		setName((String) dict.objectForKey("name"));
		if (dict.objectForKey("columnName") != null)
			setColumnName((String) dict.objectForKey("columnName"));
		if (dict.objectForKey("definition") != null)
			setDefinition((String) dict.objectForKey("definition"));
		_prototypeName = (String) dict.objectForKey("prototypeName");
		setExternalType((String) dict.objectForKey("externalType"));
		setClassName((String) dict.objectForKey("valueClassName"));
		setValueType((String) dict.objectForKey("valueType"));
		_writeFormat = (String) dict.objectForKey("writeFormat");
		_readFormat = (String) dict.objectForKey("readFormat");
		if (dict.objectForKey("precision") != null)
			setPrecision(Integer.parseInt((String) dict.objectForKey("precision")));
		if (dict.objectForKey("scale") != null)
			setScale(Integer.parseInt((String) dict.objectForKey("scale")));
		if (dict.objectForKey("width") != null)
			setWidth(Integer.parseInt((String) dict.objectForKey("width")));
		if (dict.objectForKey("parameterDirection") != null)
			setParameterDirection(Integer.parseInt((String) dict.objectForKey("parameterDirection")));
		setAllowsNull("Y".equals(dict.objectForKey("allowsNull")));
	}

	void setEntity(EOEntity value) {
		_entity = value;
	}

	public void setName(String name) {
		_name = name;
	}

	public String name() {
		return _name;
	}

	public void setColumnName(String name) {
		_columnName = name;
	}

	public String columnName() {
		if (_columnName != null)
			return _columnName;
		if (prototype() != null)
			if (_prototype.columnName() != null)
				return _prototype.columnName();
		return null;
	}

	public void setClassName(String name) {
		_className = name;
	}

	public String className() {
		if (_className != null)
			return _className;
		if (prototype() != null)
			if (_prototype.className() != null)
				return _prototype.className();
		return null;
	}

	public void setDefinition(String def) {
		_definition = def;
		_columnName = null;
	}

	public String definition() {
		if (_definition != null)
			return _definition;
		if (prototype() != null)
			if (_prototype.definition() != null)
				return _prototype.definition();
		return null;
	}

	public void setExternalType(String type) {
		_externalType = type;
	}

	public String externalType() {
		if (_externalType != null)
			return _externalType;
		if (prototype() != null)
			if (_prototype.externalType() != null)
				return _prototype.externalType();
		return null;
	}

	public void setAllowsNull(boolean flag) {
		_allowsNull = flag;
		_has_allowsNull = true;
	}

	public boolean allowsNull() {
		if (_has_allowsNull)
			return _allowsNull;
		if (prototype() != null)
			return _prototype.allowsNull();
		return _allowsNull;
	}

	public void setReadOnly(boolean flag) {
		_readOnly = flag;
	}

	public boolean readOnly() {
		return _readOnly;
	}

	public void setPrototype(EOAttribute proto) {
		_prototype = proto;
		if (proto != null)
			_prototypeName = proto.name();
		else
			_prototypeName = null;
	}

	public EOAttribute prototype() {
		if (_prototypeName != null && _prototype == null) {
			try {
				EOModel m = _entity.model();
				EOModelGroup g = m.modelGroup();
				EOEntity protos = g.entityNamed("EO" + m.adaptorName() + "Prototypes");
				if (protos == null)
					protos = g.entityNamed("EOPrototypes");
				_prototype = protos.attributeNamed(_prototypeName);
			} catch (NullPointerException e) {
			}
		}
		return _prototype;
	}

	public void setPrecision(int value) {
		_precision = value;
	}

	public int precision() {
		if (_precision > 0)
			return _precision;
		if (prototype() != null)
			return _prototype.precision();
		return _precision;
	}

	public void setScale(int value) {
		_scale = value;
	}

	public int scale() {
		if (_scale > 0)
			return _scale;
		if (prototype() != null)
			return _prototype.scale();
		return _scale;
	}

	public void setWidth(int value) {
		_width = value;
	}

	public int width() {
		if (_width > 0)
			return _width;
		if (prototype() != null)
			return _prototype.width();
		return _width;
	}

	/** @deprecated Use setClassName instead. */
	public void setValueClassName(String name) {
		setClassName(name);
	}

	/** @deprecated Use className() instead. */
	public String valueClassName() {
		return className();
	}

	public void setValueType(String type) {
		_valueType = type;
	}

	public String valueType() {
		if (_valueType != null)
			return _valueType;
		if (prototype() != null)
			return _prototype.valueType();
		return null;
	}

	public void setReadFormat(String value) {
		_readFormat = value;
	}

	public String readFormat() {
		return _readFormat;
	}

	public void setWriteFormat(String value) {
		_writeFormat = value;
	}

	public String writeFormat() {
		return _writeFormat;
	}

	public boolean isDerived() {
		return (definition() != null);
	}

	/**
	 * Determines whether the receiver is a flattened attribute. A flattened
	 * attribute has as its definition a relationship path that can be resolved to
	 * an attribute.
	 * 
	 * @return true if the receiver is flattened.
	 */
	public boolean isFlattened() {
		if (_knowsIfFlattened)
			return _isFlattened;
		_knowsIfFlattened = true;
		if (definition() == null)
			return false;
		_isFlattened = (entity()._attributeForPath(definition()) != null);
		return _isFlattened;
	}

	public EOEntity entity() {
		return _entity;
	}

	public void setParameterDirection(int dir) {
		_parameterDirection = dir;
	}

	public int parameterDirection() {
		return _parameterDirection;
	}

	public String relationshipPath() {
		if (isFlattened())
			return definition();
		return null;
	}

	public void setUserInfo(NSDictionary value) {
		_userInfo = value;
	}

	public NSDictionary userInfo() {
		return _userInfo;
	}

	public void awakeWithPropertyList(NSDictionary plist) {
	}

	public void encodeIntoPropertyList(NSMutableDictionary dict) {
		dict.setObjectForKey(name(), "name");
		if (_prototypeName != null)
			dict.setObjectForKey(_prototypeName, "prototypeName");
		if (_columnName != null)
			dict.setObjectForKey(_columnName, "columnName");
		if (_definition != null)
			dict.setObjectForKey(_definition, "definition");
		if (_className != null)
			dict.setObjectForKey(_className, "valueClassName");
		if (_valueType != null)
			dict.setObjectForKey(_valueType, "valueType");
		if (_precision > 0)
			dict.setObjectForKey(new Integer(_precision), "precision");
		if (_scale > 0)
			dict.setObjectForKey(new Integer(_scale), "scale");
		if (_width > 0)
			dict.setObjectForKey(new Integer(_width), "width");
		if (_externalType != null)
			dict.setObjectForKey(_externalType, "externalType");
		if (_readFormat != null)
			dict.setObjectForKey(_readFormat, "readFormat");
		if (_writeFormat != null)
			dict.setObjectForKey(_writeFormat, "writeFormat");
		if (_allowsNull)
			dict.setObjectForKey("Y", "allowsNull");
		if (_entity == null)
			dict.setObjectForKey(new Integer(parameterDirection()), "parameterDirection");
		if (userInfo() != null && userInfo().count() > 0)
			dict.setObjectForKey(userInfo(), "userInfo");
		if (_internalInfo != null && _internalInfo.count() > 0)
			dict.setObjectForKey(_internalInfo, "internalInfo");
	}

}
/*
 * $Log$ Revision 1.2 2006/02/16 16:47:13 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.7 2003/08/14 02:13:56 chochos implement and use
 * _attributeForPath()
 *
 * Revision 1.6 2003/08/12 01:45:04 chochos added some code to handle prototypes
 *
 * Revision 1.5 2003/08/11 19:38:27 chochos Can now read from a file and
 * re-write to another file.
 *
 * Revision 1.4 2003/08/09 01:35:35 chochos implement EOPropertyListEncoding
 *
 * Revision 1.3 2003/08/08 06:52:09 chochos isFlattened() works
 *
 * Revision 1.2 2003/08/08 02:15:03 chochos added parameterDirection (for use
 * with stored procedures)
 *
 * Revision 1.1 2003/08/07 02:39:45 chochos EOAttribute. Can be initialized from
 * a property list.
 *
 */