001: package org.apache.torque.map;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.Collections;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import org.apache.commons.collections.map.ListOrderedMap;
027:
028: /**
029: * ColumnMap is used to model a column of a table in a database.
030: * <p>
031: * Note that this information should be set via the <Table>MapBuilder class and
032: * not changed by applications. The set methods are only public because this
033: * class needs them.
034: *
035: * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
036: * @author <a href="mailto:greg.monroe@dukece.com">Greg Monroe</a>
037: * @version $Id: ColumnMap.java 473821 2006-11-11 22:37:25Z tv $
038: */
039: public class ColumnMap implements java.io.Serializable {
040: /** The serialVersionUID for this class. */
041: private static final long serialVersionUID = -5971184507395399165L;
042:
043: /** A sample object having the same java Type as the column. */
044: private Object type = null;
045:
046: /** The name of the Torque Type of the column. */
047: private String torqueType = null;
048:
049: /** Should object type be converted to primitive. */
050: private boolean usePrimitive = true;
051:
052: /** Size of the column. */
053: private int size = 0;
054:
055: /** Scale of the column */
056: private int scale = 0;
057:
058: /** Is it a primary key? */
059: private boolean pk = false;
060:
061: /** Is null value allowed ? */
062: private boolean notNull = false;
063:
064: /** Name of the table that this column is related to. */
065: private String relatedTableName = "";
066:
067: /** Name of the column that this column is related to. */
068: private String relatedColumnName = "";
069:
070: /** The TableMap for this column. */
071: private TableMap table;
072:
073: /** The name of the column. */
074: private String columnName;
075:
076: /**
077: * The Java Name of this column as defined in XML or created by the
078: * generator code.
079: */
080: private String javaName;
081:
082: /** Is this column an autoincrement column ? */
083: private boolean autoIncrement = false;
084:
085: /** Column description info (if any). */
086: private String description = "";
087:
088: /** is Column protected ? */
089: private boolean isProtected = false;
090:
091: /**
092: * String representing the default value defined for field. Note that
093: * default is a keyword, so defaultValue is used to store the value for the
094: * get/setDefault() methods.
095: */
096: private String defaultValue = null;
097:
098: /** Inheritance type used. */
099: private String inheritance = "false";
100:
101: /**
102: * Does column uses Inheritance subclasses? Note that this is tied to the
103: * TableMap useInheritance thru the set function.
104: */
105: private boolean useInheritance;
106:
107: /** Associated of inheritance maps. */
108: private Map inheritanceMaps = Collections
109: .synchronizedMap(new ListOrderedMap());
110:
111: /** Input validator class name. (in DTD but not used?) */
112: private String inputValidator;
113:
114: /** Java naming method the generator used. */
115: private String javaNamingMethod;
116:
117: /** Java type string specified in XML. */
118: private String javaType;
119:
120: /** Column position in the table (one based). */
121: private int position = -1;
122:
123: /**
124: * Constructor.
125: *
126: * @param name The name of the column.
127: * @param containingTable TableMap of the table this column is in.
128: */
129: public ColumnMap(String name, TableMap containingTable) {
130: table = containingTable;
131: this .columnName = normalizeName(name);
132: }
133:
134: /**
135: * Makes sure that the column names don't include table prefixes. E.g.,
136: * SCARAB_PROJECT.PROJECT_ID should be PROJECT_ID.
137: *
138: * @param name The name to check
139: * @return The corrected name if needed or the same name if not.
140: */
141: protected String normalizeName(String name) {
142: if (name.indexOf('.') > 0) {
143: return name.substring(name.lastIndexOf('.') + 1);
144: }
145: return name;
146: }
147:
148: /**
149: * Get the name of a column.
150: *
151: * @return A String with the column name.
152: */
153: public String getColumnName() {
154: return columnName;
155: }
156:
157: /**
158: * Get the table name + column name.
159: *
160: * @return A String with the full column name.
161: */
162: public String getFullyQualifiedName() {
163: return table.getName() + "." + columnName;
164: }
165:
166: /**
167: * Get the name of the table this column is in.
168: *
169: * @return A String with the table name.
170: */
171: public String getTableName() {
172: return table.getName();
173: }
174:
175: /**
176: * Set the type of this column.
177: *
178: * @param type An Object specifying the type.
179: */
180: public void setType(Object type) {
181: this .type = type;
182: }
183:
184: /**
185: * Set the Torque type of this column.
186: *
187: * @param torqueType the Torque type of the column.
188: */
189: public void setTorqueType(String torqueType) {
190: this .torqueType = torqueType;
191: }
192:
193: /**
194: * Set the size of this column.
195: *
196: * @param size An int specifying the size.
197: */
198: public void setSize(int size) {
199: this .size = size;
200: }
201:
202: /**
203: * Set if this column is a primary key or not.
204: *
205: * @param pk True if column is a primary key.
206: */
207: public void setPrimaryKey(boolean pk) {
208: this .pk = pk;
209: }
210:
211: /**
212: * Set if this column may be null.
213: *
214: * @param nn True if column may be null.
215: */
216: public void setNotNull(boolean nn) {
217: this .notNull = nn;
218: }
219:
220: /**
221: * Set the foreign key for this column.
222: *
223: * @param fullyQualifiedName The name of the table.column that is
224: * foreign.
225: */
226: public void setForeignKey(String fullyQualifiedName) {
227: if (fullyQualifiedName != null
228: && fullyQualifiedName.length() > 0) {
229: relatedTableName = fullyQualifiedName.substring(0,
230: fullyQualifiedName.indexOf('.'));
231: relatedColumnName = fullyQualifiedName
232: .substring(fullyQualifiedName.indexOf('.') + 1);
233: } else {
234: relatedTableName = "";
235: relatedColumnName = "";
236: }
237: }
238:
239: /**
240: * Set the foreign key for this column.
241: *
242: * @param tableName The name of the table that is foreign.
243: * @param columnName The name of the column that is foreign.
244: */
245: public void setForeignKey(String tableName, String columnName) {
246: if (tableName != null && tableName.length() > 0
247: && columnName != null && columnName.length() > 0) {
248: relatedTableName = tableName;
249: relatedColumnName = normalizeName(columnName);
250: } else {
251: relatedTableName = "";
252: relatedColumnName = "";
253: }
254: }
255:
256: /**
257: * Get the type of this column. Note that if usePrimitive is true, this may
258: * need to be converted.
259: *
260: * @return An Object specifying the type.
261: */
262: public Object getType() {
263: return type;
264: }
265:
266: /**
267: * Get the name of the Torque type of this column.
268: *
269: * @return The name of the Torque type of this column.
270: */
271: public String getTorqueType() {
272: return torqueType;
273: }
274:
275: /**
276: * The "precision" value from the XML
277: * size="<precision>[,<scale>]"
278: * attribute. Where [,<scale>] is optional.
279: *
280: * If the size attribute has not been set in the XML, it will return 0.
281: * <p>
282: *
283: * Note that the size="P,S" format should be replaced with size="P"
284: * scale="S".
285: *
286: * @return An int specifying the size.
287: */
288: public int getSize() {
289: return size;
290: }
291:
292: /**
293: * Is this column a primary key?
294: *
295: * @return True if column is a primary key.
296: */
297: public boolean isPrimaryKey() {
298: return pk;
299: }
300:
301: /**
302: * Is null value allowed ?
303: *
304: * @return True if column may be null.
305: */
306: public boolean isNotNull() {
307: return (notNull || isPrimaryKey());
308: }
309:
310: /**
311: * Is this column a foreign key?
312: *
313: * @return True if column is a foreign key.
314: */
315: public boolean isForeignKey() {
316: return (relatedTableName != null && relatedTableName.length() > 0);
317: }
318:
319: /**
320: * Get the table.column that this column is related to.
321: *
322: * @return A String with the full name for the related column.
323: */
324: public String getRelatedName() {
325: return relatedTableName + "." + relatedColumnName;
326: }
327:
328: /**
329: * Get the table name that this column is related to.
330: *
331: * @return A String with the name for the related table.
332: */
333: public String getRelatedTableName() {
334: return relatedTableName;
335: }
336:
337: /**
338: * Get the column name that this column is related to.
339: *
340: * @return A String with the name for the related column.
341: */
342: public String getRelatedColumnName() {
343: return relatedColumnName;
344: }
345:
346: /**
347: * Gets the scale set for this column (if any) as set in the XML database
348: * definition. E.g., the value of the scale attribute or the scale portion
349: * of a size="P,S" attribute. (Note: size="P,S" format is being
350: * deprecated!).
351: *
352: * @return Returns the scale.
353: */
354: public int getScale() {
355: return scale;
356: }
357:
358: /**
359: * @param scale The scale to set.
360: */
361: public void setScale(int scale) {
362: this .scale = scale;
363: }
364:
365: /**
366: * Gets the Java Name for this column as defined in XML or created by
367: * generator code.
368: *
369: * @return the Java Name.
370: */
371: public String getJavaName() {
372: return this .javaName;
373: }
374:
375: /**
376: * Sets the Java Name for this column.
377: *
378: * @param name the Java Name.
379: */
380: public void setJavaName(String name) {
381: this .javaName = name;
382: }
383:
384: /**
385: * Returns whether this column is an autoincrement column.
386: *
387: * @return true if this column is an autoIncrement column, false otherwise.
388: */
389: public boolean isAutoIncrement() {
390: return autoIncrement;
391: }
392:
393: /**
394: * Sets whether this column is an autoincrement column.
395: *
396: * @param autoIncrement whether this colimn is an autoincrement column.
397: */
398: public void setAutoIncrement(boolean autoIncrement) {
399: this .autoIncrement = autoIncrement;
400: }
401:
402: /**
403: * A string representing the default value defined for this column.
404: *
405: * @return The default value of this column, if any.
406: */
407: public String getDefault() {
408: return defaultValue;
409: }
410:
411: /**
412: * Sets the default value for this column.
413: *
414: * @param defaultValue The defaultValue to set.
415: */
416: public void setDefault(String defaultValue) {
417: this .defaultValue = defaultValue;
418: }
419:
420: /**
421: * Returns the column description info.
422: *
423: * @return the description, if any.
424: */
425: public String getDescription() {
426: return description;
427: }
428:
429: /**
430: * Sets the description for this column.
431: *
432: * @param description The description to set.
433: */
434: public void setDescription(String description) {
435: this .description = description;
436: }
437:
438: /**
439: * Get the inheritance information associated with this column,
440: *
441: * @return Returns an array of associated inheritanceMap.
442: * The array is in XML order.
443: */
444: public InheritanceMap[] getInheritanceMaps() {
445: InheritanceMap[] iMaps = new InheritanceMap[inheritanceMaps
446: .size()];
447: synchronized (inheritanceMaps) {
448: Iterator it = inheritanceMaps.values().iterator();
449: int i = 0;
450: while (it.hasNext()) {
451: iMaps[i++] = (InheritanceMap) it.next();
452: }
453: }
454: return iMaps;
455: }
456:
457: /**
458: * Add an associated inheritance mapping.
459: *
460: * @param map The inheritanceMap to associate with this column.
461: */
462: public void addInheritanceMap(InheritanceMap map) {
463: setUseInheritance(true);
464: this .inheritanceMaps.put(map.getKey(), map);
465: }
466:
467: /**
468: * Gets the inheritance type used.
469: *
470: * @return the inheritance type used.
471: */
472: public String getInheritance() {
473: return inheritance;
474: }
475:
476: /**
477: * Sets the inheritance type.
478: *
479: * @param inheritanceType The inheritance type to set.
480: */
481: public void setInheritance(String inheritanceType) {
482: this .inheritance = inheritanceType;
483: }
484:
485: /**
486: * Returns the input validator class name.
487: * (This property is in the DTD, but currently not used by Torque?)
488: *
489: * @return Returns the inputValidator.
490: */
491: public String getInputValidator() {
492: return inputValidator;
493: }
494:
495: /**
496: * Sets the input validator class name.
497: *
498: * @param inputValidator The inputValidator to set.
499: */
500: public void setInputValidator(String inputValidator) {
501: this .inputValidator = inputValidator;
502: }
503:
504: /**
505: * Returns whether getters and setters are generated with the
506: * access modifier "protected" rather than "public".
507: *
508: * @return whether the accessors should be protected rather than public.
509: */
510: public boolean isProtected() {
511: return isProtected;
512: }
513:
514: /**
515: * Sets whether getters and setters should be generated with the
516: * access modifier "protected" rather than "public".
517: *
518: * @param isProtected whether getters and setters for this column
519: * are protected.
520: */
521: public void setProtected(boolean isProtected) {
522: this .isProtected = isProtected;
523: }
524:
525: /**
526: * Returns whether this column is a primary key.
527: *
528: * @return whether this column is a primary key.
529: */
530: public boolean isPk() {
531: return pk;
532: }
533:
534: /**
535: * Sets whether this column is a primary key.
536: *
537: * @param pk whether this column is a primary key.
538: */
539: public void setPk(boolean pk) {
540: this .pk = pk;
541: }
542:
543: /**
544: * Returns whether this column uses inheritance subclasses.
545: *
546: * @return true if inheritance subclasses are used, false otherwise.
547: */
548: public boolean isUseInheritance() {
549: return useInheritance;
550: }
551:
552: /**
553: * Sets whether this column uses inheritance subclasses.
554: *
555: * @param useInheritance whether this column uses Inheritance subclasses.
556: */
557: public void setUseInheritance(boolean useInheritance) {
558: this .useInheritance = useInheritance;
559: }
560:
561: /**
562: * Get the inheritance map with the specified key.
563: *
564: * @param key the key of the inheritance map.
565: * @return the inheritance map with the specified key, or null if no
566: * inheritance map with the specified key exists in this column.
567: */
568: public InheritanceMap getInheritanceMap(String key) {
569: return (InheritanceMap) inheritanceMaps.get(key);
570: }
571:
572: /**
573: * Returns whether this colum uses primitive values rather than objects.
574: *
575: * @return true if this colum uses primitive values, false if it uses
576: * objects.
577: */
578: public boolean isUsePrimitive() {
579: return usePrimitive;
580: }
581:
582: /**
583: * Sets whether this colum uses primitive values rather than objects.
584: *
585: * @param usePrimitive whether primitive objects are used
586: * rather than objects.
587: */
588: public void setUsePrimitive(boolean usePrimitive) {
589: this .usePrimitive = usePrimitive;
590: }
591:
592: /**
593: * Returns the Java naming method for this column.
594: *
595: * @return the javaNamingMethod for this column.
596: */
597: public String getJavaNamingMethod() {
598: return javaNamingMethod;
599: }
600:
601: /**
602: * Sets the java naming method for this column.
603: *
604: * @param javaNamingMethod The javaNamingMethod to set.
605: */
606: public void setJavaNamingMethod(String javaNamingMethod) {
607: this .javaNamingMethod = javaNamingMethod;
608: }
609:
610: /**
611: * Returns the map for the table this column belongs to.
612: *
613: * @return the table map for this column.
614: */
615: public TableMap getTable() {
616: return table;
617: }
618:
619: /**
620: * Returns the position (one based) of this column in the table.
621: * XML order is preserved.
622: *
623: * @return The position of this column, one-based.
624: */
625: public int getPosition() {
626: return position;
627: }
628:
629: /**
630: * Sets the position (one based) of this column in the table.
631: *
632: * @param position The position to set.
633: */
634: public void setPosition(int position) {
635: this .position = position;
636: }
637:
638: /**
639: * Returns the java type of this column.
640: *
641: * @return the javaType.
642: */
643: public String getJavaType() {
644: return javaType;
645: }
646:
647: /**
648: * Sets the java type of this column.
649: *
650: * @param javaType The javaType to set.
651: */
652: public void setJavaType(String javaType) {
653: this.javaType = javaType;
654: }
655: }
|