summaryrefslogtreecommitdiffstats
path: root/das-java/branches/das-java-beta2/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultMetadata.java
blob: e94f3e3b3bfdcc216ac316a1a9a2fc2c4ebb7395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.tuscany.das.rdb.graphbuilder.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.tuscany.das.rdb.Converter;
import org.apache.tuscany.das.rdb.config.Column;
import org.apache.tuscany.das.rdb.config.Config;
import org.apache.tuscany.das.rdb.config.Table;
import org.apache.tuscany.das.rdb.config.wrapper.MappingWrapper;
import org.apache.tuscany.das.rdb.config.wrapper.TableWrapper;
import org.apache.tuscany.das.rdb.impl.ResultSetShape;

import commonj.sdo.Type;

public class ResultMetadata {

    private Map tableToPropertyMap = new HashMap();

    private List typeNames = new ArrayList();

    private List propertyNames = new ArrayList();

    private final ResultSet resultSet;

    private final ResultSetShape resultSetShape;

    private final MappingWrapper configWrapper;

    private Converter[] converters;

    private Map tableToPrimaryKeysMap = new HashMap();
    
    //JIRA-952
    public ResultMetadata(ResultSet rs, MappingWrapper cfgWrapper, ResultSetShape shape) throws SQLException {

        this.resultSet = rs;
        this.configWrapper = cfgWrapper;

        if (shape == null) {
            this.resultSetShape = new ResultSetShape(rs.getMetaData(), configWrapper.getConfig());
        } else {
            this.resultSetShape = shape;
        }

        this.converters = new Converter[resultSetShape.getColumnCount()];

        Map impliedRelationships = new HashMap();
        String schemaName = "";
        String idSpell = null;
        for (int i = 1; i <= resultSetShape.getColumnCount(); i++) {
            String tableName = resultSetShape.getTableName(i);
             schemaName = resultSetShape.getSchemaName(i);
            if (( tableName == null ) || ( tableName.equals(""))) {
                throw new RuntimeException("Unable to obtain table information from JDBC. DAS configuration must specify ResultDescriptors");
            }
            String typeName = null;
            
            if(this.configWrapper.getConfig().isDatabaseSchemaNameSupported()){
            	typeName = configWrapper.getTableTypeName(schemaName+"."+tableName);
            }
            else{
            	typeName = configWrapper.getTableTypeName(tableName);	
            }
            String columnName = resultSetShape.getColumnName(i);

            String colName = "";
            if (columnName.regionMatches(true, columnName.length()-3, "_ID", 0, 3)) {
            	idSpell = columnName.substring(columnName.length()-3, columnName.length());            	
            	if(this.configWrapper.getConfig().isDatabaseSchemaNameSupported()){
            		colName = schemaName+"."+columnName;
            		impliedRelationships.put(colName, schemaName+"."+tableName);
            	}
            	else{
            		colName = columnName;
            		impliedRelationships.put(colName, tableName);	
            	}
            } else if (columnName.equalsIgnoreCase("ID")) {
                configWrapper.addImpliedPrimaryKey(schemaName, tableName, columnName);
            }

            String propertyName = null;
            
            if(this.configWrapper.getConfig().isDatabaseSchemaNameSupported()){
            	propertyName = configWrapper.getColumnPropertyName(schemaName+"."+tableName, columnName);	
            }
            else{
            	propertyName = configWrapper.getColumnPropertyName(tableName, columnName);
            }
            String converterName = null;
            
            if(this.configWrapper.getConfig().isDatabaseSchemaNameSupported()){
            	converterName = configWrapper.getConverter(schemaName+"."+tableName, resultSetShape.getColumnName(i));
            }
            else{
            	converterName = configWrapper.getConverter(tableName, resultSetShape.getColumnName(i));	
            }

            converters[i - 1] = loadConverter(converterName);

            typeNames.add(typeName);
            propertyNames.add(propertyName);

            Collection properties = (Collection) tableToPropertyMap.get(typeName);
            if (properties == null) {
                properties = new ArrayList();
            }
            properties.add(propertyName);
            tableToPropertyMap.put(typeName, properties);
            
        }
        
        //System.out.println("tableToPropertyMap "+tableToPropertyMap);
        fillTableToPrimaryKeysMap();
        
        Iterator i = impliedRelationships.keySet().iterator();
        while (i.hasNext()) {
            String columnName = (String) i.next();
            String pkTableName = columnName.substring(0, columnName.indexOf(idSpell));//_id, _Id, _iD, _ID anything
            String fkTableName = (String) impliedRelationships.get(columnName);
            List pkTableProperties = (List) tableToPropertyMap.get(pkTableName);
            if ((pkTableProperties != null) && (pkTableProperties.contains("ID"))) {
                configWrapper.addImpliedRelationship(pkTableName, fkTableName, columnName);
            }
        }
        // Add any tables defined in the model but not included in the ResultSet
        // to the list of propertyNames
        Config model = configWrapper.getConfig();
        if (model != null) {
            Iterator tablesFromModel = model.getTable().iterator();
            while (tablesFromModel.hasNext()) {
                TableWrapper t = new TableWrapper((Table) tablesFromModel.next());
                if (tableToPropertyMap.get(t.getTypeName()) == null) {
                    tableToPropertyMap.put(t.getTypeName(), Collections.EMPTY_LIST);
                }
            }
        }
    }

    //Now fill tableToPrimaryKeysMap.Complete for whichever tables are there in tableToPrimaryKeysMap,
    //Also case of implied PK and it is not there in SELECT, provide way to still fill it in
    //tableToPrimaryKeysMap - the column should be present in Config (though not defed as PK)
    //And consider the classic case, when we assume all columns to be PKs - when no info
    //in config for table or "all columns"
    private void fillTableToPrimaryKeysMap(){
	    Iterator itr = tableToPropertyMap.keySet().iterator();
	    while(itr.hasNext()){
	    	String curTableName = (String)itr.next();
	    	boolean treatAllPKs = false;//flag for, when all cols need to be treated as PKs
	    	
	    	if(tableToPrimaryKeysMap.containsKey(curTableName)){
	    		continue;//don't keep refilling same hashset for each ResultMetadata constructor,
	    	}
	    	
	    	List columnsForTable = null;
	    	if(configWrapper.getTableByTypeName(curTableName) != null) {
	    		 columnsForTable = configWrapper.getTableByTypeName(curTableName).getColumn();        		 
	    	}
	    	else if(configWrapper.getTable(curTableName) != null){
			 columnsForTable = configWrapper.getTable(curTableName).getColumn();
			 configWrapper.getTable(curTableName).setTypeName(curTableName);//keep configWrapper consistent with Type info
	    	}
	    	else{
	    		treatAllPKs = true;//can not find table/type, need to consider all columns as PKs
	    	}
	    	
	    	if(columnsForTable != null){
	            for(int ii=0; ii<columnsForTable.size(); ii++){
	            	Column curCol = (Column)columnsForTable.get(ii);
	            	
	            	if(curCol.isPrimaryKey() || curCol.getColumnName().equalsIgnoreCase("ID")){//need to compare col name
	            		//with ID as that is the one from dbms metadata or resul set shape metadata
	            		//but when putting in map, need to put property and if not present then column
	    	            Collection pks = (Collection) tableToPrimaryKeysMap.get(curTableName);
	    	            if(pks == null){
	    	            	pks = new HashSet();
	    	            }
	
	    	            if(curCol.getPropertyName() != null){
	    	            	pks.add(curCol.getPropertyName());
	    	            }
	    	            else{
	        	            pks.add(curCol.getColumnName());
	        	            curCol.setPropertyName(curCol.getColumnName());//make config consistent
	        	            if(!((Collection)tableToPropertyMap.get(curTableName)).contains(curCol.getColumnName())){
	        	            	((Collection)tableToPropertyMap.get(curTableName)).add(curCol.getColumnName());
	        	            }
	    	            }
	    	            tableToPrimaryKeysMap.put(curTableName, pks);	        	            		
	            	}
	            }        		
	    	}
	    	else{
	    		treatAllPKs = true;//table present in cfg , but no cols
	    	}
	    	
	    	if(treatAllPKs){
	    		tableToPrimaryKeysMap.put(curTableName, null);//case when all columns are considered PKs
	    	}
	    }
    }
    
    private Converter loadConverter(String converterName) {
        if (converterName != null) {

            try {
                Class converterClazz = Class.forName(converterName, true, 
                        Thread.currentThread().getContextClassLoader());
                if (null != converterClazz) {
                    return (Converter) converterClazz.newInstance();
                }

                converterClazz = Class.forName(converterName);
                if (converterClazz != null) {
                    return (Converter) converterClazz.newInstance();
                }
            } catch (ClassNotFoundException ex) {
                throw new RuntimeException(ex);
            } catch (IllegalAccessException ex) {
                throw new RuntimeException(ex);
            } catch (InstantiationException ex) {
                throw new RuntimeException(ex);
            }
        }
        return new DefaultConverter();
    }

    public String getColumnPropertyName(int i) {
        return (String) propertyNames.get(i - 1);
    }

    public String getDatabaseColumnName(int i) {
        return resultSetShape.getColumnName(i);
    }

    public String getTableName(String columnName) {
        return (String) typeNames.get(propertyNames.indexOf(columnName));
    }

    public int getTableSize(String tableName) {
        return ((Collection) tableToPropertyMap.get(tableName)).size();
    }

    public Type getDataType(String columnName) {
        return resultSetShape.getColumnType(propertyNames.indexOf(columnName));
    }

    public String getTablePropertyName(int i) {
        return (String) typeNames.get(i - 1);
    }

    public Collection getAllTablePropertyNames() {
        return tableToPropertyMap.keySet();
    }

    public HashSet getAllPKsForTable(String tableName){
    	if(tableToPrimaryKeysMap.containsKey(tableName))
    		return (HashSet)tableToPrimaryKeysMap.get(tableName);
    	else{
    		HashSet tmpHashSet = new HashSet();
    		tmpHashSet.add("");//case when there were cols in cfg but could not find any PK in it and no ID column in cfg/result set
    		return tmpHashSet;
    	}
    		
    }
    
    public String toString() {

        StringBuffer result = new StringBuffer(super.toString());
        result.append(" (Table Names: ");
        Iterator i = typeNames.iterator();
        while (i.hasNext()) {
            String tableName = (String) i.next();
            result.append(' ');
            result.append(tableName);
            result.append(',');
        }

        result.append(" columnNames: ");

        i = propertyNames.iterator();
        while (i.hasNext()) {
            String columnName = (String) i.next();
            result.append(' ');
            result.append(columnName);
            result.append(',');
        }

        result.append(" mappingModel: ");
        result.append(this.configWrapper.getConfig());

        result.append(" resultSet: ");
        result.append(resultSet);

        result.append(" resultSetSize: ");
        result.append(resultSetShape.getColumnCount());
        result.append(')');
        return result.toString();

    }

    /**
     * @return
     */
    public int getNumberOfTables() {
        return tableToPropertyMap.keySet().size();
    }

    /**
     * Return whether the column at the given position is part of a primary key.
     * If we don't have this information, we assume every column is a primary
     * key. This results in uniqueness checks using all columns in a table.
     * 
     * @param i
     * @return
     */
    public boolean isPKColumn(int i) {

        Table t = configWrapper.getTableByTypeName(getTablePropertyName(i));
        if (t == null) {
            return true;
        }

        // If no Columns have been defined, consider every column to be part of
        // the PK
        if (t.getColumn().isEmpty()) {
            return true;
        }

        Column c = configWrapper.getColumn(t, getDatabaseColumnName(i));

        if (c == null) {
            return false;
        }

        if (c.isPrimaryKey()) {
            return true;
        }

        return false;
    }

    /**
     * @param i
     * @return Type
     */
    public Type getDataType(int i) {
        return resultSetShape.getColumnType(i);
    }

    /**
     * @param tableName
     * @return Collection
     */
    public Collection getPropertyNames(String tableName) {
        return (Collection) tableToPropertyMap.get(tableName);
    }

    public ResultSet getResultSet() {
        return this.resultSet;
    }

    public int getResultSetSize() {
        return resultSetShape.getColumnCount();
    }

    public boolean isRecursive() {
        return configWrapper.hasRecursiveRelationships();
    }

    public Converter getConverter(int i) {
        return converters[i - 1];
    }

}