001package org.hl7.fhir.convertors.misc;
002
003import java.io.IOException;
004import java.util.Date;
005
006import javax.annotation.Nonnull;
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.advisors.impl.BaseAdvisor_10_30;
039import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_30;
040import org.hl7.fhir.dstu3.formats.IParser.OutputStyle;
041import org.hl7.fhir.dstu3.model.Bundle;
042import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent;
043import org.hl7.fhir.dstu3.model.Bundle.BundleType;
044import org.hl7.fhir.dstu3.model.CodeSystem;
045import org.hl7.fhir.dstu3.model.ValueSet;
046import org.hl7.fhir.exceptions.FHIRException;
047import org.hl7.fhir.utilities.FhirPublication;
048import org.hl7.fhir.utilities.Utilities;
049import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
050
051@SuppressWarnings("checkstyle:systemout")
052public class IGPackConverter102 extends BaseAdvisor_10_30 {
053
054  private final Bundle cslist = new Bundle();
055
056  public static void main(String[] args) throws Exception {
057    new IGPackConverter102().process();
058  }
059
060  private void process() throws IOException, FHIRException {
061    initCSList();
062    for (String s : ManagedFileAccess.file(Utilities.path("[tmp]", "igpack2")).list()) {
063      if (s.endsWith(".xml") && !s.startsWith("z-") && !Utilities.existsInList(s, "expansions.xml", "v3-codesystems.xml", "v2-tables.xml")) {
064        System.out.println("process " + s);
065        org.hl7.fhir.dstu2.formats.XmlParser xp = new org.hl7.fhir.dstu2.formats.XmlParser();
066        org.hl7.fhir.dstu2.model.Resource r10 = xp.parse(ManagedFileAccess.inStream(Utilities.path("[tmp]", "igpack2\\" + s)));
067        org.hl7.fhir.dstu3.model.Resource r17 = VersionConvertorFactory_10_30.convertResource(r10, this);
068        org.hl7.fhir.dstu3.formats.XmlParser xc = new org.hl7.fhir.dstu3.formats.XmlParser();
069        xc.setOutputStyle(OutputStyle.PRETTY);
070        xc.compose(ManagedFileAccess.outStream(Utilities.path("[tmp]", "igpack2\\" + s)), r17);
071      }
072    }
073    System.out.println("save codesystems");
074    org.hl7.fhir.dstu3.formats.XmlParser xc = new org.hl7.fhir.dstu3.formats.XmlParser();
075    xc.setOutputStyle(OutputStyle.PRETTY);
076    xc.compose(ManagedFileAccess.outStream(Utilities.path("[tmp]", "igpack2\\codesystems.xml")), cslist);
077    System.out.println("done");
078  }
079
080  private void initCSList() {
081    cslist.setId("codesystems");
082    cslist.setType(BundleType.COLLECTION);
083    cslist.getMeta().setLastUpdated(new Date());
084  }
085
086  @Override
087  public boolean ignoreEntry(@Nonnull BundleEntryComponent src, @Nonnull FhirPublication publication) {
088    return false;
089  }
090
091
092  @Override
093  public void handleCodeSystem(@Nonnull CodeSystem tgtcs, @Nonnull ValueSet vs) {
094    cslist.addEntry().setFullUrl(tgtcs.getUrl()).setResource(tgtcs);
095  }
096
097  @Override
098  public CodeSystem getCodeSystem(@Nonnull ValueSet src) {
099    return null;
100  }
101
102}