001package org.hl7.fhir.validation.instance.utils;
002
003import java.util.ArrayList;
004import java.util.HashMap;
005import java.util.List;
006import java.util.Map;
007
008import org.hl7.fhir.r5.model.StructureDefinition;
009import org.hl7.fhir.utilities.validation.ValidationMessage;
010
011/**
012 * The validator keeps one of these classes for each resource it validates (e.g. when chasing references)
013 * <p>
014 * It keeps track of the state of resource validation - a given resrouce may be validated agaisnt multiple
015 * profiles during a single validation, and it may be valid against some, and invalid against others.
016 * <p>
017 * We don't want to keep doing the same validation, so we remember the outcomes for each combination
018 * of resource + profile
019 * <p>
020 * Additionally, profile validation may be circular - e.g. a Patient profile that applies the same profile
021 * to linked patients, and 2 patient resources that link to each other. So this class also tracks validation
022 * in process.
023 * <p>
024 * If the validator comes back to the same point, and validates the same instance against the same profile
025 * while it's already validating, it assumes it's valid - it cannot have new errors that wouldn't already
026 * be found in the first iteration - in other words, there's no errors
027 *
028 * @author graha
029 */
030public class ResourceValidationTracker {
031    private Map<String, List<ValidationMessage>> validations = new HashMap<>();
032
033    public void startValidating(StructureDefinition sd) {
034        validations.put(sd.getUrl(), new ArrayList<ValidationMessage>());
035    }
036
037    public List<ValidationMessage> getOutcomes(StructureDefinition sd) {
038        return validations.get(sd.getUrl());
039    }
040
041    public void storeOutcomes(StructureDefinition sd, List<ValidationMessage> errors) {
042        validations.put(sd.getUrl(), errors);
043    }
044}