Search in sources :

Example 1 with MultipartHandler

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);
    }
}
Also used : HashMap(java.util.HashMap) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) MultipartHandler(eu.esdihumboldt.hale.io.gml.writer.internal.MultipartHandler) DefaultMultipartHandler(eu.esdihumboldt.hale.io.gml.writer.internal.DefaultMultipartHandler) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) URI(java.net.URI) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) MultiInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.MultiInstanceCollection) ReferenceGraph(eu.esdihumboldt.hale.common.instance.graph.reference.ReferenceGraph) Path(java.nio.file.Path) MultiInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.MultiInstanceCollection) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLInspector(eu.esdihumboldt.hale.common.instance.graph.reference.impl.XMLInspector)

Aggregations

ReferenceGraph (eu.esdihumboldt.hale.common.instance.graph.reference.ReferenceGraph)1 XMLInspector (eu.esdihumboldt.hale.common.instance.graph.reference.impl.XMLInspector)1 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)1 DefaultInstanceCollection (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection)1 MultiInstanceCollection (eu.esdihumboldt.hale.common.instance.model.impl.MultiInstanceCollection)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 DefaultMultipartHandler (eu.esdihumboldt.hale.io.gml.writer.internal.DefaultMultipartHandler)1 MultipartHandler (eu.esdihumboldt.hale.io.gml.writer.internal.MultipartHandler)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 XMLStreamException (javax.xml.stream.XMLStreamException)1