001package org.hl7.fhir.dstu2016may.hapi.validation;
002
003import ca.uhn.fhir.context.FhirContext;
004import org.hl7.fhir.dstu2016may.model.CodeSystem;
005import org.hl7.fhir.dstu2016may.model.StructureDefinition;
006import org.hl7.fhir.dstu2016may.model.ValueSet.ConceptSetComponent;
007import org.hl7.fhir.dstu2016may.model.ValueSet.ValueSetExpansionComponent;
008import org.hl7.fhir.instance.model.api.IBaseResource;
009
010import java.util.ArrayList;
011import java.util.HashSet;
012import java.util.List;
013import java.util.Set;
014
015import static org.apache.commons.lang3.StringUtils.isBlank;
016
017public class ValidationSupportChain implements IValidationSupport {
018
019        private List<IValidationSupport> myChain;
020
021        /**
022         * Constructor
023         */
024        public ValidationSupportChain() {
025                myChain = new ArrayList<IValidationSupport>();
026        }
027
028        /**
029         * Constructor
030         */
031        public ValidationSupportChain(IValidationSupport... theValidationSupportModules) {
032                this();
033                for (IValidationSupport next : theValidationSupportModules) {
034                        if (next != null) {
035                                myChain.add(next);
036                        }
037                }
038        }
039
040        public void addValidationSupport(IValidationSupport theValidationSupport) {
041                myChain.add(theValidationSupport);
042        }
043
044        @Override
045        public ValueSetExpansionComponent expandValueSet(FhirContext theCtx, ConceptSetComponent theInclude) {
046                for (IValidationSupport next : myChain) {
047                        if (next.isCodeSystemSupported(theCtx, theInclude.getSystem())) {
048                                return next.expandValueSet(theCtx, theInclude);
049                        }
050                }
051                return myChain.get(0).expandValueSet(theCtx, theInclude);
052        }
053
054        @Override
055        public List<IBaseResource> fetchAllConformanceResources(FhirContext theContext) {
056                List<IBaseResource> retVal = new ArrayList<>();
057                for (IValidationSupport next : myChain) {
058                        List<IBaseResource> candidates = next.fetchAllConformanceResources(theContext);
059                        if (candidates != null) {
060                                retVal.addAll(candidates);
061                        }
062                }
063                return retVal;
064        }
065
066        @Override
067        public List<StructureDefinition> fetchAllStructureDefinitions(FhirContext theContext) {
068                ArrayList<StructureDefinition> retVal = new ArrayList<StructureDefinition>();
069                Set<String> urls = new HashSet<String>();
070                for (IValidationSupport nextSupport : myChain) {
071                        for (StructureDefinition next : nextSupport.fetchAllStructureDefinitions(theContext)) {
072                                if (isBlank(next.getUrl()) || urls.add(next.getUrl())) {
073                                        retVal.add(next);
074                                }
075                        }
076                }
077                return retVal;
078        }
079
080        @Override
081        public CodeSystem fetchCodeSystem(FhirContext theCtx, String theSystem) {
082                for (IValidationSupport next : myChain) {
083                        CodeSystem retVal = next.fetchCodeSystem(theCtx, theSystem);
084                        if (retVal != null) {
085                                return retVal;
086                        }
087                }
088                return null;
089        }
090
091        @Override
092        public <T extends IBaseResource> T fetchResource(FhirContext theContext, Class<T> theClass, String theUri) {
093                for (IValidationSupport next : myChain) {
094                        T retVal = next.fetchResource(theContext, theClass, theUri);
095                        if (retVal != null) {
096                                return retVal;
097                        }
098                }
099                return null;
100        }
101
102        @Override
103        public StructureDefinition fetchStructureDefinition(FhirContext theCtx, String theUrl) {
104                for (IValidationSupport next : myChain) {
105                        StructureDefinition retVal = next.fetchStructureDefinition(theCtx, theUrl);
106                        if (retVal != null) {
107                                return retVal;
108                        }
109                }
110                return null;
111        }
112
113        @Override
114        public boolean isCodeSystemSupported(FhirContext theCtx, String theSystem) {
115                for (IValidationSupport next : myChain) {
116                        if (next.isCodeSystemSupported(theCtx, theSystem)) {
117                                return true;
118                        }
119                }
120                return false;
121        }
122
123        @Override
124        public CodeValidationResult validateCode(FhirContext theCtx, String theCodeSystem, String theCode, String theDisplay) {
125                for (IValidationSupport next : myChain) {
126                        if (next.isCodeSystemSupported(theCtx, theCodeSystem)) {
127                                return next.validateCode(theCtx, theCodeSystem, theCode, theDisplay);
128                        }
129                }
130                return myChain.get(0).validateCode(theCtx, theCodeSystem, theCode, theDisplay);
131        }
132
133}