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.IIdType;
25  import org.hl7.fhir.r4.model.CodeSystem;
26  import org.hl7.fhir.r4.model.ConceptMap;
27  import org.hl7.fhir.r4.model.ValueSet;
28  import org.springframework.beans.factory.annotation.Autowired;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import static org.apache.commons.lang3.StringUtils.isNotBlank;
34  
35  public class HapiTerminologySvcDstu2 extends BaseHapiTerminologySvcImpl {
36  
37  	@Autowired
38  	private IValidationSupport myValidationSupport;
39  
40  	private void addAllChildren(String theSystemString, org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent theCode, List<VersionIndependentConcept> theListToPopulate) {
41  		if (isNotBlank(theCode.getCode())) {
42  			theListToPopulate.add(new VersionIndependentConcept(theSystemString, theCode.getCode()));
43  		}
44  		for (org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent nextChild : theCode.getConcept()) {
45  			addAllChildren(theSystemString, nextChild, theListToPopulate);
46  		}
47  	}
48  
49  	private boolean addTreeIfItContainsCode(String theSystemString, org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent theNext, String theCode, List<VersionIndependentConcept> theListToPopulate) {
50  		boolean foundCodeInChild = false;
51  		for (org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent nextChild : theNext.getConcept()) {
52  			foundCodeInChild |= addTreeIfItContainsCode(theSystemString, nextChild, theCode, theListToPopulate);
53  		}
54  
55  		if (theCode.equals(theNext.getCode()) || foundCodeInChild) {
56  			theListToPopulate.add(new VersionIndependentConcept(theSystemString, theNext.getCode()));
57  			return true;
58  		}
59  
60  		return false;
61  	}
62  
63  	@Override
64  	protected IIdType createOrUpdateCodeSystem(CodeSystem theCodeSystemResource) {
65  		throw new UnsupportedOperationException();
66  	}
67  
68  	@Override
69  	protected void createOrUpdateConceptMap(ConceptMap theNextConceptMap) {
70  		throw new UnsupportedOperationException();
71  	}
72  
73  	@Override
74  	protected void createOrUpdateValueSet(ValueSet theValueSet) {
75  		throw new UnsupportedOperationException();
76  	}
77  
78  	@Override
79  	protected CodeSystem getCodeSystemFromContext(String theSystem) {
80  		return null;
81  	}
82  
83  	@Override
84  	public List<VersionIndependentConcept> expandValueSet(String theValueSet) {
85  		throw new UnsupportedOperationException();
86  	}
87  
88  	private void findCodesAbove(org.hl7.fhir.instance.model.ValueSet theSystem, String theSystemString, String theCode, List<VersionIndependentConcept> theListToPopulate) {
89  		List<org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent> conceptList = theSystem.getCodeSystem().getConcept();
90  		for (org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent next : conceptList) {
91  			addTreeIfItContainsCode(theSystemString, next, theCode, theListToPopulate);
92  		}
93  	}
94  
95  	@Override
96  	public List<VersionIndependentConcept> findCodesAboveUsingBuiltInSystems(String theSystem, String theCode) {
97  		ArrayList<VersionIndependentConcept> retVal = new ArrayList<>();
98  		org.hl7.fhir.instance.model.ValueSet system = myValidationSupport.fetchCodeSystem(myContext, theSystem);
99  		if (system != null) {
100 			findCodesAbove(system, theSystem, theCode, retVal);
101 		}
102 		return retVal;
103 	}
104 
105 	private void findCodesBelow(org.hl7.fhir.instance.model.ValueSet theSystem, String theSystemString, String theCode, List<VersionIndependentConcept> theListToPopulate) {
106 		List<org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent> conceptList = theSystem.getCodeSystem().getConcept();
107 		findCodesBelow(theSystemString, theCode, theListToPopulate, conceptList);
108 	}
109 
110 	private void findCodesBelow(String theSystemString, String theCode, List<VersionIndependentConcept> theListToPopulate, List<org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent> conceptList) {
111 		for (org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent next : conceptList) {
112 			if (theCode.equals(next.getCode())) {
113 				addAllChildren(theSystemString, next, theListToPopulate);
114 			} else {
115 				findCodesBelow(theSystemString, theCode, theListToPopulate, next.getConcept());
116 			}
117 		}
118 	}
119 
120 	@Override
121 	public List<VersionIndependentConcept> findCodesBelowUsingBuiltInSystems(String theSystem, String theCode) {
122 		ArrayList<VersionIndependentConcept> retVal = new ArrayList<>();
123 		org.hl7.fhir.instance.model.ValueSet system = myValidationSupport.fetchCodeSystem(myContext, theSystem);
124 		if (system != null) {
125 			findCodesBelow(system, theSystem, theCode, retVal);
126 		}
127 		return retVal;
128 	}
129 
130 }