001package org.hl7.fhir.convertors.misc;
002
003import java.io.File;
004import java.io.IOException;
005import java.util.ArrayList;
006import java.util.List;
007
008import org.hl7.fhir.utilities.TextFile;
009import org.hl7.fhir.utilities.Utilities;
010import org.hl7.fhir.utilities.json.model.JsonObject;
011import org.hl7.fhir.utilities.json.parser.JsonParser;
012
013/*
014  Copyright (c) 2011+, HL7, Inc.
015  All rights reserved.
016  
017  Redistribution and use in source and binary forms, with or without modification, 
018  are permitted provided that the following conditions are met:
019    
020   * Redistributions of source code must retain the above copyright notice, this 
021     list of conditions and the following disclaimer.
022   * Redistributions in binary form must reproduce the above copyright notice, 
023     this list of conditions and the following disclaimer in the documentation 
024     and/or other materials provided with the distribution.
025   * Neither the name of HL7 nor the names of its contributors may be used to 
026     endorse or promote products derived from this software without specific 
027     prior written permission.
028  
029  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
030  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
031  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
032  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
033  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
034  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
035  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
036  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
037  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
038  POSSIBILITY OF SUCH DAMAGE.
039  
040 */
041
042public class PackageMaintainer {
043
044
045  private static final String PATH = "C:\\work\\org.hl7.fhir\\packages\\core";
046
047  public static void main(String[] args) throws IOException {
048    new PackageMaintainer().check("r4");
049    new PackageMaintainer().check("r2");
050    new PackageMaintainer().check("r3");
051    new PackageMaintainer().check("r2b");
052  }
053
054  private void check(String ver) throws IOException {
055    System.out.println("Check " + ver);
056    List<String> allIds = listResources(Utilities.path(PATH, "hl7.fhir." + ver + ".examples", "package"));
057    List<String> coreIds = listResources(Utilities.path(PATH, "hl7.fhir." + ver + ".core", "package"));
058    for (String s : coreIds) {
059      if (!allIds.contains(s)) {
060        System.out.println("Core contains " + s + " but allIds doesn't");
061      }
062    }
063    for (String s : allIds) {
064      if (!coreIds.contains(s)) {
065        String c = s.substring(0, s.indexOf("-"));
066        if (Utilities.existsInList(c, "CodeSystem", "ValueSet", "ConceptMap", "StructureDefinition", "StructureMap", "NamingSystem", "SearchParameter", "OperationDefinition", "CapabilityStatement", "Conformance"))
067          System.out.println("Examples contains " + s + " but core doesn't");
068      }
069    }
070    strip(new File(Utilities.path(PATH, "hl7.fhir." + ver + ".core", "package")));
071    strip(new File(Utilities.path(PATH, "hl7.fhir." + ver + ".expansions", "package")));
072    if (!ver.equals("r2b"))
073      strip(new File(Utilities.path(PATH, "hl7.fhir." + ver + ".elements", "package")));
074  }
075
076
077  private List<String> listResources(String dir) {
078    File folder = new File(dir);
079    List<String> res = new ArrayList<>();
080    for (String fn : folder.list()) {
081      if (fn.endsWith(".json") && fn.contains("-")) {
082        String s = fn;
083        s = s.substring(0, s.indexOf("."));
084        res.add(s);
085      }
086    }
087    return res;
088  }
089
090  private void strip(File folder) throws IOException {
091    for (File f : folder.listFiles()) {
092      if (f.isDirectory())
093        strip(f);
094      else if (f.getName().endsWith(".json")) {
095        JsonObject json = JsonParser.parseObject(f);
096        if (json.has("resourceType") && json.has("text")) {
097          json.remove("text");
098          String src = JsonParser.compose(json);
099          TextFile.stringToFile(src, f.getAbsolutePath());
100        }
101      }
102    }
103  }
104
105}