View Javadoc

1   package pl.aislib.tools.mapping.db;
2   
3   import java.util.HashMap;
4   import java.util.Iterator;
5   
6   import org.jdom.Element;
7   
8     /***
9      * @author Micha? Ja?tak, AIS.PL
10     */
11  public class Table {
12  
13    private String tbCatalog;
14    private String tbName;
15    private String tbRemarks;
16    private String tbSchema;
17    private String tbType;
18    
19    private HashMap columns;
20    private HashMap primaryKeys;
21    private HashMap foreignKeys;
22    private HashMap indexes;
23    
24    /***
25     *
26     */
27    public Table(String tName) {
28      columns     = new HashMap ();
29      primaryKeys = new HashMap ();
30      foreignKeys = new HashMap ();
31      indexes     = new HashMap ();
32      tbName      = tName;
33    }
34    
35    /***
36     *
37     */
38    public void addColumn(Column column) {
39      columns.put (column.getName (), column);
40    }
41    
42    /***
43     *
44     */
45    public Column getColumn(String columnName) {
46      return (Column) columns.get (columnName);
47    }
48    
49    /***
50     *
51     */
52    public void addPrimaryKey(PrimaryKey primaryKey) {
53      primaryKeys.put (primaryKey.getColumnName (), primaryKey);
54    }
55  
56    /***
57     *
58     */
59    public void addForeignKey(ForeignKey foreignKey) {
60      foreignKeys.put (foreignKey.getSourceColumnName (), foreignKey);
61    }
62  
63    /***
64     *
65     */
66    public void addIndex(Index index) {
67      indexes.put ("" + index.getOrdinalPosition (), index);
68    }
69  
70    /***
71     *
72     */
73    public String getName() {
74      return ((tbName != null) ? new String (tbName) : tbName);
75    }
76  
77    /***
78     *
79     */
80    public String getType() {
81      return ((tbType != null) ? new String (tbType) : tbType);
82    }
83    
84    /***
85     *
86     */
87    public void setCatalog(String tCatalog) {
88      tbCatalog = tCatalog;
89    }
90  
91    /***
92     *
93     */
94    public void setSchema(String tSchema) {
95      tbSchema = tSchema;
96    }
97  
98    /***
99     *
100    */
101   public void setType(String tType) {
102     tbType = tType;
103   }
104 
105   /***
106    *
107    */
108   private String firstCharToUpper(String source) {
109     return source.substring (0, 1).toUpperCase ().concat (source.substring (1));
110   }
111 
112   /***
113    *
114    */
115   private String dropUnderscores(String name) {
116 
117     String       source = new String (name);
118     StringBuffer result = new StringBuffer ();
119     
120     int          idx   = 0;
121     int          vSize = source.length ();
122     
123     while (idx < vSize) {
124       int spaceIdx = source.indexOf ('_', idx);
125       if (spaceIdx >= 0) { result.append (firstCharToUpper (source.substring (idx, spaceIdx))); }
126       else {
127         result.append (firstCharToUpper (source.substring (idx)));
128         spaceIdx = vSize;
129       }
130       idx = spaceIdx + 1;
131     }
132     return new String (result);
133   }
134   
135   /***
136    *
137    */
138   private String canonizeFieldName(String name) {
139     String result = dropUnderscores (name);
140     return result.substring(0,1).toLowerCase().concat (result.substring(1));
141   }
142   
143   /***
144    *
145    */
146   private String canonizeClassName(String name) { return dropUnderscores (name); }
147 
148   /***
149    * FIXME
150    */
151   public String toString() {
152     return new String ("Table: " + tbName + "(" + tbCatalog + ", " + tbSchema 
153                       + ", " + tbType + ")\n" + columns);
154   }
155 
156   /***
157    *
158    */
159   public Element toXML() {
160     Element result = new Element ("table");
161     result.setAttribute ("name", tbName);
162     result.setAttribute ("java-name", canonizeClassName (tbName.toLowerCase ()));
163     if (tbCatalog != null) { result.setAttribute ("catalog", tbCatalog); }
164     if (tbSchema != null)  { result.setAttribute ("schema", tbSchema); }
165     if (tbType != null)    { result.setAttribute ("type", tbType); }
166   
167     if (!columns.isEmpty ()) {
168       for (Iterator it = columns.keySet ().iterator ();  it.hasNext (); ) {
169         String columnName = (String) it.next ();
170         Column column = (Column) columns.get (columnName);
171         result.addContent (column.toXML ());
172       }
173     }
174   
175     if (!indexes.isEmpty ()) {
176       for (Iterator it = indexes.keySet ().iterator ();  it.hasNext (); ) {
177         String idxName = (String) it.next ();
178         Index index = (Index) indexes.get (idxName);
179         result.addContent (index.toXML ());
180       }
181     }
182   
183     if (!primaryKeys.isEmpty ()) {
184       for (Iterator it = primaryKeys.keySet ().iterator ();  it.hasNext (); ) {
185         String keyName = (String) it.next ();
186         PrimaryKey pKey = (PrimaryKey) primaryKeys.get (keyName);
187         result.addContent (pKey.toXML ());
188       }
189     }
190   
191     if (!foreignKeys.isEmpty ()) {
192       for (Iterator it = foreignKeys.keySet ().iterator ();  it.hasNext (); ) {
193         String keyName = (String) it.next ();
194         ForeignKey fKey = (ForeignKey) foreignKeys.get (keyName);
195         result.addContent (fKey.toXML ());
196       }
197     }
198   
199     return result;  
200   }
201 
202 } // class