001package org.hl7.fhir.r5.context;
002
003import java.io.File;
004import java.sql.Connection;
005import java.sql.DriverManager;
006import java.sql.PreparedStatement;
007import java.sql.SQLException;
008import java.sql.Statement;
009import java.sql.Types;
010import java.util.HashSet;
011import java.util.Set;
012
013import org.hl7.fhir.utilities.json.model.JsonObject;
014import org.hl7.fhir.utilities.json.parser.JsonParser;
015
016public class OidIndexBuilder {
017
018  private File folder;
019  private File target;
020
021  public OidIndexBuilder(File ff, File of) {
022    super();
023    this.folder = ff;
024    this.target = of;
025  }
026
027  public void build() {
028    System.out.println("Generate OID index for "+folder.getAbsolutePath());
029    target.delete();
030    try {
031      Set<String> matches = new HashSet<String>();
032      
033      Connection db = DriverManager.getConnection("jdbc:sqlite:"+target.getAbsolutePath());
034      Statement stmt = db.createStatement();
035      stmt.execute("CREATE TABLE OIDMap (\r\n"+
036          "OID            nvarchar NOT NULL,\r\n"+
037          "TYPE           nvarchar NOT NULL,\r\n"+
038          "URL            nvarchar NOT NULL,\r\n"+
039          "VERSION        nvarchar NOT NULL,\r\n"+
040          "Status         nvarchar NOT NULL,\r\n"+
041          "PRIMARY KEY (OID, URL))\r\n");
042
043      PreparedStatement psql = db.prepareStatement("Insert into OIDMap (OID, TYPE, URL, VERSION, Status) values (?, ?, ?, ?, ?)");
044      for (File f : folder.listFiles()) {
045        if (!f.getName().startsWith(".") && f.getName().endsWith(".json")) {
046          try {
047            JsonObject json = JsonParser.parseObject(f);
048            processFile(psql, matches, json);
049          } catch (Exception e) {
050            System.out.println("Error processing "+f.getAbsolutePath()+" while generating OIDs: "+e.getMessage());
051          }
052        }
053      }
054      db.close();
055    } catch (Exception e) {
056      System.out.println("Error processing "+folder.getAbsolutePath()+" while generating OIDs: "+e.getMessage());
057//      e.printStackTrace();
058    }
059  }  
060
061  private void processFile(PreparedStatement psql, Set<String> matches, JsonObject json) throws SQLException {
062    String rt = json.asString("resourceType");
063    if (rt != null) {
064      Set<String> oids = new HashSet<String>();
065      String url = null;
066      String status = json.asString("status");
067      String version = json.asString("version");
068      if ("NamingSystem".equals(rt)) {        
069        for (JsonObject id : json.getJsonObjects("uniqueId")) {
070          String t = id.asString("type");
071          String v = id.asString("value");
072          if ("url".equals(t) && v != null) {
073            url = v;
074          } else if ("oid".equals(t) && v != null) {
075            oids.add(v);
076          }
077        }
078        if (url != null) {
079          for (String s : oids) {
080            addOid(psql, matches, s, rt, url, version, status);
081          }
082        }
083      } else {            
084        if (json.hasPrimitive("url")) { 
085          url = json.asString("url");
086          if (json.has("oid")) {
087            oids.add(json.asString("oid"));
088          }
089          if (json.has("url")) {
090            String v = json.asString("url");
091            if (v != null && v.startsWith("urn:oid:")) {
092              oids.add(v.substring(8));
093            }
094          }
095
096          for (JsonObject id : json.getJsonObjects("identifier")) {
097            String v = id.asString("value");
098            if (v != null && v.startsWith("urn:oid:")) {
099              oids.add(v.substring(8));
100            }
101          }
102          if (!oids.isEmpty()) {
103            for (String s : oids) {
104              addOid(psql, matches, s, rt, url, version, status);
105            }
106          }
107        }
108      }
109    }
110  }
111
112  private void addOid(PreparedStatement psql, Set<String> matches, String oid, String type, String url, String version, String status) throws SQLException {
113    String key = oid+"@"+url;
114    if (!matches.contains(key)) {
115      matches.add(key);
116      psql.setString(1, oid);
117      psql.setString(2, type);
118      psql.setString(3, url);
119      if (version == null) {
120        psql.setNull(4, Types.NVARCHAR);
121      } else {
122        psql.setString(4, version);  
123      }
124      psql.setString(5, status);
125      psql.execute();
126    }
127
128  }
129
130}