001package org.hl7.fhir.dstu2.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
032import java.util.List;
033
034import org.hl7.fhir.dstu2.model.UriType;
035import org.hl7.fhir.dstu2.model.ValueSet;
036import org.hl7.fhir.dstu2.model.ValueSet.ConceptDefinitionComponent;
037import org.hl7.fhir.dstu2.model.ValueSet.ConceptReferenceComponent;
038import org.hl7.fhir.dstu2.model.ValueSet.ConceptSetComponent;
039import org.hl7.fhir.dstu2.model.ValueSet.ConceptSetFilterComponent;
040import org.hl7.fhir.dstu2.model.ValueSet.ValueSetExpansionContainsComponent;
041import org.hl7.fhir.dstu2.terminologies.ValueSetExpander.ValueSetExpansionOutcome;
042import org.hl7.fhir.dstu2.utils.EOperationOutcome;
043import org.hl7.fhir.dstu2.utils.IWorkerContext;
044import org.hl7.fhir.dstu2.utils.IWorkerContext.ValidationResult;
045
046public class ValueSetCheckerSimple implements ValueSetChecker {
047
048  private ValueSet valueset;
049  private ValueSetExpanderFactory factory;
050  private IWorkerContext context;
051
052  public ValueSetCheckerSimple(ValueSet source, ValueSetExpanderFactory factory, IWorkerContext context) {
053    this.valueset = source;
054    this.factory = factory;
055    this.context = context;
056  }
057
058  @Override
059  public boolean codeInValueSet(String system, String code) throws EOperationOutcome, Exception {
060    if (valueset.hasCodeSystem() && system.equals(valueset.getCodeSystem().getSystem())
061        && codeInDefine(valueset.getCodeSystem().getConcept(), code, valueset.getCodeSystem().getCaseSensitive()))
062      return true;
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)
088      throws EOperationOutcome, Exception {
089    if (vso.getService() != null) {
090      return vso.getService().codeInValueSet(system, code);
091    } else {
092      for (ValueSetExpansionContainsComponent c : vso.getValueset().getExpansion().getContains()) {
093        if (code.equals(c.getCode()) && (system == null || system.equals(c.getSystem())))
094          return true;
095        if (codeinExpansion(c, system, code))
096          return true;
097      }
098    }
099    return false;
100  }
101
102  private boolean codeinExpansion(ValueSetExpansionContainsComponent cnt, String system, String code) {
103    for (ValueSetExpansionContainsComponent c : cnt.getContains()) {
104      if (code.equals(c.getCode()) && system.equals(c.getSystem().toString()))
105        return true;
106      if (codeinExpansion(c, system, code))
107        return true;
108    }
109    return false;
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
116    // value
117    for (ConceptReferenceComponent cc : vsi.getConcept())
118      if (cc.getCode().equals(code)) {
119        return true;
120      }
121
122    ValueSet def = context.fetchCodeSystem(system);
123    if (def != null) {
124      if (!def.getCodeSystem().getCaseSensitive()) {
125        // well, ok, it's not case sensitive - we'll check that too now
126        for (ConceptReferenceComponent cc : vsi.getConcept())
127          if (cc.getCode().equalsIgnoreCase(code)) {
128            return false;
129          }
130      }
131      if (vsi.getConcept().isEmpty() && vsi.getFilter().isEmpty()) {
132        return codeInDefine(def.getCodeSystem().getConcept(), code, def.getCodeSystem().getCaseSensitive());
133      }
134      for (ConceptSetFilterComponent f : vsi.getFilter())
135        throw new Error("not done yet: " + f.getValue());
136
137      return false;
138    } else if (context.supportsSystem(system)) {
139      ValidationResult vv = context.validateCode(system, code, null, vsi);
140      return vv.isOk();
141    } else
142      // we don't know this system, and can't resolve it
143      return false;
144  }
145
146  private boolean codeInDefine(List<ConceptDefinitionComponent> concepts, String code, boolean caseSensitive) {
147    for (ConceptDefinitionComponent c : concepts) {
148      if (caseSensitive && code.equals(c.getCode()))
149        return true;
150      if (!caseSensitive && code.equalsIgnoreCase(c.getCode()))
151        return true;
152      if (codeInDefine(c.getConcept(), code, caseSensitive))
153        return true;
154    }
155    return false;
156  }
157
158}