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