001package org.hl7.fhir.convertors.misc;
002
003import org.hl7.fhir.utilities.SimpleHTTPClient;
004import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
005
006/*
007  Copyright (c) 2011+, HL7, Inc.
008  All rights reserved.
009  
010  Redistribution and use in source and binary forms, with or without modification, 
011  are permitted provided that the following conditions are met:
012    
013   * Redistributions of source code must retain the above copyright notice, this 
014     list of conditions and the following disclaimer.
015   * Redistributions in binary form must reproduce the above copyright notice, 
016     this list of conditions and the following disclaimer in the documentation 
017     and/or other materials provided with the distribution.
018   * Neither the name of HL7 nor the names of its contributors may be used to 
019     endorse or promote products derived from this software without specific 
020     prior written permission.
021  
022  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
023  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
024  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
025  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
026  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
027  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
028  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
029  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
030  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
031  POSSIBILITY OF SUCH DAMAGE.
032  
033 */
034
035
036import org.hl7.fhir.convertors.misc.adl.ADLImporter;
037import org.hl7.fhir.utilities.TextFile;
038import org.hl7.fhir.utilities.Utilities;
039import org.hl7.fhir.utilities.xml.XMLUtil;
040import org.w3c.dom.Document;
041import org.w3c.dom.Element;
042
043import javax.xml.parsers.DocumentBuilder;
044import javax.xml.parsers.DocumentBuilderFactory;
045
046import java.io.ByteArrayInputStream;
047import java.io.File;
048import java.io.InputStream;
049import java.util.ArrayList;
050import java.util.List;
051
052public class CKMImporter {
053
054  private String ckm;
055  private String dest;
056  private String config;
057  private String info;
058
059  public static void main(String[] args) throws Exception {
060    CKMImporter self = new CKMImporter();
061    self.ckm = getParam(args, "ckm");
062    self.dest = getParam(args, "dest");
063    self.config = getParam(args, "config");
064    self.info = getParam(args, "info");
065    if (self.ckm == null || self.dest == null || self.config == null) {
066      System.out.println("ADL to FHIR StructureDefinition Converter");
067      System.out.println("This tool takes 4 parameters:");
068      System.out.println("-ckm: Baase URL of CKM");
069      System.out.println("-dest: folder for output");
070      System.out.println("-config: filename of OpenEHR/FHIR knowlege base (required)");
071      System.out.println("-info: folder for additional knowlege of archetypes");
072    } else {
073      self.execute();
074    }
075  }
076
077  private static String getParam(String[] args, String name) {
078    for (int i = 0; i < args.length - 1; i++) {
079      if (args[i].equals("-" + name)) {
080        return args[i + 1];
081      }
082    }
083    return null;
084  }
085
086
087  private void execute() throws Exception {
088    List<String> ids = new ArrayList<String>();
089    Document xml = loadXml(ckm + "/services/ArchetypeFinderBean/getAllArchetypeIds");
090    Element e = XMLUtil.getFirstChild(xml.getDocumentElement());
091    while (e != null) {
092      ids.add(e.getTextContent());
093      e = XMLUtil.getNextSibling(e);
094    }
095//              for (String id : ids) {
096//                      downloadArchetype(id);
097//              }
098    for (String id : ids) {
099      processArchetype(id);
100    }
101  }
102
103  private void downloadArchetype(String id) throws Exception {
104    System.out.println("Fetch " + id);
105    Document sxml = loadXml(ckm + "/services/ArchetypeFinderBean/getArchetypeInXML?archetypeId=" + id);
106    Element e = XMLUtil.getFirstChild(sxml.getDocumentElement());
107
108    String src = Utilities.path("c:\\temp", id + ".xml");
109    TextFile.stringToFile(e.getTextContent(), src);
110  }
111
112  private void processArchetype(String id) throws Exception {
113    System.out.println("Process " + id);
114
115    String cfg = info == null ? null : Utilities.path(info, id + ".config");
116    String src = Utilities.path("c:\\temp", id + ".xml");
117    String dst = Utilities.path(dest, id + ".xml");
118
119    if (!new File(src).exists())
120      downloadArchetype(id);
121    if (cfg != null && new File(cfg).exists())
122      ADLImporter.main(new String[]{"-source", src, "-dest", dst, "-config", config, "-info", cfg});
123    else
124      ADLImporter.main(new String[]{"-source", src, "-dest", dst, "-config", config});
125  }
126
127  private Document loadXml(String address) throws Exception {
128    SimpleHTTPClient http = new SimpleHTTPClient();
129    HTTPResult res = http.get(address, "application/xml");
130    res.checkThrowException();
131    InputStream xml = new ByteArrayInputStream(res.getContent());
132
133    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
134    DocumentBuilder db = dbf.newDocumentBuilder();
135    return db.parse(xml);
136  }
137}