001package org.hl7.fhir.convertors.misc;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_40;
034import org.hl7.fhir.dstu3.model.Bundle;
035import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent;
036import org.hl7.fhir.utilities.Utilities;
037
038import java.io.File;
039import java.io.FileInputStream;
040import java.io.FileOutputStream;
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}