Search in sources :

Example 1 with TypeFilter

use of eu.esdihumboldt.hale.common.instance.model.TypeFilter 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 2 with TypeFilter

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

the class FilteredInstanceCollection method applyFilter.

/**
 * Create an instance collection that applies a filter to the given instance
 * collection.
 *
 * @param instances the instance collection to filter
 * @param filter the filter
 * @return the filtered instance collection
 */
public static InstanceCollection applyFilter(InstanceCollection instances, Filter filter) {
    if (filter instanceof TypeFilter && instances instanceof InstanceCollection2) {
        /*
			 * For type filters check if we can make use of fan-out.
			 */
        InstanceCollection2 instances2 = (InstanceCollection2) instances;
        if (instances2.supportsFanout()) {
            TypeDefinition type = ((TypeFilter) filter).getType();
            InstanceCollection result = instances2.fanout().get(type);
            if (result == null) {
                result = EmptyInstanceCollection.INSTANCE;
            }
            return result;
        }
    }
    // create a filtered collection
    return new FilteredInstanceCollection(instances, filter);
}
Also used : EmptyInstanceCollection(eu.esdihumboldt.hale.common.instance.model.ext.helper.EmptyInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) InstanceCollection2(eu.esdihumboldt.hale.common.instance.model.ext.InstanceCollection2) TypeFilter(eu.esdihumboldt.hale.common.instance.model.TypeFilter) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 3 with TypeFilter

use of eu.esdihumboldt.hale.common.instance.model.TypeFilter 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);
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) TypeFilter(eu.esdihumboldt.hale.common.instance.model.TypeFilter) Test(org.junit.Test)

Example 4 with TypeFilter

use of eu.esdihumboldt.hale.common.instance.model.TypeFilter 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;
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) TypeFilter(eu.esdihumboldt.hale.common.instance.model.TypeFilter) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection)

Example 5 with TypeFilter

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

the class InstanceTestValues method getInstance.

/**
 * Get an instance that may hold a value for the given property.
 *
 * @param entity the property
 * @return an instance or <code>null</code>
 */
protected Instance getInstance(EntityDefinition entity) {
    // TODO cache instance?
    InstanceService is = PlatformUI.getWorkbench().getService(InstanceService.class);
    InstanceCollection instances = is.getInstances(DataSet.SOURCE).select(new TypeFilter(entity.getType()));
    if (entity.getFilter() != null) {
        instances = instances.select(entity.getFilter());
    }
    ResourceIterator<Instance> it = instances.iterator();
    try {
        // TODO use a random instance?
        if (it.hasNext()) {
            return it.next();
        }
    } finally {
        it.close();
    }
    return null;
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) InstanceService(eu.esdihumboldt.hale.ui.service.instance.InstanceService) TypeFilter(eu.esdihumboldt.hale.common.instance.model.TypeFilter)

Aggregations

InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)6 TypeFilter (eu.esdihumboldt.hale.common.instance.model.TypeFilter)6 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)3 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)3 Geometry (com.vividsolutions.jts.geom.Geometry)1 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 InstanceCollection2 (eu.esdihumboldt.hale.common.instance.model.ext.InstanceCollection2)1 EmptyInstanceCollection (eu.esdihumboldt.hale.common.instance.model.ext.helper.EmptyInstanceCollection)1 DefaultInstanceCollection (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection)1 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)1 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)1 InstanceService (eu.esdihumboldt.hale.ui.service.instance.InstanceService)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 QName (javax.xml.namespace.QName)1 Test (org.junit.Test)1