View Javadoc
1   package ca.uhn.fhir.jpa.term;
2   
3   /*
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import org.hl7.fhir.instance.hapi.validation.IValidationSupport;
24  import org.hl7.fhir.instance.model.api.IBaseResource;
25  import org.hl7.fhir.instance.model.api.IIdType;
26  import org.hl7.fhir.r4.model.CodeSystem;
27  import org.hl7.fhir.r4.model.ConceptMap;
28  import org.hl7.fhir.r4.model.ValueSet;
29  import org.springframework.beans.factory.annotation.Autowired;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import static org.apache.commons.lang3.StringUtils.isNotBlank;
35  
36  public class HapiTerminologySvcDstu2 extends BaseHapiTerminologySvcImpl {
37  
38  	@Autowired
39  	private IValidationSupport myValidationSupport;
40  
41  	private void addAllChildren(String theSystemString, org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent theCode, List<VersionIndependentConcept> theListToPopulate) {
42  		if (isNotBlank(theCode.getCode())) {
43  			theListToPopulate.add(new VersionIndependentConcept(theSystemString, theCode.getCode()));
44  		}
45  		for (org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent nextChild : theCode.getConcept()) {
46  			addAllChildren(theSystemString, nextChild, theListToPopulate);
47  		}
48  	}
49  
50  	private boolean addTreeIfItContainsCode(String theSystemString, org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent theNext, String theCode, List<VersionIndependentConcept> theListToPopulate) {
51  		boolean foundCodeInChild = false;
52  		for (org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent nextChild : theNext.getConcept()) {
53  			foundCodeInChild |= addTreeIfItContainsCode(theSystemString, nextChild, theCode, theListToPopulate);
54  		}
55  
56  		if (theCode.equals(theNext.getCode()) || foundCodeInChild) {
57  			theListToPopulate.add(new VersionIndependentConcept(theSystemString, theNext.getCode()));
58  			return true;
59  		}
60  
61  		return false;
62  	}
63  
64  	@Override
65  	protected IIdType createOrUpdateCodeSystem(CodeSystem theCodeSystemResource) {
66  		throw new UnsupportedOperationException();
67  	}
68  
69  	@Override
70  	protected void createOrUpdateConceptMap(ConceptMap theNextConceptMap) {
71  		throw new UnsupportedOperationException();
72  	}
73  
74  	@Override
75  	protected void createOrUpdateValueSet(ValueSet theValueSet) {
76  		throw new UnsupportedOperationException();
77  	}
78  
79  	@Override
80  	protected CodeSystem getCodeSystemFromContext(String theSystem) {
81  		return null;
82  	}
83  
84  	@Override
85  	public IBaseResource expandValueSet(IBaseResource theValueSetToExpand) {
86  		throw new UnsupportedOperationException();
87  	}
88  
89  	@Override
90  	public List<VersionIndependentConcept> expandValueSet(String theValueSet) {
91  		throw new UnsupportedOperationException();
92  	}
93  
94  	private void findCodesAbove(org.hl7.fhir.instance.model.ValueSet theSystem, String theSystemString, String theCode, List<VersionIndependentConcept> theListToPopulate) {
95  		List<org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent> conceptList = theSystem.getCodeSystem().getConcept();
96  		for (org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent next : conceptList) {
97  			addTreeIfItContainsCode(theSystemString, next, theCode, theListToPopulate);
98  		}
99  	}
100 
101 	@Override
102 	public List<VersionIndependentConcept> findCodesAboveUsingBuiltInSystems(String theSystem, String theCode) {
103 		ArrayList<VersionIndependentConcept> retVal = new ArrayList<>();
104 		org.hl7.fhir.instance.model.ValueSet system = myValidationSupport.fetchCodeSystem(myContext, theSystem);
105 		if (system != null) {
106 			findCodesAbove(system, theSystem, theCode, retVal);
107 		}
108 		return retVal;
109 	}
110 
111 	private void findCodesBelow(org.hl7.fhir.instance.model.ValueSet theSystem, String theSystemString, String theCode, List<VersionIndependentConcept> theListToPopulate) {
112 		List<org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent> conceptList = theSystem.getCodeSystem().getConcept();
113 		findCodesBelow(theSystemString, theCode, theListToPopulate, conceptList);
114 	}
115 
116 	private void findCodesBelow(String theSystemString, String theCode, List<VersionIndependentConcept> theListToPopulate, List<org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent> conceptList) {
117 		for (org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent next : conceptList) {
118 			if (theCode.equals(next.getCode())) {
119 				addAllChildren(theSystemString, next, theListToPopulate);
120 			} else {
121 				findCodesBelow(theSystemString, theCode, theListToPopulate, next.getConcept());
122 			}
123 		}
124 	}
125 
126 	@Override
127 	public List<VersionIndependentConcept> findCodesBelowUsingBuiltInSystems(String theSystem, String theCode) {
128 		ArrayList<VersionIndependentConcept> retVal = new ArrayList<>();
129 		org.hl7.fhir.instance.model.ValueSet system = myValidationSupport.fetchCodeSystem(myContext, theSystem);
130 		if (system != null) {
131 			findCodesBelow(system, theSystem, theCode, retVal);
132 		}
133 		return retVal;
134 	}
135 
136 }