001package org.hl7.fhir.dstu3.terminologies;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033
034import java.util.List;
035
036import org.hl7.fhir.dstu3.context.IWorkerContext;
037import org.hl7.fhir.dstu3.context.IWorkerContext.ValidationResult;
038import org.hl7.fhir.dstu3.model.CodeSystem;
039import org.hl7.fhir.dstu3.model.CodeSystem.CodeSystemContentMode;
040import org.hl7.fhir.dstu3.model.CodeSystem.ConceptDefinitionComponent;
041import org.hl7.fhir.dstu3.model.UriType;
042import org.hl7.fhir.dstu3.model.ValueSet;
043import org.hl7.fhir.dstu3.model.ValueSet.ConceptReferenceComponent;
044import org.hl7.fhir.dstu3.model.ValueSet.ConceptSetComponent;
045import org.hl7.fhir.dstu3.model.ValueSet.ConceptSetFilterComponent;
046import org.hl7.fhir.dstu3.model.ValueSet.ValueSetExpansionContainsComponent;
047import org.hl7.fhir.dstu3.terminologies.ValueSetExpander.ValueSetExpansionOutcome;
048import org.hl7.fhir.dstu3.utils.EOperationOutcome;
049
050public class ValueSetCheckerSimple implements ValueSetChecker {
051
052  private ValueSet valueset;
053  private ValueSetExpanderFactory factory;
054  private IWorkerContext context;
055
056  public ValueSetCheckerSimple(ValueSet source, ValueSetExpanderFactory factory, IWorkerContext context) {
057    this.valueset = source;
058    this.factory = factory;
059    this.context = context;
060  }
061
062  @Override
063  public boolean codeInValueSet(String system, String code) throws EOperationOutcome, Exception {
064
065    if (valueset.hasCompose()) {
066      boolean ok = false;
067      for (ConceptSetComponent vsi : valueset.getCompose().getInclude()) {
068        ok = ok || inComponent(vsi, system, code);
069      }
070      for (ConceptSetComponent vsi : valueset.getCompose().getExclude()) {
071        ok = ok && !inComponent(vsi, system, code);
072      }
073    }
074    
075    return false;
076  }
077
078  private boolean inImport(String uri, String system, String code) throws EOperationOutcome, Exception {
079    ValueSet vs = context.fetchResource(ValueSet.class, uri);
080    if (vs == null) 
081      return false ; // we can't tell
082    return codeInExpansion(factory.getExpander().expand(vs, null), system, code);
083  }
084
085  private boolean codeInExpansion(ValueSetExpansionOutcome vso, String system, String code) throws EOperationOutcome, Exception {
086    if (vso.getService() != null) {
087      return vso.getService().codeInValueSet(system, code);
088    } else {
089      for (ValueSetExpansionContainsComponent c : vso.getValueset().getExpansion().getContains()) {
090        if (code.equals(c.getCode()) && (system == null || system.equals(c.getSystem())))
091          return true;
092        if (codeinExpansion(c, system, code)) 
093          return true;
094      }
095    }
096    return false;
097  }
098
099  private boolean codeinExpansion(ValueSetExpansionContainsComponent cnt, String system, String code) {
100    for (ValueSetExpansionContainsComponent c : cnt.getContains()) {
101      if (code.equals(c.getCode()) && system.equals(c.getSystem().toString()))
102        return true;
103      if (codeinExpansion(c, system, code)) 
104        return true;
105    }
106    return false;
107  }
108
109
110  private boolean inComponent(ConceptSetComponent vsi, String system, String code) throws Exception {
111    if (vsi.hasSystem() && !vsi.getSystem().equals(system))
112      return false; 
113    
114    for (UriType uri : vsi.getValueSet()) {
115      if (!inImport(uri.getValue(), system, code))
116        return false;
117    }
118
119    if (!vsi.hasSystem())
120      return false;
121    
122    // whether we know the system or not, we'll accept the stated codes at face value
123    for (ConceptReferenceComponent cc : vsi.getConcept())
124      if (cc.getCode().equals(code)) {
125        return true;
126      }
127      
128    CodeSystem def = context.fetchCodeSystem(system);
129    if (def != null && def.getContent() == CodeSystemContentMode.COMPLETE) {
130      if (!def.getCaseSensitive()) {
131        // well, ok, it's not case sensitive - we'll check that too now
132        for (ConceptReferenceComponent cc : vsi.getConcept())
133          if (cc.getCode().equalsIgnoreCase(code)) {
134            return false;
135          }
136      }
137      if (vsi.getConcept().isEmpty() && vsi.getFilter().isEmpty()) {
138        return codeInDefine(def.getConcept(), code, def.getCaseSensitive());
139      }
140      for (ConceptSetFilterComponent f: vsi.getFilter())
141        throw new Error("not done yet: "+f.getValue());
142
143      return false;
144    } else if (context.supportsSystem(system)) {
145      ValidationResult vv = context.validateCode(system, code, null, vsi);
146      return vv.isOk();
147    } else
148      // we don't know this system, and can't resolve it
149      return false;
150  }
151
152  private boolean codeInDefine(List<ConceptDefinitionComponent> concepts, String code, boolean caseSensitive) {
153    for (ConceptDefinitionComponent c : concepts) {
154      if (caseSensitive && code.equals(c.getCode()))
155        return true;
156      if (!caseSensitive && code.equalsIgnoreCase(c.getCode()))
157        return true;
158      if (codeInDefine(c.getConcept(), code, caseSensitive))
159        return true;
160    }
161    return false;
162  }
163
164}