KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > listenerevents > ColumnMappingHandler


1 package com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents;
2
3 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.ColumnMapping;
4
5 import java.util.*;
6 import com.daffodilwoods.database.resource.DException;
7 import com.daffodilwoods.database.utility.P;
8 import com.daffodilwoods.database.general.QualifiedIdentifier;
9 import com.daffodilwoods.daffodildb.server.sql99.common.TableDetails;
10
11 /**
12  * This class is helpful in insertion, updation and deletion on the updatable
13  * resultset. It represents table and columnvalue Mapping.
14  * <p>Title: </p>
15  * <p>Description: </p>
16  * <p>Copyright: Copyright (c) 2003</p>
17  * <p>Company: </p>
18  * @author unascribed
19  * @version 1.0
20  */

21
22 public class ColumnMappingHandler {
23
24    /**
25     * It represents the tables on which insertion has to perform.
26     */

27
28    ArrayList tablesForInsertion ;
29
30    /**
31     * It represents the tables on which deletion has to perform.
32     */

33
34    ArrayList tablesForDeletion ;
35
36    /**
37     * It contains table as key and ColumnMapping(column and value) as value.
38     */

39
40    HashMap tableColumnMapping;
41
42    public ColumnMappingHandler() {
43       tablesForInsertion = new ArrayList();
44       tableColumnMapping = new HashMap();
45       tablesForDeletion = new ArrayList();
46    }
47
48    /**
49     * This method is required to add the tables on which insertion will take
50     * place.
51     * @param obj
52     * @throws DException
53     */

54
55    public void addTableForInsertion( TableDetails obj ) throws DException {
56       if( obj != null && !tablesForInsertion.contains(obj) ){
57          tablesForInsertion.add(obj);
58       }
59    }
60
61    /**
62     * This method is required to add the tables on which deletion will take
63     * place.
64     * @param obj
65     * @throws DException
66     */

67
68    public void addTableForDeletion( TableDetails[] obj ) {
69       for (int i = 0; i < obj.length; i++) {
70          addTablesForDeletion( obj[i] );
71       }
72    }
73
74    /**
75     * This method is required to add the table on which deletion will take
76     * place.
77     * @param obj
78     * @throws DException
79     */

80
81    public void addTablesForDeletion( TableDetails td ) {
82       if( !tablesForDeletion.contains(td) )
83          tablesForDeletion.add(td);
84    }
85
86    /**
87     * This method adds the entries in tableColumnMapping.
88     * @param columnMappings
89     * @throws DException
90     */

91
92    public void setTableColumnMapping( ColumnMapping[] columnMappings ) throws DException {
93       int len = columnMappings.length;
94       for( int i=0; i<len; i++ )
95          addColumnMapping( columnMappings[i] );
96
97    }
98
99    public void addColumnMapping( ColumnMapping cm ) throws DException {
100       tableColumnMapping.put( cm.getTableDetails(),cm );
101    }
102
103    /**
104     * This method is used to check whether passed tables are already present in
105     * the map.
106     * @param tables
107     * @usage qualifiedjoin
108     * @return
109     * @throws DException
110     */

111
112    public boolean isTableInUserTableMapping( TableDetails[] tables ) throws DException {
113       int len = tables.length;
114       for( int i=0; i<len; i++ )
115          if( tableColumnMapping.containsKey(tables[i]) )
116             return true;
117       return false;
118    }
119
120    public boolean tableAlreadyInsertedForInsert( TableDetails tableName ) throws DException {
121       return tablesForInsertion.contains(tableName);
122    }
123
124    /**
125     * This method is used to get the tables in which insertion has to perform.
126     * These tables included the minimum base tables on which insertion will
127     * definitely performed as well as the tables which are the resultant of
128     * select query based on the minimum base table and condition of select query.
129     * The minimum base tables for Inner join, cross joins are all tables and for
130     * left join, tables of left side and for right join, tables of right side.
131     * @param tableKeyMapping
132     * @return
133     * @throws DException
134     */

135
136    public TableDetails[] getTablesForInsertProcess( HashMap tableKeyMapping ) throws DException {
137       int len = tablesForInsertion.size();
138       ArrayList toReturn = new ArrayList();
139       boolean inserted = false;
140
141       if( len > 0 )
142          for( int i=0; i<len; i++ )
143             if( !tableKeyMapping.containsKey( tablesForInsertion.get(i) ) ){
144       inserted = true;
145       toReturn.add( tablesForInsertion.get(i) );
146             }
147
148             Iterator iterator = tableColumnMapping.keySet().iterator();
149             while( iterator.hasNext() ){
150                TableDetails td = (TableDetails)iterator.next();
151                if( !tableKeyMapping.containsKey( td ) ){
152                   ColumnMapping cmp = (ColumnMapping) tableColumnMapping.get(td) ;
153                   if( cmp.isHasRecord_False()!= 0 && cmp.isHasRecord_False()!= 1 && !toReturn.contains(td)){
154                      inserted = true;
155                      toReturn.add( td );
156                   }
157                }
158             }
159             if(inserted)
160                return (TableDetails[]) toReturn.toArray( new TableDetails[0] );
161
162             return null;
163
164    }
165
166    /**
167     * This method is used to check whether column of the table is present in
168     * mapping or not.
169     * @param table
170     * @param columnName
171     * @usage VariableValueOperation
172     * @return index of passed column in mapping
173     * @throws DException
174     */

175
176    public int containsTableAndColumnInMapping( TableDetails table , String JavaDoc columnName ) throws DException {
177       if( tableColumnMapping.containsKey( table ) ){
178          ColumnMapping cm = (ColumnMapping) tableColumnMapping.get(table);
179          return cm.getIndexOfColumn(columnName);
180       }
181       return -1;
182    }
183
184    /**
185     * This method is called after containsTableAndColumnInMapping which returns
186     * the index and that index and table is passed to this method to retrieve
187     * the value.
188     * @param table
189     * @param index
190     * @return value at particular index
191     * @throws DException
192     */

193
194    public Object JavaDoc getValueAtIndex( TableDetails table , int index ) throws DException {
195       ColumnMapping cm = (ColumnMapping) tableColumnMapping.get(table);
196       return cm.getValueAtIndex(index);
197    }
198
199    /**
200     * This method is used for insertion,updation to find how many tables are
201     * candidates for modifications.
202     * @return length of tables
203     * @usage UpdatableSelectIterator
204     * @throws DException
205     */

206
207    public int getLengthOfTableColumnValues() throws DException {
208       return tableColumnMapping.size();
209    }
210
211    /**
212     * This method is used for insertion,updation to find the tables on which
213     * modifications has to perform.
214     * @return Iterator on tables
215     * @throws DException
216     */

217
218    public Iterator getIteratorOnTableKeys( ) throws DException {
219       return tableColumnMapping.keySet().iterator();
220    }
221
222    public ColumnMapping getColumnMappingForTableDetail( TableDetails td ) throws DException {
223       return (ColumnMapping)tableColumnMapping.get( td );
224    }
225
226    public ColumnMapping[] getColumnMappings() throws DException {
227       return (ColumnMapping[]) tableColumnMapping.values().toArray( new ColumnMapping[0]);
228    }
229
230    /**
231     * This method is required to get the tables on which insertion will take
232     * place.
233     * @return tables in which insertion will take place.
234     * @throws DException
235     */

236
237    public TableDetails[] getTablesForInsert() throws DException {
238       return (TableDetails[]) tablesForInsertion.toArray( new TableDetails[0] );
239    }
240
241    /**
242     * This method is required to get the tables on which deletion will take
243     * place.
244     * @return tables in which deletion will take place.
245     * @throws DException
246     */

247
248    public TableDetails[] getTablesForDelete() throws DException {
249       return (TableDetails[]) tablesForDeletion.toArray( new TableDetails[0] );
250    }
251
252    private void addUserTables( ArrayList arr , HashMap tableKeyMapping ) throws DException {
253    }
254
255    public int isHasRecord_False( TableDetails tdd ) throws DException {
256       ColumnMapping cm = (ColumnMapping)tableColumnMapping.get( tdd );
257       return cm==null ? -1 : cm.hasRecord;
258    }
259
260    private void show() {
261       Iterator iter = tableColumnMapping.keySet().iterator();
262       while( iter.hasNext() ){
263          Object JavaDoc oo = iter.next();
264       }
265    }
266
267    public ColumnMapping getColumnMappingForTableDetails( TableDetails td ) throws DException {
268       return (ColumnMapping) tableColumnMapping.get(td);
269    }
270
271    public void showForTablesToBEInserted() {
272       int len = tablesForInsertion.size();
273       if( len == 0 )
274          ;//// Removed By Program ** System.out.println(" NO TABLE FOR WHICH INSERTION IS TO BE MADE ");
275
for( int i=0; i<len; i++ )
276          ;//// Removed By Program ** System.out.println(" ================== " + tablesForInsertion.get(i) );
277
}
278 }
279
Popular Tags