001package org.hl7.fhir.r4.terminologies; 002 003import static org.apache.commons.lang3.StringUtils.isNotBlank; 004 005import java.io.FileNotFoundException; 006import java.io.IOException; 007 008/* 009 * Copyright (c) 2011+, HL7, Inc 010 * All rights reserved. 011 * 012 * Redistribution and use in source and binary forms, with or without modification, 013 * are permitted provided that the following conditions are met: 014 * 015 * Redistributions of source code must retain the above copyright notice, this 016 * list of conditions and the following disclaimer. 017 * Redistributions in binary form must reproduce the above copyright notice, 018 * this list of conditions and the following disclaimer in the documentation 019 * and/or other materials provided with the distribution. 020 * Neither the name of HL7 nor the names of its contributors may be used to 021 * endorse or promote products derived from this software without specific 022 * prior written permission. 023 * 024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 025 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 026 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 027 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 028 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 029 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 030 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 031 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 032 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 033 * POSSIBILITY OF SUCH DAMAGE. 034 * 035 */ 036 037import java.util.ArrayList; 038import java.util.HashMap; 039import java.util.HashSet; 040import java.util.List; 041import java.util.Map; 042import java.util.Set; 043import java.util.UUID; 044 045import org.apache.commons.lang3.NotImplementedException; 046import org.hl7.fhir.r4.context.IWorkerContext; 047import org.hl7.fhir.r4.model.BackboneElement; 048import org.hl7.fhir.r4.model.Base; 049import org.hl7.fhir.r4.model.CodeSystem; 050import org.hl7.fhir.r4.model.CodeSystem.CodeSystemContentMode; 051import org.hl7.fhir.r4.model.CodeSystem.ConceptDefinitionComponent; 052import org.hl7.fhir.r4.model.CodeSystem.ConceptDefinitionDesignationComponent; 053import org.hl7.fhir.r4.model.DateTimeType; 054import org.hl7.fhir.r4.model.Factory; 055import org.hl7.fhir.r4.model.Parameters; 056import org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent; 057import org.hl7.fhir.r4.model.PrimitiveType; 058import org.hl7.fhir.r4.model.Type; 059import org.hl7.fhir.r4.model.UriType; 060import org.hl7.fhir.r4.model.ValueSet; 061import org.hl7.fhir.r4.model.ValueSet.ConceptReferenceComponent; 062import org.hl7.fhir.r4.model.ValueSet.ConceptReferenceDesignationComponent; 063import org.hl7.fhir.r4.model.ValueSet.ConceptSetComponent; 064import org.hl7.fhir.r4.model.ValueSet.ConceptSetFilterComponent; 065import org.hl7.fhir.r4.model.ValueSet.FilterOperator; 066import org.hl7.fhir.r4.model.ValueSet.ValueSetComposeComponent; 067import org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionComponent; 068import org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionContainsComponent; 069import org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionParameterComponent; 070import org.hl7.fhir.r4.utils.ToolingExtensions; 071import org.hl7.fhir.exceptions.FHIRException; 072import org.hl7.fhir.exceptions.FHIRFormatError; 073import org.hl7.fhir.exceptions.NoTerminologyServiceException; 074import org.hl7.fhir.exceptions.TerminologyServiceException; 075import org.hl7.fhir.utilities.Utilities; 076 077public class ValueSetExpanderSimple implements ValueSetExpander { 078 079 private List<ValueSetExpansionContainsComponent> codes = new ArrayList<ValueSet.ValueSetExpansionContainsComponent>(); 080 private List<ValueSetExpansionContainsComponent> roots = new ArrayList<ValueSet.ValueSetExpansionContainsComponent>(); 081 private Map<String, ValueSetExpansionContainsComponent> map = new HashMap<String, ValueSet.ValueSetExpansionContainsComponent>(); 082 private IWorkerContext context; 083 private boolean canBeHeirarchy = true; 084 private Set<String> excludeKeys = new HashSet<String>(); 085 private Set<String> excludeSystems = new HashSet<String>(); 086 private ValueSet focus; 087 private int maxExpansionSize = 500; 088 089 private int total; 090 091 public ValueSetExpanderSimple(IWorkerContext context) { 092 super(); 093 this.context = context; 094 } 095 096 public void setMaxExpansionSize(int theMaxExpansionSize) { 097 maxExpansionSize = theMaxExpansionSize; 098 } 099 100 private ValueSetExpansionContainsComponent addCode(String system, String code, String display, ValueSetExpansionContainsComponent parent, List<ConceptDefinitionDesignationComponent> designations, Parameters expParams, boolean isAbstract, boolean inactive, List<ValueSet> filters) { 101 102 if (filters != null && !filters.isEmpty() && !filterContainsCode(filters, system, code)) 103 return null; 104 ValueSetExpansionContainsComponent n = new ValueSet.ValueSetExpansionContainsComponent(); 105 n.setSystem(system); 106 n.setCode(code); 107 if (isAbstract) 108 n.setAbstract(true); 109 if (inactive) 110 n.setInactive(true); 111 112 if (expParams.getParameterBool("includeDesignations") && designations != null) { 113 for (ConceptDefinitionDesignationComponent t : designations) { 114 ToolingExtensions.addLanguageTranslation(n, t.getLanguage(), t.getValue()); 115 } 116 } 117 ConceptDefinitionDesignationComponent t = expParams.hasLanguage() ? getMatchingLang(designations, expParams.getLanguage()) : null; 118 if (t == null) 119 n.setDisplay(display); 120 else 121 n.setDisplay(t.getValue()); 122 123 String s = key(n); 124 if (map.containsKey(s) || excludeKeys.contains(s)) { 125 canBeHeirarchy = false; 126 } else { 127 codes.add(n); 128 map.put(s, n); 129 total++; 130 } 131 if (canBeHeirarchy && parent != null) { 132 parent.getContains().add(n); 133 } else { 134 roots.add(n); 135 } 136 return n; 137 } 138 139 private boolean filterContainsCode(List<ValueSet> filters, String system, String code) { 140 for (ValueSet vse : filters) 141 if (expansionContainsCode(vse.getExpansion().getContains(), system, code)) 142 return true; 143 return false; 144 } 145 146 private boolean expansionContainsCode(List<ValueSetExpansionContainsComponent> contains, String system, String code) { 147 for (ValueSetExpansionContainsComponent cc : contains) { 148 if (system.equals(cc.getSystem()) && code.equals(cc.getCode())) 149 return true; 150 if (expansionContainsCode(cc.getContains(), system, code)) 151 return true; 152 } 153 return false; 154 } 155 156 private ConceptDefinitionDesignationComponent getMatchingLang(List<ConceptDefinitionDesignationComponent> list, String lang) { 157 for (ConceptDefinitionDesignationComponent t : list) 158 if (t.getLanguage().equals(lang)) 159 return t; 160 for (ConceptDefinitionDesignationComponent t : list) 161 if (t.getLanguage().startsWith(lang)) 162 return t; 163 return null; 164 } 165 166 private void addCodeAndDescendents(ValueSetExpansionContainsComponent focus, ValueSetExpansionContainsComponent parent, Parameters expParams, List<ValueSet> filters) throws FHIRException { 167 focus.checkNoModifiers("Expansion.contains", "expanding"); 168 ValueSetExpansionContainsComponent np = addCode(focus.getSystem(), focus.getCode(), focus.getDisplay(), parent, 169 convert(focus.getDesignation()), expParams, focus.getAbstract(), focus.getInactive(), filters); 170 for (ValueSetExpansionContainsComponent c : focus.getContains()) 171 addCodeAndDescendents(focus, np, expParams, filters); 172 } 173 174 private List<ConceptDefinitionDesignationComponent> convert(List<ConceptReferenceDesignationComponent> designations) { 175 List<ConceptDefinitionDesignationComponent> list = new ArrayList<ConceptDefinitionDesignationComponent>(); 176 for (ConceptReferenceDesignationComponent d : designations) { 177 ConceptDefinitionDesignationComponent n = new ConceptDefinitionDesignationComponent(); 178 n.setLanguage(d.getLanguage()); 179 n.setUse(d.getUse()); 180 n.setValue(d.getValue()); 181 list.add(n); 182 } 183 return list; 184 } 185 186 private void addCodeAndDescendents(CodeSystem cs, String system, ConceptDefinitionComponent def, ValueSetExpansionContainsComponent parent, Parameters expParams, List<ValueSet> filters, ConceptDefinitionComponent exclusion) throws FHIRException { 187 def.checkNoModifiers("Code in Code System", "expanding"); 188 if (exclusion != null) { 189 if (exclusion.getCode().equals(def.getCode())) 190 return; // excluded. 191 } 192 if (!CodeSystemUtilities.isDeprecated(cs, def)) { 193 ValueSetExpansionContainsComponent np = null; 194 boolean abs = CodeSystemUtilities.isNotSelectable(cs, def); 195 boolean inc = CodeSystemUtilities.isInactive(cs, def); 196 if (canBeHeirarchy || !abs) 197 np = addCode(system, def.getCode(), def.getDisplay(), parent, def.getDesignation(), expParams, abs, inc, filters); 198 for (ConceptDefinitionComponent c : def.getConcept()) 199 addCodeAndDescendents(cs, system, c, np, expParams, filters, exclusion); 200 } else { 201 for (ConceptDefinitionComponent c : def.getConcept()) 202 addCodeAndDescendents(cs, system, c, null, expParams, filters, exclusion); 203 } 204 205 } 206 207 private void addCodes(ValueSetExpansionComponent expand, List<ValueSetExpansionParameterComponent> params, Parameters expParams, List<ValueSet> filters) throws ETooCostly, FHIRException { 208 if (expand != null) { 209 if (expand.getContains().size() > maxExpansionSize) 210 throw new ETooCostly("Too many codes to display (>" + Integer.toString(expand.getContains().size()) + ")"); 211 for (ValueSetExpansionParameterComponent p : expand.getParameter()) { 212 if (!existsInParams(params, p.getName(), p.getValue())) 213 params.add(p); 214 } 215 216 copyImportContains(expand.getContains(), null, expParams, filters); 217 } 218 } 219 220 private void excludeCode(String theSystem, String theCode) { 221 ValueSetExpansionContainsComponent n = new ValueSet.ValueSetExpansionContainsComponent(); 222 n.setSystem(theSystem); 223 n.setCode(theCode); 224 String s = key(n); 225 excludeKeys.add(s); 226 } 227 228 private void excludeCodes(ConceptSetComponent exc, List<ValueSetExpansionParameterComponent> params, String ctxt) throws FHIRException { 229 exc.checkNoModifiers("Compose.exclude", "expanding"); 230 if (exc.hasSystem() && exc.getConcept().size() == 0 && exc.getFilter().size() == 0) { 231 excludeSystems.add(exc.getSystem()); 232 } 233 234 if (exc.hasValueSet()) 235 throw new Error("Processing Value set references in exclude is not yet done in "+ctxt); 236 // importValueSet(imp.getValue(), params, expParams); 237 238 CodeSystem cs = context.fetchCodeSystem(exc.getSystem()); 239 if ((cs == null || cs.getContent() != CodeSystemContentMode.COMPLETE) && context.supportsSystem(exc.getSystem())) { 240 ValueSetExpansionOutcome vse = context.expandVS(exc, false); 241 ValueSet valueset = vse.getValueset(); 242 if (valueset == null) 243 throw new TerminologyServiceException("Error Expanding ValueSet: "+vse.getError()); 244 excludeCodes(valueset.getExpansion(), params); 245 return; 246 } 247 248 for (ConceptReferenceComponent c : exc.getConcept()) { 249 excludeCode(exc.getSystem(), c.getCode()); 250 } 251 252 if (exc.getFilter().size() > 0) 253 throw new NotImplementedException("not done yet"); 254 } 255 256 private void excludeCodes(ValueSetExpansionComponent expand, List<ValueSetExpansionParameterComponent> params) { 257 for (ValueSetExpansionContainsComponent c : expand.getContains()) { 258 excludeCode(c.getSystem(), c.getCode()); 259 } 260 } 261 262 private boolean existsInParams(List<ValueSetExpansionParameterComponent> params, String name, Type value) { 263 for (ValueSetExpansionParameterComponent p : params) { 264 if (p.getName().equals(name) && PrimitiveType.compareDeep(p.getValue(), value, false)) 265 return true; 266 } 267 return false; 268 } 269 270 @Override 271 public ValueSetExpansionOutcome expand(ValueSet source, Parameters expParams) { 272 try { 273 return doExpand(source, expParams); 274 } catch (RuntimeException e) { 275 // TODO: we should put something more specific instead of just Exception below, since 276 // it swallows bugs.. what would be expected to be caught there? 277 throw e; 278 } catch (Exception e) { 279 // well, we couldn't expand, so we'll return an interface to a checker that can check membership of the set 280 // that might fail too, but it might not, later. 281 return new ValueSetExpansionOutcome(e.getMessage(), TerminologyServiceErrorClass.UNKNOWN); 282 } 283 } 284 285 public ValueSetExpansionOutcome doExpand(ValueSet source, Parameters expParams) throws FHIRException, ETooCostly, FileNotFoundException, IOException { 286 if (expParams == null) 287 expParams = makeDefaultExpansion(); 288 source.checkNoModifiers("ValueSet", "expanding"); 289 focus = source.copy(); 290 focus.setExpansion(new ValueSet.ValueSetExpansionComponent()); 291 focus.getExpansion().setTimestampElement(DateTimeType.now()); 292 focus.getExpansion().setIdentifier(Factory.createUUID()); 293 for (ParametersParameterComponent p : expParams.getParameter()) { 294 if (Utilities.existsInList(p.getName(), "includeDesignations", "excludeNested")) 295 focus.getExpansion().addParameter().setName(p.getName()).setValue(p.getValue()); 296 } 297 298 if (source.hasCompose()) 299 handleCompose(source.getCompose(), focus.getExpansion().getParameter(), expParams, source.getUrl()); 300 301 if (canBeHeirarchy) { 302 for (ValueSetExpansionContainsComponent c : roots) { 303 focus.getExpansion().getContains().add(c); 304 } 305 } else { 306 for (ValueSetExpansionContainsComponent c : codes) { 307 if (map.containsKey(key(c)) && !c.getAbstract()) { // we may have added abstract codes earlier while we still thought it might be heirarchical, but later we gave up, so now ignore them 308 focus.getExpansion().getContains().add(c); 309 c.getContains().clear(); // make sure any heirarchy is wiped 310 } 311 } 312 } 313 314 if (total > 0) { 315 focus.getExpansion().setTotal(total); 316 } 317 318 return new ValueSetExpansionOutcome(focus); 319 } 320 321 private Parameters makeDefaultExpansion() { 322 Parameters res = new Parameters(); 323 res.addParameter("excludeNested", true); 324 res.addParameter("includeDesignations", false); 325 return res; 326 } 327 328 private void addToHeirarchy(List<ValueSetExpansionContainsComponent> target, List<ValueSetExpansionContainsComponent> source) { 329 for (ValueSetExpansionContainsComponent s : source) { 330 target.add(s); 331 } 332 } 333 334 private String getCodeDisplay(CodeSystem cs, String code) throws TerminologyServiceException { 335 ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), code); 336 if (def == null) 337 throw new TerminologyServiceException("Unable to find code '" + code + "' in code system " + cs.getUrl()); 338 return def.getDisplay(); 339 } 340 341 private ConceptDefinitionComponent getConceptForCode(List<ConceptDefinitionComponent> clist, String code) { 342 for (ConceptDefinitionComponent c : clist) { 343 if (code.equals(c.getCode())) 344 return c; 345 ConceptDefinitionComponent v = getConceptForCode(c.getConcept(), code); 346 if (v != null) 347 return v; 348 } 349 return null; 350 } 351 352 private void handleCompose(ValueSetComposeComponent compose, List<ValueSetExpansionParameterComponent> params, Parameters expParams, String ctxt) 353 throws ETooCostly, FileNotFoundException, IOException, FHIRException { 354 compose.checkNoModifiers("ValueSet.compose", "expanding"); 355 // Exclude comes first because we build up a map of things to exclude 356 for (ConceptSetComponent inc : compose.getExclude()) 357 excludeCodes(inc, params, ctxt); 358 canBeHeirarchy = !expParams.getParameterBool("excludeNested") && excludeKeys.isEmpty() && excludeSystems.isEmpty(); 359 boolean first = true; 360 for (ConceptSetComponent inc : compose.getInclude()) { 361 if (first == true) 362 first = false; 363 else 364 canBeHeirarchy = false; 365 includeCodes(inc, params, expParams, canBeHeirarchy); 366 } 367 368 } 369 370 private ValueSet importValueSet(String value, List<ValueSetExpansionParameterComponent> params, Parameters expParams) throws ETooCostly, TerminologyServiceException, FileNotFoundException, IOException, FHIRFormatError { 371 if (value == null) 372 throw new TerminologyServiceException("unable to find value set with no identity"); 373 ValueSet vs = context.fetchResource(ValueSet.class, value); 374 if (vs == null) 375 throw new TerminologyServiceException("Unable to find imported value set " + value); 376 ValueSetExpansionOutcome vso = new ValueSetExpanderSimple(context).expand(vs, expParams); 377 if (vso.getError() != null) 378 throw new TerminologyServiceException("Unable to expand imported value set: " + vso.getError()); 379 if (vs.hasVersion()) 380 if (!existsInParams(params, "version", new UriType(vs.getUrl() + "|" + vs.getVersion()))) 381 params.add(new ValueSetExpansionParameterComponent().setName("version").setValue(new UriType(vs.getUrl() + "|" + vs.getVersion()))); 382 for (ValueSetExpansionParameterComponent p : vso.getValueset().getExpansion().getParameter()) { 383 if (!existsInParams(params, p.getName(), p.getValue())) 384 params.add(p); 385 } 386 canBeHeirarchy = false; // if we're importing a value set, we have to be combining, so we won't try for a heirarchy 387 return vso.getValueset(); 388 } 389 390 private void copyImportContains(List<ValueSetExpansionContainsComponent> list, ValueSetExpansionContainsComponent parent, Parameters expParams, List<ValueSet> filter) throws FHIRException { 391 for (ValueSetExpansionContainsComponent c : list) { 392 c.checkNoModifiers("Imported Expansion in Code System", "expanding"); 393 ValueSetExpansionContainsComponent np = addCode(c.getSystem(), c.getCode(), c.getDisplay(), parent, null, expParams, c.getAbstract(), c.getInactive(), filter); 394 copyImportContains(c.getContains(), np, expParams, filter); 395 } 396 } 397 398 private void includeCodes(ConceptSetComponent inc, List<ValueSetExpansionParameterComponent> params, Parameters expParams, boolean heirarchical) throws ETooCostly, FileNotFoundException, IOException, FHIRException { 399 inc.checkNoModifiers("Compose.include", "expanding"); 400 List<ValueSet> imports = new ArrayList<ValueSet>(); 401 for (UriType imp : inc.getValueSet()) { 402 imports.add(importValueSet(imp.getValue(), params, expParams)); 403 } 404 405 if (!inc.hasSystem()) { 406 if (imports.isEmpty()) // though this is not supposed to be the case 407 return; 408 ValueSet base = imports.get(0); 409 imports.remove(0); 410 base.checkNoModifiers("Imported ValueSet", "expanding"); 411 copyImportContains(base.getExpansion().getContains(), null, expParams, imports); 412 } else { 413 CodeSystem cs = context.fetchCodeSystem(inc.getSystem()); 414 if ((cs == null || cs.getContent() != CodeSystemContentMode.COMPLETE)) { 415 doServerIncludeCodes(inc, heirarchical, params, imports, expParams); 416 } else { 417 doInternalIncludeCodes(inc, params, expParams, imports, cs); 418 } 419 } 420 } 421 422 private void doServerIncludeCodes(ConceptSetComponent inc, boolean heirarchical, List<ValueSetExpansionParameterComponent> params, List<ValueSet> imports, Parameters expParams) throws FHIRException { 423 ValueSetExpansionOutcome vso = context.expandVS(inc, heirarchical); 424 if (vso.getError() != null) 425 throw new TerminologyServiceException("Unable to expand imported value set: " + vso.getError()); 426 ValueSet vs = vso.getValueset(); 427 if (vs.hasVersion()) 428 if (!existsInParams(params, "version", new UriType(vs.getUrl() + "|" + vs.getVersion()))) 429 params.add(new ValueSetExpansionParameterComponent().setName("version").setValue(new UriType(vs.getUrl() + "|" + vs.getVersion()))); 430 for (ValueSetExpansionParameterComponent p : vso.getValueset().getExpansion().getParameter()) { 431 if (!existsInParams(params, p.getName(), p.getValue())) 432 params.add(p); 433 } 434 for (ValueSetExpansionContainsComponent cc : vs.getExpansion().getContains()) { 435 addCodeAndDescendents(cc, null, expParams, imports); 436 } 437 } 438 439 public void doInternalIncludeCodes(ConceptSetComponent inc, List<ValueSetExpansionParameterComponent> params, Parameters expParams, List<ValueSet> imports, 440 CodeSystem cs) throws NoTerminologyServiceException, TerminologyServiceException, FHIRException { 441 if (cs == null) { 442 if (context.isNoTerminologyServer()) 443 throw new NoTerminologyServiceException("unable to find code system " + inc.getSystem().toString()); 444 else 445 throw new TerminologyServiceException("unable to find code system " + inc.getSystem().toString()); 446 } 447 cs.checkNoModifiers("Code System", "expanding"); 448 if (cs.getContent() != CodeSystemContentMode.COMPLETE) 449 throw new TerminologyServiceException("Code system " + inc.getSystem().toString() + " is incomplete"); 450 if (cs.hasVersion()) 451 if (!existsInParams(params, "version", new UriType(cs.getUrl() + "|" + cs.getVersion()))) 452 params.add(new ValueSetExpansionParameterComponent().setName("version").setValue(new UriType(cs.getUrl() + "|" + cs.getVersion()))); 453 454 if (inc.getConcept().size() == 0 && inc.getFilter().size() == 0) { 455 // special case - add all the code system 456 for (ConceptDefinitionComponent def : cs.getConcept()) { 457 addCodeAndDescendents(cs, inc.getSystem(), def, null, expParams, imports, null); 458 } 459 } 460 461 if (!inc.getConcept().isEmpty()) { 462 canBeHeirarchy = false; 463 for (ConceptReferenceComponent c : inc.getConcept()) { 464 c.checkNoModifiers("Code in Code System", "expanding"); 465 addCode(inc.getSystem(), c.getCode(), Utilities.noString(c.getDisplay()) ? getCodeDisplay(cs, c.getCode()) : c.getDisplay(), null, convertDesignations(c.getDesignation()), expParams, false, 466 CodeSystemUtilities.isInactive(cs, c.getCode()), imports); 467 } 468 } 469 if (inc.getFilter().size() > 1) { 470 canBeHeirarchy = false; // which will bt the case if we get around to supporting this 471 throw new TerminologyServiceException("Multiple filters not handled yet"); // need to and them, and this isn't done yet. But this shouldn't arise in non loinc and snomed value sets 472 } 473 if (inc.getFilter().size() == 1) { 474 ConceptSetFilterComponent fc = inc.getFilter().get(0); 475 if ("concept".equals(fc.getProperty()) && fc.getOp() == FilterOperator.ISA) { 476 // special: all codes in the target code system under the value 477 ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), fc.getValue()); 478 if (def == null) 479 throw new TerminologyServiceException("Code '" + fc.getValue() + "' not found in system '" + inc.getSystem() + "'"); 480 addCodeAndDescendents(cs, inc.getSystem(), def, null, expParams, imports, null); 481 } else if ("concept".equals(fc.getProperty()) && fc.getOp() == FilterOperator.ISNOTA) { 482 // special: all codes in the target code system that are not under the value 483 ConceptDefinitionComponent defEx = getConceptForCode(cs.getConcept(), fc.getValue()); 484 if (defEx == null) 485 throw new TerminologyServiceException("Code '" + fc.getValue() + "' not found in system '" + inc.getSystem() + "'"); 486 for (ConceptDefinitionComponent def : cs.getConcept()) { 487 addCodeAndDescendents(cs, inc.getSystem(), def, null, expParams, imports, defEx); 488 } 489 } else if ("concept".equals(fc.getProperty()) && fc.getOp() == FilterOperator.DESCENDENTOF) { 490 // special: all codes in the target code system under the value 491 ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), fc.getValue()); 492 if (def == null) 493 throw new TerminologyServiceException("Code '" + fc.getValue() + "' not found in system '" + inc.getSystem() + "'"); 494 for (ConceptDefinitionComponent c : def.getConcept()) 495 addCodeAndDescendents(cs, inc.getSystem(), c, null, expParams, imports, null); 496 } else if ("display".equals(fc.getProperty()) && fc.getOp() == FilterOperator.EQUAL) { 497 // gg; note: wtf is this: if the filter is display=v, look up the code 'v', and see if it's diplsay is 'v'? 498 canBeHeirarchy = false; 499 ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), fc.getValue()); 500 if (def != null) { 501 if (isNotBlank(def.getDisplay()) && isNotBlank(fc.getValue())) { 502 if (def.getDisplay().contains(fc.getValue())) { 503 addCode(inc.getSystem(), def.getCode(), def.getDisplay(), null, def.getDesignation(), expParams, CodeSystemUtilities.isNotSelectable(cs, def), CodeSystemUtilities.isInactive(cs, def), 504 imports); 505 } 506 } 507 } 508 } else 509 throw new NotImplementedException("Search by property[" + fc.getProperty() + "] and op[" + fc.getOp() + "] is not supported yet"); 510 } 511 } 512 513 private List<ConceptDefinitionDesignationComponent> convertDesignations(List<ConceptReferenceDesignationComponent> list) { 514 List<ConceptDefinitionDesignationComponent> res = new ArrayList<CodeSystem.ConceptDefinitionDesignationComponent>(); 515 for (ConceptReferenceDesignationComponent t : list) { 516 ConceptDefinitionDesignationComponent c = new ConceptDefinitionDesignationComponent(); 517 c.setLanguage(t.getLanguage()); 518 c.setUse(t.getUse()); 519 c.setValue(t.getValue()); 520 } 521 return res; 522 } 523 524 private String key(String uri, String code) { 525 return "{" + uri + "}" + code; 526 } 527 528 private String key(ValueSetExpansionContainsComponent c) { 529 return key(c.getSystem(), c.getCode()); 530 } 531 532}