001package org.hl7.fhir.r5.utils.validation;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import org.hl7.fhir.exceptions.FHIRException;
007import org.hl7.fhir.r5.elementmodel.Element;
008import org.hl7.fhir.r5.model.DomainResource;
009import org.hl7.fhir.r5.model.Questionnaire;
010import org.hl7.fhir.r5.model.Resource;
011import org.hl7.fhir.utilities.validation.ValidationMessage;
012
013public class ValidationContextCarrier {
014  /**
015   * 
016   * When the validator is calling validateCode, it typically has a partially loaded resource that may provide
017   * additional resources that are relevant to the validation. This is a handle back into the validator context
018   * to ask for the resource to be fully loaded if it becomes relevant. Note that the resource may fail to load 
019   * (e.g. if it's part of what's being validated) and if it does, the validator will record the validation 
020   * issues before throwing an error
021   *  
022   * This is a reference back int
023   *
024   */
025  public interface IValidationContextResourceLoader {     
026    public Resource loadContainedResource(List<ValidationMessage> errors, String path, Element resource, String id, Class<? extends Resource> class1) throws FHIRException;
027  }
028
029  /**
030   * A list of resources that provide context - typically, a container resource, and a bundle resource. 
031   * iterate these in order looking for contained resources 
032   * 
033   */
034  public static class ValidationContextResourceProxy {
035
036    // either a resource
037    private Resource resource;
038    
039    
040    // or an element and a loader
041    private Element element;
042    private IValidationContextResourceLoader loader;
043    private List<ValidationMessage> errors;
044    private String path;
045    
046    public ValidationContextResourceProxy(Resource resource) {
047      this.resource = resource;
048    }
049
050    public ValidationContextResourceProxy(List<ValidationMessage> errors, String path, Element element,  IValidationContextResourceLoader loader) {
051      this.errors = errors;
052      this.path = path;
053      this.element = element;
054      this.loader = loader;
055    }
056
057    public Resource loadContainedResource(String id, Class<? extends Resource> class1) throws FHIRException {
058      if (resource == null) {
059        Resource res = loader.loadContainedResource(errors, path, element, id, class1);
060        return res;        
061      } else {
062        if (resource instanceof DomainResource) {
063          for (Resource r : ((DomainResource) resource).getContained()) {
064            if (r.getId().equals(id)) {
065              if (class1.isInstance(r)) 
066                return r;              
067            }
068          }
069        }
070        return null;       
071      }
072    }
073  }
074  
075  private List<ValidationContextResourceProxy> resources = new ArrayList<>();
076
077  public List<ValidationContextResourceProxy> getResources() {
078    return resources;
079  }
080  
081}