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