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  					if (target.getEquivalence() != null) {
87  						translationMatch.setEquivalence(new CodeType(target.getEquivalence().toCode()));
88  					}
89  
90  					translationMatch.setConcept(
91  						new Coding()
92  							.setCode(target.getCode())
93  							.setSystem(target.getSystem())
94  							.setVersion(target.getSystemVersion())
95  							.setDisplay(target.getDisplay())
96  					);
97  
98  					translationMatch.setSource(new UriType(target.getConceptMapUrl()));
99  
100 					retVal.addMatch(translationMatch);
101 				}
102 			}
103 		}
104 
105 		return retVal;
106 	}
107 
108 	private TranslationResult buildReverseTranslationResult(List<TermConceptMapGroupElement> theElements) {
109 		TranslationResult retVal = new TranslationResult();
110 
111 		String msg;
112 		if (theElements.isEmpty()) {
113 
114 			retVal.setResult(new BooleanType(false));
115 
116 			msg = getContext().getLocalizer().getMessage(
117 				FhirResourceDaoConceptMapR4.class,
118 				"noMatchesFound");
119 
120 			retVal.setMessage(new StringType(msg));
121 
122 		} else {
123 
124 			retVal.setResult(new BooleanType(true));
125 
126 			msg = getContext().getLocalizer().getMessage(
127 				FhirResourceDaoConceptMapR4.class,
128 				"matchesFound");
129 
130 			retVal.setMessage(new StringType(msg));
131 
132 			TranslationMatch translationMatch;
133 			Set<TermConceptMapGroupElement> elementsToReturn = new HashSet<>();
134 			for (TermConceptMapGroupElement element : theElements) {
135 				if (elementsToReturn.add(element)) {
136 					translationMatch = new TranslationMatch();
137 
138 					translationMatch.setConcept(
139 						new Coding()
140 							.setCode(element.getCode())
141 							.setSystem(element.getSystem())
142 							.setVersion(element.getSystemVersion())
143 							.setDisplay(element.getDisplay())
144 					);
145 
146 					translationMatch.setSource(new UriType(element.getConceptMapUrl()));
147 
148 					if (element.getConceptMapGroupElementTargets().size() == 1) {
149 						translationMatch.setEquivalence(new CodeType(element.getConceptMapGroupElementTargets().get(0).getEquivalence().toCode()));
150 					}
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 			ConceptMap conceptMap = (ConceptMap) theResource;
167 			myHapiTerminologySvc.storeTermConceptMapAndChildren(retVal, conceptMap);
168 		} else {
169 			myHapiTerminologySvc.deleteConceptMapAndChildren(retVal);
170 		}
171 
172 		return retVal;
173 	}
174 }