Search in sources :

Example 1 with SingleAttributeTypeBuilder

use of org.geotoolkit.feature.SingleAttributeTypeBuilder in project geotoolkit by Geomatys.

the class JAXBFeatureTypeReader method getGroupAttributes.

private List<PropertyType> getGroupAttributes(String namespaceURI, Group group, BuildStack stack) throws MismatchedFeatureException {
    if (group == null)
        return Collections.EMPTY_LIST;
    final List<PropertyType> atts = new ArrayList<>();
    final List<Object> particles = group.getParticle();
    for (Object particle : particles) {
        if (particle instanceof JAXBElement) {
            particle = ((JAXBElement) particle).getValue();
        }
        if (particle instanceof Element) {
            final Element ele = (Element) particle;
            final PropertyType att = elementToAttribute(namespaceURI, ele, stack);
            atts.add(att);
        } else if (particle instanceof Any) {
            final Any ele = (Any) particle;
            final SingleAttributeTypeBuilder atb = new SingleAttributeTypeBuilder();
            atb.setName(namespaceURI, Utils.ANY_PROPERTY_NAME);
            atb.setValueClass(Object.class);
            // override properties which are defined
            atb.setMinimumOccurs(ele.getMinOccurs() == null ? 0 : ele.getMinOccurs().intValue());
            final String maxxAtt = ele.getMaxOccurs();
            if ("unbounded".equalsIgnoreCase(maxxAtt)) {
                atb.setMaximumOccurs(Integer.MAX_VALUE);
            } else if (maxxAtt != null) {
                atb.setMaximumOccurs(Integer.parseInt(maxxAtt));
            }
            atts.add(atb.build());
        } else if (particle instanceof GroupRef) {
            final GroupRef ref = (GroupRef) particle;
            final QName groupRef = ref.getRef();
            final NamedGroup ng = xsdContext.findGlobalGroup(groupRef);
            final List<PropertyType> groupAttributes = getGroupAttributes(namespaceURI, ng, stack);
            // change min/max occurences
            int minOcc = ref.getMinOccurs() == null ? 0 : ref.getMinOccurs().intValue();
            int maxOcc = 1;
            String maxxAtt = ref.getMaxOccurs();
            if ("unbounded".equalsIgnoreCase(maxxAtt)) {
                maxOcc = Integer.MAX_VALUE;
            } else if (maxxAtt != null) {
                maxOcc = Integer.parseInt(maxxAtt);
            }
            for (PropertyType pt : groupAttributes) {
                pt = new FeatureTypeBuilder().addProperty(pt).setMinimumOccurs(minOcc).setMaximumOccurs(maxOcc).build();
                atts.add(pt);
            }
        } else if (particle instanceof ExplicitGroup) {
            final ExplicitGroup eg = (ExplicitGroup) particle;
            atts.addAll(getGroupAttributes(namespaceURI, eg, stack));
        } else {
            throw new MismatchedFeatureException("Unexpected TYPE : " + particle);
        }
    }
    return atts;
}
Also used : NamedGroup(org.geotoolkit.xsd.xml.v2001.NamedGroup) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) QName(javax.xml.namespace.QName) Element(org.geotoolkit.xsd.xml.v2001.Element) JAXBElement(javax.xml.bind.JAXBElement) TopLevelElement(org.geotoolkit.xsd.xml.v2001.TopLevelElement) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) JAXBElement(javax.xml.bind.JAXBElement) Any(org.geotoolkit.xsd.xml.v2001.Any) ExplicitGroup(org.geotoolkit.xsd.xml.v2001.ExplicitGroup) SingleAttributeTypeBuilder(org.geotoolkit.feature.SingleAttributeTypeBuilder) MismatchedFeatureException(org.opengis.feature.MismatchedFeatureException) AttributeGroupRef(org.geotoolkit.xsd.xml.v2001.AttributeGroupRef) GroupRef(org.geotoolkit.xsd.xml.v2001.GroupRef)

Example 2 with SingleAttributeTypeBuilder

use of org.geotoolkit.feature.SingleAttributeTypeBuilder in project geotoolkit by Geomatys.

the class DbaseFileHeader method createDescriptors.

/**
 * Create the list of matching attribute descriptor from header informations.
 *
 * @return List of AttributDescriptor
 */
public List<AttributeType> createDescriptors() {
    final int nbFields = getNumFields();
    final SingleAttributeTypeBuilder atb = new SingleAttributeTypeBuilder();
    final List<AttributeType> attributes = new ArrayList<>(nbFields);
    for (int i = 0; i < nbFields; i++) {
        final String name = getFieldName(i);
        final Class attributeClass = getFieldClass(i);
        final int length = getFieldLength(i);
        atb.reset();
        atb.setName(name);
        atb.setValueClass(attributeClass);
        atb.setLength(length);
        attributes.add(atb.build());
    }
    return attributes;
}
Also used : SingleAttributeTypeBuilder(org.geotoolkit.feature.SingleAttributeTypeBuilder) AttributeType(org.opengis.feature.AttributeType) ArrayList(java.util.ArrayList)

Example 3 with SingleAttributeTypeBuilder

use of org.geotoolkit.feature.SingleAttributeTypeBuilder in project geotoolkit by Geomatys.

the class DataBaseModel method analyzeResult.

/**
 * Analyze the metadata of the ResultSet to rebuild a feature type.
 */
public FeatureType analyzeResult(final ResultSet result, final String name) throws SQLException, DataStoreException {
    final SQLDialect dialect = store.getDialect();
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName(name);
    final ResultSetMetaData metadata = result.getMetaData();
    final int nbcol = metadata.getColumnCount();
    for (int i = 1; i <= nbcol; i++) {
        final String columnName = metadata.getColumnName(i);
        final String columnLabel = metadata.getColumnLabel(i);
        final String typeName = metadata.getColumnTypeName(i);
        final String schemaName = metadata.getSchemaName(i);
        final String tableName = metadata.getTableName(i);
        final int sqlType = metadata.getColumnType(i);
        final String sqlTypeName = metadata.getColumnTypeName(i);
        // search if we already have this minute
        PropertyType desc = null;
        final SchemaMetaModel schema = getSchemaMetaModel(schemaName);
        if (schema != null) {
            TableMetaModel table = schema.getTable(tableName);
            if (table != null) {
                try {
                    desc = table.getType(TableMetaModel.View.SIMPLE_FEATURE_TYPE).build().getProperty(columnName);
                } catch (PropertyNotFoundException ex) {
                }
            }
        }
        if (desc == null) {
            // could not find the original type
            // this column must be calculated
            final SingleAttributeTypeBuilder atb = new SingleAttributeTypeBuilder();
            final int nullable = metadata.isNullable(i);
            atb.setName(ensureGMLNS(columnLabel));
            atb.setMinimumOccurs(nullable == metadata.columnNullable ? 0 : 1);
            atb.setMaximumOccurs(1);
            atb.setName(ensureGMLNS(columnLabel));
            Connection cx = null;
            try {
                cx = store.getDataSource().getConnection();
                final Class type = dialect.getJavaType(sqlType, sqlTypeName);
                if (type.equals(Geometry.class)) {
                    // try to determine the real geometric type
                    dialect.decodeGeometryColumnType(atb, cx, result, i, true);
                } else {
                    // why so this a sencond time ?
                    atb.setName(columnLabel);
                    atb.setValueClass(type);
                }
            } catch (SQLException e) {
                throw new DataStoreException("Error occurred analyzing column : " + columnName, e);
            } finally {
                closeSafe(store.getLogger(), cx);
            }
            desc = atb.build();
        }
        ftb.addProperty(desc);
    }
    return ftb.build();
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) DataStoreException(org.apache.sis.storage.DataStoreException) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PropertyType(org.opengis.feature.PropertyType) ResultSetMetaData(java.sql.ResultSetMetaData) SingleAttributeTypeBuilder(org.geotoolkit.feature.SingleAttributeTypeBuilder) SQLDialect(org.geotoolkit.db.dialect.SQLDialect)

Example 4 with SingleAttributeTypeBuilder

use of org.geotoolkit.feature.SingleAttributeTypeBuilder in project geotoolkit by Geomatys.

the class DataBaseModel method analyzeColumn.

private AttributeType analyzeColumn(final Map columnSet, final Connection cx) throws SQLException, DataStoreException {
    final SQLDialect dialect = store.getDialect();
    final SingleAttributeTypeBuilder atb = new SingleAttributeTypeBuilder();
    final String schemaName = (String) columnSet.get(Column.TABLE_SCHEM);
    final String tableName = (String) columnSet.get(Column.TABLE_NAME);
    final String columnName = (String) columnSet.get(Column.COLUMN_NAME);
    final int columnSize = ((Number) columnSet.get(Column.COLUMN_SIZE)).intValue();
    final int columnDataType = ((Number) columnSet.get(Column.DATA_TYPE)).intValue();
    final String columnTypeName = (String) columnSet.get(Column.TYPE_NAME);
    final String columnNullable = (String) columnSet.get(Column.IS_NULLABLE);
    atb.setName(columnName);
    atb.setLength(columnSize);
    try {
        dialect.decodeColumnType(atb, cx, columnTypeName, columnDataType, schemaName, tableName, columnName);
    } catch (SQLException e) {
        throw new DataStoreException("Error occurred analyzing column : " + columnName, e);
    }
    atb.setMinimumOccurs(Column.VALUE_NO.equalsIgnoreCase(columnNullable) ? 1 : 0);
    atb.setMaximumOccurs(1);
    return atb.build();
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) SingleAttributeTypeBuilder(org.geotoolkit.feature.SingleAttributeTypeBuilder) SQLException(java.sql.SQLException) SQLDialect(org.geotoolkit.db.dialect.SQLDialect)

Example 5 with SingleAttributeTypeBuilder

use of org.geotoolkit.feature.SingleAttributeTypeBuilder in project geotoolkit by Geomatys.

the class DataBaseModel method reverseComplexFeatureTypes.

/**
 * Rebuild complex feature types using foreign key relations.
 */
private void reverseComplexFeatureTypes() {
    final SingleAttributeTypeBuilder atb = new SingleAttributeTypeBuilder();
    // result map
    final Map<String, TableMetaModel> builded = new HashMap<>();
    // first pass to create the real types but without relations types-------
    // since we must have all of them before creating relations
    final List<Object[]> secondPass = new ArrayList<>();
    for (final SchemaMetaModel schema : schemas.values()) {
        for (final TableMetaModel table : schema.tables.values()) {
            final String code = schema.name + "." + table.name;
            builded.put(code, table);
            // create the complex model by replacing descriptors
            final FeatureTypeBuilder ftb = new FeatureTypeBuilder(table.simpleFeatureType.build());
            // add 0:1 relations operations --------------------------------
            for (final RelationMetaModel relation : table.importedKeys) {
                final GenericName relationName = NamesExt.create(relation.getRelationName());
                final DBRelationOperation op = new DBRelationOperation(relationName, store, relation, relation.getForeignTable());
                ftb.addProperty(op);
                final Object[] futur = new Object[] { table, relation };
                secondPass.add(futur);
            }
            // add N:1 relations operations---------------------------------
            for (final RelationMetaModel relation : table.exportedKeys) {
                String relationNameTip = relation.getRelationName();
                if (relationNameTip == null) {
                    // find an appropriate name
                    relationNameTip = relation.getForeignColumn();
                    for (PropertyTypeBuilder dpd : ftb.properties()) {
                        if (relationNameTip.equals(dpd.getName().tip().toString())) {
                            // name already used, make it unique by including reference table name
                            relationNameTip = relation.getForeignTable() + ASSOCIATION_SEPARATOR + relation.getForeignColumn();
                            break;
                        }
                    }
                }
                final GenericName relationName = NamesExt.create(relationNameTip);
                final DBRelationOperation op = new DBRelationOperation(relationName, store, relation, relation.getForeignTable());
                ftb.addProperty(op);
                final Object[] futur = new Object[] { table, relation };
                secondPass.add(futur);
            }
            table.complexFeatureType = ftb;
            table.allType = ftb;
        }
    }
// //second pass to fill relations-----------------------------------------
// for(Object[] futur : secondPass){
// final TableMetaModel primaryTable = (TableMetaModel) futur[0];
// final RelationMetaModel relation = (RelationMetaModel) futur[1];
// final String relCode = relation.getForeignSchema() +"."+relation.getForeignTable();
// final TableMetaModel foreignTable = (TableMetaModel)builded.get(relCode);
// 
// //update complex feature type
// final FeatureTypeBuilder cft = primaryTable.getType(TableMetaModel.View.COMPLEX_FEATURE_TYPE);
// modifyField(foreignTable, relation, cft);
// 
// //update full type
// final FeatureTypeBuilder allt = primaryTable.getType(TableMetaModel.View.ALLCOMPLEX);
// modifyField(foreignTable, relation, allt);
// }
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) DBRelationOperation(org.geotoolkit.db.DBRelationOperation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PropertyTypeBuilder(org.apache.sis.feature.builder.PropertyTypeBuilder) GenericName(org.opengis.util.GenericName) SingleAttributeTypeBuilder(org.geotoolkit.feature.SingleAttributeTypeBuilder)

Aggregations

SingleAttributeTypeBuilder (org.geotoolkit.feature.SingleAttributeTypeBuilder)13 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)4 AttributeType (org.opengis.feature.AttributeType)4 PropertyType (org.opengis.feature.PropertyType)4 ArrayList (java.util.ArrayList)3 GenericName (org.opengis.util.GenericName)3 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 DataStoreException (org.apache.sis.storage.DataStoreException)2 SQLDialect (org.geotoolkit.db.dialect.SQLDialect)2 MismatchedFeatureException (org.opengis.feature.MismatchedFeatureException)2 Connection (java.sql.Connection)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 AbstractMap (java.util.AbstractMap)1 Map (java.util.Map)1 JAXBElement (javax.xml.bind.JAXBElement)1 QName (javax.xml.namespace.QName)1 DefaultAssociationRole (org.apache.sis.feature.DefaultAssociationRole)1 PropertyTypeBuilder (org.apache.sis.feature.builder.PropertyTypeBuilder)1 DBRelationOperation (org.geotoolkit.db.DBRelationOperation)1