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.List; 008 009import javax.xml.parsers.DocumentBuilder; 010import javax.xml.parsers.DocumentBuilderFactory; 011 012/* 013 Copyright (c) 2011+, HL7, Inc. 014 All rights reserved. 015 016 Redistribution and use in source and binary forms, with or without modification, 017 are permitted provided that the following conditions are met: 018 019 * Redistributions of source code must retain the above copyright notice, this 020 list of conditions and the following disclaimer. 021 * Redistributions in binary form must reproduce the above copyright notice, 022 this list of conditions and the following disclaimer in the documentation 023 and/or other materials provided with the distribution. 024 * Neither the name of HL7 nor the names of its contributors may be used to 025 endorse or promote products derived from this software without specific 026 prior written permission. 027 028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 029 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 030 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 031 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 032 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 033 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 034 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 035 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 036 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 037 POSSIBILITY OF SUCH DAMAGE. 038 039 */ 040 041 042import org.hl7.fhir.convertors.misc.adl.ADLImporter; 043import org.hl7.fhir.utilities.TextFile; 044import org.hl7.fhir.utilities.Utilities; 045import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; 046import org.hl7.fhir.utilities.http.HTTPResult; 047import org.hl7.fhir.utilities.http.ManagedWebAccess; 048import org.hl7.fhir.utilities.xml.XMLUtil; 049import org.w3c.dom.Document; 050import org.w3c.dom.Element; 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(Utilities.path("[tmp]"), 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(Utilities.path("[tmp]"), id + ".xml"); 117 String dst = Utilities.path(dest, id + ".xml"); 118 119 if (!ManagedFileAccess.file(src).exists()) 120 downloadArchetype(id); 121 if (cfg != null && ManagedFileAccess.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 129 HTTPResult res = ManagedWebAccess.get(address, "application/xml"); 130 res.checkThrowException(); 131 InputStream xml = new ByteArrayInputStream(res.getContent()); 132 133 DocumentBuilderFactory dbf = XMLUtil.newXXEProtectedDocumentBuilderFactory(); 134 DocumentBuilder db = dbf.newDocumentBuilder(); 135 return db.parse(xml); 136 } 137}