Search in sources :

Example 11 with FlowRef

use of org.openlca.io.maps.FlowRef in project olca-app by GreenDelta.

the class Sync method packageSync.

/**
 * Synchronizes the given (external) flow references which are the target or
 * source flow definitions of a mapping with the (internal) flow references
 * provided by the given data package (which can be an ILCD or JSON-LD package).
 * The sync.-state of the flow references of the mapping is mutated so that it
 * can be displayed in the mapping tool.
 */
static void packageSync(IProvider pack, Stream<FlowRef> mappingRefs) {
    if (mappingRefs == null || pack == null)
        return;
    Map<String, FlowRef> packRefs = pack.getFlowRefs().stream().collect(Collectors.toMap(ref -> ref.flow.refId, ref -> ref));
    mappingRefs.forEach(mapRef -> {
        if (Sync.isInvalidFlowRef(mapRef))
            return;
        // we update the status in the following sync. steps
        mapRef.status = null;
        // check the flow
        String flowID = mapRef.flow.refId;
        FlowRef packRef = packRefs.get(flowID);
        if (packRef == null) {
            mapRef.status = MappingStatus.error("there is no flow with id=" + flowID + " in the data package");
            return;
        }
        // the IDs are equal, the flow properties should have the same name
        if (mapRef.property == null) {
            mapRef.property = packRef.property;
        } else {
            if (packRef.property == null) {
                mapRef.status = MappingStatus.error("the flow in the data package" + " has no corresponding flow property");
                return;
            }
            Descriptor packProp = packRef.property;
            Descriptor mapProp = mapRef.property;
            if (Strings.nullOrEqual(packProp.refId, mapProp.refId) && packProp.name != null && mapProp.name != null && !Strings.nullOrEqual(packProp.name, mapProp.name)) {
                mapRef.status = MappingStatus.error("the flow property in the data " + "package has the same ID but a different name: " + packProp.name + " != " + mapProp.name);
                return;
            }
        }
        // when the IDs are equal, the names must be equal too
        if (mapRef.unit == null) {
            mapRef.unit = packRef.unit;
        } else if (packRef.unit != null) {
            // mapping packages not always contain their reference units
            // if (packRef.unit == null) {
            // mapRef.status = Status.error("the flow in the data package"
            // + " has no corresponding unit");
            // return;
            // }
            Descriptor packUnit = packRef.unit;
            Descriptor mapUnit = mapRef.unit;
            if (Strings.nullOrEqual(packUnit.refId, mapUnit.refId) && packUnit.name != null && mapUnit.name != null && !Strings.nullOrEqual(packUnit.name, mapUnit.name)) {
                mapRef.status = MappingStatus.error("the flow unit in the data " + "package has the same ID but a different name: " + packUnit.name + " != " + mapUnit.name);
                return;
            }
        }
        Sync.checkFlowName(mapRef, packRef.flow.name);
        Sync.checkFlowCategory(mapRef, packRef.flowCategory);
        Sync.checkFlowType(mapRef, packRef.flow.flowType);
        Sync.checkFlowLocation(mapRef, packRef.flowLocation);
        if (mapRef.status == null) {
            mapRef.status = MappingStatus.ok("flow in sync with data package");
        }
    });
}
Also used : FlowType(org.openlca.core.model.FlowType) Descriptor(org.openlca.core.model.descriptors.Descriptor) Stream(java.util.stream.Stream) Strings(org.openlca.util.Strings) Map(java.util.Map) Collectors(java.util.stream.Collectors) MappingStatus(org.openlca.io.maps.MappingStatus) FlowRef(org.openlca.io.maps.FlowRef) FlowRef(org.openlca.io.maps.FlowRef) Descriptor(org.openlca.core.model.descriptors.Descriptor)

Aggregations

FlowRef (org.openlca.io.maps.FlowRef)11 FlowMapEntry (org.openlca.io.maps.FlowMapEntry)5 Flow (org.openlca.core.model.Flow)4 FlowType (org.openlca.core.model.FlowType)4 ArrayList (java.util.ArrayList)3 FlowDao (org.openlca.core.database.FlowDao)3 Map (java.util.Map)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 DBProvider (org.openlca.app.tools.mapping.model.DBProvider)2 Fn (org.openlca.app.util.Fn)2 IDatabase (org.openlca.core.database.IDatabase)2 LocationDao (org.openlca.core.database.LocationDao)2 ProcessDao (org.openlca.core.database.ProcessDao)2 Descriptor (org.openlca.core.model.descriptors.Descriptor)2 MappingStatus (org.openlca.io.maps.MappingStatus)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1