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 (NoTerminologyServiceException e) { 275 // well, we couldn't expand, so we'll return an interface to a checker that can check membership of the set 276 // that might fail too, but it might not, later. 277 return new ValueSetExpansionOutcome(e.getMessage(), TerminologyServiceErrorClass.NOSERVICE); 278 } catch (RuntimeException e) { 279 // TODO: we should put something more specific instead of just Exception below, since 280 // it swallows bugs.. what would be expected to be caught there? 281 throw e; 282 } catch (Exception e) { 283 // well, we couldn't expand, so we'll return an interface to a checker that can check membership of the set 284 // that might fail too, but it might not, later. 285 return new ValueSetExpansionOutcome(e.getMessage(), TerminologyServiceErrorClass.UNKNOWN); 286 } 287 } 288 289 public ValueSetExpansionOutcome doExpand(ValueSet source, Parameters expParams) throws FHIRException, ETooCostly, FileNotFoundException, IOException { 290 if (expParams == null) 291 expParams = makeDefaultExpansion(); 292 source.checkNoModifiers("ValueSet", "expanding"); 293 focus = source.copy(); 294 focus.setExpansion(new ValueSet.ValueSetExpansionComponent()); 295 focus.getExpansion().setTimestampElement(DateTimeType.now()); 296 focus.getExpansion().setIdentifier(Factory.createUUID()); 297 for (ParametersParameterComponent p : expParams.getParameter()) { 298 if (Utilities.existsInList(p.getName(), "includeDesignations", "excludeNested")) 299 focus.getExpansion().addParameter().setName(p.getName()).setValue(p.getValue()); 300 } 301 302 if (source.hasCompose()) 303 handleCompose(source.getCompose(), focus.getExpansion().getParameter(), expParams, source.getUrl()); 304 305 if (canBeHeirarchy) { 306 for (ValueSetExpansionContainsComponent c : roots) { 307 focus.getExpansion().getContains().add(c); 308 } 309 } else { 310 for (ValueSetExpansionContainsComponent c : codes) { 311 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 312 focus.getExpansion().getContains().add(c); 313 c.getContains().clear(); // make sure any heirarchy is wiped 314 } 315 } 316 } 317 318 if (total > 0) { 319 focus.getExpansion().setTotal(total); 320 } 321 322 return new ValueSetExpansionOutcome(focus); 323 } 324 325 private Parameters makeDefaultExpansion() { 326 Parameters res = new Parameters(); 327 res.addParameter("excludeNested", true); 328 res.addParameter("includeDesignations", false); 329 return res; 330 } 331 332 private void addToHeirarchy(List<ValueSetExpansionContainsComponent> target, List<ValueSetExpansionContainsComponent> source) { 333 for (ValueSetExpansionContainsComponent s : source) { 334 target.add(s); 335 } 336 } 337 338 private String getCodeDisplay(CodeSystem cs, String code) throws TerminologyServiceException { 339 ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), code); 340 if (def == null) 341 throw new TerminologyServiceException("Unable to find code '" + code + "' in code system " + cs.getUrl()); 342 return def.getDisplay(); 343 } 344 345 private ConceptDefinitionComponent getConceptForCode(List<ConceptDefinitionComponent> clist, String code) { 346 for (ConceptDefinitionComponent c : clist) { 347 if (code.equals(c.getCode())) 348 return c; 349 ConceptDefinitionComponent v = getConceptForCode(c.getConcept(), code); 350 if (v != null) 351 return v; 352 } 353 return null; 354 } 355 356 private void handleCompose(ValueSetComposeComponent compose, List<ValueSetExpansionParameterComponent> params, Parameters expParams, String ctxt) 357 throws ETooCostly, FileNotFoundException, IOException, FHIRException { 358 compose.checkNoModifiers("ValueSet.compose", "expanding"); 359 // Exclude comes first because we build up a map of things to exclude 360 for (ConceptSetComponent inc : compose.getExclude()) 361 excludeCodes(inc, params, ctxt); 362 canBeHeirarchy = !expParams.getParameterBool("excludeNested") && excludeKeys.isEmpty() && excludeSystems.isEmpty(); 363 boolean first = true; 364 for (ConceptSetComponent inc : compose.getInclude()) { 365 if (first == true) 366 first = false; 367 else 368 canBeHeirarchy = false; 369 includeCodes(inc, params, expParams, canBeHeirarchy); 370 } 371 372 } 373 374 private ValueSet importValueSet(String value, List<ValueSetExpansionParameterComponent> params, Parameters expParams) throws ETooCostly, TerminologyServiceException, FileNotFoundException, IOException, FHIRFormatError { 375 if (value == null) 376 throw new TerminologyServiceException("unable to find value set with no identity"); 377 ValueSet vs = context.fetchResource(ValueSet.class, value); 378 if (vs == null) 379 throw new TerminologyServiceException("Unable to find imported value set " + value); 380 ValueSetExpansionOutcome vso = new ValueSetExpanderSimple(context).expand(vs, expParams); 381 if (vso.getError() != null) 382 throw new TerminologyServiceException("Unable to expand imported value set: " + vso.getError()); 383 if (vs.hasVersion()) 384 if (!existsInParams(params, "version", new UriType(vs.getUrl() + "|" + vs.getVersion()))) 385 params.add(new ValueSetExpansionParameterComponent().setName("version").setValue(new UriType(vs.getUrl() + "|" + vs.getVersion()))); 386 for (ValueSetExpansionParameterComponent p : vso.getValueset().getExpansion().getParameter()) { 387 if (!existsInParams(params, p.getName(), p.getValue())) 388 params.add(p); 389 } 390 canBeHeirarchy = false; // if we're importing a value set, we have to be combining, so we won't try for a heirarchy 391 return vso.getValueset(); 392 } 393 394 private void copyImportContains(List<ValueSetExpansionContainsComponent> list, ValueSetExpansionContainsComponent parent, Parameters expParams, List<ValueSet> filter) throws FHIRException { 395 for (ValueSetExpansionContainsComponent c : list) { 396 c.checkNoModifiers("Imported Expansion in Code System", "expanding"); 397 ValueSetExpansionContainsComponent np = addCode(c.getSystem(), c.getCode(), c.getDisplay(), parent, null, expParams, c.getAbstract(), c.getInactive(), filter); 398 copyImportContains(c.getContains(), np, expParams, filter); 399 } 400 } 401 402 private void includeCodes(ConceptSetComponent inc, List<ValueSetExpansionParameterComponent> params, Parameters expParams, boolean heirarchical) throws ETooCostly, FileNotFoundException, IOException, FHIRException { 403 inc.checkNoModifiers("Compose.include", "expanding"); 404 List<ValueSet> imports = new ArrayList<ValueSet>(); 405 for (UriType imp : inc.getValueSet()) { 406 imports.add(importValueSet(imp.getValue(), params, expParams)); 407 } 408 409 if (!inc.hasSystem()) { 410 if (imports.isEmpty()) // though this is not supposed to be the case 411 return; 412 ValueSet base = imports.get(0); 413 imports.remove(0); 414 base.checkNoModifiers("Imported ValueSet", "expanding"); 415 copyImportContains(base.getExpansion().getContains(), null, expParams, imports); 416 } else { 417 CodeSystem cs = context.fetchCodeSystem(inc.getSystem()); 418 if ((cs == null || cs.getContent() != CodeSystemContentMode.COMPLETE)) { 419 doServerIncludeCodes(inc, heirarchical, params, imports, expParams); 420 } else { 421 doInternalIncludeCodes(inc, params, expParams, imports, cs); 422 } 423 } 424 } 425 426 private void doServerIncludeCodes(ConceptSetComponent inc, boolean heirarchical, List<ValueSetExpansionParameterComponent> params, List<ValueSet> imports, Parameters expParams) throws FHIRException { 427 ValueSetExpansionOutcome vso = context.expandVS(inc, heirarchical); 428 if (vso.getError() != null) 429 throw new TerminologyServiceException("Unable to expand imported value set: " + vso.getError()); 430 ValueSet vs = vso.getValueset(); 431 if (vs.hasVersion()) 432 if (!existsInParams(params, "version", new UriType(vs.getUrl() + "|" + vs.getVersion()))) 433 params.add(new ValueSetExpansionParameterComponent().setName("version").setValue(new UriType(vs.getUrl() + "|" + vs.getVersion()))); 434 for (ValueSetExpansionParameterComponent p : vso.getValueset().getExpansion().getParameter()) { 435 if (!existsInParams(params, p.getName(), p.getValue())) 436 params.add(p); 437 } 438 for (ValueSetExpansionContainsComponent cc : vs.getExpansion().getContains()) { 439 addCodeAndDescendents(cc, null, expParams, imports); 440 } 441 } 442 443 public void doInternalIncludeCodes(ConceptSetComponent inc, List<ValueSetExpansionParameterComponent> params, Parameters expParams, List<ValueSet> imports, 444 CodeSystem cs) throws NoTerminologyServiceException, TerminologyServiceException, FHIRException { 445 if (cs == null) { 446 if (context.isNoTerminologyServer()) 447 throw new NoTerminologyServiceException("unable to find code system " + inc.getSystem().toString()); 448 else 449 throw new TerminologyServiceException("unable to find code system " + inc.getSystem().toString()); 450 } 451 cs.checkNoModifiers("Code System", "expanding"); 452 if (cs.getContent() != CodeSystemContentMode.COMPLETE) 453 throw new TerminologyServiceException("Code system " + inc.getSystem().toString() + " is incomplete"); 454 if (cs.hasVersion()) 455 if (!existsInParams(params, "version", new UriType(cs.getUrl() + "|" + cs.getVersion()))) 456 params.add(new ValueSetExpansionParameterComponent().setName("version").setValue(new UriType(cs.getUrl() + "|" + cs.getVersion()))); 457 458 if (inc.getConcept().size() == 0 && inc.getFilter().size() == 0) { 459 // special case - add all the code system 460 for (ConceptDefinitionComponent def : cs.getConcept()) { 461 addCodeAndDescendents(cs, inc.getSystem(), def, null, expParams, imports, null); 462 } 463 } 464 465 if (!inc.getConcept().isEmpty()) { 466 canBeHeirarchy = false; 467 for (ConceptReferenceComponent c : inc.getConcept()) { 468 c.checkNoModifiers("Code in Code System", "expanding"); 469 addCode(inc.getSystem(), c.getCode(), Utilities.noString(c.getDisplay()) ? getCodeDisplay(cs, c.getCode()) : c.getDisplay(), null, convertDesignations(c.getDesignation()), expParams, false, 470 CodeSystemUtilities.isInactive(cs, c.getCode()), imports); 471 } 472 } 473 if (inc.getFilter().size() > 1) { 474 canBeHeirarchy = false; // which will bt the case if we get around to supporting this 475 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 476 } 477 if (inc.getFilter().size() == 1) { 478 ConceptSetFilterComponent fc = inc.getFilter().get(0); 479 if ("concept".equals(fc.getProperty()) && fc.getOp() == FilterOperator.ISA) { 480 // special: all codes in the target code system under the value 481 ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), fc.getValue()); 482 if (def == null) 483 throw new TerminologyServiceException("Code '" + fc.getValue() + "' not found in system '" + inc.getSystem() + "'"); 484 addCodeAndDescendents(cs, inc.getSystem(), def, null, expParams, imports, null); 485 } else if ("concept".equals(fc.getProperty()) && fc.getOp() == FilterOperator.ISNOTA) { 486 // special: all codes in the target code system that are not under the value 487 ConceptDefinitionComponent defEx = getConceptForCode(cs.getConcept(), fc.getValue()); 488 if (defEx == null) 489 throw new TerminologyServiceException("Code '" + fc.getValue() + "' not found in system '" + inc.getSystem() + "'"); 490 for (ConceptDefinitionComponent def : cs.getConcept()) { 491 addCodeAndDescendents(cs, inc.getSystem(), def, null, expParams, imports, defEx); 492 } 493 } else if ("concept".equals(fc.getProperty()) && fc.getOp() == FilterOperator.DESCENDENTOF) { 494 // special: all codes in the target code system under the value 495 ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), fc.getValue()); 496 if (def == null) 497 throw new TerminologyServiceException("Code '" + fc.getValue() + "' not found in system '" + inc.getSystem() + "'"); 498 for (ConceptDefinitionComponent c : def.getConcept()) 499 addCodeAndDescendents(cs, inc.getSystem(), c, null, expParams, imports, null); 500 } else if ("display".equals(fc.getProperty()) && fc.getOp() == FilterOperator.EQUAL) { 501 // gg; note: wtf is this: if the filter is display=v, look up the code 'v', and see if it's diplsay is 'v'? 502 canBeHeirarchy = false; 503 ConceptDefinitionComponent def = getConceptForCode(cs.getConcept(), fc.getValue()); 504 if (def != null) { 505 if (isNotBlank(def.getDisplay()) && isNotBlank(fc.getValue())) { 506 if (def.getDisplay().contains(fc.getValue())) { 507 addCode(inc.getSystem(), def.getCode(), def.getDisplay(), null, def.getDesignation(), expParams, CodeSystemUtilities.isNotSelectable(cs, def), CodeSystemUtilities.isInactive(cs, def), 508 imports); 509 } 510 } 511 } 512 } else 513 throw new NotImplementedException("Search by property[" + fc.getProperty() + "] and op[" + fc.getOp() + "] is not supported yet"); 514 } 515 } 516 517 private List<ConceptDefinitionDesignationComponent> convertDesignations(List<ConceptReferenceDesignationComponent> list) { 518 List<ConceptDefinitionDesignationComponent> res = new ArrayList<CodeSystem.ConceptDefinitionDesignationComponent>(); 519 for (ConceptReferenceDesignationComponent t : list) { 520 ConceptDefinitionDesignationComponent c = new ConceptDefinitionDesignationComponent(); 521 c.setLanguage(t.getLanguage()); 522 c.setUse(t.getUse()); 523 c.setValue(t.getValue()); 524 } 525 return res; 526 } 527 528 private String key(String uri, String code) { 529 return "{" + uri + "}" + code; 530 } 531 532 private String key(ValueSetExpansionContainsComponent c) { 533 return key(c.getSystem(), c.getCode()); 534 } 535 536}