Search in sources :

Example 76 with InstanceCollection

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;
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) MultiInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.MultiInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) PerTypeInstanceCollection(eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection) Connection(java.sql.Connection) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) IOException(java.io.IOException) SQLException(java.sql.SQLException) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DatabaseTable(eu.esdihumboldt.hale.io.jdbc.constraints.DatabaseTable) PerTypeInstanceCollection(eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection)

Example 77 with InstanceCollection

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);
        }
    }
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference) InstanceReference(eu.esdihumboldt.hale.common.instance.model.InstanceReference) IdentifiableInstanceReference(eu.esdihumboldt.hale.common.instance.model.IdentifiableInstanceReference) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) IdentifiableInstanceReference(eu.esdihumboldt.hale.common.instance.model.IdentifiableInstanceReference) InstanceService(eu.esdihumboldt.hale.ui.service.instance.InstanceService) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference)

Example 78 with InstanceCollection

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;
}
Also used : ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) PerTypeInstanceCollection(eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOException(java.io.IOException) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Name(org.opengis.feature.type.Name) QName(javax.xml.namespace.QName) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) PerTypeInstanceCollection(eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection)

Example 79 with InstanceCollection

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);
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) XmlDateTime(org.apache.xmlbeans.XmlDateTime) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) ResourceIterator(eu.esdihumboldt.hale.common.instance.model.ResourceIterator) Date(java.util.Date)

Example 80 with InstanceCollection

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]);
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) Test(org.junit.Test)

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