001package org.hl7.fhir.dstu2016may.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.dstu2016may.model.CodeSystem;
037import org.hl7.fhir.dstu2016may.model.CodeSystem.ConceptDefinitionComponent;
038import org.hl7.fhir.dstu2016may.model.UriType;
039import org.hl7.fhir.dstu2016may.model.ValueSet;
040import org.hl7.fhir.dstu2016may.model.ValueSet.ConceptReferenceComponent;
041import org.hl7.fhir.dstu2016may.model.ValueSet.ConceptSetComponent;
042import org.hl7.fhir.dstu2016may.model.ValueSet.ConceptSetFilterComponent;
043import org.hl7.fhir.dstu2016may.model.ValueSet.ValueSetExpansionContainsComponent;
044import org.hl7.fhir.dstu2016may.terminologies.ValueSetExpander.ValueSetExpansionOutcome;
045import org.hl7.fhir.dstu2016may.utils.EOperationOutcome;
046import org.hl7.fhir.dstu2016may.utils.IWorkerContext;
047import org.hl7.fhir.dstu2016may.utils.IWorkerContext.ValidationResult;
048
049public class ValueSetCheckerSimple implements ValueSetChecker {
050
051  private ValueSet valueset;
052  private ValueSetExpanderFactory factory;
053  private IWorkerContext context;
054
055  public ValueSetCheckerSimple(ValueSet source, ValueSetExpanderFactory factory, IWorkerContext context) {
056    this.valueset = source;
057    this.factory = factory;
058    this.context = context;
059  }
060
061  @Override
062  public boolean codeInValueSet(String system, String code) throws EOperationOutcome, Exception {
063
064    if (valueset.hasCompose()) {
065      boolean ok = false;
066      for (UriType uri : valueset.getCompose().getImport()) {
067        ok = ok || inImport(uri.getValue(), system, code);
068      }
069      for (ConceptSetComponent vsi : valueset.getCompose().getInclude()) {
070        ok = ok || inComponent(vsi, system, code);
071      }
072      for (ConceptSetComponent vsi : valueset.getCompose().getExclude()) {
073        ok = ok && !inComponent(vsi, system, code);
074      }
075    }
076    
077    return false;
078  }
079
080  private boolean inImport(String uri, String system, String code) throws EOperationOutcome, Exception {
081    ValueSet vs = context.fetchResource(ValueSet.class, uri);
082    if (vs == null) 
083      return false ; // we can't tell
084    return codeInExpansion(factory.getExpander().expand(vs), system, code);
085  }
086
087  private boolean codeInExpansion(ValueSetExpansionOutcome vso, String system, String code) throws EOperationOutcome, Exception {
088    if (vso.getService() != null) {
089      return vso.getService().codeInValueSet(system, code);
090    } else {
091      for (ValueSetExpansionContainsComponent c : vso.getValueset().getExpansion().getContains()) {
092        if (code.equals(c.getCode()) && (system == null || system.equals(c.getSystem())))
093          return true;
094        if (codeinExpansion(c, system, code)) 
095          return true;
096      }
097    }
098    return false;
099  }
100
101  private boolean codeinExpansion(ValueSetExpansionContainsComponent cnt, String system, String code) {
102    for (ValueSetExpansionContainsComponent c : cnt.getContains()) {
103      if (code.equals(c.getCode()) && system.equals(c.getSystem().toString()))
104        return true;
105      if (codeinExpansion(c, system, code)) 
106        return true;
107    }
108    return false;
109  }
110
111
112  private boolean inComponent(ConceptSetComponent vsi, String system, String code) {
113    if (!vsi.getSystem().equals(system))
114      return false; 
115    // whether we know the system or not, we'll accept the stated codes at face value
116    for (ConceptReferenceComponent cc : vsi.getConcept())
117      if (cc.getCode().equals(code)) {
118        return true;
119      }
120      
121    CodeSystem def = context.fetchCodeSystem(system);
122    if (def != null) {
123      if (!def.getCaseSensitive()) {
124        // well, ok, it's not case sensitive - we'll check that too now
125        for (ConceptReferenceComponent cc : vsi.getConcept())
126          if (cc.getCode().equalsIgnoreCase(code)) {
127            return false;
128          }
129      }
130      if (vsi.getConcept().isEmpty() && vsi.getFilter().isEmpty()) {
131        return codeInDefine(def.getConcept(), code, def.getCaseSensitive());
132      }
133      for (ConceptSetFilterComponent f: vsi.getFilter())
134        throw new Error("not done yet: "+f.getValue());
135
136      return false;
137    } else if (context.supportsSystem(system)) {
138      ValidationResult vv = context.validateCode(system, code, null, vsi);
139      return vv.isOk();
140    } else
141      // we don't know this system, and can't resolve it
142      return false;
143  }
144
145  private boolean codeInDefine(List<ConceptDefinitionComponent> concepts, String code, boolean caseSensitive) {
146    for (ConceptDefinitionComponent c : concepts) {
147      if (caseSensitive && code.equals(c.getCode()))
148        return true;
149      if (!caseSensitive && code.equalsIgnoreCase(c.getCode()))
150        return true;
151      if (codeInDefine(c.getConcept(), code, caseSensitive))
152        return true;
153    }
154    return false;
155  }
156
157}