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 032 033 034import java.util.List; 035 036import org.hl7.fhir.dstu2.model.UriType; 037import org.hl7.fhir.dstu2.model.ValueSet; 038import org.hl7.fhir.dstu2.model.ValueSet.ConceptDefinitionComponent; 039import org.hl7.fhir.dstu2.model.ValueSet.ConceptReferenceComponent; 040import org.hl7.fhir.dstu2.model.ValueSet.ConceptSetComponent; 041import org.hl7.fhir.dstu2.model.ValueSet.ConceptSetFilterComponent; 042import org.hl7.fhir.dstu2.model.ValueSet.ValueSetExpansionContainsComponent; 043import org.hl7.fhir.dstu2.terminologies.ValueSetExpander.ValueSetExpansionOutcome; 044import org.hl7.fhir.dstu2.utils.EOperationOutcome; 045import org.hl7.fhir.dstu2.utils.IWorkerContext; 046import org.hl7.fhir.dstu2.utils.IWorkerContext.ValidationResult; 047 048public class ValueSetCheckerSimple implements ValueSetChecker { 049 050 private ValueSet valueset; 051 private ValueSetExpanderFactory factory; 052 private IWorkerContext context; 053 054 public ValueSetCheckerSimple(ValueSet source, ValueSetExpanderFactory factory, IWorkerContext context) { 055 this.valueset = source; 056 this.factory = factory; 057 this.context = context; 058 } 059 060 @Override 061 public boolean codeInValueSet(String system, String code) throws EOperationOutcome, Exception { 062 if (valueset.hasCodeSystem() && system.equals(valueset.getCodeSystem().getSystem()) && codeInDefine(valueset.getCodeSystem().getConcept(), code, valueset.getCodeSystem().getCaseSensitive())) 063 return true; 064 065 if (valueset.hasCompose()) { 066 boolean ok = false; 067 for (UriType uri : valueset.getCompose().getImport()) { 068 ok = ok || inImport(uri.getValue(), system, code); 069 } 070 for (ConceptSetComponent vsi : valueset.getCompose().getInclude()) { 071 ok = ok || inComponent(vsi, system, code); 072 } 073 for (ConceptSetComponent vsi : valueset.getCompose().getExclude()) { 074 ok = ok && !inComponent(vsi, system, code); 075 } 076 } 077 078 return false; 079 } 080 081 private boolean inImport(String uri, String system, String code) throws EOperationOutcome, Exception { 082 ValueSet vs = context.fetchResource(ValueSet.class, uri); 083 if (vs == null) 084 return false ; // we can't tell 085 return codeInExpansion(factory.getExpander().expand(vs), system, code); 086 } 087 088 private boolean codeInExpansion(ValueSetExpansionOutcome vso, String system, String code) 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 113 private boolean inComponent(ConceptSetComponent vsi, String system, String code) { 114 if (!vsi.getSystem().equals(system)) 115 return false; 116 // whether we know the system or not, we'll accept the stated codes at face 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}