001package org.hl7.fhir.r5.utils.structuremap; 002 003import org.apache.commons.lang3.NotImplementedException; 004import org.hl7.fhir.exceptions.FHIRException; 005import org.hl7.fhir.exceptions.PathEngineException; 006import org.hl7.fhir.r5.elementmodel.Element; 007import org.hl7.fhir.r5.model.Base; 008import org.hl7.fhir.r5.model.Resource; 009import org.hl7.fhir.r5.model.TypeDetails; 010import org.hl7.fhir.r5.model.ValueSet; 011import org.hl7.fhir.r5.utils.FHIRPathEngine; 012import org.hl7.fhir.r5.utils.validation.IResourceValidator; 013import org.hl7.fhir.utilities.validation.ValidationMessage; 014 015import java.util.ArrayList; 016import java.util.List; 017 018public class FFHIRPathHostServices implements FHIRPathEngine.IEvaluationContext { 019 020 private final StructureMapUtilities structureMapUtilities; 021 022 public FFHIRPathHostServices(StructureMapUtilities structureMapUtilities) { 023 this.structureMapUtilities = structureMapUtilities; 024 } 025 026 public List<Base> resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException { 027 Variables vars = (Variables) appContext; 028 Base res = vars.get(VariableMode.INPUT, name); 029 if (res == null) 030 res = vars.get(VariableMode.OUTPUT, name); 031 List<Base> result = new ArrayList<Base>(); 032 if (res != null) 033 result.add(res); 034 return result; 035 } 036 037 @Override 038 public TypeDetails resolveConstantType(Object appContext, String name) throws PathEngineException { 039 if (!(appContext instanceof VariablesForProfiling)) 040 throw new Error("Internal Logic Error (wrong type '" + appContext.getClass().getName() + "' in resolveConstantType)"); 041 VariablesForProfiling vars = (VariablesForProfiling) appContext; 042 VariableForProfiling v = vars.get(null, name); 043 if (v == null) 044 throw new PathEngineException("Unknown variable '" + name + "' from variables " + vars.summary()); 045 return v.getProperty().getTypes(); 046 } 047 048 @Override 049 public boolean log(String argument, List<Base> focus) { 050 throw new Error("Not Implemented Yet"); 051 } 052 053 @Override 054 public FunctionDetails resolveFunction(String functionName) { 055 return null; // throw new Error("Not Implemented Yet"); 056 } 057 058 @Override 059 public TypeDetails checkFunction(Object appContext, String functionName, List<TypeDetails> parameters) throws PathEngineException { 060 throw new Error("Not Implemented Yet"); 061 } 062 063 @Override 064 public List<Base> executeFunction(Object appContext, List<Base> focus, String functionName, List<List<Base>> parameters) { 065 throw new Error("Not Implemented Yet"); 066 } 067 068 @Override 069 public Base resolveReference(Object appContext, String url, Base refContext) throws FHIRException { 070 if (structureMapUtilities.getServices() == null) 071 return null; 072 return structureMapUtilities.getServices().resolveReference(appContext, url); 073 } 074 075 private boolean noErrorValidationMessages(List<ValidationMessage> valerrors) { 076 boolean ok = true; 077 for (ValidationMessage v : valerrors) 078 ok = ok && !v.getLevel().isError(); 079 return ok; 080 } 081 082 @Override 083 public boolean conformsToProfile(Object appContext, Base item, String url) throws FHIRException { 084 IResourceValidator val = structureMapUtilities.getWorker().newValidator(); 085 List<ValidationMessage> valerrors = new ArrayList<ValidationMessage>(); 086 if (item instanceof Resource) { 087 val.validate(appContext, valerrors, (Resource) item, url); 088 return noErrorValidationMessages(valerrors); 089 } 090 if (item instanceof Element) { 091 val.validate(appContext, valerrors, null, (Element) item, url); 092 return noErrorValidationMessages(valerrors); 093 } 094 throw new NotImplementedException("Not done yet (FFHIRPathHostServices.conformsToProfile), when item is not element or not resource"); 095 } 096 097 @Override 098 public ValueSet resolveValueSet(Object appContext, String url) { 099 throw new Error("Not Implemented Yet"); 100 } 101 102}