Search in sources :

Example 1 with ResourceIterator

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

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

the class IndexMergeHandler method partitionInstances.

/**
 * @see eu.esdihumboldt.cst.functions.core.merge.AbstractMergeHandler#partitionInstances(eu.esdihumboldt.hale.common.instance.model.InstanceCollection,
 *      java.lang.String,
 *      eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
 *      com.google.common.collect.ListMultimap, java.util.Map,
 *      eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
 */
@Override
public ResourceIterator<FamilyInstance> partitionInstances(InstanceCollection instances, String transformationIdentifier, TransformationEngine engine, ListMultimap<String, ParameterValue> transformationParameters, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
    PropertiesMergeHandler fallbackHandler = new PropertiesMergeHandler();
    InstanceIndexService indexService = serviceProvider.getService(InstanceIndexService.class);
    if (indexService == null) {
        log.warn(MessageFormat.format("Index service not available, falling back to merge handler {0}", fallbackHandler.getClass().getCanonicalName()));
        return fallbackHandler.partitionInstances(instances, transformationIdentifier, engine, transformationParameters, executionParameters, log);
    }
    final IndexMergeConfig mergeConfig = createMergeConfiguration(transformationParameters);
    QName typeName;
    try (ResourceIterator<Instance> it = instances.iterator()) {
        if (it.hasNext()) {
            typeName = it.next().getDefinition().getName();
        } else {
            // Nothing to partition
            return new ResourceIterator<FamilyInstance>() {

                @Override
                public boolean hasNext() {
                    return false;
                }

                @Override
                public FamilyInstance next() {
                    return null;
                }

                @Override
                public void close() {
                // Do nothing
                }
            };
        }
    }
    // Querying the index will yield a result over all instances. We must,
    // however, be able to operate only on the given input instances instead
    // of all instances.
    // We must, therefore, be able to uniquely identify every instance in
    // the index, so that we can retain from the index query only the
    // relevant instances.
    List<Object> inputInstanceIds = new ArrayList<>();
    try (ResourceIterator<Instance> it = instances.iterator()) {
        while (it.hasNext()) {
            Instance i = InstanceDecorator.getRoot(it.next());
            if (!Identifiable.is(i)) {
                log.warn(MessageFormat.format("At least one instance does not have an ID, falling back to merge handler {0}", fallbackHandler.getClass().getCanonicalName()));
                return fallbackHandler.partitionInstances(instances, transformationIdentifier, engine, transformationParameters, executionParameters, log);
            }
            inputInstanceIds.add(Identifiable.getId(i));
        }
    }
    Collection<Collection<ResolvableInstanceReference>> partitionedIndex = indexService.groupBy(typeName, mergeConfig.keyProperties);
    // Remove instance groups from the partitioned index where none of the
    // instances in the group are in the processed instances.
    partitionedIndex.removeIf(part -> !part.stream().map(ref -> ref.getId()).anyMatch(id -> inputInstanceIds.contains(id)));
    Iterator<Collection<ResolvableInstanceReference>> it = partitionedIndex.iterator();
    return new ResourceIterator<FamilyInstance>() {

        @Override
        public boolean hasNext() {
            return it.hasNext();
        }

        @Override
        public FamilyInstance next() {
            Collection<ResolvableInstanceReference> instanceRefs = it.next();
            InstanceCollection instancesToBeMerged = new DefaultInstanceCollection(instanceRefs.stream().map(ref -> ref.resolve()).collect(Collectors.toList()));
            return new FamilyInstanceImpl(merge(instancesToBeMerged, mergeConfig));
        }

        @Override
        public void close() {
        // TODO Auto-generated method stub
        }
    };
}
Also used : MergeUtil(eu.esdihumboldt.hale.common.align.model.functions.merge.MergeUtil) ListMultimap(com.google.common.collect.ListMultimap) ServiceProviderAware(eu.esdihumboldt.hale.common.core.service.ServiceProviderAware) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference) InstanceFactory(eu.esdihumboldt.hale.common.instance.model.InstanceFactory) InstanceIndexService(eu.esdihumboldt.hale.common.instance.index.InstanceIndexService) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Identifiable(eu.esdihumboldt.hale.common.instance.model.Identifiable) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Map(java.util.Map) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) InstanceDecorator(eu.esdihumboldt.hale.common.instance.model.impl.InstanceDecorator) InstanceHandler(eu.esdihumboldt.hale.common.align.transformation.function.InstanceHandler) DeepIterableKey(eu.esdihumboldt.hale.common.instance.index.DeepIterableKey) Iterator(java.util.Iterator) MergeFunction(eu.esdihumboldt.hale.common.align.model.functions.MergeFunction) Collection(java.util.Collection) TransformationLog(eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog) Set(java.util.Set) TransformationEngine(eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine) ServiceProvider(eu.esdihumboldt.hale.common.core.service.ServiceProvider) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) Collectors(java.util.stream.Collectors) InstanceMetadata(eu.esdihumboldt.hale.common.instance.model.InstanceMetadata) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) FamilyInstanceImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl) HalePlatform(eu.esdihumboldt.hale.common.core.HalePlatform) List(java.util.List) ResourceIterator(eu.esdihumboldt.hale.common.instance.model.ResourceIterator) QName(javax.xml.namespace.QName) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) ArrayList(java.util.ArrayList) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) FamilyInstanceImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl) Collection(java.util.Collection) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) ResourceIterator(eu.esdihumboldt.hale.common.instance.model.ResourceIterator) ResolvableInstanceReference(eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference) InstanceIndexService(eu.esdihumboldt.hale.common.instance.index.InstanceIndexService)

Aggregations

Instance (eu.esdihumboldt.hale.common.instance.model.Instance)2 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)2 ResourceIterator (eu.esdihumboldt.hale.common.instance.model.ResourceIterator)2 ListMultimap (com.google.common.collect.ListMultimap)1 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)1 MergeFunction (eu.esdihumboldt.hale.common.align.model.functions.MergeFunction)1 MergeUtil (eu.esdihumboldt.hale.common.align.model.functions.merge.MergeUtil)1 TransformationEngine (eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine)1 InstanceHandler (eu.esdihumboldt.hale.common.align.transformation.function.InstanceHandler)1 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)1 FamilyInstanceImpl (eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl)1 TransformationLog (eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)1 HalePlatform (eu.esdihumboldt.hale.common.core.HalePlatform)1 ServiceProvider (eu.esdihumboldt.hale.common.core.service.ServiceProvider)1 ServiceProviderAware (eu.esdihumboldt.hale.common.core.service.ServiceProviderAware)1 DeepIterableKey (eu.esdihumboldt.hale.common.instance.index.DeepIterableKey)1 InstanceIndexService (eu.esdihumboldt.hale.common.instance.index.InstanceIndexService)1 FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)1 Identifiable (eu.esdihumboldt.hale.common.instance.model.Identifiable)1 InstanceFactory (eu.esdihumboldt.hale.common.instance.model.InstanceFactory)1