CPD Results

The following document contains the results of PMD's CPD 4.3.

Duplications

File Line
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTable.java 471
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTableOld.java 457
            columnNameToMD = new LinkedHashMap<SQLIdentifier, ColumnMetadata>();
            ResultSet rs = null;
            try {
                LOG.info( "Analyzing metadata for table {}", qTable );
                rs = dialect.getTableColumnMetadata( md, qTable );
                while ( rs.next() ) {
                    String column = rs.getString( 4 );
                    int sqlType = rs.getInt( 5 );
                    String sqlTypeName = rs.getString( 6 );
                    boolean isNullable = "YES".equals( rs.getString( 18 ) );
                    boolean isAutoincrement = false;
                    try {
                        isAutoincrement = "YES".equals( rs.getString( 23 ) );
                    } catch ( Throwable t ) {
                        // thanks to Larry E. for this
                    }
                    LOG.debug( "Found column '" + column + "', typeName: '" + sqlTypeName + "', typeCode: '" + sqlType
                               + "', isNullable: '" + isNullable + "', isAutoincrement:' " + isAutoincrement + "'" );

                    // type name ("geometry") works for PostGIS, MSSQL and Oracle
                    if ( sqlTypeName.toLowerCase().contains( "geometry" ) ) {
                        String srid = dialect.getUndefinedSrid();
                        ICRS crs = CRSManager.getCRSRef( "EPSG:4326", true );
                        CoordinateDimension dim = DIM_2;
                        GeometryPropertyType.GeometryType geomType = GeometryType.GEOMETRY;
                        Statement stmt = null;
                        ResultSet rs2 = null;
                        try {
                            stmt = conn.createStatement();
                            String sql = dialect.geometryMetadata( qTable, column, false );
                            rs2 = stmt.executeQuery( sql );
                            if ( rs2.next() ) {
                                if ( rs2.getInt( 2 ) != -1 ) {
                                    crs = CRSManager.lookup( "EPSG:" + rs2.getInt( 2 ), true );
                                    srid = "" + rs2.getInt( 2 );
                                } else {
                                    srid = dialect.getUndefinedSrid();
                                }
                                if ( rs2.getInt( 1 ) == 3 ) {
                                    dim = DIM_3;
                                }
                                geomType = getGeometryType( rs2.getString( 3 ) );
                                LOG.debug( "Derived geometry type: " + geomType + ", crs: " + crs + ", srid: " + srid
                                           + ", dim: " + dim + "" );
                            } else {
                                LOG.warn( "No metadata for geometry column '" + column
                                          + "' available in DB. Using defaults." );
                            }
                        } catch ( Exception e ) {
                            LOG.warn( "Unable to determine geometry column details: " + e.getMessage()
                                      + ". Using defaults.", e );
                        } finally {
                            JDBCUtils.close( rs2, stmt, null, LOG );
                        }
File Line
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTable.java 220
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTableOld.java 224
        for ( ColumnMetadata md : getColumns( table ).values() ) {
            if ( fidColumnNames.contains( new SQLIdentifier( md.column.toLowerCase() ) ) ) {
                LOG.debug( "Omitting column '" + md.column + "' from properties. Used in FIDMapping." );
                continue;
            }

            DBField dbField = new DBField( md.column );
            QName ptName = makeFullyQualified( new QName( md.column.toLowerCase() ), ftName.getPrefix(),
                                               ftName.getNamespaceURI() );
            if ( md.geomType == null ) {
                try {
                    BaseType type = BaseType.valueOf( md.sqlType );
                    PropertyType pt = new SimplePropertyType( ptName, 0, 1, type, null, null );
                    pts.add( pt );
                    ValueReference path = new ValueReference( ptName );
                    PrimitiveType primType = new PrimitiveType( type );
                    PrimitiveMapping mapping = new PrimitiveMapping( path, true, dbField, primType, null, null );
                    mappings.add( mapping );
                } catch ( IllegalArgumentException e ) {
                    LOG.warn( "Skipping column with type code '" + md.sqlType + "' from list of properties:"
                              + e.getMessage() );
                }
            } else {
                PropertyType pt = new GeometryPropertyType( ptName, 0, 1, null, null, md.geomType,
                                                            md.geometryParams.getDim(), INLINE );
                pts.add( pt );
                ValueReference path = new ValueReference( ptName );
                GeometryMapping mapping = new GeometryMapping( path, true, dbField, md.geomType, md.geometryParams,
                                                               null );
                mappings.add( mapping );
            }
        }

        FeatureType ft = new GenericFeatureType( ftName, pts, false );
        ftNameToFt.put( ftName, ft );

        FeatureTypeMapping ftMapping = new FeatureTypeMapping( ftName, table, fidMapping, mappings );
        ftNameToMapping.put( ftName, ftMapping );
    }

    private void process( TableName table, QName ftName, FIDMapping fidMapping,
                          List<JAXBElement<? extends AbstractParticleJAXB>> propDecls )
File Line
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTable.java 525
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTableOld.java 511
                        ColumnMetadata columnMd = new ColumnMetadata( column, sqlType, sqlTypeName, isNullable,
                                                                      geomType, dim, crs, srid );
                        columnNameToMD.put( new SQLIdentifier( column ), columnMd );
                    } else if ( sqlTypeName.toLowerCase().contains( "geography" ) ) {

                        LOG.warn( "Detected geography column. This is not fully supported yet. Expect bugs." );
                        String srid = dialect.getUndefinedSrid();
                        ICRS crs = CRSManager.getCRSRef( "EPSG:4326", true );
                        CoordinateDimension dim = DIM_2;
                        GeometryPropertyType.GeometryType geomType = GeometryType.GEOMETRY;
                        Statement stmt = null;
                        ResultSet rs2 = null;
                        try {
                            stmt = conn.createStatement();
                            String sql = dialect.geometryMetadata( qTable, column, true );
                            rs2 = stmt.executeQuery( sql );
                            if ( rs2.next() ) {
                                if ( rs2.getInt( 2 ) != -1 ) {
                                    crs = CRSManager.lookup( "EPSG:" + rs2.getInt( 2 ), true );
                                    srid = "" + rs2.getInt( 2 );
                                } else {
                                    srid = dialect.getUndefinedSrid();
                                }
                                if ( rs2.getInt( 1 ) == 3 ) {
                                    dim = DIM_3;
                                }
                                geomType = getGeometryType( rs2.getString( 3 ).toUpperCase() );
                                LOG.debug( "Derived geometry type (geography): " + geomType + ", crs: " + crs
                                           + ", srid: " + srid + ", dim: " + dim + "" );
                            } else {
                                LOG.warn( "No metadata for geography column '" + column
                                          + "' available in DB. Using defaults." );
                            }
                        } catch ( Exception e ) {
                            LOG.warn( "Unable to determine geography column details: " + e.getMessage()
                                      + ". Using defaults.", e );
                        } finally {
                            JDBCUtils.close( rs2, stmt, null, LOG );
                        }
File Line
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTable.java 374
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTableOld.java 360
            pt = new GeometryPropertyType( propName, minOccurs, maxOccurs, null, null, type, dim, INLINE );
            m = new GeometryMapping( path, minOccurs == 0, mapping, type, new GeometryStorageParams( crs, srid, dim ),
                                     jc );
        } else {
            LOG.warn( "Unhandled property declaration '" + propDecl.getClass() + "'. Skipping it." );
        }
        return new Pair<PropertyType, Mapping>( pt, m );
    }

    private FIDMapping buildFIDMapping( TableName table, QName ftName, FIDMappingJAXB config )
                            throws FeatureStoreException, SQLException {

        String prefix = config != null ? config.getPrefix() : null;
        if ( prefix == null ) {
            prefix = ftName.getPrefix().toUpperCase() + "_" + ftName.getLocalPart().toUpperCase() + "_";
        }

        // build FID columns / types from configuration
        List<Pair<SQLIdentifier, BaseType>> columns = new ArrayList<Pair<SQLIdentifier, BaseType>>();
        if ( config != null && config.getColumn() != null ) {
            for ( ColumnJAXB configColumn : config.getColumn() ) {
                SQLIdentifier columnName = new SQLIdentifier( configColumn.getName() );
                BaseType columnType = configColumn.getType() != null ? getPrimitiveType( configColumn.getType() )
                                                                    : null;
                if ( columnType == null ) {
File Line
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTable.java 410
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTableOld.java 396
                for ( ColumnMetadata md : getColumns( table ).values() ) {
                    if ( md.isAutoincrement ) {
                        BaseType columnType = BaseType.valueOf( md.sqlType );
                        columns.add( new Pair<SQLIdentifier, BaseType>( new SQLIdentifier( md.column ), columnType ) );
                        break;
                    }
                }
                if ( columns.isEmpty() ) {
                    throw new FeatureStoreException( "No autoincrement column in table '" + table
                                                     + "' found. Please specify column in FIDMapping manually." );
                }
            }
        } else {
            if ( columns.isEmpty() ) {
                throw new FeatureStoreException( "No FIDMapping columns for table '" + table
                                                 + "' specified. This is only possible for AutoIDGenerator." );
            }
        }

        return new FIDMapping( prefix, "_", columns, generator );
    }

    private QName makeFullyQualified( QName qName, String defaultPrefix, String defaultNamespace ) {
        String prefix = qName.getPrefix();
        String namespace = qName.getNamespaceURI();
        String localPart = qName.getLocalPart();
        if ( DEFAULT_NS_PREFIX.equals( prefix ) ) {
            prefix = defaultPrefix;
            namespace = defaultNamespace;
        }
        if ( "".equals( namespace ) ) {
            namespace = defaultNamespace;
        }
        return new QName( namespace, localPart, prefix );
    }

    private DatabaseMetaData getDBMetadata()
                            throws SQLException {
        if ( md == null ) {
            md = conn.getMetaData();
        }
        return md;
    }

    private ColumnMetadata getColumn( TableName qTable, SQLIdentifier columnName )
File Line
org/deegree/feature/persistence/sql/SQLFeatureStore.java 850
org/deegree/feature/persistence/sql/SQLFeatureStore.java 1256
            sql.append( " WHERE " );
            sql.append( alias );
            sql.append( "." );
            sql.append( blobMapping.getTypeColumn() );
            sql.append( "=?" );
            if ( wb != null ) {
                sql.append( " AND " );
                sql.append( wb.getWhere().getSQL() );
            }
            LOG.debug( "SQL: {}", sql );
            long begin = System.currentTimeMillis();
            stmt = conn.prepareStatement( sql.toString() );
            LOG.debug( "Preparing SELECT took {} [ms] ", System.currentTimeMillis() - begin );

            int i = 1;
            stmt.setShort( i++, getSchema().getFtId( ftName ) );
            if ( wb != null ) {
                for ( SQLArgument o : wb.getWhere().getArguments() ) {
                    o.setArgument( stmt, i++ );
                }
            }

            begin = System.currentTimeMillis();
File Line
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTable.java 175
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTableOld.java 179
    private void process( FeatureTypeMappingJAXB ftDecl )
                            throws SQLException, FeatureStoreException {

        if ( ftDecl.getTable() == null || ftDecl.getTable().isEmpty() ) {
            String msg = "Feature type element without or with empty table attribute.";
            throw new FeatureStoreException( msg );
        }

        TableName table = new TableName( ftDecl.getTable() );
        LOG.debug( "Processing feature type mapping for table '" + table + "'." );
        if ( getColumns( table ).isEmpty() ) {
            throw new FeatureStoreException( "No table with name '" + table + "' exists (or no columns defined)." );
        }

        QName ftName = ftDecl.getName();
        if ( ftName == null ) {
            LOG.debug( "Using table name for feature type." );
            ftName = new QName( table.getTable() );
        }
        ftName = makeFullyQualified( ftName, "app", "http://www.deegree.org/app" );
        LOG.debug( "Feature type name: '" + ftName + "'." );

        FIDMapping fidMapping = buildFIDMapping( table, ftName, ftDecl.getFIDMapping() );

        List<JAXBElement<? extends AbstractParticleJAXB>> propDecls = ftDecl.getAbstractParticle();
File Line
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTable.java 147
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTableOld.java 151
            for ( FeatureTypeMappingJAXB ftDecl : ftDecls ) {
                process( ftDecl );
            }
        } finally {
            JDBCUtils.close( conn );
        }
        this.deleteCascadingByDB = deleteCascadingByDB;
    }

    /**
     * Returns the {@link MappedAppSchema} derived from configuration / tables.
     * 
     * @return mapped application schema, never <code>null</code>
     */
    public MappedAppSchema getMappedSchema() {
        FeatureType[] fts = ftNameToFt.values().toArray( new FeatureType[ftNameToFt.size()] );
        FeatureTypeMapping[] ftMappings = ftNameToMapping.values().toArray( new FeatureTypeMapping[ftNameToMapping.size()] );
        Map<FeatureType, FeatureType> ftToSuperFt = null;
        Map<String, String> prefixToNs = null;
        GMLSchemaInfoSet xsModel = null;
        // TODO
        GeometryStorageParams geometryParams = new GeometryStorageParams( CRSManager.getCRSRef( "EPSG:4326" ),
                                                                          dialect.getUndefinedSrid(),
                                                                          CoordinateDimension.DIM_2 );
        return new MappedAppSchema( fts, ftToSuperFt, prefixToNs, xsModel, ftMappings, null, null, geometryParams,
                                    deleteCascadingByDB, null, null, null );
    }

    private void process( FeatureTypeMappingJAXB ftDecl )
File Line
org/deegree/feature/persistence/sql/ddl/MSSQLDDLCreator.java 152
org/deegree/feature/persistence/sql/ddl/OracleDDLCreator.java 231
            sql.append( " geometry" );
            ddls.addAll( getGeometryCreate( mapping, (DBField) me, table ) );
        } else {
            LOG.info( "Skipping geometry mapping -- not mapped to a db field. " );
        }
    }

    @Override
    protected void featureMappingSnippet( StringBuffer sql, FeatureMapping mapping ) {
        SQLIdentifier col = mapping.getJoinedTable().get( mapping.getJoinedTable().size() - 1 ).getFromColumns().get( 0 );
        if ( col != null ) {
            sql.append( ",\n    " );
            sql.append( col );
            sql.append( " varchar(2000)" );
        }
        MappingExpression hrefMe = mapping.getHrefMapping();
        if ( hrefMe instanceof DBField ) {
            sql.append( ",\n    " );
            sql.append( ( (DBField) hrefMe ).getColumn() );
            sql.append( " varchar(2000)" );
        }
    }
File Line
org/deegree/feature/persistence/sql/ddl/MSSQLDDLCreator.java 130
org/deegree/feature/persistence/sql/ddl/OracleDDLCreator.java 209
    }

    @Override
    protected void primitiveMappingSnippet( StringBuffer sql, PrimitiveMapping mapping ) {
        MappingExpression me = mapping.getMapping();
        if ( me instanceof DBField ) {
            DBField dbField = (DBField) me;
            sql.append( ",\n    " );
            sql.append( dbField.getColumn() );
            sql.append( " " );
            sql.append( getDBType( mapping.getType().getBaseType() ) );
        }
    }

    @Override
    protected void geometryMappingSnippet( StringBuffer sql, GeometryMapping mapping, List<StringBuffer> ddls,
                                           TableName table ) {
        MappingExpression me = mapping.getMapping();
        if ( me instanceof DBField ) {
            DBField dbField = (DBField) me;
            sql.append( ",\n    " );
            sql.append( dbField.getColumn() );
            sql.append( " geometry" );
File Line
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTable.java 199
org/deegree/feature/persistence/sql/config/MappedSchemaBuilderTableOld.java 203
        List<JAXBElement<? extends AbstractParticleJAXB>> propDecls = ftDecl.getAbstractParticle();
        if ( propDecls != null && !propDecls.isEmpty() ) {
            process( table, ftName, fidMapping, propDecls );
        } else {
            process( table, ftName, fidMapping );
        }
    }

    private void process( TableName table, QName ftName, FIDMapping fidMapping )
                            throws SQLException {

        LOG.debug( "Deriving properties and mapping for feature type '" + ftName + "' from table '" + table + "'" );

        List<PropertyType> pts = new ArrayList<PropertyType>();
        List<Mapping> mappings = new ArrayList<Mapping>();

        Set<SQLIdentifier> fidColumnNames = new HashSet<SQLIdentifier>();
        for ( Pair<SQLIdentifier, BaseType> column : fidMapping.getColumns() ) {
            fidColumnNames.add( column.first );
        }

        for ( ColumnMetadata md : getColumns( table ).values() ) {
File Line
org/deegree/feature/persistence/sql/mapper/XPathSchemaWalker.java 93
org/deegree/feature/persistence/sql/mapper/XPathSchemaWalker.java 145
    public Pair<XSElementDeclaration, Boolean> getTargetElement( Pair<XSElementDeclaration, Boolean> context,
                                                                 ValueReference propName ) {

        Expr path = propName.getAsXPath();
        if ( !( path instanceof LocationPath ) ) {
            throw new IllegalArgumentException( "XPath '" + propName + "' does not denote a location path." );
        }

        Pair<XSElementDeclaration, Boolean> currentEl = context;
        for ( Object o : ( (LocationPath) path ).getSteps() ) {
            if ( o instanceof NameStep ) {
                NameStep step = (NameStep) o;
                if ( step.getAxis() == Axis.CHILD ) {
                    // TODO check predicates
                    QName qName = getQName( step );
                    int num = getNumber( step );
                    currentEl = getTargetElement( currentEl, qName, num );
                    if ( currentEl == null ) {
                        throw new IllegalArgumentException( "Unable to match XPath '" + propName.getAsText()
File Line
org/deegree/feature/persistence/sql/rules/FeatureBuilderRelational.java 504
org/deegree/feature/persistence/sql/rules/FeatureBuilderRelational.java 601
                        XSComplexTypeDefinition complexType = (XSComplexTypeDefinition) cm.getElementDecl().getTypeDefinition();
                        XSObjectList attrUses = complexType.getAttributeUses();
                        for ( int i = 0; i < attrUses.getLength(); i++ ) {
                            XSAttributeUse attrUse = (XSAttributeUse) attrUses.item( i );
                            if ( attrUse.getRequired() ) {
                                QName attrName = null;
                                XSAttributeDeclaration attrDecl = attrUse.getAttrDeclaration();
                                if ( attrDecl.getNamespace() == null || attrDecl.getNamespace().isEmpty() ) {
                                    attrName = new QName( attrDecl.getName() );
                                } else {
                                    attrName = new QName( attrDecl.getNamespace(), attrDecl.getName() );
                                }
                                PrimitiveValue attrValue = attrs.get( attrName );
                                if ( attrValue == null ) {
File Line
org/deegree/feature/persistence/sql/insert/InsertRowManager.java 176
org/deegree/feature/persistence/sql/insert/InsertRowManager.java 228
            LOG.debug( "Built rows for feature '" + feature.getId() + "': " + allRows.size() );

            for ( InsertRow insertRow : allRows ) {
                if ( !insertRow.hasParents() ) {
                    rootRows.add( insertRow );
                }
            }

            LOG.debug( "Before heap run: uninserted rows: " + delayedRows.size() + ", root rows: " + rootRows.size() );
            processHeap();
            LOG.debug( "After heap run: uninserted rows: " + delayedRows.size() + ", root rows: " + rootRows.size() );

        } catch ( Throwable t ) {
            LOG.debug( t.getMessage(), t );
            throw new FeatureStoreException( t.getMessage(), t );
        }
        return featureRow;
    }
File Line
org/deegree/feature/persistence/sql/mapper/AppSchemaMapper.java 551
org/deegree/feature/persistence/sql/mapper/AppSchemaMapper.java 609
            if ( attrDecl.getNamespace() != null ) {
                attrName = new QName( attrDecl.getNamespace(), attrDecl.getName() );
            }
            MappingContext attrMc = mcManager.mapOneToOneAttribute( mc, attrName );
            // TODO
            NamespaceContext nsContext = null;
            ValueReference path = new ValueReference( "@" + getName( attrName ), nsContext );
            DBField dbField = new DBField( attrMc.getTable(), attrMc.getColumn() );
            PrimitiveType pt = new PrimitiveType( attrDecl.getTypeDefinition() );
            particles.add( new PrimitiveMapping( path, !attrUse.getRequired(), dbField, pt, null, null ) );
        }

        // xsi:nil attribute
        if ( isNillable ) {
File Line
org/deegree/feature/persistence/sql/SQLFeatureStore.java 1288
org/deegree/feature/persistence/sql/SQLFeatureStore.java 1414
                for ( SQLArgument o : wb.getWhere().getArguments() ) {
                    o.setArgument( stmt, i++ );
                }
            }
            // }
            // if ( wb != null && wb.getWhere() != null ) {
            // for ( SQLArgument o : wb.getWhere().getArguments() ) {
            // o.setArgument( stmt, i++ );
            // }
            // }
            // if ( wb != null && wb.getOrderBy() != null ) {
            // for ( SQLArgument o : wb.getOrderBy().getArguments() ) {
            // o.setArgument( stmt, i++ );
            // }
            // }

            begin = System.currentTimeMillis();
            stmt.setFetchSize( fetchSize );
            rs = stmt.executeQuery();
            LOG.debug( "Executing SELECT took {} [ms] ", System.currentTimeMillis() - begin );

            result = new IteratorFeatureInputStream( new FeatureResultSetIterator( builder, rs, conn, stmt ) );
        } catch ( Exception e ) {
            close( rs, stmt, conn, LOG );
            String msg = "Error performing query by operator filter: " + e.getMessage();
            LOG.error( msg, e );
            throw new FeatureStoreException( msg, e );
        }

        if ( filter != null ) {
File Line
org/deegree/feature/persistence/sql/ddl/MSSQLDDLCreator.java 130
org/deegree/feature/persistence/sql/ddl/OracleDDLCreator.java 209
org/deegree/feature/persistence/sql/ddl/PostGISDDLCreator.java 131
    }

    @Override
    protected void primitiveMappingSnippet( StringBuffer sql, PrimitiveMapping mapping ) {
        MappingExpression me = mapping.getMapping();
        if ( me instanceof DBField ) {
            DBField dbField = (DBField) me;
            sql.append( ",\n    " );
            sql.append( dbField.getColumn() );
            sql.append( " " );
            sql.append( getDBType( mapping.getType().getBaseType() ) );
        }
    }

    @Override
    protected void geometryMappingSnippet( StringBuffer sql, GeometryMapping mapping, List<StringBuffer> ddls,
                                           TableName table ) {
        MappingExpression me = mapping.getMapping();
        if ( me instanceof DBField ) {
File Line
org/deegree/feature/persistence/sql/mapper/AppSchemaMapper.java 349
org/deegree/feature/persistence/sql/mapper/AppSchemaMapper.java 366
        LOG.debug( "Mapping simple property '" + pt.getName() + "'" );
        ValueReference path = getPropName( pt.getName() );
        MappingContext propMc = null;
        List<TableJoin> jc = null;
        MappingExpression mapping = null;
        if ( pt.getMaxOccurs() == 1 ) {
            propMc = mcManager.mapOneToOneElement( mc, pt.getName() );
            mapping = new DBField( propMc.getColumn() );
        } else {
            propMc = mcManager.mapOneToManyElements( mc, pt.getName() );
            jc = generateJoinChain( mc, propMc );
            mapping = new DBField( "value" );
        }
        return new PrimitiveMapping( path, false, mapping, pt.getPrimitiveType(), jc, null );