
001package org.hl7.fhir.convertors.misc; 002 003import java.io.File; 004import java.io.FileInputStream; 005import java.io.FileOutputStream; 006import java.io.IOException; 007 008/* 009 Copyright (c) 2011+, HL7, Inc. 010 All rights reserved. 011 012 Redistribution and use in source and binary forms, with or without modification, 013 are permitted provided that the following conditions are met: 014 015 * Redistributions of source code must retain the above copyright notice, this 016 list of conditions and the following disclaimer. 017 * Redistributions in binary form must reproduce the above copyright notice, 018 this list of conditions and the following disclaimer in the documentation 019 and/or other materials provided with the distribution. 020 * Neither the name of HL7 nor the names of its contributors may be used to 021 endorse or promote products derived from this software without specific 022 prior written permission. 023 024 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 025 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 026 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 027 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 028 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 029 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 030 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 031 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 032 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 033 POSSIBILITY OF SUCH DAMAGE. 034 035 */ 036 037 038import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_40; 039import org.hl7.fhir.dstu3.model.Bundle; 040import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent; 041import org.hl7.fhir.utilities.FileUtilities; 042import org.hl7.fhir.utilities.Utilities; 043import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; 044 045/* 046 * load reosurces in xml format, and sve them in package format (json, with correct name 047 * 048 * C:\work\fhirserver\resources\resources\dicom 049 * 050 */ 051@SuppressWarnings("checkstyle:systemout") 052public class PackagePreparer { 053 054 055 public static void main(String[] args) throws IOException { 056 for (File f : ManagedFileAccess.file("C:\\work\\fhirserver\\resources\\mihin").listFiles()) { 057 try { 058 org.hl7.fhir.dstu3.model.Resource r = new org.hl7.fhir.dstu3.formats.JsonParser().parse(ManagedFileAccess.inStream(f)); 059 if (r instanceof Bundle) { 060 Bundle b = (Bundle) r; 061 for (BundleEntryComponent be : b.getEntry()) { 062 try { 063 org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_30_40.convertResource(be.getResource()); 064 if (r4.getId().startsWith(r4.fhirType() + "-")) 065 be.getResource().setId(r4.getId().substring(r4.fhirType().length() + 1)); 066 if (be.getResource().hasId()) 067 new org.hl7.fhir.r4.formats.JsonParser().compose(ManagedFileAccess.outStream(Utilities.path("C:\\work\\fhirserver\\resources\\fhir.test.data\\3.5.0\\package", be.getResource().fhirType() + "-" + be.getResource().getId() + ".json")), r4); 068 else 069 System.out.println(f.getName() + " bundle entry has no id"); 070 } catch (Exception e) { 071 System.out.println(f.getName() + ": " + e.getMessage()); 072 } 073 } 074 } else if (r.hasId()) 075 new org.hl7.fhir.r4.formats.JsonParser().compose(ManagedFileAccess.outStream(Utilities.path(FileUtilities.getDirectoryForFile(f.getAbsolutePath()), r.fhirType() + "-" + r.getId() + ".json")), VersionConvertorFactory_30_40.convertResource(r)); 076 else 077 System.out.println(f.getName() + " has no id"); 078 } catch (Exception e) { 079 System.out.println(f.getName() + ": " + e.getMessage()); 080 e.printStackTrace(); 081 } 082 } 083 084 System.out.println("Completed OK"); 085 } 086 087}