From 4f048776bac90f7a3a5dac2387e6d84e552a0f2a Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Wed, 4 Dec 2019 11:10:52 +1100 Subject: [PATCH] add more log statements --- .../fhir/r5/context/SimpleWorkerContext.java | 1 + .../fhir/utilities/cache/PackageHacker.java | 94 -------- .../fhir/utilities/cache/PackageScanner.java | 219 ------------------ 3 files changed, 1 insertion(+), 313 deletions(-) delete mode 100644 org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageHacker.java delete mode 100644 org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageScanner.java diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java index 60afba291..72f38d7c2 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java @@ -286,6 +286,7 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon } public void loadFromPackage(NpmPackage pi, IContextResourceLoader loader, String... types) throws FileNotFoundException, IOException, FHIRException { + System.out.println("Load Package "+pi.name()+"#"+pi.version()); if (types.length == 0) types = new String[] { "StructureDefinition", "ValueSet", "CodeSystem", "SearchParameter", "OperationDefinition", "Questionnaire","ConceptMap","StructureMap", "NamingSystem"}; for (String s : pi.listResources(types)) { diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageHacker.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageHacker.java deleted file mode 100644 index 7dcf5849b..000000000 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageHacker.java +++ /dev/null @@ -1,94 +0,0 @@ -package org.hl7.fhir.utilities.cache; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.hl7.fhir.utilities.TextFile; -import org.hl7.fhir.utilities.Utilities; - -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; - -/** - * intenral use only - set the file name to edit in main(), and fill out the edit routine - * - * @author grahame - * - */ -public class PackageHacker { - - public static void main(String[] args) throws FileNotFoundException, IOException { - new PackageHacker().edit("C:\\web\\hl7.org\\fhir\\uv\\pocd\\2018Jan\\package.tgz"); - } - - private void edit(String name) throws FileNotFoundException, IOException { - File f = new File(name); - if (!f.exists()) - throw new Error("Unable to find "+f.getAbsolutePath()); - - NpmPackage pck = NpmPackage.fromPackage(new FileInputStream(f)); - System.out.println("Altering Package "+f.getAbsolutePath()); - System.out.println(nice(pck.getNpm())); - - change(pck.getNpm(), pck.getContent()); - - System.out.println("Revised Package"); - System.out.println("======================="); - System.out.println(nice(pck.getNpm())); - System.out.println("======================="); - System.out.print("save? y/n: "); - int r = System.in.read(); - if (r == 'y') { - f.renameTo(new File(Utilities.changeFileExt(name, ".tgz.bak"))); - pck.save(new FileOutputStream(f)); - } - - } - - private String nice(JsonObject json) { - return new GsonBuilder().setPrettyPrinting().create().toJson(json); - } - - private void change(JsonObject npm, Map content) throws FileNotFoundException, IOException { -// addContentFrom("C:\\web\\hl7.org\\fhir\\us\\meds", content); - fixNames(content); -// setProperty(npm, "homepage", "http://hl7.org/fhir"); - - } - - private void setProperty(JsonObject npm, String name, String value) { - npm.remove("homepage"); - npm.addProperty("homepage", "http://hl7.org/fhir"); - } - - private void fixNames(Map content) { - List names = new ArrayList<>(); - names.addAll(content.keySet()); - for (String s : names) { - if (s.endsWith("json") && !s.endsWith(".json")) { - String n = s.substring(0, s.length()-4)+".json"; - content.put(n, content.get(s)); - content.remove(s); - } - } - } - - private void addContentFrom(String folder, Map content) throws FileNotFoundException, IOException { - for (File f : new File(folder).listFiles()) { - if (f.getName().endsWith(".json") && !f.getName().endsWith(".canonical.json")) { - String cnt = TextFile.fileToString(f); - if (cnt.contains("\"resourceType\"")) { - content.put("package/"+f.getName(), TextFile.fileToBytes(f)); - } - } - } - } - -} diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageScanner.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageScanner.java deleted file mode 100644 index 8a8e96745..000000000 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/PackageScanner.java +++ /dev/null @@ -1,219 +0,0 @@ -package org.hl7.fhir.utilities.cache; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map.Entry; - -import org.hl7.fhir.utilities.CSVWriter; -import org.hl7.fhir.utilities.TextFile; -import org.hl7.fhir.utilities.Utilities; -import org.hl7.fhir.utilities.cache.PackageScanner.ResourceInfo; -import org.hl7.fhir.utilities.json.JsonTrackingParser; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - -public class PackageScanner { - public class ResourceInfo { - - private String filenameRoot; - private String htmlName; - private String id; - - public ResourceInfo(String filename, String id) { - this.filenameRoot = Utilities.changeFileExt(filename, ""); - this.id = id; - } - - } - - public static void main(String[] args) throws FileNotFoundException, IOException { - new PackageScanner().scan(new File("C:\\web\\hl7.org\\fhir"), "c:\\temp\\package-scan.csv", "http://hl7.org/fhir"); - } - - private void scan(File folder, String dest, String url) throws IOException { - CSVWriter csv = new CSVWriter(new FileOutputStream(dest)); - csv.line("path", "error", "name", "version", "fhir-version", "fhir-version-explicit", "canonical", "homepage", - "url", "type", "#types", "#urls", "dependencies", "spec.internals"); - scanFolders(folder, csv, folder.getAbsolutePath(), url); - csv.close(); - System.out.println("done"); - } - - private void scanFolders(File folder, CSVWriter csv, String root, String url) { - System.out.println(folder.getAbsolutePath()); - for (File file : folder.listFiles()) { - if (file.isDirectory()) { - scanFolders(file, csv, root, url); - } else if (file.getName().endsWith(".tgz")) { - processPackage(file, csv, root, url); - } - } - } - - private void processPackage(File file, CSVWriter csv, String root, String url) { - try { - NpmPackage npm = NpmPackage.fromPackage(new FileInputStream(file)); - fix(npm, file, root, url); - csv.line(file.getAbsolutePath(), "", npm.name(), npm.version(), npm.fhirVersion(), npm.fhirVersionList(), npm.canonical(), npm.homepage(), - npm.url(), npm.type(), Integer.toString(npm.getTypes().size()), Integer.toString(npm.getUrls().size()), npm.dependencySummary(), Boolean.toString(npm.hasFile("other", "spec.internals"))); - } catch (Exception e) { - try { - csv.line(file.getAbsolutePath(), e.getMessage() == null ? "NPE" : e.getMessage()); - } catch (IOException e1) { - e1.printStackTrace(); - } - } - } - - private void fix(NpmPackage npm, File file, String root, String url) throws FileNotFoundException, IOException { - String u = Utilities.pathURL(url, Utilities.getDirectoryForFile(file.getAbsolutePath()).substring(root.length()).replace("\\", "/")); - if (Utilities.getDirectoryForFile(file.getAbsolutePath()).equals("C:\\web\\hl7.org\\fhir")) { - u = "http://hl7.org/fhir/R4"; - } - boolean save = false; - List names = new ArrayList<>(); - names.addAll(npm.getContent().keySet()); - for (String s : names) { - if (!s.startsWith("package/")) { - save = true; - String n = "package/"+s; - npm.getContent().put(n, npm.getContent().get(s)); - npm.getContent().remove(s); - } - } - - if (!npm.getNpm().has("fhirVersions")) { - JsonArray fv = new JsonArray(); - fv.add(npm.fhirVersion()); - npm.getNpm().add("fhirVersions", fv); - save = true; - } - if (npm.getNpm().has("dependencies")) { - JsonObject dep = npm.getNpm().getAsJsonObject("dependencies"); - for (Entry e : dep.entrySet()) { - if ("current".equals(e.getValue().getAsString())) { - save = true; - if ("hl7.fhir.us.core.r4".equals(e.getKey())) { - dep.remove("hl7.fhir.us.core.r4"); - dep.addProperty("hl7.fhir.us.core", "3.0.1"); - } else if ("hl7.fhir.us.davinci-pas".equals(e.getKey())) { - dep.remove("hl7.fhir.us.davinci-pas"); - dep.addProperty("hl7.fhir.us.davinci-pas", "0.1.0"); - } else if ("hl7.fhir.uv.sdc".equals(e.getKey())) { - dep.remove("hl7.fhir.uv.sdc"); - if ("0.1.0".equals(npm.version())) - dep.addProperty("hl7.fhir.uv.sdc", "2.5.0"); - else - dep.addProperty("hl7.fhir.uv.sdc", "2.7.0"); - } else if ("hl7.fhir.us.core".equals(e.getKey())) { - dep.remove("hl7.fhir.us.core"); - dep.addProperty("hl7.fhir.us.core", "3.0.1"); - } else if ("hl7.fhir.us.ccda.r4".equals(e.getKey())) { - dep.remove("hl7.fhir.us.ccda.r4"); - dep.addProperty("hl7.fhir.us.ccda", "1.0.0"); - } - } - } - } - if (!npm.hasFile("other", "spec.internals") && !file.getName().startsWith("hl7.fhir.r")) { - save = true; - npm.getContent().put("package/other/spec.internals", buildSpecInternals(npm.canonical(), Utilities.getDirectoryForFile(file.getAbsolutePath()))); - } -// if (npm.url() == null || !npm.url().equals(u)) { -// npm.getNpm().remove("url"); -// npm.getNpm().addProperty("url", u); -// save = true; -// } -// if (!npm.canonical().startsWith("http://hl7.org")) { -// npm.getNpm().remove("canonical"); -// npm.getNpm().addProperty("canonical", "http://hl7.org/fhir/smart-app-launch"); -// save = true; -// } -// - if (save) { - File bck = new File(Utilities.changeFileExt(file.getAbsolutePath(), ".bcknpm")); - if (bck.exists()) { - bck.delete(); - } - file.renameTo(bck); - npm.save(new FileOutputStream(file)); - } - } - - private byte[] buildSpecInternals(String canonical, String dir) throws FileNotFoundException, IOException { - // the spec internals file contains 2 important things: a map from canonical URL to html path - // and a list of html target files - - List rl = makeResList(dir); - List targets = new ArrayList<>(); - for (File f : new File(dir).listFiles()) { - if (f.getName().endsWith(".html")) { - targets.add(f.getName()); - String src = TextFile.fileToString(f); - for (ResourceInfo r : rl) { - if (r.htmlName == null) { - if (src.contains(r.filenameRoot+".json")) { - r.htmlName = f.getName(); - } - } - } - } - } - StringBuilder b = new StringBuilder(); - b.append("{\r\n"); - b.append(" \"paths\": {"); - boolean first = true; - for (ResourceInfo r : rl) { - String link = r.htmlName; - if (link == null) { - System.out.println("No link found for "+r.id); - } else { - if (first) first = false; else b.append(","); - b.append("\r\n \""); - b.append(Utilities.pathURL(canonical, r.id)); - b.append("\": \""); - if (link.contains(".json.html") && new File(Utilities.path(dir,link. replace(".json", ""))).exists()) { - link = link.replace(".json", ""); - } - b.append(link); - b.append("\""); - } - } - b.append("\r\n },\r\n"); - b.append(" \"targets\": ["); - first = true; - for (String s : targets) { - if (first) first = false; else b.append(","); - b.append("\r\n \""); - b.append(s); - b.append("\""); - } - b.append(" ]\r\n"); - b.append("}\r\n"); - return TextFile.stringToBytes(b.toString(), false); - } - - private List makeResList(String dir) { - List rl = new ArrayList<>(); - for (File f : new File(dir).listFiles()) { - if (f.getName().endsWith(".json") && !f.getName().endsWith(".canonical.json")) { - try { - JsonObject obj = JsonTrackingParser.parseJson(f); - if (obj.has("resourceType") && obj.has("id")) { - rl.add(new ResourceInfo(f.getName(), obj.get("resourceType").getAsString() +"/"+ obj.get("id").getAsString())); - } - } catch (IOException e) { - } - } - } - return rl; - } - -}