View Javadoc
1   package ca.uhn.fhir.jpa.dao.dstu3;
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 ca.uhn.fhir.jpa.dao.IFhirResourceDaoConceptMap;
24  import ca.uhn.fhir.jpa.term.TranslationMatch;
25  import ca.uhn.fhir.jpa.term.TranslationRequest;
26  import ca.uhn.fhir.jpa.term.TranslationResult;
27  import ca.uhn.fhir.jpa.entity.ResourceTable;
28  import ca.uhn.fhir.jpa.entity.TermConceptMapGroupElement;
29  import ca.uhn.fhir.jpa.entity.TermConceptMapGroupElementTarget;
30  import ca.uhn.fhir.jpa.term.IHapiTerminologySvc;
31  import ca.uhn.fhir.rest.api.server.RequestDetails;
32  import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
33  import org.hl7.fhir.convertors.VersionConvertor_30_40;
34  import org.hl7.fhir.dstu3.model.ConceptMap;
35  import org.hl7.fhir.exceptions.FHIRException;
36  import org.hl7.fhir.instance.model.api.IBaseResource;
37  import org.hl7.fhir.r4.model.*;
38  import org.springframework.beans.factory.annotation.Autowired;
39  
40  import java.util.Date;
41  import java.util.HashSet;
42  import java.util.List;
43  import java.util.Set;
44  
45  import static org.apache.commons.lang3.StringUtils.isNotBlank;
46  
47  public class FhirResourceDaoConceptMapDstu3 extends FhirResourceDaoDstu3<ConceptMap> implements IFhirResourceDaoConceptMap<ConceptMap> {
48  	@Autowired
49  	private IHapiTerminologySvc myHapiTerminologySvc;
50  
51  	@Override
52  	public TranslationResult translate(TranslationRequest theTranslationRequest, RequestDetails theRequestDetails) {
53  		if (theTranslationRequest.hasReverse() && theTranslationRequest.getReverseAsBoolean()) {
54  			return buildReverseTranslationResult(myHapiTerminologySvc.translateWithReverse(theTranslationRequest));
55  		}
56  
57  		return buildTranslationResult(myHapiTerminologySvc.translate(theTranslationRequest));
58  	}
59  
60  	private TranslationResult buildTranslationResult(List<TermConceptMapGroupElementTarget> theTargets) {
61  		TranslationResult retVal = new TranslationResult();
62  
63  		String msg;
64  		if (theTargets.isEmpty()) {
65  
66  			retVal.setResult(new BooleanType(false));
67  
68  			msg = getContext().getLocalizer().getMessage(
69  				FhirResourceDaoConceptMapDstu3.class,
70  				"noMatchesFound");
71  
72  			retVal.setMessage(new StringType(msg));
73  
74  		} else {
75  
76  			retVal.setResult(new BooleanType(true));
77  
78  			msg = getContext().getLocalizer().getMessage(
79  				FhirResourceDaoConceptMapDstu3.class,
80  				"matchesFound");
81  
82  			retVal.setMessage(new StringType(msg));
83  
84  			TranslationMatch translationMatch;
85  			Set<TermConceptMapGroupElementTarget> targetsToReturn = new HashSet<>();
86  			for (TermConceptMapGroupElementTarget target : theTargets) {
87  				if (targetsToReturn.add(target)) {
88  					translationMatch = new TranslationMatch();
89  
90  					translationMatch.setEquivalence(new CodeType(target.getEquivalence().toCode()));
91  
92  					translationMatch.setConcept(
93  						new Coding()
94  							.setCode(target.getCode())
95  							.setSystem(target.getSystem())
96  							.setVersion(target.getSystemVersion())
97  							.setDisplay(target.getDisplay())
98  					);
99  
100 					translationMatch.setSource(new UriType(target.getConceptMapUrl()));
101 
102 					retVal.addMatch(translationMatch);
103 				}
104 			}
105 		}
106 
107 		return retVal;
108 	}
109 
110 	private TranslationResult buildReverseTranslationResult(List<TermConceptMapGroupElement> theElements) {
111 		TranslationResult retVal = new TranslationResult();
112 
113 		String msg;
114 		if (theElements.isEmpty()) {
115 
116 			retVal.setResult(new BooleanType(false));
117 
118 			msg = getContext().getLocalizer().getMessage(
119 				FhirResourceDaoConceptMapDstu3.class,
120 				"noMatchesFound");
121 
122 			retVal.setMessage(new StringType(msg));
123 
124 		} else {
125 
126 			retVal.setResult(new BooleanType(true));
127 
128 			msg = getContext().getLocalizer().getMessage(
129 				FhirResourceDaoConceptMapDstu3.class,
130 				"matchesFound");
131 
132 			retVal.setMessage(new StringType(msg));
133 
134 			TranslationMatch translationMatch;
135 			Set<TermConceptMapGroupElement> elementsToReturn = new HashSet<>();
136 			for (TermConceptMapGroupElement element : theElements) {
137 				if (elementsToReturn.add(element)) {
138 					translationMatch = new TranslationMatch();
139 
140 					translationMatch.setConcept(
141 						new Coding()
142 							.setCode(element.getCode())
143 							.setSystem(element.getSystem())
144 							.setVersion(element.getSystemVersion())
145 							.setDisplay(element.getDisplay())
146 					);
147 
148 					translationMatch.setSource(new UriType(element.getConceptMapUrl()));
149 
150 					retVal.addMatch(translationMatch);
151 				}
152 			}
153 		}
154 
155 		return retVal;
156 	}
157 
158 	@Override
159 	protected ResourceTable updateEntity(RequestDetails theRequestDetails, IBaseResource theResource, ResourceTable theEntity, Date theDeletedTimestampOrNull, boolean thePerformIndexing,
160 													 boolean theUpdateVersion, Date theUpdateTime, boolean theForceUpdate, boolean theCreateNewHistoryEntry) {
161 		ResourceTable retVal = super.updateEntity(theRequestDetails, theResource, theEntity, theDeletedTimestampOrNull, thePerformIndexing, theUpdateVersion, theUpdateTime, theForceUpdate, theCreateNewHistoryEntry);
162 
163 		ConceptMap conceptMap = (ConceptMap) theResource;
164 
165 		// Convert from DSTU3 to R4
166 		try {
167 			myHapiTerminologySvc.storeTermConceptMapAndChildren(retVal, VersionConvertor_30_40.convertConceptMap(conceptMap));
168 		} catch (FHIRException fe) {
169 			throw new InternalErrorException(fe);
170 		}
171 
172 		return retVal;
173 	}
174 }