001package org.hl7.fhir.utilities.npm;
002
003import java.io.File;
004import java.io.IOException;
005
006import com.fasterxml.jackson.core.JsonFactory;
007import com.fasterxml.jackson.core.JsonToken;
008import lombok.extern.slf4j.Slf4j;
009import org.hl7.fhir.exceptions.FHIRException;
010import org.hl7.fhir.utilities.FileUtilities;
011import org.hl7.fhir.utilities.Utilities;
012import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
013import org.hl7.fhir.utilities.json.model.JsonArray;
014import org.hl7.fhir.utilities.json.model.JsonObject;
015import org.hl7.fhir.utilities.json.parser.JsonParser;
016
017/**
018 * This class builds the .index.json for a package 
019 * <br/>
020 * it also builds a .index.db since that may provide faster access
021 * 
022 * @author grahame
023 *
024 */
025@Slf4j
026public class NpmPackageIndexBuilder {
027
028  public interface INpmPackageIndexBuilderDBImpl {
029
030    void start(String filename);
031    void seeFile(String name, JsonObject fi) throws Exception;
032    void close();
033  }
034
035  public interface INpmPackageIndexBuilderDBImplFactory {
036    INpmPackageIndexBuilderDBImpl create();
037  }
038
039  private static INpmPackageIndexBuilderDBImplFactory extensionFactory;
040
041  public static final Integer CURRENT_INDEX_VERSION = 2;
042  private JsonObject index;
043  private JsonArray files;
044  private String dbFilename;
045  private static INpmPackageIndexBuilderDBImpl extension;
046
047  public void start(String filename) {
048    index = new JsonObject();
049    index.add("index-version", CURRENT_INDEX_VERSION);
050    files = new JsonArray();
051    index.add("files", files);
052    if (filename != null) {
053      dbFilename = filename;
054      try {
055        if (extensionFactory != null) {
056          extension = extensionFactory.create();
057          extension.start(filename);
058        }
059      } catch (Exception e) {
060        // nothing?
061      }
062    }
063  }
064
065  public boolean seeFile(String name, byte[] content) {
066    if (name.endsWith(".json")) {
067      /* We are only interested in some string fields on the first level of the JSON file.
068       * We can then use a streaming parser to get the values of these fields instead of parsing the whole file and
069       * allocating memory for everything in it.
070       */
071      try (final var parser = new JsonFactory().createParser(content)) {
072        final var fi = new JsonObject();
073        int level = 0;
074
075        while (parser.nextToken() != null) {
076          if (parser.currentToken() == JsonToken.START_OBJECT || parser.currentToken() == JsonToken.START_ARRAY) {
077            level++;
078          } else if (parser.currentToken() == JsonToken.END_OBJECT || parser.currentToken() == JsonToken.END_ARRAY) {
079            level--;
080          }
081          if (level != 1) {
082            continue;
083          }
084
085          String fieldName = parser.currentName();
086
087          if ("resourceType".equals(fieldName)) {
088            parser.nextToken();
089            files.add(fi);
090            fi.add("filename", name);
091            fi.add(fieldName, parser.getText());
092          }
093
094          if ("id".equals(fieldName)
095              || "url".equals(fieldName)
096              || "version".equals(fieldName)
097              || "kind".equals(fieldName)
098              || "type".equals(fieldName)
099              || "supplements".equals(fieldName)
100              || "content".equals(fieldName)
101              || "valueSet".equals(fieldName)
102              || "derivation".equals(fieldName)) {
103            parser.nextToken();
104            fi.add(fieldName, parser.getText());
105          }
106        }
107
108        if (!fi.has("resourceType")) {
109          // We haven't seen the 'resourceType' key, it's not a FHIR resource
110          return true;
111        }
112
113        if (extension != null) {
114          extension.seeFile(name, fi);
115        }
116      } catch (Exception e) {
117        if (name.contains("openapi")) {
118          return false;
119        }
120      }
121    }
122    return true;
123  }
124
125  public String build() {
126    if (extension != null) {
127      extension.close();
128    }
129    String res = JsonParser.compose(index, true);
130    index = null;
131    files = null;
132    return res;
133  }
134  
135//  private Map<String, List<String>> types = new HashMap<>();
136//  private Map<String, String> canonicalMap = new HashMap<>();
137
138
139  public void executeWithStatus(String folder) throws IOException {
140    log.info("Indexing Package "+folder+" ... ");
141    execute(folder);
142    log.info("Indexing Package "+folder+ " done");
143  }
144  
145  public void execute(String folder) throws IOException {
146    if (existsFolder(folder, "package")) {
147      folder = Utilities.path(folder, "package"); 
148    }
149    if (!existsFile(folder, "package.json")) {
150      throw new FHIRException("Not a proper package? (can't find package.json)");
151    }
152    start(Utilities.path(folder, ".index.db"));
153    File dir = ManagedFileAccess.file(folder);
154    for (File f : dir.listFiles()) {
155      seeFile(f.getName(), FileUtilities.fileToBytes(f));
156    }
157    FileUtilities.stringToFile(build(), Utilities.path(folder, ".index.json"));
158  }
159
160  private boolean existsFolder(String... args) throws IOException {
161    File f = ManagedFileAccess.file(Utilities.path(args));
162    return f.exists() && f.isDirectory();
163  }
164
165  private boolean existsFile(String... args) throws IOException {
166    File f = ManagedFileAccess.file(Utilities.path(args));
167    return f.exists() && !f.isDirectory();
168  }
169
170  public static void main(String[] args) throws IOException {
171    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r4.core");
172    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r4.examples");
173    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r4.expansions");
174    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r4.elements");
175    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r3.core");
176    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r3.examples");
177    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r3.expansions");
178    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r3.elements");
179    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2b.core");
180    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2b.examples");
181    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2b.expansions");
182    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2.core");
183    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2.examples");
184    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2.expansions");
185    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2.elements");
186    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.test.data\\fhir.test.data.r2");
187    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.test.data\\fhir.test.data.r3");
188    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.test.data\\fhir.test.data.r4");
189    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.tx.support\\fhir.tx.support.r2");
190    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.tx.support\\fhir.tx.support.r3");
191    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.tx.support\\fhir.tx.support.r4");
192    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.core#1.0.2");
193    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.core#1.4.0");
194    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.core#3.0.2");
195    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.core#4.0.1");
196  }
197
198  public String getDbFilename() {
199    return dbFilename;
200  }
201
202  public static INpmPackageIndexBuilderDBImplFactory getExtensionFactory() {
203    return extensionFactory;
204  }
205
206  public static void setExtensionFactory(INpmPackageIndexBuilderDBImplFactory extensionFactory) {
207    NpmPackageIndexBuilder.extensionFactory = extensionFactory;
208  }
209
210}