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.io.FileInputStream;
035import java.io.FileOutputStream;
036import java.util.ArrayList;
037import java.util.HashMap;
038import java.util.List;
039import java.util.Map;
040
041import javax.xml.parsers.DocumentBuilder;
042import javax.xml.parsers.DocumentBuilderFactory;
043
044import org.hl7.fhir.dstu2016may.formats.IParser.OutputStyle;
045import org.hl7.fhir.dstu2016may.formats.XmlParser;
046import org.hl7.fhir.dstu2016may.model.CodeSystem;
047import org.hl7.fhir.dstu2016may.model.CodeSystem.ConceptDefinitionComponent;
048import org.hl7.fhir.dstu2016may.model.Coding;
049import org.hl7.fhir.dstu2016may.model.DateTimeType;
050import org.hl7.fhir.dstu2016may.model.Enumerations.ConformanceResourceStatus;
051import org.hl7.fhir.dstu2016may.model.Identifier;
052import org.hl7.fhir.dstu2016may.model.ValueSet;
053import org.hl7.fhir.utilities.xml.XMLUtil;
054import org.w3c.dom.Document;
055import org.w3c.dom.Element;
056
057
058/**
059 * This is defined as a prototype ClaML importer
060 * 
061 * @author Grahame
062 *
063 */
064
065public class ICPC2Importer {
066
067  public static void main(String[] args) {
068    try {
069      ICPC2Importer r = new ICPC2Importer();
070      r.setSourceFileName("c:\\temp\\ICPC-2e-v5.0.xml");
071      r.setTargetFileNameCS("C:\\temp\\icpc2.xml");
072      r.setTargetFileNameVS("C:\\temp\\icpc2-vs.xml");
073      r.go();
074      System.out.println("Completed OK");
075    } catch (Exception e) {
076      e.printStackTrace();
077    }
078  }
079
080  private String sourceFileName; // the ICPC2 ClaML file
081  private String targetFileNameVS; // the value set to produce
082  private String targetFileNameCS; // the value set to produce
083  
084  public ICPC2Importer() {
085    super();
086  }
087  public ICPC2Importer(String sourceFileName, String targetFileNameCS, String targetFileNameVS) {
088    super();
089    this.sourceFileName = sourceFileName;
090    this.targetFileNameCS = targetFileNameCS;
091    this.targetFileNameVS = targetFileNameVS;
092  }
093  public String getSourceFileName() {
094    return sourceFileName;
095  }
096  public void setSourceFileName(String sourceFileName) {
097    this.sourceFileName = sourceFileName;
098  }
099  public String getTargetFileNameCS() {
100    return targetFileNameCS;
101  }
102  public void setTargetFileNameCS(String targetFileName) {
103    this.targetFileNameCS = targetFileName;
104  }
105
106  public String getTargetFileNameVS() {
107    return targetFileNameVS;
108  }
109  public void setTargetFileNameVS(String targetFileName) {
110    this.targetFileNameVS = targetFileName;
111  }
112
113  public void go() throws Exception {
114    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
115    factory.setNamespaceAware(false);
116    DocumentBuilder builder = factory.newDocumentBuilder();
117    Document doc = builder.parse(new FileInputStream(sourceFileName));
118
119    ValueSet vs = new ValueSet();
120    vs.setUrl("http://hl7.org/fhir/sid/icpc2/vs");
121    Element title = XMLUtil.getNamedChild(doc.getDocumentElement(), "Title");
122    vs.setVersion(title.getAttribute("version"));
123    vs.setName(title.getAttribute("name"));
124    vs.setImmutable(true);
125    Element identifier = XMLUtil.getNamedChild(doc.getDocumentElement(), "Identifier");
126    vs.setPublisher(identifier.getAttribute("authority"));
127    vs.setIdentifier(new Identifier().setValue(identifier.getAttribute("uid")));
128    List<Element> authors = new ArrayList<Element>(); 
129    XMLUtil.getNamedChildren(XMLUtil.getNamedChild(doc.getDocumentElement(), "Authors"), "Author", authors);
130    for (Element a : authors)
131      if (!a.getAttribute("name").contains("+"))
132        vs.addContact().setName(a.getTextContent());
133    vs.setCopyright("The copyright of ICPC, both in hard copy and in electronic form, is owned by Wonca. See http://www.kith.no/templates/kith_WebPage____1110.aspx");
134    vs.setStatus(ConformanceResourceStatus.ACTIVE);
135    vs.setDateElement(new DateTimeType(title.getAttribute("date")));
136    
137    vs.getCompose().addInclude().setSystem("http://hl7.org/fhir/sid/icpc2");
138    CodeSystem cs = new CodeSystem();
139    cs.setUrl("http://hl7.org/fhir/sid/icpc2");
140    cs.setVersion(title.getAttribute("version"));
141    cs.setName(title.getAttribute("name"));
142    identifier = XMLUtil.getNamedChild(doc.getDocumentElement(), "Identifier");
143    cs.setPublisher(identifier.getAttribute("authority"));
144    cs.setIdentifier(new Identifier().setValue(identifier.getAttribute("uid")));
145    authors = new ArrayList<Element>(); 
146    XMLUtil.getNamedChildren(XMLUtil.getNamedChild(doc.getDocumentElement(), "Authors"), "Author", authors);
147    for (Element a : authors)
148      if (!a.getAttribute("name").contains("+"))
149        cs.addContact().setName(a.getTextContent());
150    cs.setCopyright("The copyright of ICPC, both in hard copy and in electronic form, is owned by Wonca. See http://www.kith.no/templates/kith_WebPage____1110.aspx");
151    cs.setStatus(ConformanceResourceStatus.ACTIVE);
152    cs.setDateElement(new DateTimeType(title.getAttribute("date")));
153    cs.setValueSet(vs.getUrl());
154    
155    Map<String, ConceptDefinitionComponent> concepts = new HashMap<String, ConceptDefinitionComponent>();
156    List<Element> classes = new ArrayList<Element>(); 
157    XMLUtil.getNamedChildren(doc.getDocumentElement(), "Class", classes);
158    for (Element cls : classes) {
159      processClass(cls, concepts, cs);
160    }
161    
162    XmlParser xml = new XmlParser();
163    xml.setOutputStyle(OutputStyle.PRETTY);
164    xml.compose(new FileOutputStream(targetFileNameVS), vs);
165    xml.compose(new FileOutputStream(targetFileNameCS), cs);
166  }
167  
168  private void processClass(Element cls, Map<String, ConceptDefinitionComponent> concepts, CodeSystem define) {
169    ConceptDefinitionComponent concept = new ConceptDefinitionComponent();
170    concept.setCode(cls.getAttribute("code"));
171    concept.setDefinition(getRubric(cls, "preferred"));
172    String s = getRubric(cls, "shortTitle");
173    if (s != null && !s.equals(concept.getDefinition()))
174      concept.addDesignation().setUse(new Coding().setSystem("http://hl7.org/fhir/sid/icpc2/rubrics").setCode("shortTitle")).setValue(s);
175    s = getRubric(cls, "inclusion");
176    if (s != null)
177      concept.addDesignation().setUse(new Coding().setSystem("http://hl7.org/fhir/sid/icpc2/rubrics").setCode("inclusion")).setValue(s);
178    s = getRubric(cls, "exclusion");
179    if (s != null)
180      concept.addDesignation().setUse(new Coding().setSystem("http://hl7.org/fhir/sid/icpc2/rubrics").setCode("exclusion")).setValue(s);
181    s = getRubric(cls, "criteria");
182    if (s != null)
183      concept.addDesignation().setUse(new Coding().setSystem("http://hl7.org/fhir/sid/icpc2/rubrics").setCode("criteria")).setValue(s);
184    s = getRubric(cls, "consider");
185    if (s != null)
186      concept.addDesignation().setUse(new Coding().setSystem("http://hl7.org/fhir/sid/icpc2/rubrics").setCode("consider")).setValue(s);
187    s = getRubric(cls, "note");
188    if (s != null)
189      concept.addDesignation().setUse(new Coding().setSystem("http://hl7.org/fhir/sid/icpc2/rubrics").setCode("note")).setValue(s);
190    
191    concepts.put(concept.getCode(), concept);
192    List<Element> children = new ArrayList<Element>(); 
193    XMLUtil.getNamedChildren(cls, "SubClass", children);
194    if (children.size() > 0)
195      CodeSystemUtilities.setAbstract(define, concept);
196    
197    Element parent = XMLUtil.getNamedChild(cls, "SuperClass");
198    if (parent == null) {
199      define.addConcept(concept);
200    } else {
201      ConceptDefinitionComponent p = concepts.get(parent.getAttribute("code"));
202      p.getConcept().add(concept);
203    }
204  }
205  
206  private String getRubric(Element cls, String kind) {
207    List<Element> rubrics = new ArrayList<Element>(); 
208    XMLUtil.getNamedChildren(cls, "Rubric", rubrics);
209    for (Element r : rubrics) {
210      if (r.getAttribute("kind").equals(kind))
211        return XMLUtil.getNamedChild(r,  "Label").getTextContent();
212    }
213    return null;
214  }
215  
216}