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);
}
}
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;
}
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();
}
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();
}
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];
}
Aggregations