View Javadoc
1   package ca.uhn.fhir.jpa.dao;
2   
3   import ca.uhn.fhir.context.support.IContextValidationSupport;
4   import ca.uhn.fhir.rest.api.server.RequestDetails;
5   import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
6   import org.hl7.fhir.instance.model.api.IBaseResource;
7   import org.hl7.fhir.instance.model.api.IIdType;
8   import org.hl7.fhir.instance.model.api.IPrimitiveType;
9   import org.hl7.fhir.r4.model.*;
10  
11  import java.util.Collections;
12  import java.util.List;
13  import java.util.Set;
14  import java.util.stream.Collectors;
15  
16  import static org.apache.commons.lang3.StringUtils.isNotBlank;
17  
18  /*
19   * #%L
20   * HAPI FHIR JPA Server
21   * %%
22   * Copyright (C) 2014 - 2018 University Health Network
23   * %%
24   * Licensed under the Apache License, Version 2.0 (the "License");
25   * you may not use this file except in compliance with the License.
26   * You may obtain a copy of the License at
27   * 
28   *      http://www.apache.org/licenses/LICENSE-2.0
29   * 
30   * Unless required by applicable law or agreed to in writing, software
31   * distributed under the License is distributed on an "AS IS" BASIS,
32   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33   * See the License for the specific language governing permissions and
34   * limitations under the License.
35   * #L%
36   */
37  
38  public interface IFhirResourceDaoCodeSystem<T extends IBaseResource, CD, CC> extends IFhirResourceDao<T> {
39  
40  	List<IIdType> findCodeSystemIdsContainingSystemAndCode(String theCode, String theSystem);
41  
42  	LookupCodeResult lookupCode(IPrimitiveType<String> theCode, IPrimitiveType<String> theSystem, CD theCoding, RequestDetails theRequestDetails);
43  
44  	class LookupCodeResult {
45  
46  		private String myCodeDisplay;
47  		private boolean myCodeIsAbstract;
48  		private String myCodeSystemDisplayName;
49  		private String myCodeSystemVersion;
50  		private boolean myFound;
51  		private String mySearchedForCode;
52  		private String mySearchedForSystem;
53  		private List<IContextValidationSupport.BaseConceptProperty> myProperties;
54  
55  		/**
56  		 * Constructor
57  		 */
58  		public LookupCodeResult() {
59  			super();
60  		}
61  
62  		public String getCodeDisplay() {
63  			return myCodeDisplay;
64  		}
65  
66  		public void setCodeDisplay(String theCodeDisplay) {
67  			myCodeDisplay = theCodeDisplay;
68  		}
69  
70  		public String getCodeSystemDisplayName() {
71  			return myCodeSystemDisplayName;
72  		}
73  
74  		public void setCodeSystemDisplayName(String theCodeSystemDisplayName) {
75  			myCodeSystemDisplayName = theCodeSystemDisplayName;
76  		}
77  
78  		public String getCodeSystemVersion() {
79  			return myCodeSystemVersion;
80  		}
81  
82  		public void setCodeSystemVersion(String theCodeSystemVersion) {
83  			myCodeSystemVersion = theCodeSystemVersion;
84  		}
85  
86  		public String getSearchedForCode() {
87  			return mySearchedForCode;
88  		}
89  
90  		public void setSearchedForCode(String theSearchedForCode) {
91  			mySearchedForCode = theSearchedForCode;
92  		}
93  
94  		public String getSearchedForSystem() {
95  			return mySearchedForSystem;
96  		}
97  
98  		public void setSearchedForSystem(String theSearchedForSystem) {
99  			mySearchedForSystem = theSearchedForSystem;
100 		}
101 
102 		public boolean isCodeIsAbstract() {
103 			return myCodeIsAbstract;
104 		}
105 
106 		public void setCodeIsAbstract(boolean theCodeIsAbstract) {
107 			myCodeIsAbstract = theCodeIsAbstract;
108 		}
109 
110 		public boolean isFound() {
111 			return myFound;
112 		}
113 
114 		public void setFound(boolean theFound) {
115 			myFound = theFound;
116 		}
117 
118 		public void setProperties(List<IContextValidationSupport.BaseConceptProperty> theProperties) {
119 			myProperties = theProperties;
120 		}
121 
122 		public void throwNotFoundIfAppropriate() {
123 			if (isFound() == false) {
124 				throw new ResourceNotFoundException("Unable to find code[" + getSearchedForCode() + "] in system[" + getSearchedForSystem() + "]");
125 			}
126 		}
127 
128 		public Parameters toParameters(List<? extends IPrimitiveType<String>> theProperties) {
129 			Parameters retVal = new Parameters();
130 
131 			retVal.addParameter().setName("name").setValue(new StringType(getCodeSystemDisplayName()));
132 			if (isNotBlank(getCodeSystemVersion())) {
133 				retVal.addParameter().setName("version").setValue(new StringType(getCodeSystemVersion()));
134 			}
135 			retVal.addParameter().setName("display").setValue(new StringType(getCodeDisplay()));
136 			retVal.addParameter().setName("abstract").setValue(new BooleanType(isCodeIsAbstract()));
137 
138 			if (myProperties != null) {
139 
140 				Set<String> properties = Collections.emptySet();
141 				if (theProperties != null) {
142 					properties = theProperties
143 						.stream()
144 						.map(IPrimitiveType::getValueAsString)
145 						.collect(Collectors.toSet());
146 				}
147 
148 				for (IContextValidationSupport.BaseConceptProperty next : myProperties) {
149 
150 					if (!properties.isEmpty()) {
151 						if (!properties.contains(next.getPropertyName())) {
152 							continue;
153 						}
154 					}
155 
156 					Parameters.ParametersParameterComponent property = retVal.addParameter().setName("property");
157 					property
158 						.addPart()
159 						.setName("code")
160 						.setValue(new CodeType(next.getPropertyName()));
161 
162 					if (next instanceof IContextValidationSupport.StringConceptProperty) {
163 						IContextValidationSupport.StringConceptProperty prop = (IContextValidationSupport.StringConceptProperty) next;
164 						property
165 							.addPart()
166 							.setName("value")
167 							.setValue(new StringType(prop.getValue()));
168 					} else if (next instanceof IContextValidationSupport.CodingConceptProperty) {
169 						IContextValidationSupport.CodingConceptProperty prop = (IContextValidationSupport.CodingConceptProperty) next;
170 						property
171 							.addPart()
172 							.setName("value")
173 							.setValue(new Coding()
174 								.setSystem(prop.getCodeSystem())
175 								.setCode(prop.getCode())
176 								.setDisplay(prop.getDisplay()));
177 					} else {
178 						throw new IllegalStateException("Don't know how to handle " + next.getClass());
179 					}
180 				}
181 			}
182 
183 			return retVal;
184 		}
185 	}
186 
187 }