use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class JDBCInstanceReader method execute.
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
progress.begin("Configure database connection", ProgressIndicator.UNKNOWN);
try {
testConnection();
String user = getParameter(PARAM_USER).as(String.class);
String password = getParameter(PARAM_PASSWORD).as(String.class);
Map<TypeDefinition, InstanceCollection> collections = new HashMap<>();
// only load instances for mapping relevant types
for (TypeDefinition type : getSourceSchema().getMappingRelevantTypes()) {
// check constraint if a Database table or not
if (type.getConstraint(DatabaseTable.class).isTable()) {
collections.put(type, new JDBCTableCollection(type, getSource().getLocation(), user, password, getCrsProvider(), getServiceProvider()) {
// To provide extensibility for getting customized
// database connection for
// Instance reading.
@Override
protected Connection createConnection() throws SQLException {
return JDBCInstanceReader.this.getConnection();
}
});
} else // database?
if (type.getConstraint(SQLQuery.class).hasQuery()) {
collections.put(type, new JDBCTableCollection(type, getSource().getLocation(), user, password, getCrsProvider(), getServiceProvider()) {
// To provide extensibility for getting customized
// database connection for
// Instance reading.
@Override
protected Connection createConnection() throws SQLException {
return JDBCInstanceReader.this.getConnection();
}
});
}
}
collection = new PerTypeInstanceCollection(collections);
reporter.setSuccess(true);
} catch (Exception e) {
reporter.error(new IOMessageImpl("Error configuring database connection", e));
reporter.setSuccess(false);
} finally {
progress.end();
}
return reporter;
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class InstanceIndexUpdateServiceImpl method reindex.
private void reindex() {
getIndexService().clearIndexedValues();
InstanceService is = serviceProvider.getService(InstanceService.class);
InstanceCollection source = is.getInstances(DataSet.SOURCE);
try (ResourceIterator<Instance> it = source.iterator()) {
while (it.hasNext()) {
Instance i = it.next();
InstanceReference ref = source.getReference(i);
if (Identifiable.is(ref)) {
ref = new IdentifiableInstanceReference(ref, Identifiable.getId(ref));
}
ResolvableInstanceReference rir = new ResolvableInstanceReference(ref, source);
getIndexService().add(i, rir);
}
}
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class ShapeInstanceReader method execute.
/**
* @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
*/
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
// $NON-NLS-1$
progress.begin(Messages.getString("ShapeSchemaProvider.1"), ProgressIndicator.UNKNOWN);
// DataStore store = new ShapefileDataStoreFactory().createDataStore(location.toURL());
// DataStore store = FileDataStoreFinder.getDataStore(getSource().getLocation().toURL());
ShapefileDataStore store = new ShapefileDataStore(getSource().getLocation().toURL());
store.setCharset(getCharset());
progress.setCurrentTask("Extracting shape instances");
String typename = getParameter(PARAM_TYPENAME).as(String.class);
TypeDefinition defaultType = null;
if (typename != null && !typename.isEmpty()) {
try {
defaultType = getSourceSchema().getType(QName.valueOf(typename));
} catch (Exception e) {
// ignore
}
}
if (defaultType == null) {
// check if typename was supplied w/o namespace
try {
defaultType = getSourceSchema().getType(new QName(ShapefileConstants.SHAPEFILE_NS, typename));
} catch (Exception e) {
// ignore
// TODO report?
}
}
if (defaultType == null) {
reporter.info(new IOMessageImpl("No type name supplied as parameter, trying to auto-detect the schema type.", null));
TypeDefinition dataType = ShapeSchemaReader.readShapeType(getSource());
if (dataType == null) {
throw new IOException("Could not read shapefile structure information");
}
String preferredName = null;
Name name = store.getNames().iterator().next();
if (name != null) {
preferredName = name.getLocalPart();
}
Pair<TypeDefinition, Integer> tp = getMostCompatibleShapeType(getSourceSchema(), dataType, preferredName);
if (tp == null) {
throw new IOProviderConfigurationException("No schema type specified and auto-detection failed");
}
defaultType = tp.getFirst();
reporter.info(new IOMessageImpl(MessageFormat.format("Auto-deteted {0} as schema type, with a {1}% compatibility rating.", defaultType.getName(), tp.getSecond()), null));
}
Map<TypeDefinition, InstanceCollection> collections = new HashMap<>();
// create a collection for each type
for (Name name : store.getNames()) {
SimpleFeatureSource features = store.getFeatureSource(name);
TypeDefinition type = defaultType;
if (type == null) {
QName typeName = new QName(ShapefileConstants.SHAPEFILE_NS, name.getLocalPart());
type = getSourceSchema().getType(typeName);
}
collections.put(type, new ShapesInstanceCollection(features, type, getCrsProvider(), name.getLocalPart()));
}
instances = new PerTypeInstanceCollection(collections);
reporter.setSuccess(true);
return reporter;
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class WFSFeatureCollectionWriter method writeAdditionalElements.
@Override
protected void writeAdditionalElements(XMLStreamWriter writer, TypeDefinition containerDefinition, IOReporter reporter) throws XMLStreamException {
// write additional needed attributes for WFS 2.0
boolean requiresCount = requiresCount();
boolean skipCount = getParameter(PARAM_SKIP_COUNT).as(Boolean.class, false);
if (requiresCount) {
String countString = null;
InstanceCollection source = getInstances();
if (source.hasSize()) {
// no iteration needed
countString = String.valueOf(source.size());
} else if (!skipCount) {
// count features
int count = 0;
// need to iterate collection to determine size
try (ResourceIterator<Instance> it = source.iterator()) {
while (it.hasNext()) {
Instance candidate = it.next();
if (GmlWriterUtil.isFeatureType(candidate.getDefinition())) {
count++;
}
}
}
countString = String.valueOf(count);
}
// numberMatched
if (countString != null) {
writer.writeAttribute("numberMatched", countString);
} else {
writer.writeAttribute("numberMatched", "unknown");
}
// numberReturned
if (countString != null) {
writer.writeAttribute("numberReturned", countString);
} else {
writer.writeAttribute("numberReturned", "0");
}
// timestamp
XmlDateTime result = XmlDateTime.Factory.newInstance();
result.setDateValue(new Date());
writer.writeAttribute("timeStamp", result.getStringValue());
}
super.writeAdditionalElements(writer, containerDefinition, reporter);
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class XLSReaderTest method testReadMultipleValues.
/**
* Test - read xls file and data. Check multiple values of one instance.
* Check formula cells
*
* @throws Exception , if an error occurs
*/
@Test
public void testReadMultipleValues() throws Exception {
// read schema ###
Schema schema = readXLSSchema("/data/cmplxSheetMultipleValues.xls", 0, typeName, "java.lang.String,java.lang.String,java.lang.String");
// read instance ###
InstanceCollection instances = readXLSInstances("/data/cmplxSheetMultipleValues.xls", 0, typeName, true, schema);
// Number of instances should be the same
assertTrue(instances.hasSize());
assertEquals(numberOfInstances, instances.size());
// Check blank property of first type
Instance instance = instances.iterator().next();
Object[] value;
value = instance.getProperty(QName.valueOf(properties[0]));
assertEquals("1234", value[0]);
value = instance.getProperty(QName.valueOf(properties[1]));
assertEquals("Glasses", value[0]);
value = instance.getProperty(QName.valueOf(properties[2]));
assertEquals("Pair of Glasses", value[0]);
}
Aggregations