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 com.google.gson.Gson;
034import com.google.gson.GsonBuilder;
035import com.google.gson.JsonObject;
036import org.hl7.fhir.utilities.TextFile;
037import org.hl7.fhir.utilities.Utilities;
038import org.hl7.fhir.utilities.json.JsonTrackingParser;
039
040import java.io.File;
041import java.io.IOException;
042import java.util.ArrayList;
043import java.util.List;
044
045public class PackageMaintainer {
046
047
048  private static final String PATH = "C:\\work\\org.hl7.fhir\\packages\\core";
049
050  public static void main(String[] args) throws IOException {
051    new PackageMaintainer().check("r4");
052    new PackageMaintainer().check("r2");
053    new PackageMaintainer().check("r3");
054    new PackageMaintainer().check("r2b");
055  }
056
057  private void check(String ver) throws IOException {
058    System.out.println("Check " + ver);
059    List<String> allIds = listResources(Utilities.path(PATH, "hl7.fhir." + ver + ".examples", "package"));
060    List<String> coreIds = listResources(Utilities.path(PATH, "hl7.fhir." + ver + ".core", "package"));
061    for (String s : coreIds) {
062      if (!allIds.contains(s)) {
063        System.out.println("Core contains " + s + " but allIds doesn't");
064      }
065    }
066    for (String s : allIds) {
067      if (!coreIds.contains(s)) {
068        String c = s.substring(0, s.indexOf("-"));
069        if (Utilities.existsInList(c, "CodeSystem", "ValueSet", "ConceptMap", "StructureDefinition", "StructureMap", "NamingSystem", "SearchParameter", "OperationDefinition", "CapabilityStatement", "Conformance"))
070          System.out.println("Examples contains " + s + " but core doesn't");
071      }
072    }
073    strip(new File(Utilities.path(PATH, "hl7.fhir." + ver + ".core", "package")));
074    strip(new File(Utilities.path(PATH, "hl7.fhir." + ver + ".expansions", "package")));
075    if (!ver.equals("r2b"))
076      strip(new File(Utilities.path(PATH, "hl7.fhir." + ver + ".elements", "package")));
077  }
078
079
080  private List<String> listResources(String dir) {
081    File folder = new File(dir);
082    List<String> res = new ArrayList<>();
083    for (String fn : folder.list()) {
084      if (fn.endsWith(".json") && fn.contains("-")) {
085        String s = fn;
086        s = s.substring(0, s.indexOf("."));
087        res.add(s);
088      }
089    }
090    return res;
091  }
092
093  private void strip(File folder) throws IOException {
094    for (File f : folder.listFiles()) {
095      if (f.isDirectory())
096        strip(f);
097      else if (f.getName().endsWith(".json")) {
098        JsonObject json = JsonTrackingParser.parseJson(f);
099        if (json.has("resourceType") && json.has("text")) {
100          json.remove("text");
101          Gson gson = new GsonBuilder().create();
102          String src = gson.toJson(json);
103          TextFile.stringToFile(src, f.getAbsolutePath());
104        }
105      }
106    }
107  }
108
109}