private ColumnMetadata getColumn( TableName qTable, SQLIdentifier columnName ) throws SQLException, FeatureStoreException { ColumnMetadata md = getColumnMetadataFromDb( qTable ).get( columnName ); if ( md == null ) md = getUnescapedColumnMetadata( qTable, columnName ); if ( md == null ) { throw new FeatureStoreException( "Table '" + qTable + "' does not have a column with name '" + columnName + "'" ); } return md; }
private ColumnMetadataOld getColumn( TableName qTable, SQLIdentifier columnName ) throws SQLException, FeatureStoreException { ColumnMetadataOld md = getColumns( qTable ).get( columnName ); if ( md == null ) { throw new FeatureStoreException( "Table '" + qTable + "' does not have a column with name '" + columnName + "'" ); } return md; }
private Mapping buildMapping( TableName currentTable, Pair<XSElementDeclaration, Boolean> elDecl, AbstractParticleJAXB value ) { LOG.debug( "Building mapping for path '{}' on element '{}'", value.getPath(), elDecl ); if ( value instanceof PrimitiveParticleJAXB ) { return buildMapping( currentTable, elDecl, (PrimitiveParticleJAXB) value ); } if ( value instanceof GeometryParticleJAXB ) { return buildMapping( currentTable, elDecl, (GeometryParticleJAXB) value ); } if ( value instanceof FeatureParticleJAXB ) { return buildMapping( currentTable, elDecl, (FeatureParticleJAXB) value ); } if ( value instanceof ComplexParticleJAXB ) { return buildMapping( currentTable, elDecl, (ComplexParticleJAXB) value ); } throw new RuntimeException( "Internal error. Unhandled particle mapping JAXB bean '" + value.getClass().getName() + "'." ); }
public static MappedAppSchema build( String configURL, SQLFeatureStoreJAXB config, SQLDialect dialect, Workspace workspace ) throws SQLException, FeatureStoreException { boolean deleteCascadingByDB = true; if ( config.getJoinTableDeletePropagation() != null ) { deleteCascadingByDB = config.getJoinTableDeletePropagation().equals( "db" ); } AbstractMappedSchemaBuilder builder = null; if ( config.getGMLSchema() == null || config.getGMLSchema().isEmpty() ) { LOG.debug( "Table-driven mode configuration" ); if ( config.getFeatureType() != null && !config.getFeatureType().isEmpty() ) { builder = new MappedSchemaBuilderTableOld( config.getJDBCConnId().getValue(), config.getFeatureType(), dialect, deleteCascadingByDB, workspace ); } else { builder = new MappedSchemaBuilderTable( config.getJDBCConnId().getValue(), config.getFeatureTypeMapping(), dialect, deleteCascadingByDB, workspace ); } } else { LOG.debug( "Schema-driven mode configuration" ); List<String> gmlSchemas = config.getGMLSchema(); StorageCRS storageCRS = config.getStorageCRS(); List<NamespaceHint> nsHints = config.getNamespaceHint(); BLOBMapping blobConf = config.getBLOBMapping(); List<FeatureTypeMappingJAXB> ftMappingConfs = config.getFeatureTypeMapping(); builder = new MappedSchemaBuilderGML( configURL, gmlSchemas, storageCRS, nsHints, blobConf, ftMappingConfs, deleteCascadingByDB ); } return builder.getMappedSchema(); }
private void process( FeatureTypeJAXB 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 AbstractPropertyJAXB>> propDecls = ftDecl.getAbstractProperty(); if ( propDecls != null && !propDecls.isEmpty() ) { process( table, ftName, fidMapping, propDecls ); } else { process( table, ftName, fidMapping ); } }
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 ( getColumnMetadataFromDb( 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(); if ( propDecls != null && !propDecls.isEmpty() ) { buildFeatureTypeAndMapping( table, ftName, fidMapping, propDecls ); } else { buildFeatureTypeAndMapping( table, ftName, fidMapping ); } }
private Mapping buildMapping( TableName currentTable, Pair<XSElementDeclaration, Boolean> elDecl, PrimitiveParticleJAXB config ) { ValueReference path = new ValueReference( config.getPath(), nsBindings ); Pair<PrimitiveType, Boolean> pt = null; try { pt = schemaWalker.getTargetType( elDecl, path ); } catch ( RuntimeException e ) { throw new RuntimeException( "Error in mapping of table '" + currentTable + "': " + e ); } if ( config.getType() != null ) { PrimitiveType forcedType = new PrimitiveType( getPrimitiveType( config.getType() ) ); LOG.debug( "Overriding schema-derived primitive type '" + pt.getFirst() + "'. Forcing '" + forcedType + "'." ); pt.first = forcedType; } MappingExpression me = parseMappingExpression( config.getMapping() ); List<TableJoin> joinedTable = buildJoinTable( currentTable, config.getJoin() ); LOG.debug( "Targeted primitive type: " + pt ); boolean escalateVoid = determineParticleVoidability( pt.second, config.getNullEscalation() ); return new PrimitiveMapping( path, escalateVoid, me, pt.first, joinedTable, config.getCustomConverter() ); }
public MappedSchemaBuilderGML( String configURL, List<String> gmlSchemas, StorageCRS storageCRS, List<NamespaceHint> nsHints, BLOBMapping blobConf, List<FeatureTypeMappingJAXB> ftMappingConfs, boolean deleteCascadingByDB ) throws FeatureStoreException { gmlSchema = buildGMLSchema( configURL, gmlSchemas ); CRSRef crs = CRSManager.getCRSRef( storageCRS.getValue() ); CoordinateDimension dim = crs.getDimension() == 3 ? DIM_3 : DIM_2; geometryParams = new GeometryStorageParams( crs, storageCRS.getSrid(), dim ); // add namespace bindings addNamespaceBindings( configURL, gmlSchema, nsHints ); schemaWalker = new XPathSchemaWalker( gmlSchema, nsBindings ); if ( blobConf != null ) { Pair<BlobMapping, BBoxTableMapping> pair = buildBlobMapping( blobConf, gmlSchema.getGMLSchema().getVersion() ); blobMapping = pair.first; bboxMapping = pair.second; } if ( ftMappingConfs != null ) { for ( FeatureTypeMappingJAXB ftMappingConf : ftMappingConfs ) { org.deegree.feature.persistence.sql.FeatureTypeMapping ftMapping = buildFtMapping( ftMappingConf ); ftNameToMapping.put( ftMapping.getFeatureType(), ftMapping ); } } this.deleteCascadingByDB = deleteCascadingByDB; }
private GeometryMapping buildMapping( TableName currentTable, Pair<XSElementDeclaration, Boolean> elDecl, GeometryParticleJAXB config ) { ValueReference path = new ValueReference( config.getPath(), nsBindings ); MappingExpression me = parseMappingExpression( config.getMapping() ); elDecl = schemaWalker.getTargetElement( elDecl, path ); QName ptName = new QName( elDecl.first.getNamespace(), elDecl.getFirst().getName() ); ObjectPropertyType pt = gmlSchema.getGMLSchema().getGMLPropertyDecl( elDecl.first, ptName, 1, 1, null ); GeometryType type = GeometryType.GEOMETRY; if ( pt instanceof GeometryPropertyType ) { type = ( (GeometryPropertyType) pt ).getGeometryType(); } boolean escalateVoid = determineParticleVoidability( elDecl.second, config.getNullEscalation() ); List<TableJoin> joinedTable = buildJoinTable( currentTable, config.getJoin() ); return new GeometryMapping( path, escalateVoid, me, type, geometryParams, joinedTable, config.getCustomConverter() ); }
private CompoundMapping buildMapping( TableName currentTable, Pair<XSElementDeclaration, Boolean> elDecl, ComplexParticleJAXB config ) { ValueReference path = new ValueReference( config.getPath(), nsBindings ); elDecl = schemaWalker.getTargetElement( elDecl, path ); boolean escalateVoid = determineParticleVoidability( elDecl.second, config.getNullEscalation() ); List<TableJoin> joinedTable = buildJoinTable( currentTable, config.getJoin() ); if ( joinedTable != null ) { currentTable = joinedTable.get( joinedTable.size() - 1 ).getToTable(); } List<JAXBElement<? extends AbstractParticleJAXB>> children = config.getAbstractParticle(); List<Mapping> particles = new ArrayList<Mapping>( children.size() ); for ( JAXBElement<? extends AbstractParticleJAXB> child : children ) { Mapping particle = buildMapping( currentTable, elDecl, child.getValue() ); if ( particle != null ) { particles.add( particle ); } } return new CompoundMapping( path, escalateVoid, particles, joinedTable, elDecl.first, config.getCustomConverter() ); }
DatabaseMetaData md = getDbMetadata(); columnNameToMD = new LinkedHashMap<SQLIdentifier, ColumnMetadata>(); ResultSet rs = null; dim = DIM_3; geomType = getGeometryType( rs2.getString( 3 ) ); LOG.debug( "Derived geometry type: " + geomType + ", srid: " + srid + ", dim: " + dim + "" ); JDBCUtils.close( rs2, stmt, null, LOG ); ColumnMetadata columnMd = new ColumnMetadata( column, sqlType, sqlTypeName, isNullable, geomType, dim, srid ); columnNameToMD.put( new SQLIdentifier( column ), columnMd ); geomType = getGeometryType( rs2.getString( 3 ).toUpperCase() ); LOG.debug( "Derived geometry type (geography): " + geomType + ", srid: " + srid + ", dim: " + dim + "" ); ColumnMetadata columnMd = new ColumnMetadata( column, sqlType, sqlTypeName, isNullable, geomType, dim, srid ); columnNameToMD.put( new SQLIdentifier( column ), columnMd ); } else { ColumnMetadata columnMd = new ColumnMetadata( column, sqlType, sqlTypeName, isNullable, isAutoincrement ); columnNameToMD.put( new SQLIdentifier( column ), columnMd );
private FeatureTypeMapping buildFtMapping( FeatureTypeMappingJAXB ftMappingConf ) throws FeatureStoreException { QName ftName = ftMappingConf.getName(); TableName ftTable = new TableName( ftMappingConf.getTable() ); FIDMapping fidMapping = buildFIDMapping( ftTable, ftName, ftMappingConf.getFIDMapping() ); List<Mapping> particleMappings = new ArrayList<Mapping>(); XSElementDeclaration elDecl = gmlSchema.getGMLSchema().getElementDecl( ftName ); for ( JAXBElement<? extends AbstractParticleJAXB> particle : ftMappingConf.getAbstractParticle() ) { particleMappings.add( buildMapping( ftTable, new Pair<XSElementDeclaration, Boolean>( elDecl, TRUE ), particle.getValue() ) ); } return new FeatureTypeMapping( ftName, ftTable, fidMapping, particleMappings ); }
private FIDMapping buildFIDMapping( TableName table, QName ftName, FIDMappingJAXB config ) throws FeatureStoreException { String prefix = config != null ? config.getPrefix() : null; if ( prefix == null ) { prefix = ftName.getPrefix().toUpperCase() + "_" + ftName.getLocalPart().toUpperCase() + "_"; } List<Pair<SQLIdentifier, BaseType>> columns = new ArrayList<Pair<SQLIdentifier, BaseType>>(); if ( config != null && config.getColumn() != null ) { for ( ColumnJAXB configColumn : config.getColumn() ) { String column = configColumn.getName(); BaseType pt = null; if ( configColumn.getType() != null ) { pt = getPrimitiveType( configColumn.getType() ); } columns.add( new Pair<SQLIdentifier, BaseType>( new SQLIdentifier( column ), pt ) ); } } IDGenerator generator = buildGenerator( config == null ? null : config.getAbstractIDGenerator() ); if ( !( generator instanceof AutoIDGenerator ) ) { if ( columns.isEmpty() ) { throw new FeatureStoreException( "No FIDMapping column for table '" + table + "' specified. This is only possible for AutoIDGenerator." ); } } return new FIDMapping( prefix, "_", columns, generator ); }
private void process( TableName table, QName ftName, FIDMapping fidMapping, List<JAXBElement<? extends AbstractPropertyJAXB>> propDecls ) throws FeatureStoreException, SQLException { List<PropertyType> pts = new ArrayList<PropertyType>(); List<Mapping> mappings = new ArrayList<Mapping>(); for ( JAXBElement<? extends AbstractPropertyJAXB> propDeclEl : propDecls ) { AbstractPropertyJAXB propDecl = propDeclEl.getValue(); Pair<PropertyType, Mapping> pt = process( table, propDecl ); pts.add( pt.first ); mappings.add( pt.second ); } FeatureType ft = new GenericFeatureType( ftName, pts, false ); ftNameToFt.put( ftName, ft ); FeatureTypeMapping ftMapping = new FeatureTypeMapping( ftName, table, fidMapping, mappings ); ftNameToMapping.put( ftName, ftMapping ); }
private ColumnMetadata getUnescapedColumnMetadata( TableName qTable, SQLIdentifier columnName ) throws SQLException { String name = columnName.getName(); char leadingEscapeChar = dialect.getLeadingEscapeChar(); char tailingEscapeChar = dialect.getTailingEscapeChar(); if ( leadingEscapeChar == name.charAt( 0 ) && tailingEscapeChar == name.charAt( name.length() - 1 ) ) { SQLIdentifier unescapedColumnName = new SQLIdentifier( name.substring( 1, name.length() - 1 ) ); return getColumnMetadataFromDb( qTable ).get( unescapedColumnName ); } return null; }
private void buildFeatureTypeAndMapping( TableName table, QName ftName, FIDMapping fidMapping, List<JAXBElement<? extends AbstractParticleJAXB>> propDecls ) throws FeatureStoreException, SQLException { List<PropertyType> pts = new ArrayList<PropertyType>(); List<Mapping> mappings = new ArrayList<Mapping>(); for ( JAXBElement<? extends AbstractParticleJAXB> propDeclEl : propDecls ) { AbstractParticleJAXB propDecl = propDeclEl.getValue(); Pair<PropertyType, Mapping> pt = buildPropertyAndMapping( table, propDecl, ftName.getPrefix(), ftName.getNamespaceURI() ); pts.add( pt.first ); mappings.add( pt.second ); } FeatureType ft = new GenericFeatureType( ftName, pts, false ); ftNameToFt.put( ftName, ft ); FeatureTypeMapping ftMapping = new FeatureTypeMapping( ftName, table, fidMapping, mappings ); ftNameToMapping.put( ftName, ftMapping ); }
/** * Creates a new {@link MappedSchemaBuilderTable} instance. * * @param jdbcConnId * identifier of JDBC connection, must not be <code>null</code> (used to determine columns / types) * @param ftDecls * JAXB feature type declarations, must not be <code>null</code> * @throws SQLException * @throws FeatureStoreException */ public MappedSchemaBuilderTable( String jdbcConnId, List<FeatureTypeMappingJAXB> ftDecls, SQLDialect dialect, boolean deleteCascadingByDB, Workspace workspace ) throws SQLException, FeatureStoreException { this.dialect = dialect; ConnectionProvider prov = workspace.getResource( ConnectionProviderProvider.class, jdbcConnId ); conn = prov.getConnection(); try { for ( FeatureTypeMappingJAXB ftDecl : ftDecls ) { process( ftDecl ); } } finally { JDBCUtils.close( conn ); } this.deleteCascadingByDB = deleteCascadingByDB; }
protected List<TableJoin> buildJoinTable( TableName from, org.deegree.feature.persistence.sql.jaxb.Join join ) { if ( join != null ) { TableName target = createFromQualifiedName( join.getTable() ); if ( join.getFromColumns().size() != join.getToColumns().size() ) { throw new UnsupportedOperationException( "Joins must use same number of from and to columns." ); } if ( join.getFromColumns().isEmpty() ) { throw new UnsupportedOperationException( "Joins must use at least a single column." ); } boolean isNumbered = join.isNumbered() == null ? false : join.isNumbered(); Map<SQLIdentifier, IDGenerator> keyColumnToGenerator = new HashMap<SQLIdentifier, IDGenerator>(); for ( AutoKeyColumn keyColumn : join.getAutoKeyColumn() ) { SQLIdentifier columnName = new SQLIdentifier( keyColumn.getName() ); IDGenerator idGenerator = buildGenerator( keyColumn.getAbstractIDGenerator() ); keyColumnToGenerator.put( columnName, idGenerator ); } if ( keyColumnToGenerator.isEmpty() ) { // defaulting keyColumnToGenerator.put( new SQLIdentifier( "id" ), new AutoIDGenerator() ); } TableJoin tj = new TableJoin( from, target, join.getFromColumns(), join.getToColumns(), join.getOrderColumns(), isNumbered, keyColumnToGenerator ); return Collections.singletonList( tj ); } return null; }
private FeatureMapping buildMapping( TableName currentTable, Pair<XSElementDeclaration, Boolean> elDecl, FeatureParticleJAXB config ) { ValueReference path = new ValueReference( config.getPath(), nsBindings ); MappingExpression hrefMe = null; if ( config.getHref() != null ) { hrefMe = parseMappingExpression( config.getHref().getMapping() ); } elDecl = schemaWalker.getTargetElement( elDecl, path ); QName ptName = new QName( elDecl.first.getNamespace(), elDecl.first.getName() ); // TODO rework this FeaturePropertyType pt = (FeaturePropertyType) gmlSchema.getGMLSchema().getGMLPropertyDecl( elDecl.first, ptName, 0, 1, null ); boolean escalateVoid = determineParticleVoidability( elDecl.second, config.getNullEscalation() ); List<TableJoin> joinedTable = buildJoinTable( currentTable, config.getJoin() ); return new FeatureMapping( path, escalateVoid, hrefMe, pt.getFTName(), joinedTable, config.getCustomConverter() ); }
/** * Creates a new {@link MappedSchemaBuilderTableOld} instance. * * @param jdbcConnId * identifier of JDBC connection, must not be <code>null</code> (used to determine columns / types) * @param ftDecls * JAXB feature type declarations, must not be <code>null</code> * @throws SQLException * @throws FeatureStoreException */ public MappedSchemaBuilderTableOld( String jdbcConnId, List<FeatureTypeJAXB> ftDecls, SQLDialect dialect, boolean deleteCascadingByDB, Workspace workspace ) throws SQLException, FeatureStoreException { this.dialect = dialect; ConnectionProvider prov = workspace.getResource( ConnectionProviderProvider.class, jdbcConnId ); conn = prov.getConnection(); try { for ( FeatureTypeJAXB ftDecl : ftDecls ) { process( ftDecl ); } } finally { JDBCUtils.close( conn ); } this.deleteCascadingByDB = deleteCascadingByDB; }