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.Utilities;
042import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
043
044/*
045 * load reosurces in xml format, and sve them in package format (json, with correct name
046 *
047 * C:\work\fhirserver\resources\resources\dicom
048 *
049 */
050public class PackagePreparer {
051
052
053  public static void main(String[] args) throws IOException {
054    for (File f : ManagedFileAccess.file("C:\\work\\fhirserver\\resources\\mihin").listFiles()) {
055      try {
056        org.hl7.fhir.dstu3.model.Resource r = new org.hl7.fhir.dstu3.formats.JsonParser().parse(ManagedFileAccess.inStream(f));
057        if (r instanceof Bundle) {
058          Bundle b = (Bundle) r;
059          for (BundleEntryComponent be : b.getEntry()) {
060            try {
061              org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_30_40.convertResource(be.getResource());
062              if (r4.getId().startsWith(r4.fhirType() + "-"))
063                be.getResource().setId(r4.getId().substring(r4.fhirType().length() + 1));
064              if (be.getResource().hasId())
065                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);
066              else
067                System.out.println(f.getName() + " bundle entry has no id");
068            } catch (Exception e) {
069              System.out.println(f.getName() + ": " + e.getMessage());
070            }
071          }
072        } else if (r.hasId())
073          new org.hl7.fhir.r4.formats.JsonParser().compose(ManagedFileAccess.outStream(Utilities.path(Utilities.getDirectoryForFile(f.getAbsolutePath()), r.fhirType() + "-" + r.getId() + ".json")), VersionConvertorFactory_30_40.convertResource(r));
074        else
075          System.out.println(f.getName() + " has no id");
076      } catch (Exception e) {
077        System.out.println(f.getName() + ": " + e.getMessage());
078        e.printStackTrace();
079      }
080    }
081
082    System.out.println("Completed OK");
083  }
084
085}