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