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