001: package net.sourceforge.jaxor.parser;
002:
003: import net.sourceforge.jaxor.util.ObjectUtils;
004: import net.sourceforge.jaxor.mappers.*;
005:
006: import java.sql.Timestamp;
007:
008: public class Attribute {
009:
010: private String _name;
011: private String _typeClass;
012: private boolean _nullable;
013: private boolean _readOnly;
014: private boolean _primaryKey;
015: private boolean _aggregate;
016:
017: private boolean _matchOnUpdate;
018: private String _mapper;
019: private String _alias;
020: private String _default;
021: private boolean _autoAssign;
022:
023: private String _fieldAdapter = FieldAdapterImpl.class.getName();
024:
025: public Attribute() {
026: }
027:
028: public Attribute(String name, String type, boolean nullable) {
029: _name = name;
030: _typeClass = type;
031: _nullable = nullable;
032: }
033:
034: public Attribute(String name, String type, boolean nullable,
035: boolean readOnly) {
036: this (name, type, nullable);
037: _readOnly = readOnly;
038: }
039:
040: public boolean isMatchOnUpdate() {
041: if (_primaryKey)
042: return true;
043: return _matchOnUpdate;
044: }
045:
046: public String getDefaultValue() {
047: if (_default == null)
048: return "null";
049:
050: if ("String".equals(_typeClass)) {
051: return "\"" + _default + "\"";
052: } else
053: return _default;
054: }
055:
056: public void setDefault(String aDefault) {
057: _default = aDefault;
058: }
059:
060: public void setMatchonupdate(boolean matchOnUpdate) {
061: _matchOnUpdate = matchOnUpdate;
062: }
063:
064: public boolean isReadOnly() {
065: return _readOnly;
066: }
067:
068: public void setName(String name) {
069: _name = name;
070: }
071:
072: public String getName() {
073: return _name;
074: }
075:
076: public String getJavaName() {
077: if (_alias == null)
078: return JavaNameGenerator.toJavaName(getName());
079: return _alias;
080: }
081:
082: public String getMetaName() {
083: return getName().toUpperCase() + "_META";
084: }
085:
086: public String getFieldAdapter() {
087: return _fieldAdapter;
088: }
089:
090: public void setType(String type) {
091: if ("version".equalsIgnoreCase(type)) {
092: setNullable(false);
093: setMatchonupdate(true);
094: _fieldAdapter = LongVersionFieldAdapter.class.getName();
095: _typeClass = Long.class.getName();
096: } else if ("user".equalsIgnoreCase(type)) {
097: setNullable(false);
098: _fieldAdapter = UserFieldAdapter.class.getName();
099: _typeClass = String.class.getName();
100: } else if ("user-insert".equalsIgnoreCase(type)) {
101: setNullable(false);
102: _fieldAdapter = UserInsertFieldAdapter.class.getName();
103: _typeClass = String.class.getName();
104: } else if ("user-update".equalsIgnoreCase(type)) {
105: setNullable(true);
106: _fieldAdapter = UserUpdateFieldAdapter.class.getName();
107: _typeClass = String.class.getName();
108: } else if ("timestamp".equalsIgnoreCase(type)) {
109: setNullable(false);
110: _fieldAdapter = TimestampFieldAdapter.class.getName();
111: _typeClass = Timestamp.class.getName();
112: } else if ("timestamp-update".equalsIgnoreCase(type)) {
113: setNullable(true);
114: _fieldAdapter = TimestampUpdateFieldAdapter.class.getName();
115: _typeClass = Timestamp.class.getName();
116: } else if ("timestamp-insert".equalsIgnoreCase(type)) {
117: setNullable(false);
118: _fieldAdapter = TimestampInsertFieldAdapter.class.getName();
119: _typeClass = Timestamp.class.getName();
120: } else
121: _typeClass = type;
122: }
123:
124: public String getType() {
125: return _typeClass;
126: }
127:
128: public String getTypeName() {
129: int index = getType().lastIndexOf(".");
130: return getType().substring(index + 1);
131: }
132:
133: public String getMapperParameter(Jaxor jaxor) {
134: if (isPrimitive())
135: return "";
136: else
137: return "new " + getMapperName(jaxor) + "(),";
138: }
139:
140: public String getMapperName(Jaxor jaxor) {
141: if (_mapper != null)
142: return _mapper;
143: return jaxor.getMapperName(getType());
144: }
145:
146: public void setNullable(boolean b) {
147: _nullable = b;
148: }
149:
150: public boolean isNullable() {
151: if (isAutoAssign())
152: return true;
153: return _nullable;
154: }
155:
156: public boolean isPrimaryKey() {
157: return _primaryKey;
158: }
159:
160: public void setIsPrimaryKey(boolean primaryKey) {
161: _primaryKey = primaryKey;
162: }
163:
164: public void setMapper(String mapper) {
165: _mapper = mapper;
166: }
167:
168: public String getGetterSig() {
169: return "public " + getType() + " " + getGetter();
170: }
171:
172: public String getGetter() {
173: return "get" + getJavaName() + "()";
174: }
175:
176: public String getSetterSig(String mod) {
177: return mod + " void " + getSetterBase() + "(" + getType()
178: + " arg)";
179: }
180:
181: public String getAccessor() {
182: if (isPrimitive())
183: return getName() + ".get"
184: + JavaNameGenerator.toJavaName(getType()) + "()";
185: else
186: return getName() + ".getValue()";
187: }
188:
189: public String getSetterBase() {
190: return "set" + getJavaName();
191: }
192:
193: public void setAlias(String alias) {
194: _alias = alias;
195: }
196:
197: public boolean isAggregate() {
198: return _aggregate;
199: }
200:
201: public void setAggregate(boolean _aggregate) {
202: this ._aggregate = _aggregate;
203: }
204:
205: public String getPropertyAsString() {
206: return "\"" + getJavaName() + "\"";
207: }
208:
209: public String getBooleanConstructorList() {
210: return isNullable() + "," + isPrimaryKey() + ","
211: + isMatchOnUpdate() + "," + isAutoAssign();
212: }
213:
214: public boolean isAutoAssign() {
215: return _autoAssign;
216: }
217:
218: public void setAutoAssign(boolean autoAssign) {
219: _autoAssign = autoAssign;
220: }
221:
222: public boolean isPrimitive() {
223: return ObjectUtils.isPrimitive(getType());
224: }
225:
226: }
|