001package org.hl7.fhir.utilities.npm;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.util.ArrayList;
006import java.util.HashSet;
007import java.util.List;
008import java.util.Set;
009
010import org.hl7.fhir.utilities.FileUtilities;
011import org.hl7.fhir.utilities.Utilities;
012import org.hl7.fhir.utilities.json.model.JsonObject;
013import org.hl7.fhir.utilities.json.parser.JsonParser;
014
015@SuppressWarnings("checkstyle:systemout")
016public class PackageScanner {
017
018  public static void main(String[] args) throws IOException {
019    List<String> output = new ArrayList<>();
020    Set<String> packages = new HashSet<>();
021
022    processServer(PackageServer.primaryServer(), output, packages);
023    processServer(PackageServer.secondaryServer(), output, packages);
024    
025    StringBuilder b = new StringBuilder();
026    for (String s : output) {
027      b.append(s);
028      b.append("\r\n");
029      System.out.println(s);
030    }
031    FileUtilities.stringToFile(b.toString(), Utilities.path("[tmp]", "packages.csv"));
032  }
033
034  public static void processServer(PackageServer server, List<String> output, Set<String> packages) throws IOException {
035    System.out.println("Server: "+server);
036    PackageClient client = new PackageClient(server);
037    List<PackageInfo> list = client.search(null, null, null, false, null);
038    output.add("id\tversion\tcanonica\tfhir version\tfhir-versions\tkind\ttype\tsource");
039    for (PackageInfo pi : list) {
040      System.out.print("  fetch: "+pi.getId());
041      List<PackageInfo> versions = null;
042      while (versions == null) {
043        System.out.print("-");
044        try {
045          versions = client.getVersions(pi.getId());
046        } catch (Exception e) {
047          // nothing
048        }
049      }
050      for (PackageInfo piv : versions) {
051        if (!packages.contains(pi.getId()+"#"+piv.getVersion())) {
052          packages.add(pi.getId()+"#"+piv.getVersion());
053          try {
054            System.out.print(".");
055            InputStream cnt = client.fetch(pi.getId(), piv.getVersion());
056            NpmPackage pck = NpmPackage.fromPackage(cnt);
057            JsonObject json = pck.getNpm();
058            String fv;
059            try {
060              fv = pck.fhirVersion();
061            } catch (Exception e) {
062              fv = "--";
063            }
064            output.add(pck.name()+"\t"+pck.version()+"\t"+pck.canonical()+"\t"+fv+'\t'+pck.fhirVersionList()+'\t'+json.asString("kind")+'\t'+json.asString("type")+'\t'+JsonParser.compose(json));          } catch (Exception e) {
065            System.out.println("Error acessing "+pi.getId()+"#"+piv.getVersion()+": "+e.getMessage());
066            e.printStackTrace();
067          }
068        }
069      }
070    }
071  }
072
073}