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