001package org.hl7.fhir.dstu2016may.terminologies;
002
003import java.util.List;
004
005import org.hl7.fhir.dstu2016may.model.BooleanType;
006import org.hl7.fhir.dstu2016may.model.CodeSystem;
007import org.hl7.fhir.dstu2016may.model.CodeSystem.CodeSystemPropertyComponent;
008import org.hl7.fhir.dstu2016may.model.CodeSystem.ConceptDefinitionComponent;
009import org.hl7.fhir.dstu2016may.model.CodeSystem.ConceptDefinitionPropertyComponent;
010import org.hl7.fhir.dstu2016may.model.CodeSystem.PropertyType;
011
012public class CodeSystemUtilities {
013
014  public static boolean isDeprecated(CodeSystem cs, ConceptDefinitionComponent def) {
015    for (ConceptDefinitionPropertyComponent p : def.getProperty()) {
016      if (p.getCode().equals("deprecated") && p.hasValue() && p.getValue() instanceof BooleanType) 
017        return ((BooleanType) p.getValue()).getValue();
018    }
019    return false;
020  }
021
022  public static boolean isAbstract(CodeSystem cs, ConceptDefinitionComponent def) {
023    for (ConceptDefinitionPropertyComponent p : def.getProperty()) {
024      if (p.getCode().equals("abstract") && p.hasValue() && p.getValue() instanceof BooleanType) 
025        return ((BooleanType) p.getValue()).getValue();
026    }
027    return false;
028  }
029
030  public static void setAbstract(CodeSystem cs, ConceptDefinitionComponent concept) {
031    defineAbstractProperty(cs);
032    concept.addProperty().setCode("abstract").setValue(new BooleanType(true));    
033  }
034
035  public static void setDeprecated(CodeSystem cs, ConceptDefinitionComponent concept) {
036    defineAbstractProperty(cs);
037    concept.addProperty().setCode("deprecated").setValue(new BooleanType(true));    
038  }
039
040  public static void defineAbstractProperty(CodeSystem cs) {
041    defineCodeSystemProperty(cs, "abstract", "Indicates that the code is abstract - only intended to be used as a selector for other concepts", PropertyType.BOOLEAN);
042  }
043
044  public static void defineDeprecatedProperty(CodeSystem cs) {
045    defineCodeSystemProperty(cs, "deprecated", "Indicates that the code should not longer be used", PropertyType.BOOLEAN);
046  }
047
048  public static void defineCodeSystemProperty(CodeSystem cs, String code, String description, PropertyType type) {
049    for (CodeSystemPropertyComponent p : cs.getProperty()) {
050      if (p.getCode().equals(code))
051        return;
052    }
053    cs.addProperty().setCode(code).setDescription(description).setType(type);
054  }
055
056  public static String getCodeDefinition(CodeSystem cs, String code) {
057    return getCodeDefinition(cs.getConcept(), code);
058  }
059
060  private static String getCodeDefinition(List<ConceptDefinitionComponent> list, String code) {
061    for (ConceptDefinitionComponent c : list) {
062      if (c.getCode().equals(code))
063        return c.getDefinition();
064      String s = getCodeDefinition(c.getConcept(), code);
065      if (s != null)
066        return s;
067    }
068    return null;
069  }
070
071
072}