
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 053public class CKMImporter { 054 055 private String ckm; 056 private String dest; 057 private String config; 058 private String info; 059 060 public static void main(String[] args) throws Exception { 061 CKMImporter self = new CKMImporter(); 062 self.ckm = getParam(args, "ckm"); 063 self.dest = getParam(args, "dest"); 064 self.config = getParam(args, "config"); 065 self.info = getParam(args, "info"); 066 if (self.ckm == null || self.dest == null || self.config == null) { 067 System.out.println("ADL to FHIR StructureDefinition Converter"); 068 System.out.println("This tool takes 4 parameters:"); 069 System.out.println("-ckm: Baase URL of CKM"); 070 System.out.println("-dest: folder for output"); 071 System.out.println("-config: filename of OpenEHR/FHIR knowlege base (required)"); 072 System.out.println("-info: folder for additional knowlege of archetypes"); 073 } else { 074 self.execute(); 075 } 076 } 077 078 private static String getParam(String[] args, String name) { 079 for (int i = 0; i < args.length - 1; i++) { 080 if (args[i].equals("-" + name)) { 081 return args[i + 1]; 082 } 083 } 084 return null; 085 } 086 087 088 private void execute() throws Exception { 089 List<String> ids = new ArrayList<String>(); 090 Document xml = loadXml(ckm + "/services/ArchetypeFinderBean/getAllArchetypeIds"); 091 Element e = XMLUtil.getFirstChild(xml.getDocumentElement()); 092 while (e != null) { 093 ids.add(e.getTextContent()); 094 e = XMLUtil.getNextSibling(e); 095 } 096// for (String id : ids) { 097// downloadArchetype(id); 098// } 099 for (String id : ids) { 100 processArchetype(id); 101 } 102 } 103 104 private void downloadArchetype(String id) throws Exception { 105 System.out.println("Fetch " + id); 106 Document sxml = loadXml(ckm + "/services/ArchetypeFinderBean/getArchetypeInXML?archetypeId=" + id); 107 Element e = XMLUtil.getFirstChild(sxml.getDocumentElement()); 108 109 String src = Utilities.path(Utilities.path("[tmp]"), id + ".xml"); 110 FileUtilities.stringToFile(e.getTextContent(), src); 111 } 112 113 private void processArchetype(String id) throws Exception { 114 System.out.println("Process " + id); 115 116 String cfg = info == null ? null : Utilities.path(info, id + ".config"); 117 String src = Utilities.path(Utilities.path("[tmp]"), id + ".xml"); 118 String dst = Utilities.path(dest, id + ".xml"); 119 120 if (!ManagedFileAccess.file(src).exists()) 121 downloadArchetype(id); 122 if (cfg != null && ManagedFileAccess.file(cfg).exists()) 123 ADLImporter.main(new String[]{"-source", src, "-dest", dst, "-config", config, "-info", cfg}); 124 else 125 ADLImporter.main(new String[]{"-source", src, "-dest", dst, "-config", config}); 126 } 127 128 private Document loadXml(String address) throws Exception { 129 130 HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), address, "application/xml"); 131 res.checkThrowException(); 132 InputStream xml = new ByteArrayInputStream(res.getContent()); 133 134 DocumentBuilderFactory dbf = XMLUtil.newXXEProtectedDocumentBuilderFactory(); 135 DocumentBuilder db = dbf.newDocumentBuilder(); 136 return db.parse(xml); 137 } 138}