001package org.hl7.fhir.utilities.npm;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileNotFoundException;
006import java.io.FileOutputStream;
007import java.io.IOException;
008import java.nio.charset.StandardCharsets;
009import java.util.ArrayList;
010import java.util.List;
011import java.util.Map;
012
013import org.hl7.fhir.utilities.FileUtilities;
014import org.hl7.fhir.utilities.VersionUtilities;
015import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
016import org.hl7.fhir.utilities.json.model.JsonArray;
017import org.hl7.fhir.utilities.json.model.JsonObject;
018import org.hl7.fhir.utilities.json.parser.JsonParser;
019
020
021
022/**
023 * intenral use only - set the file name to edit in main(), and fill out the edit routine
024 * 
025 * @author grahame
026 *
027 */
028public class PackageHacker {
029
030  private static boolean useSecureReferences = false;
031
032  public static void main(String[] args) throws FileNotFoundException, IOException {
033//    new PackageHacker().massEdit(ManagedFileAccess.file("/Users/grahamegrieve/web/hl7.org/fhir"));
034//    new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot1/hl7.fhir.r6.core.tgz");
035//    new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot1/hl7.fhir.r6.corexml.tgz");
036//    new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot1/hl7.fhir.r6.examples.tgz");
037//    new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot1/hl7.fhir.r6.expansions.tgz");
038//    new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot1/hl7.fhir.r6.search.tgz");
039//    new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot2/hl7.fhir.r6.core.tgz");
040//    new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot2/hl7.fhir.r6.corexml.tgz");
041//    new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot2/hl7.fhir.r6.examples.tgz");
042//    new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot2/hl7.fhir.r6.expansions.tgz");
043//    new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/6.0.0-ballot2/hl7.fhir.r6.search.tgz");
044    
045//      new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/us/core/v311/package.tgz", "http://hl7.org/fhir/us/core/STU3.1.1");
046     // new PackageHacker().edit("/Users/grahamegrieve/web/hl7.org/fhir/us/core/v700/package.tgz", "http://hl7.org/fhir/us/core/STU7");
047    
048    PackageHacker self = new PackageHacker();
049//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/hl7.terminology.r4b.tgz");
050//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/1.0.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/1.0.0/hl7.terminology.r4b.tgz");
051//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/2.0.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/2.0.0/hl7.terminology.r4b.tgz");
052//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/2.1.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/2.1.0/hl7.terminology.r4b.tgz");
053//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/3.0.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/3.0.0/hl7.terminology.r4b.tgz");
054//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/3.1.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/3.1.0/hl7.terminology.r4b.tgz");
055//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/4.0.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/4.0.0/hl7.terminology.r4b.tgz");
056//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/5.0.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/5.0.0/hl7.terminology.r4b.tgz");
057//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/5.1.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/5.1.0/hl7.terminology.r4b.tgz");
058//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/5.2.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/5.2.0/hl7.terminology.r4b.tgz");
059//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/5.3.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/5.3.0/hl7.terminology.r4b.tgz");
060//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/5.4.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/5.4.0/hl7.terminology.r4b.tgz");
061//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/5.5.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/5.5.0/hl7.terminology.r4b.tgz");
062//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/6.0.0/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/6.0.0/hl7.terminology.r4b.tgz");
063//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/6.0.1/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/6.0.1/hl7.terminology.r4b.tgz");
064//    self.cloneToR4B("/Users/grahamegrieve/web/terminology.hl7.org/6.0.2/hl7.terminology.r4.tgz", "/Users/grahamegrieve/web/terminology.hl7.org/6.0.2/hl7.terminology.r4b.tgz");  
065   // self.packFolder("/Users/grahamegrieve/web/hl7.org/fhir/us/core/v610/package");
066    self.edit("/Users/grahamegrieve/web/www.hl7.org.fhir/us/womens-health-registries/0.2.0-withdrawal/package.tgz");
067  }
068
069  private void packFolder(String src) throws FileNotFoundException, IOException {
070    NpmPackage npm = NpmPackage.fromFolder(src);
071    npm.save(new FileOutputStream(src+".tgz"));
072    
073  }
074
075  private void cloneToR4B(String src, String dst) throws IOException {
076    FileInputStream fs = ManagedFileAccess.inStream(src);
077    NpmPackage pck = NpmPackage.fromPackage(fs);
078    System.out.println(nice(pck.getNpm()));
079    JsonObject json = pck.getNpm();
080    String name = json.asString("name");
081    json.remove("name");
082    json.add("name", name.replace(".r4", ".r4b"));
083    json.remove("fhirVersions");
084    json.remove("dependencies");
085    JsonArray fv = new JsonArray();
086    json.add("fhirVersions", fv);
087    fv.add("4.3.0");
088    JsonObject dep = new JsonObject();
089    json.add("dependencies", dep);
090    dep.add(VersionUtilities.packageForVersion("4.3.0"), "4.3.0");
091    pck.save(new FileOutputStream(dst));
092  }
093
094//  private void massEdit(File dir) throws IOException {
095//    System.out.println("process "+dir.getAbsolutePath());
096//    for (File f : dir.listFiles()) {
097//      if (f.isDirectory()) {
098//        massEdit(f);
099//      } else if (f.getName().equals("package.tgz")) {
100//        try {
101//          FileInputStream fs = ManagedFileAccess.inStream(f);
102//          NpmPackage pck = NpmPackage.fromPackage(fs);
103//          if ("fhir.core".equals(pck.getNpm().str("type"))) {
104//            System.out.println("!!change "+f.getAbsolutePath());
105//            pck.getNpm().remove("type");
106//            pck.getNpm().set("type", "Core");
107//            FileOutputStream fso = ManagedFileAccess.outStream(f);
108//            try {
109//              pck.save(fso);
110//            } finally {
111//              fso.close();
112//            }
113//          }
114//        } catch (Exception e) {
115//          System.out.println("!!Error: "+e.getMessage());
116//        }
117//      } else if (f.getName().startsWith("hl7.fhir.r") && f.getName().endsWith(".examples.tgz")) {
118//        try {
119//          FileInputStream fs = ManagedFileAccess.inStream(f);
120//          NpmPackage pck = NpmPackage.fromPackage(fs);
121//          if ("fhir.examples".equals(pck.getNpm().str("type"))) {
122//            System.out.println("!!change "+f.getAbsolutePath());
123//            pck.getNpm().remove("type");
124//            pck.getNpm().set("type", "Examples");
125//            FileOutputStream fso = ManagedFileAccess.outStream(f);
126//            try {
127//              pck.save(fso);
128//            } finally {
129//              fso.close();
130//            }
131//          }
132//        } catch (Exception e) {
133//          System.out.println("!!Error: "+e.getMessage());
134//        }
135//
136//      }
137//    }
138//  }
139
140  private void edit(String name) throws FileNotFoundException, IOException {
141    File f = ManagedFileAccess.file(name);
142    if (!f.exists())
143      throw new Error("Unable to find "+f.getAbsolutePath());
144
145    System.out.println("Loading Package "+f.getAbsolutePath());
146    NpmPackage pck = null;
147    FileInputStream fs = ManagedFileAccess.inStream(f);
148    try {
149      pck = NpmPackage.fromPackage(fs);
150    } finally {
151      fs.close();
152    }
153    System.out.println("Altering Package "+f.getAbsolutePath());
154    System.out.println(nice(pck.getNpm()));
155
156    if (change(pck.getNpm())) {
157
158      System.out.println("Revised Package");
159      System.out.println("=======================");
160      System.out.println(nice(pck.getNpm()));
161      System.out.println("=======================");
162      System.out.print("save? y/n: ");
163      int r = System.in.read();
164      if (r == 'y') {
165        f.renameTo(ManagedFileAccess.file(FileUtilities.changeFileExt(name, ".tgz.bak")));
166        FileOutputStream fso = ManagedFileAccess.outStream(f);
167        try {
168          pck.save(fso);
169        } finally {
170          fso.close();
171        }
172      } 
173    }
174  }
175
176  private void fixExampleContent(Map<String, byte[]> content) {
177    //    byte[] cnt = content.get("ServiceRequest-SDOHCC-ServiceRequestCompletedFoodPantryApplicationAssistExample.json");
178    //    content.put("ServiceRequest-SDOHCC-ServiceRequestCompletedFoodPantryApplicationAssist.json", cnt);
179    //    content.remove("ServiceRequest-SDOHCC-ServiceRequestCompletedFoodPantryApplicationAssistExample.json");
180  }
181
182  private void fixContent(Map<String, byte[]> content) {
183    //    fixVersionInContent(content);
184
185  }
186
187  private String nice(JsonObject json) {
188    return JsonParser.compose(json, true);
189  }
190
191  private boolean change(JsonObject npm) throws FileNotFoundException, IOException {
192    npm.remove("version");
193    npm.add("version", "0.2.0-withdrawal");
194    return true;
195//    if (npm.has("notForPublication")) {
196//      npm.remove("notForPublication");
197//      return true;
198//    }
199//    return false;
200  }
201
202  private void fixVersionInContent(Map<String, byte[]> content) {
203    for (String n : content.keySet()) {
204      if (n.endsWith(".json") || n.endsWith(".xml") || n.endsWith(".xsd")) {
205        String json = new String(content.get(n));
206        if (json.contains("4.3.0-cibuild") && !json.contains("4.3.0-snapshot1")) {
207          json = json.replace("4.3.0-cibuild", "4.3.0");
208          content.put(n, json.getBytes(StandardCharsets.UTF_8));
209        }
210      }
211    }
212
213  }
214
215  private void fixVersions(JsonObject npm) {
216    npm.remove("fhirVersions");
217    JsonArray a = new JsonArray();
218    npm.add("fhirVersions", a);
219    a.add("3.0.1");
220  }
221
222  private void setProperty(JsonObject npm, String name, String value) {
223    npm.remove("homepage");
224    npm.add("homepage", "http://hl7.org/fhir");    
225  }
226
227  private void fixNames(Map<String, byte[]> content) {
228    List<String> names = new ArrayList<>();
229    names.addAll(content.keySet());
230    for (String s : names) {
231      if (s.endsWith("json") && !s.endsWith(".json")) {
232        String n = s.substring(0, s.length()-4)+".json";
233        content.put(n, content.get(s));
234        content.remove(s);
235      }
236    }
237  }
238
239  private void addContentFrom(String folder, Map<String, byte[]> content) throws FileNotFoundException, IOException {
240    for (File f : ManagedFileAccess.file(folder).listFiles()) {
241      if (f.getName().endsWith(".json") && !f.getName().endsWith(".canonical.json")) {
242        String cnt = FileUtilities.fileToString(f);
243        if (cnt.contains("\"resourceType\"")) {
244          content.put("package/"+f.getName(), FileUtilities.fileToBytes(f));
245        }
246      }
247    }
248  }
249
250  public static String fixPackageUrl(String webref) {
251    if (webref == null) {
252      return null;
253    }
254    // workaround for past publishing problems
255    switch (webref) {
256    case "file://C:\\GitHub\\hl7.fhir.us.breast-radiology#0.2.0\\output":   return "http://hl7.org/fhir/us/breast-radiology/2020May"; 
257    case "file://C:\\GitHub\\hl7.fhir.us.bser#1.0.0\\output":                return "http://hl7.org/fhir/us/bser/STU1"; 
258    case "file://C:\\GitHub\\hl7.fhir.us.carin-bb#0.1.0\\output":            return "http://hl7.org/fhir/us/carin-bb/2020Feb"; 
259    case "file://C:\\GitHub\\hl7.fhir.us.carin-rtpbc#0.1.0\\output":         return "http://hl7.org/fhir/us/carin-rtpbc/2020Feb"; 
260    case "file://C:\\GitHub\\hl7.fhir.us.cqfmeasures#1.1.0\\output":         return "http://hl7.org/fhir/us/cqfmeasures/2020Feb"; 
261    case "file://C:\\GitHub\\hl7.fhir.us.cqfmeasures#2.0.0\\output":         return "http://hl7.org/fhir/us/cqfmeasures/STU2"; 
262    case "file://C:\\GitHub\\hl7.fhir.us.davinci-alerts#0.2.0\\output":      return "http://hl7.org/fhir/us/davinci-alerts/2020Feb"; 
263    case "file://C:\\GitHub\\hl7.fhir.us.davinci-atr#0.1.0\\output":         return "http://hl7.org/fhir/us/davinci-atr/2020Feb";  
264    case "file://C:\\GitHub\\hl7.fhir.us.davinci-deqm#1.1.0\\output":        return "http://hl7.org/fhir/us/davinci-deqm/2020Feb"; 
265    case "file://C:\\GitHub\\hl7.fhir.us.davinci-deqm#1.0.0\\output":        return "http://hl7.org/fhir/us/davinci-deqm/STU1"; 
266    case "file://C:\\GitHub\\hl7.fhir.us.dme-orders#0.1.1\\output":          return "http://hl7.org/fhir/us/dme-orders/2020May"; 
267    case "file://C:\\GitHub\\hl7.fhir.us.ecr#1.0.0\\output":                 return "http://hl7.org/fhir/us/ecr/STU1"; 
268    case "file://C:\\GitHub\\hl7.fhir.us.mcode#1.0.0\\output":               return "http://hl7.org/fhir/us/mcode/STU1"; 
269    case "file://C:\\GitHub\\hl7.fhir.us.odh#1.0.0\\output":                 return "http://hl7.org/fhir/us/odh/STU1"; 
270    case "file://C:\\GitHub\\hl7.fhir.us.qicore#4.0.0\\output":              return "http://hl7.org/fhir/us/qicore/STU4"; 
271    case "file://C:\\GitHub\\hl7.fhir.uv.ips#1.0.0\\output":                 return "http://hl7.org/fhir/uv/ips/STU1"; 
272    case "file://C:\\GitHub\\hl7.fhir.uv.mhealth-framework#0.1.0\\output":   return "http://hl7.org/fhir/uv/mhealth-framework/2020May"; 
273    case "file://C:\\GitHub\\hl7.fhir.uv.security-label-ds4p#0.1.0\\output": return "http://hl7.org/fhir/uv/security-label-ds4p/2020May"; 
274    case "file://C:\\GitHub\\hl7.fhir.uv.shorthand#0.12.0\\output":          return "http://hl7.org/fhir/uv/shorthand/2020May"; 
275    case "http://build.fhir.org/branches/R4B//":                             return "http://hl7.org/fhir/2021Mar"; 
276    }
277
278    // https://github.com/HL7/fhir-ig-publisher/issues/295
279    if (webref.contains("hl7.org/fhir/us/core/STU4.0.0")) {
280      return webref.replace("hl7.org/fhir/us/core/STU4.0.0", "hl7.org/fhir/us/core/STU4");
281    }
282
283    if (isUseSecureReferences()) {
284      return webref.replace("http://hl7.org/fhir", "https://hl7.org/fhir").replace("http://build.fhir.org", "https://build.fhir.org");
285    } else {
286      return webref;
287    }
288  }
289
290  public static boolean isUseSecureReferences() {
291    return useSecureReferences;
292  }
293
294  public static void setUseSecureReferences(boolean useSecureReferences) {
295    PackageHacker.useSecureReferences = useSecureReferences;
296  }
297
298}