View Javadoc

1   package org.apache.torque.map;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Collections;
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  import org.apache.commons.collections.map.ListOrderedMap;
27  
28  /***
29   * ColumnMap is used to model a column of a table in a database.
30   * <p>
31   * Note that this information should be set via the <Table>MapBuilder class and
32   * not changed by applications. The set methods are only public because this
33   * class needs them.
34   *
35   * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
36   * @author <a href="mailto:greg.monroe@dukece.com">Greg Monroe</a>
37   * @version $Id: ColumnMap.java 473821 2006-11-11 22:37:25Z tv $
38   */
39  public class ColumnMap implements java.io.Serializable
40  {
41      /*** The serialVersionUID for this class. */
42      private static final long serialVersionUID = -5971184507395399165L;
43  
44      /*** A sample object having the same java Type as the column. */
45      private Object type = null;
46  
47      /*** The name of the Torque Type of the column. */
48      private String torqueType = null;
49  
50      /*** Should object type be converted to primitive. */
51      private boolean usePrimitive = true;
52  
53      /*** Size of the column. */
54      private int size = 0;
55  
56      /*** Scale of the column */
57      private int scale = 0;
58  
59      /*** Is it a primary key? */
60      private boolean pk = false;
61  
62      /*** Is null value allowed ? */
63      private boolean notNull = false;
64  
65      /*** Name of the table that this column is related to. */
66      private String relatedTableName = "";
67  
68      /*** Name of the column that this column is related to. */
69      private String relatedColumnName = "";
70  
71      /*** The TableMap for this column. */
72      private TableMap table;
73  
74      /*** The name of the column. */
75      private String columnName;
76  
77      /***
78       * The Java Name of this column as defined in XML or created by the
79       * generator code.
80       */
81      private String javaName;
82  
83      /*** Is this column an autoincrement column ? */
84      private boolean autoIncrement = false;
85  
86      /*** Column description info (if any). */
87      private String description = "";
88  
89      /*** is Column protected ? */
90      private boolean isProtected = false;
91  
92      /***
93       * String representing the default value defined for field. Note that
94       * default is a keyword, so defaultValue is used to store the value for the
95       * get/setDefault() methods.
96       */
97      private String defaultValue = null;
98  
99      /*** Inheritance type used. */
100     private String inheritance = "false";
101 
102     /***
103      * Does column uses Inheritance subclasses? Note that this is tied to the
104      * TableMap useInheritance thru the set function.
105      */
106     private boolean useInheritance;
107 
108     /*** Associated of inheritance maps. */
109     private Map inheritanceMaps = Collections
110             .synchronizedMap(new ListOrderedMap());
111 
112     /*** Input validator class name. (in DTD but not used?) */
113     private String inputValidator;
114 
115     /*** Java naming method the generator used. */
116     private String javaNamingMethod;
117 
118     /*** Java type string specified in XML. */
119     private String javaType;
120 
121     /*** Column position in the table (one based). */
122     private int position = -1;
123 
124     /***
125      * Constructor.
126      *
127      * @param name The name of the column.
128      * @param containingTable TableMap of the table this column is in.
129      */
130     public ColumnMap(String name, TableMap containingTable)
131     {
132         table = containingTable;
133         this.columnName = normalizeName(name);
134     }
135 
136     /***
137      * Makes sure that the column names don't include table prefixes. E.g.,
138      * SCARAB_PROJECT.PROJECT_ID should be PROJECT_ID.
139      *
140      * @param name The name to check
141      * @return The corrected name if needed or the same name if not.
142      */
143     protected String normalizeName(String name)
144     {
145         if (name.indexOf('.') > 0)
146         {
147             return name.substring(name.lastIndexOf('.') + 1);
148         }
149         return name;
150     }
151 
152     /***
153      * Get the name of a column.
154      *
155      * @return A String with the column name.
156      */
157     public String getColumnName()
158     {
159         return columnName;
160     }
161 
162     /***
163      * Get the table name + column name.
164      *
165      * @return A String with the full column name.
166      */
167     public String getFullyQualifiedName()
168     {
169         return table.getName() + "." + columnName;
170     }
171 
172     /***
173      * Get the name of the table this column is in.
174      *
175      * @return A String with the table name.
176      */
177     public String getTableName()
178     {
179         return table.getName();
180     }
181 
182     /***
183      * Set the type of this column.
184      *
185      * @param type An Object specifying the type.
186      */
187     public void setType(Object type)
188     {
189         this.type = type;
190     }
191 
192     /***
193      * Set the Torque type of this column.
194      *
195      * @param torqueType the Torque type of the column.
196      */
197     public void setTorqueType(String torqueType)
198     {
199         this.torqueType = torqueType;
200     }
201 
202     /***
203      * Set the size of this column.
204      *
205      * @param size An int specifying the size.
206      */
207     public void setSize(int size)
208     {
209         this.size = size;
210     }
211 
212     /***
213      * Set if this column is a primary key or not.
214      *
215      * @param pk True if column is a primary key.
216      */
217     public void setPrimaryKey(boolean pk)
218     {
219         this.pk = pk;
220     }
221 
222     /***
223      * Set if this column may be null.
224      *
225      * @param nn True if column may be null.
226      */
227     public void setNotNull(boolean nn)
228     {
229         this.notNull = nn;
230     }
231 
232     /***
233      * Set the foreign key for this column.
234      *
235      * @param fullyQualifiedName The name of the table.column that is
236      * foreign.
237      */
238     public void setForeignKey(String fullyQualifiedName)
239     {
240         if (fullyQualifiedName != null && fullyQualifiedName.length() > 0)
241         {
242             relatedTableName = fullyQualifiedName.substring(
243                     0, fullyQualifiedName.indexOf('.'));
244             relatedColumnName = fullyQualifiedName.substring(
245                     fullyQualifiedName.indexOf('.') + 1);
246         }
247         else
248         {
249             relatedTableName = "";
250             relatedColumnName = "";
251         }
252     }
253 
254     /***
255      * Set the foreign key for this column.
256      *
257      * @param tableName The name of the table that is foreign.
258      * @param columnName The name of the column that is foreign.
259      */
260     public void setForeignKey(String tableName, String columnName)
261     {
262         if (tableName != null && tableName.length() > 0 && columnName != null
263                 && columnName.length() > 0)
264         {
265             relatedTableName = tableName;
266             relatedColumnName = normalizeName(columnName);
267         }
268         else
269         {
270             relatedTableName = "";
271             relatedColumnName = "";
272         }
273     }
274 
275     /***
276      * Get the type of this column. Note that if usePrimitive is true, this may
277      * need to be converted.
278      *
279      * @return An Object specifying the type.
280      */
281     public Object getType()
282     {
283         return type;
284     }
285 
286     /***
287      * Get the name of the Torque type of this column.
288      *
289      * @return The name of the Torque type of this column.
290      */
291     public String getTorqueType()
292     {
293         return torqueType;
294     }
295 
296     /***
297      * The "precision" value from the XML
298      * size="&lt;precision&gt;[,&lt;scale&gt;]"
299      * attribute. Where [,&lt;scale&gt;] is optional.
300      *
301      * If the size attribute has not been set in the XML, it will return 0.
302      * <p>
303      *
304      * Note that the size="P,S" format should be replaced with size="P"
305      * scale="S".
306      *
307      * @return An int specifying the size.
308      */
309     public int getSize()
310     {
311         return size;
312     }
313 
314     /***
315      * Is this column a primary key?
316      *
317      * @return True if column is a primary key.
318      */
319     public boolean isPrimaryKey()
320     {
321         return pk;
322     }
323 
324     /***
325      * Is null value allowed ?
326      *
327      * @return True if column may be null.
328      */
329     public boolean isNotNull()
330     {
331         return (notNull || isPrimaryKey());
332     }
333 
334     /***
335      * Is this column a foreign key?
336      *
337      * @return True if column is a foreign key.
338      */
339     public boolean isForeignKey()
340     {
341         return (relatedTableName != null && relatedTableName.length() > 0);
342     }
343 
344     /***
345      * Get the table.column that this column is related to.
346      *
347      * @return A String with the full name for the related column.
348      */
349     public String getRelatedName()
350     {
351         return relatedTableName + "." + relatedColumnName;
352     }
353 
354     /***
355      * Get the table name that this column is related to.
356      *
357      * @return A String with the name for the related table.
358      */
359     public String getRelatedTableName()
360     {
361         return relatedTableName;
362     }
363 
364     /***
365      * Get the column name that this column is related to.
366      *
367      * @return A String with the name for the related column.
368      */
369     public String getRelatedColumnName()
370     {
371         return relatedColumnName;
372     }
373 
374     /***
375      * Gets the scale set for this column (if any) as set in the XML database
376      * definition. E.g., the value of the scale attribute or the scale portion
377      * of a size="P,S" attribute. (Note: size="P,S" format is being
378      * deprecated!).
379      *
380      * @return Returns the scale.
381      */
382     public int getScale()
383     {
384         return scale;
385     }
386 
387     /***
388      * @param scale The scale to set.
389      */
390     public void setScale(int scale)
391     {
392         this.scale = scale;
393     }
394 
395     /***
396      * Gets the Java Name for this column as defined in XML or created by
397      * generator code.
398      *
399      * @return the Java Name.
400      */
401     public String getJavaName()
402     {
403         return this.javaName;
404     }
405 
406     /***
407      * Sets the Java Name for this column.
408      *
409      * @param name the Java Name.
410      */
411     public void setJavaName(String name)
412     {
413         this.javaName = name;
414     }
415 
416     /***
417      * Returns whether this column is an autoincrement column.
418      *
419      * @return true if this column is an autoIncrement column, false otherwise.
420      */
421     public boolean isAutoIncrement()
422     {
423         return autoIncrement;
424     }
425 
426     /***
427      * Sets whether this column is an autoincrement column.
428      *
429      * @param autoIncrement whether this colimn is an autoincrement column.
430      */
431     public void setAutoIncrement(boolean autoIncrement)
432     {
433         this.autoIncrement = autoIncrement;
434     }
435 
436     /***
437      * A string representing the default value defined for this column.
438      *
439      * @return The default value of this column, if any.
440      */
441     public String getDefault()
442     {
443         return defaultValue;
444     }
445 
446     /***
447      * Sets the default value for this column.
448      *
449      * @param defaultValue The defaultValue to set.
450      */
451     public void setDefault(String defaultValue)
452     {
453         this.defaultValue = defaultValue;
454     }
455 
456     /***
457      * Returns the column description info.
458      *
459      * @return the description, if any.
460      */
461     public String getDescription()
462     {
463         return description;
464     }
465 
466     /***
467      * Sets the description for this column.
468      *
469      * @param description The description to set.
470      */
471     public void setDescription(String description)
472     {
473         this.description = description;
474     }
475 
476     /***
477      * Get the inheritance information associated with this column,
478      *
479      * @return Returns an array of associated inheritanceMap.
480      *         The array is in XML order.
481      */
482     public InheritanceMap[] getInheritanceMaps()
483     {
484         InheritanceMap[] iMaps = new InheritanceMap[inheritanceMaps.size()];
485         synchronized (inheritanceMaps)
486         {
487             Iterator it = inheritanceMaps.values().iterator();
488             int i = 0;
489             while (it.hasNext())
490             {
491                 iMaps[i++] = (InheritanceMap) it.next();
492             }
493         }
494         return iMaps;
495     }
496 
497     /***
498      * Add an associated inheritance mapping.
499      *
500      * @param map The inheritanceMap to associate with this column.
501      */
502     public void addInheritanceMap(InheritanceMap map)
503     {
504         setUseInheritance(true);
505         this.inheritanceMaps.put(map.getKey(), map);
506     }
507 
508     /***
509      * Gets the inheritance type used.
510      *
511      * @return the inheritance type used.
512      */
513     public String getInheritance()
514     {
515         return inheritance;
516     }
517 
518     /***
519      * Sets the inheritance type.
520      *
521      * @param inheritanceType The inheritance type to set.
522      */
523     public void setInheritance(String inheritanceType)
524     {
525         this.inheritance = inheritanceType;
526     }
527 
528     /***
529      * Returns the input validator class name.
530      * (This property is in the DTD, but currently not used by Torque?)
531      *
532      * @return Returns the inputValidator.
533      */
534     public String getInputValidator()
535     {
536         return inputValidator;
537     }
538 
539     /***
540      * Sets the input validator class name.
541      *
542      * @param inputValidator The inputValidator to set.
543      */
544     public void setInputValidator(String inputValidator)
545     {
546         this.inputValidator = inputValidator;
547     }
548 
549     /***
550      * Returns whether getters and setters are generated with the
551      * access modifier "protected" rather than "public".
552      *
553      * @return whether the accessors should be protected rather than public.
554      */
555     public boolean isProtected()
556     {
557         return isProtected;
558     }
559 
560     /***
561      * Sets whether getters and setters should be generated with the
562      * access modifier "protected" rather than "public".
563      *
564      * @param isProtected whether getters and setters for this column
565      *        are protected.
566      */
567     public void setProtected(boolean isProtected)
568     {
569         this.isProtected = isProtected;
570     }
571 
572     /***
573      * Returns whether this column is a primary key.
574      *
575      * @return whether this column is a primary key.
576      */
577     public boolean isPk()
578     {
579         return pk;
580     }
581 
582     /***
583      * Sets whether this column is a primary key.
584      *
585      * @param pk whether this column is a primary key.
586      */
587     public void setPk(boolean pk)
588     {
589         this.pk = pk;
590     }
591 
592     /***
593      * Returns whether this column uses inheritance subclasses.
594      *
595      * @return true if inheritance subclasses are used, false otherwise.
596      */
597     public boolean isUseInheritance()
598     {
599         return useInheritance;
600     }
601 
602     /***
603      * Sets whether this column uses inheritance subclasses.
604      *
605      * @param useInheritance whether this column uses Inheritance subclasses.
606      */
607     public void setUseInheritance(boolean useInheritance)
608     {
609         this.useInheritance = useInheritance;
610     }
611 
612     /***
613      * Get the inheritance map with the specified key.
614      *
615      * @param key the key of the inheritance map.
616      * @return the inheritance map with the specified key, or null if no
617      *         inheritance map with the specified key exists in this column.
618      */
619     public InheritanceMap getInheritanceMap(String key)
620     {
621         return (InheritanceMap) inheritanceMaps.get(key);
622     }
623 
624     /***
625      * Returns whether this colum uses primitive values rather than objects.
626      *
627      * @return true if this colum uses primitive values, false if it uses
628      *         objects.
629      */
630     public boolean isUsePrimitive()
631     {
632         return usePrimitive;
633     }
634 
635     /***
636      * Sets whether this colum uses primitive values rather than objects.
637      *
638      * @param usePrimitive whether primitive objects are used
639      *        rather than objects.
640      */
641     public void setUsePrimitive(boolean usePrimitive)
642     {
643         this.usePrimitive = usePrimitive;
644     }
645 
646     /***
647      * Returns the Java naming method for this column.
648      *
649      * @return the javaNamingMethod for this column.
650      */
651     public String getJavaNamingMethod()
652     {
653         return javaNamingMethod;
654     }
655 
656     /***
657      * Sets the java naming method for this column.
658      *
659      * @param javaNamingMethod The javaNamingMethod to set.
660      */
661     public void setJavaNamingMethod(String javaNamingMethod)
662     {
663         this.javaNamingMethod = javaNamingMethod;
664     }
665 
666     /***
667      * Returns the map for the table this column belongs to.
668      *
669      * @return the table map for this column.
670      */
671     public TableMap getTable()
672     {
673         return table;
674     }
675 
676     /***
677      * Returns the position (one based) of this column in the table.
678      * XML order is preserved.
679      *
680      * @return The position of this column, one-based.
681      */
682     public int getPosition()
683     {
684         return position;
685     }
686 
687     /***
688      * Sets the position (one based) of this column in the table.
689      *
690      * @param position The position to set.
691      */
692     public void setPosition(int position)
693     {
694         this.position = position;
695     }
696 
697     /***
698      * Returns the java type of this column.
699      *
700      * @return the javaType.
701      */
702     public String getJavaType()
703     {
704         return javaType;
705     }
706 
707     /***
708      * Sets the java type of this column.
709      *
710      * @param javaType The javaType to set.
711      */
712     public void setJavaType(String javaType)
713     {
714         this.javaType = javaType;
715     }
716 }