/* 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. * */