001package org.hl7.fhir.convertors.misc;
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
033import org.hl7.fhir.utilities.TextFile;
034import org.hl7.fhir.utilities.Utilities;
035import org.hl7.fhir.utilities.xml.XMLUtil;
036import org.w3c.dom.Document;
037import org.w3c.dom.Element;
038
039import javax.xml.parsers.DocumentBuilder;
040import javax.xml.parsers.DocumentBuilderFactory;
041import java.io.File;
042import java.io.InputStream;
043import java.net.HttpURLConnection;
044import java.net.URL;
045import java.util.ArrayList;
046import java.util.List;
047
048public class CKMImporter {
049
050  private String ckm;
051  private String dest;
052  private String config;
053  private String info;
054
055  public static void main(String[] args) throws Exception {
056    CKMImporter self = new CKMImporter();
057    self.ckm = getParam(args, "ckm");
058    self.dest = getParam(args, "dest");
059    self.config = getParam(args, "config");
060    self.info = getParam(args, "info");
061    if (self.ckm == null || self.dest == null || self.config == null) {
062      System.out.println("ADL to FHIR StructureDefinition Converter");
063      System.out.println("This tool takes 4 parameters:");
064      System.out.println("-ckm: Baase URL of CKM");
065      System.out.println("-dest: folder for output");
066      System.out.println("-config: filename of OpenEHR/FHIR knowlege base (required)");
067      System.out.println("-info: folder for additional knowlege of archetypes");
068    } else {
069      self.execute();
070    }
071  }
072
073  private static String getParam(String[] args, String name) {
074    for (int i = 0; i < args.length - 1; i++) {
075      if (args[i].equals("-" + name)) {
076        return args[i + 1];
077      }
078    }
079    return null;
080  }
081
082
083  private void execute() throws Exception {
084    List<String> ids = new ArrayList<String>();
085    Document xml = loadXml(ckm + "/services/ArchetypeFinderBean/getAllArchetypeIds");
086    Element e = XMLUtil.getFirstChild(xml.getDocumentElement());
087    while (e != null) {
088      ids.add(e.getTextContent());
089      e = XMLUtil.getNextSibling(e);
090    }
091//              for (String id : ids) {
092//                      downloadArchetype(id);
093//              }
094    for (String id : ids) {
095      processArchetype(id);
096    }
097  }
098
099  private void downloadArchetype(String id) throws Exception {
100    System.out.println("Fetch " + id);
101    Document sxml = loadXml(ckm + "/services/ArchetypeFinderBean/getArchetypeInXML?archetypeId=" + id);
102    Element e = XMLUtil.getFirstChild(sxml.getDocumentElement());
103
104    String src = Utilities.path("c:\\temp", id + ".xml");
105    TextFile.stringToFile(e.getTextContent(), src);
106  }
107
108  private void processArchetype(String id) throws Exception {
109    System.out.println("Process " + id);
110
111    String cfg = info == null ? null : Utilities.path(info, id + ".config");
112    String src = Utilities.path("c:\\temp", id + ".xml");
113    String dst = Utilities.path(dest, id + ".xml");
114
115    if (!new File(src).exists())
116      downloadArchetype(id);
117    if (cfg != null && new File(cfg).exists())
118      ADLImporter.main(new String[]{"-source", src, "-dest", dst, "-config", config, "-info", cfg});
119    else
120      ADLImporter.main(new String[]{"-source", src, "-dest", dst, "-config", config});
121  }
122
123  private Document loadXml(String address) throws Exception {
124    URL url = new URL(address);
125    HttpURLConnection connection =
126      (HttpURLConnection) url.openConnection();
127    connection.setRequestMethod("GET");
128    connection.setRequestProperty("Accept", "application/xml");
129
130    InputStream xml = connection.getInputStream();
131
132    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
133    DocumentBuilder db = dbf.newDocumentBuilder();
134    return db.parse(xml);
135  }
136}