001: /**********************************************************************
002: Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: ...
018: **********************************************************************/package org.jpox.metadata;
019:
020: import org.jpox.util.StringUtils;
021:
022: /**
023: * Representation of the Meta-Data for a column mapping of a field.
024: * JDO metadata represented is :-
025: * <PRE>
026: * <!ELEMENT column (extension*)?>
027: * <!ATTLIST column name CDATA #IMPLIED>
028: * <!ATTLIST column target CDATA #IMPLIED>
029: * <!ATTLIST column target-field CDATA #IMPLIED>
030: * <!ATTLIST column jdbc-type CDATA #IMPLIED>
031: * <!ATTLIST column sql-type CDATA #IMPLIED>
032: * <!ATTLIST column length CDATA #IMPLIED>
033: * <!ATTLIST column scale CDATA #IMPLIED>
034: * <!ATTLIST column allows-null CDATA #IMPLIED>
035: * <!ATTLIST column default-value CDATA #IMPLIED>
036: * <!ATTLIST column insert-value CDATA #IMPLIED>
037: * </PRE>
038: *
039: * @since 1.1
040: * @version $Revision: 1.20 $
041: */
042: public class ColumnMetaData extends MetaData {
043: /** column name. */
044: protected String name;
045:
046: /** target column name. */
047: protected String target;
048:
049: /** target field/property name. */
050: protected String targetMember;
051:
052: /** jdbc-type to use (if any). */
053: protected String jdbcType;
054:
055: /** sql-type to use (if any). */
056: protected String sqlType;
057:
058: /** length to use (if any). Also known as precision */
059: protected Integer length;
060:
061: /** scale to use (if any). */
062: protected Integer scale;
063:
064: /** allows-null tag value. */
065: protected Boolean allowsNull;
066:
067: /** column default value (when constructing the table with this column). */
068: protected String defaultValue;
069:
070: /** value to use when inserting this column in the datastore (the column is not mapped to a field/property) */
071: protected String insertValue;
072:
073: /** Whether this column is to be inserted when the owning object is inserted. JPA 1.0 attribute. */
074: protected boolean insertable = true;
075:
076: /** Whether this column can be updated when the owning object is updated. JPA 1.0 attribute. */
077: protected boolean updateable = true;
078:
079: /** unique tag value. JPA 1.0 attribute. */
080: protected boolean unique;
081:
082: /**
083: * Creates a ColumnMetaData by copying contents from <code>colmd</code>.
084: * @param parent Parent MetaData component
085: * @param colmd MetaData for the column
086: */
087: public ColumnMetaData(final MetaData parent,
088: final ColumnMetaData colmd) {
089: super (parent);
090:
091: name = colmd.getName();
092: target = colmd.getTarget();
093: targetMember = colmd.getTargetMember();
094: jdbcType = colmd.getJdbcType();
095: sqlType = colmd.getSqlType();
096: length = colmd.getLength();
097: scale = colmd.getScale();
098: allowsNull = colmd.allowsNull;
099: defaultValue = colmd.getDefaultValue();
100: insertValue = colmd.getInsertValue();
101: insertable = colmd.getInsertable();
102: updateable = colmd.getUpdateable();
103: unique = colmd.getUnique();
104: }
105:
106: /**
107: * Convenience constructor specifying just the column name, and the parent metadata component.
108: * Assigns nulls to other parameters.
109: * @param parent Parent MetaData component
110: * @param name Name of the column
111: */
112: public ColumnMetaData(final MetaData parent, final String name) {
113: this (parent, name, null, null, null, null, null, null, null,
114: null, null, null, null, null);
115: }
116:
117: /**
118: * Constructor.
119: * @param parent parent MetaData instance
120: * @param name field name
121: * @param target target
122: * @param targetMember target field/property
123: * @param jdbcType JDBC Type to use
124: * @param sqlType SQL Type to use
125: * @param length length of field
126: * @param scale scale of field
127: * @param allowsNull Whether nulls are allowed
128: * @param defaultValue The default value for the column
129: * @param insertValue The insert value for the column
130: * @param insertable Whether this column is insertable
131: * @param updateable Whether this column is updateable
132: * @param unique Whether this column is unique
133: */
134: public ColumnMetaData(MetaData parent, final String name,
135: final String target, final String targetMember,
136: final String jdbcType, final String sqlType,
137: final String length, final String scale,
138: final String allowsNull, final String defaultValue,
139: final String insertValue, final String insertable,
140: final String updateable, final String unique) {
141: super (parent);
142:
143: this .name = (StringUtils.isWhitespace(name) ? null : name);
144:
145: this .target = (StringUtils.isWhitespace(target) ? null : target);
146: this .targetMember = (StringUtils.isWhitespace(targetMember) ? null
147: : targetMember);
148:
149: this .jdbcType = (StringUtils.isWhitespace(jdbcType) ? null
150: : jdbcType);
151: /*if (this.jdbcType != null && !JDBCUtils.isValidJDBCType(this.jdbcType))
152: {
153: // Invalid "jdbc-type" (maybe not supported by JPOX?)
154: // TODO This uses RDBMS-specific code so is commented out
155: throw new InvalidMetaDataException(LOCALISER, "044118", this.jdbcType);
156: }*/
157:
158: this .sqlType = (StringUtils.isWhitespace(sqlType) ? null
159: : sqlType);
160:
161: if (length != null && length.length() > 0) {
162: this .length = Integer.valueOf(length);
163: }
164:
165: if (scale != null && scale.length() > 0) {
166: this .scale = Integer.valueOf(scale);
167: }
168:
169: if (allowsNull != null) {
170: if (allowsNull.equalsIgnoreCase("true")) {
171: this .allowsNull = Boolean.TRUE;
172: } else if (allowsNull.equalsIgnoreCase("false")) {
173: this .allowsNull = Boolean.FALSE;
174: }
175: }
176:
177: this .defaultValue = (StringUtils.isWhitespace(defaultValue) ? null
178: : defaultValue);
179: this .insertValue = (StringUtils.isWhitespace(insertValue) ? null
180: : insertValue);
181:
182: if (insertable != null && insertable.equalsIgnoreCase("false")) {
183: this .insertable = false;
184: } else {
185: // Insertable unless specified otherwise
186: this .insertable = true;
187: }
188: if (updateable != null && updateable.equalsIgnoreCase("false")) {
189: this .updateable = false;
190: } else {
191: // Updateable unless specified otherwise
192: this .updateable = true;
193: }
194: if (unique != null && unique.equalsIgnoreCase("true")) {
195: this .unique = true;
196: } else {
197: // Non-unique unless specified otherwise
198: this .unique = false;
199: }
200: }
201:
202: /**
203: * Accessor for the name
204: * @return name
205: */
206: public String getName() {
207: return name;
208: }
209:
210: /**
211: * Accessor for the column that is the target of this column in the referenced table.
212: * @return target tag value
213: */
214: public String getTarget() {
215: return target;
216: }
217:
218: /**
219: * Accessor for the field/property that is the target of this column in the referenced class.
220: * @return field/property that is the target
221: */
222: public String getTargetMember() {
223: return targetMember;
224: }
225:
226: /**
227: * Accessor for the jdbc-type tag value
228: * @return jdbc-type tag value
229: */
230: public String getJdbcType() {
231: return jdbcType;
232: }
233:
234: /**
235: * Accessor for the sql-type tag value
236: * @return sql-type tag value
237: */
238: public String getSqlType() {
239: return sqlType;
240: }
241:
242: /**
243: * Accessor for the length tag value. Also known as precision
244: * @return length tag value
245: */
246: public Integer getLength() {
247: return length;
248: }
249:
250: /**
251: * Accessor for the scale tag value
252: * @return scale tag value
253: */
254: public Integer getScale() {
255: return scale;
256: }
257:
258: /**
259: * Accessor for whether the nulls allowed flag has been set.
260: * @return Whether it was set.
261: */
262: public boolean isAllowsNullSet() {
263: return (allowsNull != null);
264: }
265:
266: /**
267: * Accessor for the nulls-allowed tag value
268: * @return nulls-allowed tag value
269: */
270: public boolean isAllowsNull() {
271: if (allowsNull == null) {
272: return false;
273: } else {
274: return allowsNull.booleanValue();
275: }
276: }
277:
278: /**
279: * Accessor for the default value
280: * @return default value
281: */
282: public String getDefaultValue() {
283: return defaultValue;
284: }
285:
286: /**
287: * Accessor for the insert value
288: * @return insert value
289: */
290: public String getInsertValue() {
291: return insertValue;
292: }
293:
294: /**
295: * Accessor for whether this column can be inserted when the owning object is inserted.
296: * @return Whether it can be inserted.
297: */
298: public boolean getInsertable() {
299: return insertable;
300: }
301:
302: /**
303: * Accessor for whether this column can be update when the owning object is updated.
304: * @return Whether it can be updated.
305: */
306: public boolean getUpdateable() {
307: return updateable;
308: }
309:
310: /**
311: * Accessor for the unique tag value
312: * @return unique tag value
313: */
314: public boolean getUnique() {
315: return unique;
316: }
317:
318: // ------------------------------- Mutators --------------------------------
319:
320: /**
321: * Mutator for the scale
322: * @param scale The scale to set.
323: */
324: public final void setScale(Integer scale) {
325: this .scale = scale;
326: }
327:
328: /**
329: * Mutator for the scale
330: * @param scale The scale to set.
331: */
332: public final void setScale(int scale) {
333: this .scale = new Integer(scale);
334: }
335:
336: /**
337: * Mutator for the JDBC type
338: * @param jdbcType The jdbcType to set.
339: */
340: public final void setJdbcType(String jdbcType) {
341: this .jdbcType = jdbcType;
342: }
343:
344: /**
345: * Mutator for the length. Also known as precision
346: * @param length The length to set.
347: */
348: public final void setLength(Integer length) {
349: this .length = length;
350: }
351:
352: /**
353: * Mutator for the length. Also known as precision
354: * @param length The length to set.
355: */
356: public final void setLength(int length) {
357: this .length = new Integer(length);
358: }
359:
360: /**
361: * Mutator for the name
362: * @param name The name to set.
363: */
364: public final void setName(String name) {
365: this .name = name;
366: }
367:
368: /**
369: * Mutator for whether nulls are allowed.
370: * @param allowsNull The allowsNull to set.
371: */
372: public final void setAllowsNull(Boolean allowsNull) {
373: this .allowsNull = allowsNull;
374: }
375:
376: /**
377: * Mutator for the SQL type
378: * @param sqlType The sqlType to set.
379: */
380: public final void setSqlType(String sqlType) {
381: this .sqlType = sqlType;
382: }
383:
384: // ------------------------------- Utilities -------------------------------
385:
386: /**
387: * Returns a string representation of the object using a prefix
388: * @param prefix prefix string
389: * @param indent indent string
390: * @return a string representation of the object.
391: */
392: public String toString(String prefix, String indent) {
393: StringBuffer sb = new StringBuffer();
394: sb.append(prefix).append("<column");
395: if (name != null) {
396: sb.append(" name=\"" + name + "\"");
397: }
398: if (target != null) {
399: sb.append(" target=\"" + target + "\"");
400: }
401: if (targetMember != null) {
402: sb.append(" target-field=\"" + targetMember + "\"");
403: }
404: if (jdbcType != null) {
405: sb.append(" jdbc-type=\"" + jdbcType + "\"");
406: }
407: if (sqlType != null) {
408: sb.append(" sql-type=\"" + sqlType + "\"");
409: }
410: if (allowsNull != null) {
411: sb.append(" allows-null=\"" + allowsNull + "\"");
412: }
413: if (length != null) {
414: sb.append(" length=\"" + length + "\"");
415: }
416: if (scale != null) {
417: sb.append(" scale=\"" + scale + "\"");
418: }
419: if (defaultValue != null) {
420: sb.append(" default-value=\"" + defaultValue + "\"");
421: }
422: if (insertValue != null) {
423: sb.append(" insert-value=\"" + insertValue + "\"");
424: }
425:
426: if (extensions != null && extensions.size() > 0) {
427: sb.append(">\n");
428:
429: // Add extensions
430: sb.append(super .toString(prefix + indent, indent));
431:
432: sb.append(prefix).append("</column>\n");
433: } else {
434: sb.append("/>\n");
435: }
436:
437: return sb.toString();
438: }
439: }
|