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  					if (target.getEquivalence() != null) {
91  						translationMatch.setEquivalence(new CodeType(target.getEquivalence().toCode()));
92  					}
93  
94  					translationMatch.setConcept(
95  						new Coding()
96  							.setCode(target.getCode())
97  							.setSystem(target.getSystem())
98  							.setVersion(target.getSystemVersion())
99  							.setDisplay(target.getDisplay())
100 					);
101 
102 					translationMatch.setSource(new UriType(target.getConceptMapUrl()));
103 
104 					retVal.addMatch(translationMatch);
105 				}
106 			}
107 		}
108 
109 		return retVal;
110 	}
111 
112 	private TranslationResult buildReverseTranslationResult(List<TermConceptMapGroupElement> theElements) {
113 		TranslationResult retVal = new TranslationResult();
114 
115 		String msg;
116 		if (theElements.isEmpty()) {
117 
118 			retVal.setResult(new BooleanType(false));
119 
120 			msg = getContext().getLocalizer().getMessage(
121 				FhirResourceDaoConceptMapDstu3.class,
122 				"noMatchesFound");
123 
124 			retVal.setMessage(new StringType(msg));
125 
126 		} else {
127 
128 			retVal.setResult(new BooleanType(true));
129 
130 			msg = getContext().getLocalizer().getMessage(
131 				FhirResourceDaoConceptMapDstu3.class,
132 				"matchesFound");
133 
134 			retVal.setMessage(new StringType(msg));
135 
136 			TranslationMatch translationMatch;
137 			Set<TermConceptMapGroupElement> elementsToReturn = new HashSet<>();
138 			for (TermConceptMapGroupElement element : theElements) {
139 				if (elementsToReturn.add(element)) {
140 					translationMatch = new TranslationMatch();
141 
142 					translationMatch.setConcept(
143 						new Coding()
144 							.setCode(element.getCode())
145 							.setSystem(element.getSystem())
146 							.setVersion(element.getSystemVersion())
147 							.setDisplay(element.getDisplay())
148 					);
149 
150 					translationMatch.setSource(new UriType(element.getConceptMapUrl()));
151 
152 					retVal.addMatch(translationMatch);
153 				}
154 			}
155 		}
156 
157 		return retVal;
158 	}
159 
160 	@Override
161 	protected ResourceTable updateEntity(RequestDetails theRequestDetails, IBaseResource theResource, ResourceTable theEntity, Date theDeletedTimestampOrNull, boolean thePerformIndexing,
162 													 boolean theUpdateVersion, Date theUpdateTime, boolean theForceUpdate, boolean theCreateNewHistoryEntry) {
163 		ResourceTable retVal = super.updateEntity(theRequestDetails, theResource, theEntity, theDeletedTimestampOrNull, thePerformIndexing, theUpdateVersion, theUpdateTime, theForceUpdate, theCreateNewHistoryEntry);
164 
165 		if (retVal.getDeleted() == null) {
166 			try {
167 				ConceptMap conceptMap = (ConceptMap) theResource;
168 				org.hl7.fhir.r4.model.ConceptMap converted = VersionConvertor_30_40.convertConceptMap(conceptMap);
169 				myHapiTerminologySvc.storeTermConceptMapAndChildren(retVal, converted);
170 			} catch (FHIRException fe) {
171 				throw new InternalErrorException(fe);
172 			}
173 		} else {
174 			myHapiTerminologySvc.deleteConceptMapAndChildren(retVal);
175 		}
176 
177 		return retVal;
178 	}
179 }