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