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