Search in sources :

Example 11 with InstanceCollection

use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.

the class SpatiaLiteTestSuite method instanceWriterTest.

/**
 * Test - reads data from a source SpatiaLite database, writes them to a
 * target SpatiaLite database and checks the results.
 *
 * @throws Exception if an error occurs
 */
public void instanceWriterTest() throws Exception {
    if (!isSpatiaLiteExtensionAvailable()) {
        log.info("Skipping test because SpatiaLite extension is not available");
        return;
    }
    Map<String, Object> propertyMap = new HashMap<String, Object>();
    for (int i = 0; i < SOUURCE_TYPE_PROPERTY_NAMES.length; i++) {
        String key = SOUURCE_TYPE_PROPERTY_NAMES[i];
        Object value = SOUURCE_TYPE_PROPERTY_VALUES[i];
        propertyMap.put(key, value);
    }
    // ****** read Schema ******//
    Schema schema = readSchema(getSourceTempFilePath());
    assertNotNull(schema);
    assertEquals(1, schema.getMappingRelevantTypes().size());
    // Test properties
    TypeDefinition schemaType = schema.getMappingRelevantTypes().iterator().next();
    // Check every property for their existence
    checkType(schemaType, SOUURCE_TYPE_LOCAL_NAME, propertyMap.keySet());
    // ****** read Instances ******//
    InstanceCollection instances = readInstances(schema, getSourceTempFilePath());
    assertTrue(instances.hasSize());
    assertEquals(SOURCE_INSTANCES_COUNT, instances.size());
    checkInstances(instances, propertyMap);
    // ****** write Instances ******//
    // check target DB is empty
    InstanceCollection targetInstances = readInstances(schema, getTargetTempFilePath());
    assertTrue(targetInstances.hasSize());
    assertEquals(0, targetInstances.size());
    writeInstances(schema, getTargetTempFilePath(), instances);
    // re-read instances to check they were written correctly
    targetInstances = readInstances(schema, getTargetTempFilePath());
    assertTrue(targetInstances.hasSize());
    assertEquals(SOURCE_INSTANCES_COUNT, targetInstances.size());
    checkInstances(targetInstances, propertyMap);
}
Also used : HashMap(java.util.HashMap) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 12 with InstanceCollection

use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.

the class JDBCInstanceWriter method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    InstanceCollection instances = getInstances();
    Connection connection = null;
    try {
        // connect to the database
        try {
            connection = getConnection();
        } catch (Exception e) {
            reporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
            reporter.setSuccess(false);
            reporter.setSummary("Failed to connect to database.");
            return reporter;
        }
        if (isWriteUnordered()) {
            // write instances as they come in
            writeInstances(connection, instances, progress, reporter);
        } else {
            // write instances based on type order needed for insert
            // (to avoid violating constraints)
            Set<TypeDefinition> sortedSet = getSortedSchemas(getTargetSchema().getMappingRelevantTypes());
            for (TypeDefinition td : sortedSet) {
                writeInstances(connection, instances.select(new TypeFilter(td)), progress, reporter);
            }
        }
        reporter.setSuccess(true);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
        reporter.setSuccess(false);
        reporter.setSummary("Saving instances to database failed.");
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            // ignore
            }
        }
        progress.end();
    }
    return reporter;
}
Also used : SQLException(java.sql.SQLException) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) Connection(java.sql.Connection) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) TypeFilter(eu.esdihumboldt.hale.common.instance.model.TypeFilter) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) SQLException(java.sql.SQLException) IOException(java.io.IOException) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 13 with InstanceCollection

use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.

the class PointHandlerTest method testPointGml32_Grid.

/**
 * Test point geometries read from a GML 3.2 file.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testPointGml32_Grid() throws Exception {
    InstanceCollection instances = loadXMLInstances(getClass().getResource("/data/gml/geom-gml32.xsd").toURI(), getClass().getResource("/data/point/sample-point-gml32.xml").toURI(), gridConfig);
    // three instances expected
    ResourceIterator<Instance> it = instances.iterator();
    try {
        // 1. PointProperty with Point defined through coordinates
        assertTrue("First sample feature missing", it.hasNext());
        Instance instance = it.next();
        checkSingleGeometry(instance, gridChecker);
        // 2. GeometryProperty with Point defined through coordinates
        assertTrue("Second sample feature missing", it.hasNext());
        instance = it.next();
        checkSingleGeometry(instance, gridChecker);
        // 3. PointProperty with Point defined through pos
        assertTrue("Third sample feature missing", it.hasNext());
        instance = it.next();
        checkSingleGeometry(instance, gridChecker);
        // 4. GeometryProperty with Point defined through pos
        assertTrue("Fourth sample feature missing", it.hasNext());
        instance = it.next();
        checkSingleGeometry(instance, gridChecker);
    } finally {
        it.close();
    }
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) Test(org.junit.Test) AbstractHandlerTest(eu.esdihumboldt.hale.io.gml.geometry.handler.internal.AbstractHandlerTest)

Example 14 with InstanceCollection

use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.

the class PointHandlerTest method testPointGml31_Grid.

/**
 * Test point geometries read from a GML 3.1 file.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testPointGml31_Grid() throws Exception {
    InstanceCollection instances = loadXMLInstances(getClass().getResource("/data/gml/geom-gml31.xsd").toURI(), getClass().getResource("/data/point/sample-point-gml31.xml").toURI(), gridConfig);
    // three instances expected
    ResourceIterator<Instance> it = instances.iterator();
    try {
        // 1. PointProperty with Point defined through coordinates
        assertTrue("First sample feature missing", it.hasNext());
        Instance instance = it.next();
        checkSingleGeometry(instance, gridChecker);
        // 2. PointProperty with Point defined through coord
        assertTrue("Second sample feature missing", it.hasNext());
        instance = it.next();
        checkSingleGeometry(instance, gridChecker);
        // 3. GeometryProperty with Point defined through coord
        assertTrue("Third sample feature missing", it.hasNext());
        instance = it.next();
        checkSingleGeometry(instance, gridChecker);
        // 4. PointProperty with Point defined through pos
        assertTrue("Fourth sample feature missing", it.hasNext());
        instance = it.next();
        checkSingleGeometry(instance, gridChecker);
        // 5. GeometryProperty with Point defined through pos
        assertTrue("Fifth sample feature missing", it.hasNext());
        instance = it.next();
        checkSingleGeometry(instance, gridChecker);
    } finally {
        it.close();
    }
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) Test(org.junit.Test) AbstractHandlerTest(eu.esdihumboldt.hale.io.gml.geometry.handler.internal.AbstractHandlerTest)

Example 15 with InstanceCollection

use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.

the class PolygonHandlerTest method testPolygonGml31.

/**
 * Test polygon geometries read from a GML 3.1 file
 *
 * @throws Exception if an error occurs
 */
@Test
public void testPolygonGml31() throws Exception {
    InstanceCollection instances = AbstractHandlerTest.loadXMLInstances(getClass().getResource("/data/gml/geom-gml31.xsd").toURI(), getClass().getResource("/data/polygon/sample-polygon-gml31.xml").toURI());
    // one instance expected
    ResourceIterator<Instance> it = instances.iterator();
    try {
        // PolygonProperty with LinearRing defined through coordinates
        assertTrue("First sample feature missing", it.hasNext());
        Instance instance = it.next();
        checkSingleGeometry(instance, checker);
    } finally {
        it.close();
    }
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) Test(org.junit.Test) AbstractHandlerTest(eu.esdihumboldt.hale.io.gml.geometry.handler.internal.AbstractHandlerTest)

Aggregations

InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)151 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)131 Test (org.junit.Test)116 AbstractHandlerTest (eu.esdihumboldt.hale.io.gml.geometry.handler.internal.AbstractHandlerTest)97 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)17 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)17 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)11 ArrayList (java.util.ArrayList)10 Geometry (com.vividsolutions.jts.geom.Geometry)9 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)9 IOException (java.io.IOException)9 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)8 Polygon (com.vividsolutions.jts.geom.Polygon)8 QName (javax.xml.namespace.QName)8 Coordinate (com.vividsolutions.jts.geom.Coordinate)7 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)7 DefaultInstanceCollection (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection)7 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)6 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)6 TypeFilter (eu.esdihumboldt.hale.common.instance.model.TypeFilter)6