public ResultMetadata()

in rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultMetadata.java [63:166]


    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, resultSetShape.getColumnType(i));

            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(configWrapper.getTableTypeName(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);
                }
            }
        }
    }