001package org.hl7.fhir.dstu2016may.hapi.validation;
002
003import ca.uhn.fhir.context.FhirContext;
004import org.apache.commons.lang3.Validate;
005import org.hl7.fhir.dstu2016may.model.CodeSystem;
006import org.hl7.fhir.dstu2016may.model.StructureDefinition;
007import org.hl7.fhir.dstu2016may.model.ValueSet;
008import org.hl7.fhir.dstu2016may.model.ValueSet.ConceptSetComponent;
009import org.hl7.fhir.dstu2016may.model.ValueSet.ValueSetExpansionComponent;
010import org.hl7.fhir.instance.model.api.IBaseResource;
011
012import java.util.ArrayList;
013import java.util.HashMap;
014import java.util.List;
015import java.util.Map;
016
017/**
018 * This class is an implementation of {@link IValidationSupport} which may be pre-populated
019 * with a collection of validation resources to be used by the validator.
020 */
021public class PrePopulatedValidationSupport implements IValidationSupport {
022
023        private Map<String, StructureDefinition> myStructureDefinitions;
024        private Map<String, ValueSet> myValueSets;
025        private Map<String, CodeSystem> myCodeSystems;
026
027        /**
028         * Constructor
029         */
030        public PrePopulatedValidationSupport() {
031                myStructureDefinitions = new HashMap<String, StructureDefinition>();
032                myValueSets = new HashMap<String, ValueSet>();
033                myCodeSystems = new HashMap<String, CodeSystem>();
034        }
035
036        /**
037         * Constructor
038         *
039         * @param theStructureDefinitions The StructureDefinitions to be returned by this module. Keys are the logical URL for the resource, and
040         *                                values are the resource itself.
041         * @param theValueSets            The ValueSets to be returned by this module. Keys are the logical URL for the resource, and values are
042         *                                the resource itself.
043         * @param theCodeSystems          The CodeSystems to be returned by this module. Keys are the logical URL for the resource, and values are
044         *                                the resource itself.
045         */
046        public PrePopulatedValidationSupport(Map<String, StructureDefinition> theStructureDefinitions, Map<String, ValueSet> theValueSets, Map<String, CodeSystem> theCodeSystems) {
047                myStructureDefinitions = theStructureDefinitions;
048                myValueSets = theValueSets;
049                myCodeSystems = theCodeSystems;
050        }
051
052        /**
053         * Add a new CodeSystem resource which will be available to the validator. Note that
054         * {@link CodeSystem#getUrl() the URL field) in this resource must contain a value as this
055         * value will be used as the logical URL.
056         */
057        public void addCodeSystem(CodeSystem theCodeSystem) {
058                Validate.notBlank(theCodeSystem.getUrl(), "theCodeSystem.getUrl() must not return a value");
059                myCodeSystems.put(theCodeSystem.getUrl(), theCodeSystem);
060        }
061
062        /**
063         * Add a new StructureDefinition resource which will be available to the validator. Note that
064         * {@link StructureDefinition#getUrl() the URL field) in this resource must contain a value as this
065         * value will be used as the logical URL.
066         */
067        public void addStructureDefinition(StructureDefinition theStructureDefinition) {
068                Validate.notBlank(theStructureDefinition.getUrl(), "theStructureDefinition.getUrl() must not return a value");
069                myStructureDefinitions.put(theStructureDefinition.getUrl(), theStructureDefinition);
070        }
071
072        /**
073         * Add a new ValueSet resource which will be available to the validator. Note that
074         * {@link ValueSet#getUrl() the URL field) in this resource must contain a value as this
075         * value will be used as the logical URL.
076         */
077        public void addValueSet(ValueSet theValueSet) {
078                Validate.notBlank(theValueSet.getUrl(), "theValueSet.getUrl() must not return a value");
079                myValueSets.put(theValueSet.getUrl(), theValueSet);
080        }
081
082        @Override
083        public ValueSetExpansionComponent expandValueSet(FhirContext theContext, ConceptSetComponent theInclude) {
084                return null;
085        }
086
087        @Override
088        public List<IBaseResource> fetchAllConformanceResources(FhirContext theContext) {
089                ArrayList<IBaseResource> retVal = new ArrayList<>();
090                retVal.addAll(myCodeSystems.values());
091                retVal.addAll(myStructureDefinitions.values());
092                retVal.addAll(myValueSets.values());
093                return retVal;
094        }
095
096        @Override
097        public List<StructureDefinition> fetchAllStructureDefinitions(FhirContext theContext) {
098                return new ArrayList<StructureDefinition>(myStructureDefinitions.values());
099        }
100
101        @Override
102        public CodeSystem fetchCodeSystem(FhirContext theContext, String theSystem) {
103                return myCodeSystems.get(theSystem);
104        }
105
106        @SuppressWarnings("unchecked")
107        @Override
108        public <T extends IBaseResource> T fetchResource(FhirContext theContext, Class<T> theClass, String theUri) {
109                if (theClass.equals(StructureDefinition.class)) {
110                        return (T) myStructureDefinitions.get(theUri);
111                }
112                if (theClass.equals(ValueSet.class)) {
113                        return (T) myValueSets.get(theUri);
114                }
115                if (theClass.equals(CodeSystem.class)) {
116                        return (T) myCodeSystems.get(theUri);
117                }
118                return null;
119        }
120
121        @Override
122        public StructureDefinition fetchStructureDefinition(FhirContext theCtx, String theUrl) {
123                return myStructureDefinitions.get(theUrl);
124        }
125
126        @Override
127        public boolean isCodeSystemSupported(FhirContext theContext, String theSystem) {
128                return false;
129        }
130
131        @Override
132        public CodeValidationResult validateCode(FhirContext theContext, String theCodeSystem, String theCode, String theDisplay) {
133                return null;
134        }
135
136}