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 */
051public class PackagePreparer {
052
053
054  public static void main(String[] args) throws IOException {
055    for (File f : ManagedFileAccess.file("C:\\work\\fhirserver\\resources\\mihin").listFiles()) {
056      try {
057        org.hl7.fhir.dstu3.model.Resource r = new org.hl7.fhir.dstu3.formats.JsonParser().parse(ManagedFileAccess.inStream(f));
058        if (r instanceof Bundle) {
059          Bundle b = (Bundle) r;
060          for (BundleEntryComponent be : b.getEntry()) {
061            try {
062              org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_30_40.convertResource(be.getResource());
063              if (r4.getId().startsWith(r4.fhirType() + "-"))
064                be.getResource().setId(r4.getId().substring(r4.fhirType().length() + 1));
065              if (be.getResource().hasId())
066                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);
067              else
068                System.out.println(f.getName() + " bundle entry has no id");
069            } catch (Exception e) {
070              System.out.println(f.getName() + ": " + e.getMessage());
071            }
072          }
073        } else if (r.hasId())
074          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));
075        else
076          System.out.println(f.getName() + " has no id");
077      } catch (Exception e) {
078        System.out.println(f.getName() + ": " + e.getMessage());
079        e.printStackTrace();
080      }
081    }
082
083    System.out.println("Completed OK");
084  }
085
086}