001package org.hl7.fhir.validation.instance.utils;
002
003import java.util.HashMap;
004import java.util.List;
005import java.util.Map;
006
007import org.hl7.fhir.r5.elementmodel.Element;
008import org.hl7.fhir.r5.model.StructureDefinition;
009import org.hl7.fhir.utilities.validation.ValidationMessage;
010
011public class ValidatorHostContext {
012
013    private Object appContext;
014
015    // the resource we are actually validating right now
016    private Element resource; 
017    // the resource that is the scope of id resolution - either the same as resource, or the resource the contains that resource. This can only be one level deep.
018    private Element rootResource;
019    private Element groupingResource; // either a bundle or a parameters that holds the rootResource (for reference resolution)
020    
021    private StructureDefinition profile; // the profile that contains the content being validated
022    private boolean checkSpecials = true;
023    private Map<String, List<ValidationMessage>> sliceRecords;
024
025    public ValidatorHostContext(Object appContext) {
026        this.appContext = appContext;
027    }
028
029    public ValidatorHostContext(Object appContext, Element element) {
030      this.appContext = appContext;
031      this.resource = element;
032      this.rootResource = element;
033      // no groupingResource (Bundle or Parameters)
034      dump("creating");
035  }
036
037    public ValidatorHostContext(Object appContext, Element element, Element root) {
038      this.appContext = appContext;
039      this.resource = element;
040      this.rootResource = root;
041      // no groupingResource (Bundle or Parameters)
042      dump("creating");
043  }
044
045    public ValidatorHostContext(Object appContext, Element element, Element root, Element groupingResource) {
046      this.appContext = appContext;
047      this.resource = element;
048      this.rootResource = root;
049      this.groupingResource = groupingResource;
050      dump("creating");
051  }
052
053    public Object getAppContext() {
054        return appContext;
055    }
056
057    public ValidatorHostContext setAppContext(Object appContext) {
058        this.appContext = appContext;
059        return this;
060    }
061
062    public ValidatorHostContext setResource(Element resource) {
063        this.resource = resource;
064        return this;
065    }
066
067    public Element getRootResource() {
068        return rootResource;
069    }
070
071    public ValidatorHostContext setRootResource(Element rootResource) {
072        this.rootResource = rootResource;
073        dump("setting root resource");
074        return this;
075    }
076
077    public Element getGroupingResource() {
078      return groupingResource;
079    }
080
081    public StructureDefinition getProfile() {
082        return profile;
083    }
084
085    public ValidatorHostContext setProfile(StructureDefinition profile) {
086        this.profile = profile;
087        return this;
088    }
089
090    public Map<String, List<ValidationMessage>> getSliceRecords() {
091        return sliceRecords;
092    }
093
094    public ValidatorHostContext setSliceRecords(Map<String, List<ValidationMessage>> sliceRecords) {
095        this.sliceRecords = sliceRecords;
096        return this;
097    }
098
099    public boolean isCheckSpecials() {
100        return checkSpecials;
101    }
102
103    public void setCheckSpecials(boolean checkSpecials) {
104        this.checkSpecials = checkSpecials;
105    }
106
107    public Element getResource() {
108        return resource;
109    }
110
111    public void sliceNotes(String url, List<ValidationMessage> record) {
112      if (sliceRecords != null) {  
113        sliceRecords.put(url, record);
114      }
115    }
116
117    public ValidatorHostContext forContained(Element element) {
118        ValidatorHostContext res = new ValidatorHostContext(appContext);
119        res.rootResource = resource;
120        res.resource = element;
121        res.profile = profile;
122        res.groupingResource = groupingResource;
123        res.dump("forContained");
124        return res;
125    }
126
127    public ValidatorHostContext forEntry(Element element, Element groupingResource) {
128        ValidatorHostContext res = new ValidatorHostContext(appContext);
129        res.rootResource = element;
130        res.resource = element;
131        res.profile = profile;
132        res.groupingResource = groupingResource;
133        res.dump("forEntry");
134        return res;
135    }
136
137    public ValidatorHostContext forProfile(StructureDefinition profile) {
138        ValidatorHostContext res = new ValidatorHostContext(appContext);
139        res.resource = resource;
140        res.rootResource = rootResource;
141        res.profile = profile;
142        res.groupingResource = groupingResource;
143        res.sliceRecords = sliceRecords != null ? sliceRecords : new HashMap<String, List<ValidationMessage>>();
144        res.dump("forProfile "+profile.getUrl());
145        return res;
146    }
147
148    public ValidatorHostContext forLocalReference(StructureDefinition profile, Element resource) {
149        ValidatorHostContext res = new ValidatorHostContext(appContext);
150        res.resource = resource;
151        res.rootResource = resource;
152        res.profile = profile;
153        res.groupingResource = groupingResource;
154        res.checkSpecials = false;
155        res.dump("forLocalReference "+profile.getUrl());
156        return res;
157    }
158
159    private void dump(String ctxt) {
160//      System.out.println("** app = "+(appContext == null ? "(null)" : appContext.toString())+", res = "+resource.toString()+", root = "+rootResource.toString()+" ("+ctxt+")");
161//      if (rootResource.getName().equals("contained")) {
162//        System.out.println("** something is wrong!");        
163//      }
164    }
165
166    public ValidatorHostContext forRemoteReference(StructureDefinition profile, Element resource) {
167        ValidatorHostContext res = new ValidatorHostContext(appContext);
168        res.resource = resource;
169        res.rootResource = resource;
170        res.profile = profile;
171        res.groupingResource = null;
172        res.checkSpecials = false;
173        res.dump("forRemoteReference "+profile.getUrl());
174        return res;
175    }
176
177    public ValidatorHostContext forSlicing() {
178        ValidatorHostContext res = new ValidatorHostContext(appContext);
179        res.resource = resource;
180        res.rootResource = resource;
181        res.groupingResource = groupingResource;
182        res.profile = profile;
183        res.checkSpecials = false;
184        res.sliceRecords = new HashMap<String, List<ValidationMessage>>();
185        res.dump("forSlicing");
186        return res;
187    }
188
189
190}