KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > mapping > BaseTableMappingImpl


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mapper.mapping;
24
25 import java.util.*;
26
27 import org.xml.sax.SAXException JavaDoc;
28 import org.xquark.mapper.dbms.AbstractConnection;
29 import org.xquark.mapper.metadata.RepositoryConstants;
30 import org.xquark.schema.SchemaComponent;
31 import org.xquark.schema.SchemaConstants;
32
33 /**
34  * Implementation of the TableMapping interface for 'unmapped' elements or attributes.
35  *
36  * Elements or attributes are mapped to the default XML data table.
37  *
38  */

39 public abstract class BaseTableMappingImpl extends MappingImpl
40 implements TableMapping, MappingConstants, RepositoryConstants, SchemaConstants
41 {
42     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
43     private static final String JavaDoc RCSName = "$Name: $";
44     
45     // primary key or equivalent used for making join
46
protected ColumnMapping[] primaryKeyColumnMappings;
47     
48     /** Number of columns that make up the primary key and are used for the join
49      * between the user and the OID table.
50      * Key generators during construction then PK if any and finally key
51      * generators else.
52      */

53     protected int keyColumnCount = 0;
54     
55     /* List of ColumnMapping of this TableMapping */
56     protected ColumnMapping[] columnMappings;
57     
58     /** List of ColumnMapping that must be initialized before reading the
59      * XML data (startElement()).
60      */

61     protected List initialValues;
62     /** List of ColumnMapping that must be initialized after reading the
63      * XML data (endElement()) or whent the tuple is supposed to be completed (retrieve then the context...).
64      */

65     protected List finalValues;
66     
67     /* Name of the relational table */
68     protected String JavaDoc name;
69     /* Object encapsulating the TableMappings for a repository */
70     protected RepositoryMapping colMapping;
71     protected int index;
72     
73     /* Object containing table metadata, ie. properties */
74     protected TableMetaData tableMetaData;
75     
76     protected Collection dependencies = null;
77     
78     /* String for JDBC PreparedStatements used to create or destroy the user table */
79     /* String for JDBC PreparedStatements used to access the user table */
80     protected String JavaDoc insertStatement;
81     // make a natural join between OID & values tables
82
protected String JavaDoc joinCondition = "";
83     protected String JavaDoc columnList = "";
84     protected String JavaDoc namedColumnList = "";
85     protected String JavaDoc wildcards = "";
86     protected String JavaDoc joinColumnList = "";
87     protected String JavaDoc joinWildcards = "";
88     protected String JavaDoc joinColumnTypes = "";
89     
90     /* Statement column lists */
91     protected ColumnMapping[] updateColumnMappings;
92     protected ColumnMapping[] fetchColumnMappings;
93     protected ColumnMapping[] selectColumnMappings;
94     protected ColumnMapping[] OIDColumnMappings;
95     
96     /** Constructor.
97      * @param colMapping the RepositoryMapping object to which this object belongs.
98      * @param comp the SchemaComponent associated with this "mapping component".
99      * @param name relational table name.
100      * @param conn a connection allowing access to the metadata for the table retrieved from JDBC
101      * @param action the action to perform on the rows of the table (check, insert, update, ...)
102      * @param generate If true, mapping is loaded even if relational tables
103      * are not present : mapping is loaded for table generation.
104      */

105     protected BaseTableMappingImpl(RepositoryMapping colMapping, SchemaComponent comp, AbstractConnection conn) throws SAXException JavaDoc
106     {
107         super(comp);
108         this.colMapping = colMapping;
109         initialValues = new ArrayList(5);
110         finalValues = new ArrayList(3);
111     }
112     
113     /**
114      * Accessors.
115      */

116     
117     public String JavaDoc toString()
118     {
119         return name;
120 /* String result = "#<TableMapping "+name+"\n";
121         result+=" "+getSchemaComponent()+"\n";
122         Iterator it = columnMappings.iterator();
123         while (it.hasNext()) result+=" "+it.next()+"\n";
124         // if (dependencies != null) {
125         // it = dependencies.iterator();
126         // while (it.hasNext()) result+=" "+((TableMappingImpl)it.next()).getTableName()+"\n";
127         // }
128         //it = mappedElements.iterator();
129         //while (it.hasNext()) result+=" "+it.next()+"\n";
130         return result+">";
131  */

132     }
133     
134     /** Overriden because default mapping uses views on real table mapping.
135      * As query reconstruction use equality, we use what realy identifies the
136      * table mapping: its index.
137      */

138     public boolean equals(Object JavaDoc o)
139     {
140         boolean ret = false;
141         if (o instanceof TableMapping)
142             ret = (((TableMapping)o).getTableIndex() == getTableIndex());
143         return ret;
144     }
145     /** Overriden because default mapping uses views on real table mapping.
146      * (As query reconstruction use a map).
147      */

148     public int hashCode()
149     {
150         return getTableName().hashCode();
151     }
152     
153     
154     // public abstract int getBatchSize();
155

156     public int getColumnCount()
157     {
158         return columnMappings.length;
159     }
160
161 // public ColumnMapping[] getColumnMappings();
162
protected Iterator getColumnMappingIterator()
163     {
164         return new Iterator()
165         {
166             private int next = 0;
167             public boolean hasNext()
168             {
169                 while (next < columnMappings.length && columnMappings[next] == null)
170                     next++;
171                 return next < columnMappings.length && columnMappings[next] != null;
172             }
173
174             public Object JavaDoc next()
175             {
176                 ColumnMapping ret = null;
177                 if (hasNext())
178                     ret = columnMappings[next++];
179                 return ret;
180             }
181
182             public void remove()
183             {
184                 throw new UnsupportedOperationException JavaDoc();
185             }
186         };
187     }
188
189     public ColumnMapping getColumnMapping(String JavaDoc columnName)
190     {
191         Iterator it = getColumnMappingIterator();
192         ColumnMappingImpl column;
193         while (it.hasNext())
194         {
195             column = (ColumnMappingImpl)it.next();
196             if (column.getColumnName().equalsIgnoreCase(columnName)) return column; // SR 18/12/2000 Oracle metadata in uppercase : TO IMPROVE
197
}
198         return null;
199     }
200     
201     public ColumnMapping getColumnMapping(int index)
202     {
203         return columnMappings[index];
204     }
205     
206     public ColumnMapping[] getPrimaryKey()
207     {
208         return this.primaryKeyColumnMappings;
209     }
210     
211     public int getKeyColumnCount()
212     { return keyColumnCount;}
213     
214     public List initParameters()
215     { return initialValues;}
216     
217     public List finalParameters()
218     { return finalValues;}
219     
220     public String JavaDoc getTableName()
221     {
222         return name;
223     }
224     
225     public RepositoryMapping getRepositoryMapping()
226     {
227         return colMapping;
228     }
229     
230     public TableMetaData getMetaData()
231     {
232         return tableMetaData;
233     }
234     
235     public int getTableIndex()
236     {
237         return index;
238     }
239     
240     public void setIndex(int index)
241     {
242         this.index = index;
243     }
244     
245     /**
246      * Returns the name of the column containing the path OID (both in clustered
247      * and not clustered case).
248      */

249     public String JavaDoc getPathIDColumnName()
250     {
251         return "path"; // TO IMPROVE : should not be hardcoded
252
}
253     
254     /**
255      * Returns the name of the column containing the node UOID (both in clustered
256      * and not clustered case).
257      */

258     public String JavaDoc getUOIDColumnName()
259     {
260         return "uoid"; // TO IMPROVE : should not be hardcoded
261
}
262     
263     public String JavaDoc getJoinCondition()
264     {
265         return joinCondition;
266     }
267     public String JavaDoc getColumnList()
268     {
269         return columnList;
270     }
271     public String JavaDoc getNamedColumnList()
272     {
273         return namedColumnList;
274     }
275     public String JavaDoc getWildcards()
276     {
277         return wildcards;
278     }
279     public String JavaDoc getJoinColumnList()
280     {
281         return joinColumnList;
282     }
283     public String JavaDoc getJoinWildcards()
284     {
285         return joinWildcards;
286     }
287     public String JavaDoc getJoinColumnTypes()
288     {
289         return joinColumnTypes;
290     }
291
292     public Collection getDependencies()
293     {
294         return dependencies;
295     }
296     
297     public ColumnMapping[] getUpdateColumns()
298     {
299         return updateColumnMappings;
300     }
301     
302     public ColumnMapping[] getFetchColumns()
303     {
304         return fetchColumnMappings;
305     }
306
307     public ColumnMapping[] getJoinColumns()
308     {
309         return OIDColumnMappings;
310     }
311     
312     public ColumnMapping[] getSelectColumns()
313     {
314         return selectColumnMappings;
315     }
316     
317     public boolean isShareable()
318     {
319         return tableMetaData.areStatementsShareable();
320     }
321     
322     public boolean isTableShared()
323     {
324         return tableMetaData.isShared();
325     }
326
327     ////////////////////////////////////////////////////////////////////////////
328
// PACKAGE PRIVATE
329
////////////////////////////////////////////////////////////////////////////
330
/**
331      * Add a ColumnMapping object to the ones that must be initialized before
332      * reading the XML data.
333      * @param mapping the ColumnMapping object that can be initialized.
334      */

335     void addToInitialValues(ColumnMapping mapping)
336     {
337         initialValues.add(mapping);
338     }
339
340     /**
341      * Add a ColumnMapping object to the ones that must be initialized after
342      * reading the XML data, just before leaving the element.
343      * @param mapping the ColumnMapping object that can be initialized.
344      */

345     void addToFinalValues(ColumnMapping mapping)
346     {
347         finalValues.add(mapping);
348     }
349
350     void setStatementsNotShareable()
351     {
352         tableMetaData.setStatementsNotShareable();
353     }
354
355     void setTableShared()
356     {
357         tableMetaData.setShared();
358     }
359
360     ////////////////////////////////////////////////////////////////////////////
361
// MappingInfo IMPLEMENTATION
362
////////////////////////////////////////////////////////////////////////////
363

364     public String JavaDoc getInsertStatement()
365     {
366         return insertStatement;
367     }
368     
369     public TableMapping getTableMapping()
370     {
371         return this;
372     }
373 }
374
Popular Tags