001package org.hl7.fhir.r5.terminologies;
002
003/*-
004 * #%L
005 * org.hl7.fhir.r5
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import java.util.ArrayList;
025import java.util.EnumSet;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030import org.hl7.fhir.exceptions.FHIRException;
031import org.hl7.fhir.r5.context.IWorkerContext;
032import org.hl7.fhir.r5.context.IWorkerContext.ValidationResult;
033import org.hl7.fhir.r5.model.CanonicalType;
034import org.hl7.fhir.r5.model.CodeSystem;
035import org.hl7.fhir.r5.model.CodeSystem.CodeSystemContentMode;
036import org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent;
037import org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionDesignationComponent;
038import org.hl7.fhir.r5.model.CodeableConcept;
039import org.hl7.fhir.r5.model.Coding;
040import org.hl7.fhir.r5.model.UriType;
041import org.hl7.fhir.r5.model.ValueSet;
042import org.hl7.fhir.r5.model.ValueSet.ConceptReferenceComponent;
043import org.hl7.fhir.r5.model.ValueSet.ConceptReferenceDesignationComponent;
044import org.hl7.fhir.r5.model.ValueSet.ConceptSetComponent;
045import org.hl7.fhir.r5.model.ValueSet.ConceptSetFilterComponent;
046import org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionContainsComponent;
047import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
048import org.hl7.fhir.utilities.TerminologyServiceOptions;
049import org.hl7.fhir.utilities.Utilities;
050import org.hl7.fhir.utilities.validation.ValidationOptions;
051import org.hl7.fhir.utilities.validation.ValidationOptions.ValueSetMode;
052import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity;
053
054public class ValueSetCheckerSimple implements ValueSetChecker {
055
056  private ValueSet valueset;
057  private IWorkerContext context;
058  private Map<String, ValueSetCheckerSimple> inner = new HashMap<>();
059  private ValidationOptions options;
060
061  public ValueSetCheckerSimple(ValidationOptions options, ValueSet source, IWorkerContext context) {
062    this.valueset = source;
063    this.context = context;
064    this.options = options;
065  }
066
067  public ValidationResult validateCode(CodeableConcept code) throws FHIRException {
068    // first, we validate the codings themselves
069    List<String> errors = new ArrayList<String>();
070    List<String> warnings = new ArrayList<String>();
071    if (options.getValueSetMode() != ValueSetMode.CHECK_MEMERSHIP_ONLY) {
072      for (Coding c : code.getCoding()) {
073        if (!c.hasSystem())
074          warnings.add("Coding has no system - cannot validate");
075        CodeSystem cs = context.fetchCodeSystem(c.getSystem());
076        ValidationResult res = null;
077        if (cs == null || cs.getContent() != CodeSystemContentMode.COMPLETE) {
078          res = context.validateCode(options.noClient(), c, null);
079        } else {
080          res = validateCode(c, cs);
081        }
082        if (!res.isOk())
083          errors.add(res.getMessage());
084        else if (res.getMessage() != null)
085          warnings.add(res.getMessage());
086      }
087    }
088    if (valueset != null && options.getValueSetMode() != ValueSetMode.NO_MEMBERSHIP_CHECK) {
089      boolean ok = false;
090      for (Coding c : code.getCoding()) {
091        ok = ok || codeInValueSet(c.getSystem(), c.getCode());
092      }
093      if (!ok)
094        errors.add(0, "None of the provided codes are in the value set "+valueset.getUrl());
095    }
096    if (errors.size() > 0)
097      return new ValidationResult(IssueSeverity.ERROR, errors.toString());
098    else if (warnings.size() > 0)
099      return new ValidationResult(IssueSeverity.WARNING, warnings.toString());
100    else 
101      return new ValidationResult(IssueSeverity.INFORMATION, null);
102  }
103
104  public ValidationResult validateCode(Coding code) throws FHIRException {
105    String warningMessage = null;
106    // first, we validate the concept itself
107
108    ValidationResult res =null;
109    boolean inExpansion = false;
110    String system = code.hasSystem() ? code.getSystem() : getValueSetSystem();
111    if (options.getValueSetMode() != ValueSetMode.CHECK_MEMERSHIP_ONLY) {
112      if (system == null && !code.hasDisplay()) { // dealing with just a plain code (enum)
113        system = systemForCodeInValueSet(code.getCode());
114      }
115      if (!code.hasSystem())
116        code.setSystem(system);
117      inExpansion = checkExpansion(code);
118      CodeSystem cs = context.fetchCodeSystem(system);
119      if (cs == null) {
120        warningMessage = "Unable to resolve system "+system+" - system is not specified or implicit";
121        if (!inExpansion)
122          throw new FHIRException(warningMessage);
123      }
124      if (cs!=null && cs.getContent() != CodeSystemContentMode.COMPLETE) {
125        warningMessage = "Unable to resolve system "+system+" - system is not complete";
126        if (!inExpansion)
127          throw new FHIRException(warningMessage);
128      }
129
130      if (cs!=null)
131        res = validateCode(code, cs);
132    } else {
133      inExpansion = checkExpansion(code);
134    }
135
136    // then, if we have a value set, we check it's in the value set
137    if (valueset != null && options.getValueSetMode() != ValueSetMode.NO_MEMBERSHIP_CHECK) {
138      if ((res==null || res.isOk()) && !codeInValueSet(system, code.getCode())) {
139        if (!inExpansion)
140          res.setMessage("Not in value set "+valueset.getUrl()).setSeverity(IssueSeverity.ERROR);
141        else if (warningMessage!=null)
142          res = new ValidationResult(IssueSeverity.WARNING, "Code found in expansion, however: " + warningMessage);
143        else
144          res.setMessage("Code found in expansion, however: " + res.getMessage());
145      }
146    }
147    return res;
148  }
149
150  boolean checkExpansion(Coding code) {
151    if (valueset==null || !valueset.hasExpansion())
152      return false;
153    return checkExpansion(code, valueset.getExpansion().getContains());
154  }
155
156  boolean checkExpansion(Coding code, List<ValueSetExpansionContainsComponent> contains) {
157    for (ValueSetExpansionContainsComponent containsComponent: contains) {
158      if (containsComponent.getSystem().equals(code.getSystem()) && containsComponent.getCode().equals(code.getCode()))
159        return true;
160      if (containsComponent.hasContains() && checkExpansion(code, containsComponent.getContains()))
161        return true;
162    }
163    return false;
164  }
165
166  private ValidationResult validateCode(Coding code, CodeSystem cs) {
167    ConceptDefinitionComponent cc = findCodeInConcept(cs.getConcept(), code.getCode());
168    if (cc == null)
169      return new ValidationResult(IssueSeverity.ERROR, "Unknown Code "+gen(code)+" in "+cs.getUrl());
170    if (code.getDisplay() == null)
171      return new ValidationResult(cc);
172    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
173    if (cc.hasDisplay()) {
174      b.append(cc.getDisplay());
175      if (code.getDisplay().equalsIgnoreCase(cc.getDisplay()))
176        return new ValidationResult(cc);
177    }
178    for (ConceptDefinitionDesignationComponent ds : cc.getDesignation()) {
179      b.append(ds.getValue());
180      if (code.getDisplay().equalsIgnoreCase(ds.getValue()))
181        return new ValidationResult(cc);
182    }
183    // also check to see if the value set has another display
184    ConceptReferenceComponent vs = findValueSetRef(code.getSystem(), code.getCode());
185    if (vs != null && (vs.hasDisplay() ||vs.hasDesignation())) {
186      if (vs.hasDisplay()) {
187        b.append(vs.getDisplay());
188        if (code.getDisplay().equalsIgnoreCase(vs.getDisplay()))
189          return new ValidationResult(cc);
190      }
191      for (ConceptReferenceDesignationComponent ds : vs.getDesignation()) {
192        b.append(ds.getValue());
193        if (code.getDisplay().equalsIgnoreCase(ds.getValue()))
194          return new ValidationResult(cc);
195      }
196    }
197    return new ValidationResult(IssueSeverity.WARNING, "Display Name for "+code.getSystem()+"#"+code.getCode()+" should be one of '"+b.toString()+"' instead of '"+code.getDisplay()+"'", cc);
198  }
199
200  private ConceptReferenceComponent findValueSetRef(String system, String code) {
201    if (valueset == null)
202      return null;
203    // if it has an expansion
204    for (ValueSetExpansionContainsComponent exp : valueset.getExpansion().getContains()) {
205      if (system.equals(exp.getSystem()) && code.equals(exp.getCode())) {
206        ConceptReferenceComponent cc = new ConceptReferenceComponent();
207        cc.setDisplay(exp.getDisplay());
208        cc.setDesignation(exp.getDesignation());
209        return cc;
210      }
211    }
212    for (ConceptSetComponent inc : valueset.getCompose().getInclude()) {
213      if (system.equals(inc.getSystem())) {
214        for (ConceptReferenceComponent cc : inc.getConcept()) {
215          if (cc.getCode().equals(code))
216            return cc;
217        }
218      }
219      for (CanonicalType url : inc.getValueSet()) {
220        ConceptReferenceComponent cc = getVs(url.asStringValue()).findValueSetRef(system, code);
221        if (cc != null)
222          return cc;
223      }
224    }
225    return null;
226  }
227
228  private String gen(Coding code) {
229    if (code.hasSystem())
230      return code.getSystem()+"#"+code.getCode();
231    else
232      return null;
233  }
234
235  private String getValueSetSystem() throws FHIRException {
236    if (valueset == null)
237      throw new FHIRException("Unable to resolve system - no value set");
238    if (valueset.getCompose().hasExclude())
239      throw new FHIRException("Unable to resolve system - value set has excludes");
240    if (valueset.getCompose().getInclude().size() == 0) {
241      if (!valueset.hasExpansion() || valueset.getExpansion().getContains().size() == 0)
242        throw new FHIRException("Unable to resolve system - value set has no includes or expansion");
243      else {
244        String cs = valueset.getExpansion().getContains().get(0).getSystem();
245        if (cs != null && checkSystem(valueset.getExpansion().getContains(), cs))
246          return cs;
247        else
248          throw new FHIRException("Unable to resolve system - value set expansion has multiple systems");
249      }
250    }
251    for (ConceptSetComponent inc : valueset.getCompose().getInclude()) {
252      if (inc.hasValueSet())
253        throw new FHIRException("Unable to resolve system - value set has imports");
254      if (!inc.hasSystem())
255        throw new FHIRException("Unable to resolve system - value set has include with no system");
256    }
257    if (valueset.getCompose().getInclude().size() == 1)
258      return valueset.getCompose().getInclude().get(0).getSystem();
259    
260    return null;
261  }
262
263  /*
264   * Check that all system values within an expansion correspond to the specified system value
265   */
266  private boolean checkSystem(List<ValueSetExpansionContainsComponent> containsList, String system) {
267    for (ValueSetExpansionContainsComponent contains : containsList) {
268      if (!contains.getSystem().equals(system) || (contains.hasContains() && !checkSystem(contains.getContains(), system)))
269        return false;
270    }
271    return true;
272  }
273  private ConceptDefinitionComponent findCodeInConcept(List<ConceptDefinitionComponent> concept, String code) {
274    for (ConceptDefinitionComponent cc : concept) {
275      if (code.equals(cc.getCode()))
276        return cc;
277      ConceptDefinitionComponent c = findCodeInConcept(cc.getConcept(), code);
278      if (c != null)
279        return c;
280    }
281    return null;
282  }
283
284  
285  private String systemForCodeInValueSet(String code) {
286    String sys = null;
287    if (valueset.hasCompose()) {
288      if (valueset.getCompose().hasExclude())
289        return null;
290      for (ConceptSetComponent vsi : valueset.getCompose().getInclude()) {
291        if (vsi.hasValueSet())
292          return null;
293        if (!vsi.hasSystem()) 
294          return null;
295        if (vsi.hasFilter())
296          return null;
297        CodeSystem cs = context.fetchCodeSystem(vsi.getSystem());
298        if (cs == null)
299          return null;
300        if (vsi.hasConcept()) {
301          for (ConceptReferenceComponent cc : vsi.getConcept()) {
302            boolean match = cs.getCaseSensitive() ? cc.getCode().equals(code) : cc.getCode().equalsIgnoreCase(code);
303            if (match) {
304              if (sys == null)
305                sys = vsi.getSystem();
306              else if (!sys.equals(vsi.getSystem()))
307                return null;
308            }
309          }
310        } else {
311          ConceptDefinitionComponent cc = findCodeInConcept(cs.getConcept(), code);
312          if (cc != null) {
313            if (sys == null)
314              sys = vsi.getSystem();
315            else if (!sys.equals(vsi.getSystem()))
316              return null;
317          }
318        }
319      }
320    }
321    
322    return sys;  
323  }
324  
325  @Override
326  public boolean codeInValueSet(String system, String code) throws FHIRException {
327    if (valueset.hasExpansion()) {
328      return checkExpansion(new Coding(system, code, null));
329    } else if (valueset.hasCompose()) {
330      boolean ok = false;
331      for (ConceptSetComponent vsi : valueset.getCompose().getInclude()) {
332        ok = ok || inComponent(vsi, system, code, valueset.getCompose().getInclude().size() == 1);
333      }
334      for (ConceptSetComponent vsi : valueset.getCompose().getExclude()) {
335        ok = ok && !inComponent(vsi, system, code, valueset.getCompose().getInclude().size() == 1);
336      }
337      return ok;
338    } 
339    
340    return false;
341  }
342
343  private boolean inComponent(ConceptSetComponent vsi, String system, String code, boolean only) throws FHIRException {
344    for (UriType uri : vsi.getValueSet()) {
345      if (inImport(uri.getValue(), system, code))
346        return true;
347    }
348
349    if (!vsi.hasSystem())
350      return false;
351    
352    if (only && system == null) {
353      // whether we know the system or not, we'll accept the stated codes at face value
354      for (ConceptReferenceComponent cc : vsi.getConcept())
355        if (cc.getCode().equals(code)) 
356          return true;
357    }
358    
359    if (!system.equals(vsi.getSystem()))
360      return false;
361    // ok, we need the code system
362    CodeSystem cs = context.fetchCodeSystem(system);
363    if (cs == null || cs.getContent() != CodeSystemContentMode.COMPLETE) {
364      // make up a transient value set with 
365      ValueSet vs = new ValueSet();
366      vs.setUrl(Utilities.makeUuidUrn());
367      vs.getCompose().addInclude(vsi);
368      ValidationResult res = context.validateCode(options.noClient(), new Coding(system, code, null), vs);
369      return res.isOk();
370    } else {
371      if (vsi.hasFilter()) {
372        boolean ok = true;
373        for (ConceptSetFilterComponent f : vsi.getFilter())
374          if (!codeInFilter(cs, system, f, code)) {
375            ok = false;
376            break;
377          }
378        if (ok)
379          return true;
380      }
381      
382      List<ConceptDefinitionComponent> list = cs.getConcept();
383      boolean ok = validateCodeInConceptList(code, cs, list);
384      if (ok && vsi.hasConcept()) {
385        for (ConceptReferenceComponent cc : vsi.getConcept())
386          if (cc.getCode().equals(code)) 
387            return true;
388        return false;
389      } else
390        return ok;
391    }
392  }
393
394  private boolean codeInFilter(CodeSystem cs, String system, ConceptSetFilterComponent f, String code) throws FHIRException {
395    if ("concept".equals(f.getProperty()))
396      return codeInConceptFilter(cs, f, code);
397    else {
398      System.out.println("todo: handle filters with property = "+f.getProperty()); 
399      throw new FHIRException("Unable to handle system "+cs.getUrl()+" filter with property = "+f.getProperty());
400    }
401  }
402
403  private boolean codeInConceptFilter(CodeSystem cs, ConceptSetFilterComponent f, String code) throws FHIRException {
404    switch (f.getOp()) {
405    case ISA: return codeInConceptIsAFilter(cs, f, code);
406    case ISNOTA: return !codeInConceptIsAFilter(cs, f, code);
407    default:
408      System.out.println("todo: handle concept filters with op = "+f.getOp()); 
409      throw new FHIRException("Unable to handle system "+cs.getUrl()+" concept filter with op = "+f.getOp());
410    }
411  }
412
413  private boolean codeInConceptIsAFilter(CodeSystem cs, ConceptSetFilterComponent f, String code) {
414    if (code.equals(f.getProperty()))
415      return true;
416   ConceptDefinitionComponent cc = findCodeInConcept(cs.getConcept(), f.getValue());
417   if (cc == null)
418     return false;
419   cc = findCodeInConcept(cc.getConcept(), code);
420   return cc != null;
421  }
422
423  public boolean validateCodeInConceptList(String code, CodeSystem def, List<ConceptDefinitionComponent> list) {
424    if (def.getCaseSensitive()) {
425      for (ConceptDefinitionComponent cc : list) {
426        if (cc.getCode().equals(code)) 
427          return true;
428        if (cc.hasConcept() && validateCodeInConceptList(code, def, cc.getConcept()))
429          return true;
430      }
431    } else {
432      for (ConceptDefinitionComponent cc : list) {
433        if (cc.getCode().equalsIgnoreCase(code)) 
434          return true;
435        if (cc.hasConcept() && validateCodeInConceptList(code, def, cc.getConcept()))
436          return true;
437      }
438    }
439    return false;
440  }
441
442  private ValueSetCheckerSimple getVs(String url) {
443    if (inner.containsKey(url)) {
444      return inner.get(url);
445    }
446    ValueSet vs = context.fetchResource(ValueSet.class, url);
447    ValueSetCheckerSimple vsc = new ValueSetCheckerSimple(options, vs, context);
448    inner.put(url, vsc);
449    return vsc;
450  }
451  
452  private boolean inImport(String uri, String system, String code) throws FHIRException {
453    return getVs(uri).codeInValueSet(system, code);
454  }
455
456}