Search in sources :

Example 16 with FeatureAssociationRole

use of org.opengis.feature.FeatureAssociationRole in project geotoolkit by Geomatys.

the class PostgresVersionControl method createVersioningTable.

/**
 * Create versioning table for given table.
 *
 * @param schemaName
 * @param tableName
 * @param visited set of already visited types, there might be recursion
 *                or multiple properties with the same type.
 */
private void createVersioningTable(final String schemaName, final FeatureType type, final Set<FeatureType> visited) throws VersioningException {
    if (visited.contains(type))
        return;
    visited.add(type);
    final String tableName = type.getName().tip().toString();
    final StringBuilder sb = new StringBuilder("SELECT \"HS_CreateHistory\"(");
    sb.append('\'');
    if (schemaName != null && !schemaName.isEmpty()) {
        sb.append(schemaName).append('.');
    }
    sb.append(tableName);
    sb.append('\'');
    sb.append(',');
    final List<String> hsColumnNames = new ArrayList<>();
    for (PropertyType desc : type.getProperties(true)) {
        if (AttributeConvention.contains(desc.getName()))
            continue;
        if (desc instanceof FeatureAssociationRole) {
            // complex type, create sub table history
            FeatureAssociationRole far = (FeatureAssociationRole) desc;
            createVersioningTable(schemaName, far.getValueType(), visited);
        } else if (desc instanceof AttributeType) {
            hsColumnNames.add("'" + desc.getName().tip() + '\'');
        }
    }
    Connection cnx = null;
    Statement stmt = null;
    try {
        cnx = featureStore.getDataSource().getConnection();
        stmt = cnx.createStatement();
        sb.append("array[");
        for (int i = 0, n = hsColumnNames.size(); i < n; i++) {
            if (i != 0)
                sb.append(',');
            sb.append(hsColumnNames.get(i));
        }
        sb.append("]);");
        stmt.executeQuery(sb.toString());
    } catch (SQLException ex) {
        throw new VersioningException(ex.getMessage(), ex);
    } finally {
        JDBCFeatureStoreUtilities.closeSafe(featureStore.getLogger(), cnx, stmt, null);
    }
}
Also used : SQLException(java.sql.SQLException) AttributeType(org.opengis.feature.AttributeType) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) VersioningException(org.geotoolkit.version.VersioningException) PropertyType(org.opengis.feature.PropertyType) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole)

Example 17 with FeatureAssociationRole

use of org.opengis.feature.FeatureAssociationRole in project geotoolkit by Geomatys.

the class GeoJSONWriteTest method copy.

/**
 * @param deep true for a deep copy
 */
private static Feature copy(Feature feature, boolean deep) {
    final FeatureType type = feature.getType();
    final Feature cp = type.newInstance();
    final Collection<? extends PropertyType> props = type.getProperties(true);
    for (PropertyType pt : props) {
        if (pt instanceof AttributeType) {
            final String name = pt.getName().toString();
            final Object val = feature.getPropertyValue(name);
            if (val != null) {
                cp.setPropertyValue(name, deep ? deepCopy(val) : val);
            }
        } else if (pt instanceof FeatureAssociationRole) {
            final String name = pt.getName().toString();
            final Object val = feature.getPropertyValue(name);
            if (deep) {
                if (val != null) {
                    cp.setPropertyValue(name, deepCopy(val));
                }
            } else {
                if (val instanceof Collection) {
                    final Collection col = (Collection) val;
                    final Collection cpCol = new ArrayList(col.size());
                    for (Iterator ite = col.iterator(); ite.hasNext(); ) {
                        cpCol.add(copy((Feature) ite.next()));
                    }
                    cp.setPropertyValue(name, cpCol);
                } else if (val != null) {
                    cp.setPropertyValue(name, copy((Feature) val));
                }
            }
        }
    }
    return cp;
}
Also used : FeatureType(org.opengis.feature.FeatureType) AttributeType(org.opengis.feature.AttributeType) PropertyType(org.opengis.feature.PropertyType) SimpleInternationalString(org.apache.sis.util.SimpleInternationalString) Feature(org.opengis.feature.Feature) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole)

Example 18 with FeatureAssociationRole

use of org.opengis.feature.FeatureAssociationRole in project geotoolkit by Geomatys.

the class FeatureTypeUtils method writeProperties.

private static void writeProperties(FeatureType ft, JsonGenerator writer) throws IOException {
    writer.writeObjectFieldStart(PROPERTIES);
    Collection<? extends PropertyType> descriptors = ft.getProperties(true);
    List<String> required = new ArrayList<>();
    for (PropertyType type : descriptors) {
        boolean isRequired = false;
        if (type instanceof FeatureAssociationRole) {
            isRequired = writeComplexType((FeatureAssociationRole) type, ((FeatureAssociationRole) type).getValueType(), writer);
        } else if (type instanceof AttributeType) {
            if (Geometry.class.isAssignableFrom(((AttributeType) type).getValueClass())) {
            // GeometryType geometryType = (GeometryType) type;
            // isRequired = writeGeometryType(descriptor, geometryType, writer);
            } else {
                isRequired = writeAttributeType(ft, (AttributeType) type, writer);
            }
        }
        if (isRequired) {
            required.add(type.getName().tip().toString());
        }
    }
    if (!required.isEmpty()) {
        writer.writeArrayFieldStart(REQUIRED);
        for (String req : required) {
            writer.writeString(req);
        }
        writer.writeEndArray();
    }
    writer.writeEndObject();
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) AttributeType(org.opengis.feature.AttributeType) ArrayList(java.util.ArrayList) SimpleInternationalString(org.apache.sis.util.SimpleInternationalString) InternationalString(org.opengis.util.InternationalString) PropertyType(org.opengis.feature.PropertyType) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole)

Example 19 with FeatureAssociationRole

use of org.opengis.feature.FeatureAssociationRole in project geotoolkit by Geomatys.

the class DefaultJDBCFeatureStore method createFeatureType.

// //////////////////////////////////////////////////////////////////////////
// Schema manipulation /////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////
/**
 * Complexe feature types will be decomposed in flat types then relations
 * will be rebuilded.
 */
@Override
public void createFeatureType(final FeatureType featureType) throws DataStoreException {
    ensureOpen();
    final GenericName typeName = featureType.getName();
    if (getNames().contains(typeName)) {
        throw new DataStoreException("Type name " + typeName + " already exists.");
    }
    Connection cnx = null;
    Statement stmt = null;
    String sql = null;
    final List<FeatureType> flatTypes = new ArrayList<>();
    final List<TypeRelation> relations = new ArrayList<>();
    try {
        cnx = getDataSource().getConnection();
        cnx.setAutoCommit(false);
        stmt = cnx.createStatement();
        // we must first decompose the feature type in flat types, then recreate relations
        decompose(featureType, flatTypes, relations);
        // rebuild flat types
        for (FeatureType flatType : flatTypes) {
            sql = getQueryBuilder().createTableSQL(flatType, cnx);
            stmt.execute(sql);
            dialect.postCreateTable(getDatabaseSchema(), flatType, cnx);
        }
        // rebuild relations
        if (!relations.isEmpty()) {
            // refresh the model to find primary keys
            dbmodel.clearCache();
            for (TypeRelation relation : relations) {
                final String propertyName = relation.property.getName().tip().toString();
                FeatureAssociationRole result = (FeatureAssociationRole) relation.property;
                final int minOccurs = result.getMinimumOccurs();
                final int maxOccurs = result.getMaximumOccurs();
                if (maxOccurs <= 1) {
                    // place relation on the children with a unique constraint
                    final AttributeTypeBuilder atb = new FeatureTypeBuilder().addAttribute(Integer.class);
                    atb.setName(propertyName);
                    final SQLQueryBuilder b = getQueryBuilder();
                    final String addColumn = b.alterTableAddColumnSQL(relation.child, atb.build(), cnx);
                    stmt.execute(addColumn);
                    final String addForeignKey = b.alterTableAddForeignKey(relation.child, propertyName, relation.parent.getName(), "fid", true);
                    stmt.execute(addForeignKey);
                    final String addUnique = b.alterTableAddIndex(relation.child, atb.getName().tip().toString());
                    stmt.execute(addUnique);
                } else {
                    // place relation on the children
                    final AttributeTypeBuilder atb = new FeatureTypeBuilder().addAttribute(Integer.class);
                    atb.setName(propertyName);
                    final SQLQueryBuilder b = getQueryBuilder();
                    final String addColumn = b.alterTableAddColumnSQL(relation.child, atb.build(), cnx);
                    stmt.execute(addColumn);
                    final String addForeignKey = b.alterTableAddForeignKey(relation.child, propertyName, relation.parent.getName(), "fid", true);
                    stmt.execute(addForeignKey);
                }
            }
        }
        cnx.commit();
    } catch (SQLException ex) {
        if (cnx != null) {
            try {
                cnx.rollback();
            } catch (SQLException ex1) {
                getLogger().log(Level.WARNING, ex1.getMessage(), ex1);
            }
        }
        throw new DataStoreException("Failed to create table " + typeName.tip() + "," + ex.getMessage() + "\n Query: " + sql, ex);
    } finally {
        JDBCFeatureStoreUtilities.closeSafe(getLogger(), cnx, stmt, null);
    }
    // reset the type name cache, will be recreated when needed.
    dbmodel.clearCache();
}
Also used : FeatureType(org.opengis.feature.FeatureType) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) DataStoreException(org.apache.sis.storage.DataStoreException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) SQLQueryBuilder(org.geotoolkit.db.dialect.SQLQueryBuilder) AttributeTypeBuilder(org.apache.sis.feature.builder.AttributeTypeBuilder) GenericName(org.opengis.util.GenericName) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole)

Example 20 with FeatureAssociationRole

use of org.opengis.feature.FeatureAssociationRole in project geotoolkit by Geomatys.

the class DefaultArrayFeature method getProperty.

@Override
public Property getProperty(String name) throws IllegalArgumentException {
    final int idx = getIndex(name);
    if (properties[idx] == null) {
        final PropertyType pt = getType().getProperty(name);
        if (pt instanceof Operation) {
            final Operation op = (Operation) pt;
            return op.apply(this, op.getParameters().createValue());
        } else if (pt instanceof AttributeType) {
            properties[idx] = ((AttributeType) pt).newInstance();
            ((Attribute) properties[idx]).setValue(values[idx]);
        } else if (pt instanceof FeatureAssociationRole) {
            properties[idx] = ((FeatureAssociationRole) pt).newInstance();
            ((FeatureAssociation) properties[idx]).setValue((Feature) values[idx]);
        }
    }
    return properties[idx];
}
Also used : FeatureAssociation(org.opengis.feature.FeatureAssociation) AttributeType(org.opengis.feature.AttributeType) PropertyType(org.opengis.feature.PropertyType) Operation(org.opengis.feature.Operation) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole)

Aggregations

FeatureAssociationRole (org.opengis.feature.FeatureAssociationRole)48 FeatureType (org.opengis.feature.FeatureType)31 PropertyType (org.opengis.feature.PropertyType)25 AttributeType (org.opengis.feature.AttributeType)21 Feature (org.opengis.feature.Feature)18 GenericName (org.opengis.util.GenericName)12 Collection (java.util.Collection)9 Test (org.junit.Test)9 Operation (org.opengis.feature.Operation)9 Connection (java.sql.Connection)7 SQLException (java.sql.SQLException)7 Statement (java.sql.Statement)6 ArrayList (java.util.ArrayList)6 QName (javax.xml.namespace.QName)6 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)6 SimpleInternationalString (org.apache.sis.util.SimpleInternationalString)5 Attribute (org.opengis.feature.Attribute)5 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)4 AttributeTypeBuilder (org.apache.sis.feature.builder.AttributeTypeBuilder)4 Timestamp (java.sql.Timestamp)3