use of eu.esdihumboldt.hale.io.gml.writer.internal.MultipartHandler in project hale by halestudio.
the class XPlanGmlInstanceWriter method partitionByPlan.
private void partitionByPlan(ProgressIndicator progress, IOReporter reporter) throws IOException {
final Set<TypeDefinition> planTypes = collectPlanTypes(getTargetSchema().getTypes());
/*
* Split instances into plan and non-plan instances. Associate the ID of
* a plan with its plan type and the plan instance.
*/
final XMLInspector gadget = new XMLInspector();
final DefaultInstanceCollection nonPlanInstances = new DefaultInstanceCollection();
final Map<String, TypeDefinition> planIdToPlanTypeMapping = new HashMap<>();
final Map<String, InstanceCollection> planIdToInstancesMapping = new HashMap<>();
try (ResourceIterator<Instance> it = getInstances().iterator()) {
while (it.hasNext()) {
Instance inst = it.next();
if (!planTypes.contains(inst.getDefinition())) {
nonPlanInstances.add(inst);
continue;
}
String planId = gadget.getIdentity(inst);
planIdToInstancesMapping.put(planId, new DefaultInstanceCollection(Arrays.asList(inst)));
planIdToPlanTypeMapping.put(planId, inst.getDefinition());
}
}
/*
* Collect referenced instances for every plan instance
*/
for (String planId : planIdToInstancesMapping.keySet()) {
MultiInstanceCollection mic = new MultiInstanceCollection(Arrays.asList(planIdToInstancesMapping.get(planId), nonPlanInstances));
ReferenceGraph<String> rg = new ReferenceGraph<String>(new XMLInspector(), mic);
Iterator<InstanceCollection> p = rg.partition(1, reporter);
while (p.hasNext()) {
boolean found = false;
InstanceCollection c = p.next();
Iterator<Instance> it = c.iterator();
while (it.hasNext()) {
Instance i = it.next();
if (planId.equals(gadget.getIdentity(i))) {
planIdToInstancesMapping.put(planId, c);
found = true;
break;
}
}
if (found) {
break;
}
}
}
final MultipartHandler handler = new MultipartHandler() {
@Override
public String getTargetFilename(InstanceCollection part, URI originalTarget) {
Path origPath = Paths.get(originalTarget).normalize();
Pair<String, String> nameAndExt = DefaultMultipartHandler.getFileNameAndExtension(origPath.toString());
String planId = null;
for (Entry<String, InstanceCollection> mapping : planIdToInstancesMapping.entrySet()) {
if (part == mapping.getValue()) {
planId = mapping.getKey();
break;
}
}
if (planId == null) {
throw new RuntimeException("Plan was not seen before");
}
// Replace all characters that are not allowed in XML IDs with
// an underscore. In addition, the colon (:) is also replaced
// to make sure that the resulting String can be used safely in
// a file name.
String sanitizedPlanId = planId.replaceAll("[^A-Za-z0-9-_.]", "_");
return String.format("%s%s%s.%s.%s.%s", origPath.getParent().toString(), File.separator, nameAndExt.getFirst(), planIdToPlanTypeMapping.get(planId).getDisplayName(), sanitizedPlanId, nameAndExt.getSecond());
}
};
try {
writeParts(planIdToInstancesMapping.values().iterator(), handler, progress, reporter);
} catch (XMLStreamException e) {
throw new IOException(e.getMessage(), e);
}
}
Aggregations