use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class MsSqlDataReaderTest method testInstanceReader.
/**
* Test instance reader
*
* @throws Exception if error occurred in reading instances
*/
@SuppressWarnings("unchecked")
@Test
public void testInstanceReader() throws Exception {
// ****** read Schema ******//
Schema schema = readSchema();
assertEquals(3, schema.getMappingRelevantTypes().size());
// ****** read Instances ******//
InstanceCollection instances = readInstances(schema);
assertTrue(instances.hasSize());
assertEquals(SOURCE_TOTAL_INSTANCES_COUNT, instances.size());
InstanceCollection filteredInstances = null;
// Check SpatialTable "jdbc:sqlserver:dbo","SpatialTable"
filteredInstances = instances.select(new TypeFilter(schema.getType(new QName("jdbc:sqlserver:dbo", "SpatialTable"))));
int geometryPropertyCount = 0;
int idPropertyCount = 0;
ResourceIterator<Instance> it = filteredInstances.iterator();
while (it.hasNext()) {
Instance in = it.next();
for (String propertyName : SOURCE_GEOMETRY_TYPE_PROPERTY_NAMES) {
Object value = in.getProperty(QName.valueOf(propertyName))[0];
if (value == null)
continue;
if (value instanceof GeometryProperty) {
assertTrue(((GeometryProperty<Geometry>) value).getGeometry().toText().equalsIgnoreCase(String.valueOf(PROPERTY_GEO_VALUES[geometryPropertyCount])));
geometryPropertyCount++;
} else {
assertTrue(((int) value) == ((int) PROPERTY_ID_VALUES[idPropertyCount]));
idPropertyCount++;
}
}
}
assertEquals(SOURCE_GEOMETRY_INSTANCE_COUNT, geometryPropertyCount);
assertEquals(SOURCE_GEOMETRY_INSTANCE_COUNT, idPropertyCount);
}
use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class SpatiaLiteTestSuite method checkInstances.
/**
* Checks the property definitions and values of the provided instances.
* Values will be checked for just one instance (the one with
* {@link #PROPERTY_ID_NAME} = {@link #PROPERTY_ID_VALUE}).
*
* @param instances the instances to check
* @param propertyMap the expected property names / values
*/
@SuppressWarnings("rawtypes")
public void checkInstances(InstanceCollection instances, Map<String, Object> propertyMap) {
// get type to check property definition
TypeDefinition type = instances.iterator().next().getDefinition();
checkType(type, SOUURCE_TYPE_LOCAL_NAME, propertyMap.keySet());
// Check the values of Instance with ID = 1
Instance instance = null;
Iterator<Instance> instanceIterator = instances.iterator();
while (instanceIterator.hasNext()) {
Instance currentInstance = instanceIterator.next();
Integer id = (Integer) currentInstance.getProperty(QName.valueOf(PROPERTY_ID_NAME))[0];
if (PROPERTY_ID_VALUE.equals(id)) {
instance = currentInstance;
break;
}
}
if (instance == null) {
fail(String.format("No instance found with %s = %s", PROPERTY_ID_NAME, PROPERTY_ID_VALUE));
}
for (String propertyName : propertyMap.keySet()) {
@SuppressWarnings("null") Object value = instance.getProperty(QName.valueOf(propertyName))[0];
if (value instanceof GeometryProperty) {
assertTrue(((Geometry) propertyMap.get(propertyName)).equalsExact(((GeometryProperty) value).getGeometry(), 0.000001));
} else {
assertEquals(propertyMap.get(propertyName), value);
}
}
}
use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class AbstractDBTest method readAndCountInstances.
/**
* Read the instances from the db, check if it is same as instances written
* to the db.
*
* @param originalInstances instance created and written to db
* @param schema schema read
* @param gType the geometry type definition.
* @return The count of instances which are equal to the original instances
* @throws Exception if reading the instances fails.
*/
protected int readAndCountInstances(InstanceCollection originalInstances, Schema schema, TypeDefinition gType) throws Exception {
InstanceCollection instancesRead = readInstances(schema).select(new TypeFilter(gType));
List<Instance> originals = new DefaultInstanceCollection(originalInstances).toList();
ResourceIterator<Instance> ri = instancesRead.iterator();
int count = 0;
try {
while (ri.hasNext()) {
Instance instance = ri.next();
String error = InstanceUtil.checkInstance(instance, originals);
assertNull(error, error);
count++;
}
} finally {
ri.close();
}
return count;
}
use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class JDBCInstanceWriter method writeInstances.
/**
* Write instances to a database connection Auto incremental keys are
* written to the database with the new generated values, and updated the
* foreign key values with this newly generated auto incremental values of
* the referenced column.
*
* <p>
* <b>Limitation:</b>
* </p>
* This implementation for auto generated key insert is supported only for
* those tables which has only single auto generated fields.
*
* @param connection the database connection
* @param instances the instances to write
* @param progress the progress indicator
* @param reporter the reporter
* @throws Exception if saving the instances fails
*/
private void writeInstances(Connection connection, InstanceCollection instances, ProgressIndicator progress, IOReporter reporter) throws Exception {
connection.setAutoCommit(false);
boolean trackProgress = instances.hasSize();
progress.begin("Write instances to database", (trackProgress) ? (instances.size()) : (ProgressIndicator.UNKNOWN));
// maps type definitions to prepared statements
Map<TypeDefinition, Map<Set<QName>, PreparedStatement>> typeStatements = new HashMap<TypeDefinition, Map<Set<QName>, PreparedStatement>>();
Map<TypeDefinition, Map<Set<QName>, Integer>> typeCount = new HashMap<TypeDefinition, Map<Set<QName>, Integer>>();
ResourceIterator<Instance> it = instances.iterator();
try {
while (it.hasNext() && !progress.isCanceled()) {
Instance instance = it.next();
TypeDefinition type = instance.getDefinition();
Set<QName> properties = new HashSet<QName>();
for (QName property : instance.getPropertyNames()) properties.add(property);
filterInsertProperties(type, properties);
// per type count
Map<Set<QName>, Integer> typeCountMap = typeCount.get(type);
if (typeCountMap == null) {
typeCountMap = new HashMap<Set<QName>, Integer>();
typeCount.put(type, typeCountMap);
}
Integer count = typeCountMap.get(properties);
if (count == null)
count = 0;
typeCountMap.put(properties, count + 1);
// get prepared statement for instance type
PreparedStatement statement = getInsertStatement(type, properties, typeStatements, connection);
// populate insert statement with values
populateInsertStatementOrExecuteAutoIncStatement(statement, properties, instance, reporter, connection);
if (count % 100 == 0) {
statement.executeBatch();
// TODO statement.getGeneratedKeys() / does not work with
// batches for PostgreSQL
}
if (trackProgress) {
progress.advance(1);
}
}
// execute remaining batches
for (Map<Set<QName>, PreparedStatement> typeSpecificMap : typeStatements.values()) {
if (progress.isCanceled())
break;
for (PreparedStatement statement : typeSpecificMap.values()) {
if (progress.isCanceled())
break;
statement.executeBatch();
}
}
if (!progress.isCanceled())
connection.commit();
else
connection.rollback();
} catch (Exception e) {
if (e instanceof SQLException) {
SQLException next = ((SQLException) e).getNextException();
while (next != null) {
log.error("SQL exception", next);
next = next.getNextException();
}
}
try {
connection.rollback();
} catch (Exception e1) {
// ignore
}
throw e;
} finally {
// close iterator
it.close();
// close statements
for (Map<Set<QName>, PreparedStatement> typeSpecificMap : typeStatements.values()) for (PreparedStatement statement : typeSpecificMap.values()) statement.close();
}
// right now cancel => rollback. Otherwise this would have to be in
// front of it.close()...
// if (progress.isCanceled() && it.hasNext()) {
// reporter.error(new IOMessageImpl("Saving to database was canceled, not all instances were saved.", null));
// }
}
use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class JacksonMapper method streamWriteCollection.
/**
* Writes a collection of instances into JSON
*
* @param out the output supplier
* @param instances the collection of instances
* @param reporter the reporter
* @throws IOException if writing
*/
public void streamWriteCollection(LocatableOutputSupplier<? extends OutputStream> out, InstanceCollection instances, IOReporter reporter) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out.getOutput(), Charset.forName("UTF-8")))) {
// initialize Jackson Json Streaming Api
JsonFactory jsonFactory = new JsonFactory();
// initialize GeoJSON Api
geometryJson = new GeometryJSON();
jsonGen = jsonFactory.createJsonGenerator(writer);
jsonGen.useDefaultPrettyPrinter();
jsonGen.writeStartArray();
// iterate through Instances
try (ResourceIterator<Instance> itInstance = instances.iterator()) {
while (itInstance.hasNext()) {
Instance instance = itInstance.next();
jsonGen.writeStartObject();
jsonGen.writeFieldName(instance.getDefinition().getName().getLocalPart());
streamWriteInstanceValue(instance, reporter);
jsonGen.writeEndObject();
}
}
jsonGen.writeEndArray();
jsonGen.flush();
}
// FIXME - rather move to a validator?!
// XXX cannot cope with GZiped file
// URI targetLoc = out.getLocation();
// if (targetLoc != null) {
// File file = new File(targetLoc);
// try (InputStream in = Files.newInputStream(file.toPath())) {
// isValidJSON(in, reporter);
// }
// }
}
Aggregations