KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents;
2
3 import com.daffodilwoods.database.general.*;
4 import java.util.*;
5 import com.daffodilwoods.database.resource.DException;
6 import com.daffodilwoods.daffodildb.server.sql99.common.*;
7 import com.daffodilwoods.database.utility.P;
8
9 /**
10  * This class helps in maintaining a mapping of columns and its value for single
11  * table. It is needed for insertion through resultset of select query.
12  * <p>Title: </p>
13  * <p>Description: </p>
14  * <p>Copyright: Copyright (c) 2003</p>
15  * <p>Company: </p>
16  * @author unascribed
17  * @version 1.0
18  */

19
20 public class ColumnMapping {
21
22    /**
23     * It represents the column
24     */

25
26    ColumnDetails columnDetails;
27
28    /**
29     * It represents the table to whom the column belongs.
30     */

31
32    TableDetails td;
33
34    /**
35     * It is used to store all values of columns represented by this class.
36     */

37
38    ArrayList values;
39
40    /**
41     * It is used to store all the columns represented by this class.
42     */

43
44    ArrayList columns;
45    int hasRecord = -1;
46
47    /**
48     * It is used to store the hasRecord columns.
49     */

50
51    ArrayList hasRecordColumns;
52
53    /**
54     * It is used to store the values of hasRecord columns.
55     */

56
57    ArrayList hasRecordValues ;
58
59    public ColumnMapping( ColumnDetails cd , Object JavaDoc value ) throws DException {
60       columnDetails = cd;
61       td = cd.getTable();
62       values = new ArrayList(1);
63       columns = new ArrayList(1);
64       hasRecordColumns = new ArrayList();
65       hasRecordValues = new ArrayList();
66       columns.add( cd );
67       values.add( value);
68    }
69
70    /**
71     * Returns the table to whom the column belongs.
72     * @return
73     * @throws DException
74     */

75
76    public TableDetails getTableDetails() throws DException {
77       return td;
78    }
79
80    /**
81     * This is required to add column and value pair in the mapping denoted by
82     * this class.
83     * @param cd
84     * @param value
85     * @throws DException
86     */

87
88    public void addColumnsAndValues( ColumnDetails cd , Object JavaDoc value) throws DException {
89       columns.add( cd );
90       values.add( value );
91    }
92
93    /**
94     * Returns the index of column for the table.
95     * @return
96     * @throws DException
97     */

98
99    public int getTableColumnIndex() throws DException{
100       return columnDetails.getIndex();
101    }
102
103    /**
104     * Returns the value at first index.
105     * @return
106     * @throws DException
107     */

108
109    public Object JavaDoc getValue() throws DException {
110       return values.get(0);
111    }
112
113    /**
114     * This method is required to return the index of the passed column. It is
115     * needed in the case when insertion through resultset is performed.
116     * @param column
117     * @return index of passed column
118     */

119
120    public int getIndexOfColumn( String JavaDoc column ) {
121       int len = columns.size();
122       for( int i=0; i<len; i++ ){
123          if( column.equalsIgnoreCase( ((ColumnDetails)columns.get(i)).getColumn() ) )
124             return i;
125       }
126       return -1;
127    }
128
129    /**
130     * This method is required to return all the indexes of the columns denoted
131     * by this class. It is needed in the case when insertion through resultset
132     * is performed.
133     * @param column
134     * @return index of passed column
135     */

136
137    public int[] getIndexes() throws DException {
138       if( columns == null )
139          return null;
140       int len = columns.size();
141       int[] toRet = new int[len];
142       for( int i=0; i<len; i++ ){
143          ColumnDetails cd = (ColumnDetails)columns.get(i) ;
144          toRet[i] = cd.getType() == cd.HAS_RECORD ? -1 :
145                     cd.getTable().getColumnCharacteristics().getColumnIndex(cd.getColumn());
146       }
147       return toRet;
148    }
149
150    /**
151     * Returns all the values.
152     * @return
153     * @throws DException
154     */

155
156    public Object JavaDoc[] getValues() throws DException {
157       return values == null ? null : values.toArray();
158    }
159
160    /**
161     * Sets the values by clearing previous list of values.
162     * @param vals
163     * @throws DException
164     */

165
166    public void setValues(Object JavaDoc[] vals) throws DException {
167       if(values == null)
168          values = new ArrayList();
169       for(int i = 0 ; i < vals.length ; i++)
170          values.set(i,vals[i]);
171    }
172
173    /**
174     * Returns the value at particular index.
175     * @param index
176     * @return
177     * @throws DException
178     */

179
180    public Object JavaDoc getValueAtIndex( int index ) throws DException {
181       return values.get(index);
182    }
183
184    /**
185     * Returns the original column.
186     * @return
187     * @throws DException
188     */

189
190    public ColumnDetails getOrignalColumnDetail() throws DException {
191       return columnDetails;
192    }
193
194    public void show() throws DException {
195    }
196
197    /**
198     * This method is needed in the case when hasRecord column is present in the
199     * select query and insertion is performed on the resultset of that select
200     * query.
201     * @throws DException
202     */

203
204    public void applyColumnHasRecordCHANGES() throws DException {
205       boolean toRet = false;
206       int len = columns.size();
207       if( len > 1 ){
208          for( int k=0; k<columns.size(); k++ ){
209             ColumnDetails cd = (ColumnDetails)columns.get(k);
210             if( cd.getType() == ColumnDetails.HAS_RECORD ){
211                if( values.get(k) == Boolean.FALSE )
212                   throw new DException("DSE1306",null);
213                hasRecordColumns.add(columns.remove(k));
214                hasRecordValues.add(values.remove(k));
215                k--;
216             }
217          }
218       }
219       else{
220          if( columnDetails.getType() == ColumnDetails.HAS_RECORD ){
221             hasRecord = Boolean.TRUE.equals(values.get(0)) ? 1 : 0;
222             hasRecordColumns.add( columns.remove(0) );
223             hasRecordValues.add( values.remove(0) );
224          }
225       }
226    }
227
228    /**
229     * Used to check whether hasRecord column is present or not.
230     * @return
231     */

232
233    public int isHasRecord_False() {
234       return hasRecord;
235    }
236
237    /**
238     * Returns all the columns of hasRecord.
239     * @return
240     */

241
242    public ColumnDetails[] getHasRecordColumnDetails() {
243       return hasRecordColumns == null ? null : (ColumnDetails[]) hasRecordColumns.toArray( new ColumnDetails[0] );
244    }
245
246    /**
247     * Returns all the values of hasRecord columns
248     * @return
249     */

250
251    public Object JavaDoc[] getHasRecordColumnValues() {
252       return hasRecordValues == null ? null : hasRecordValues.toArray();
253    }
254
255    /**
256     * Returns the column at particular index.
257     * @param index
258     * @return
259     * @throws DException
260     */

261
262    public ColumnDetails getColumnDetailsAt( int index ) throws DException {
263       return (ColumnDetails)columns.get(index);
264    }
265
266 }
267
Popular Tags