001package org.hl7.fhir.convertors.misc;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileOutputStream;
006import java.io.IOException;
007
008import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat;
009import org.hl7.fhir.utilities.IniFile;
010import org.hl7.fhir.utilities.VersionUtilities;
011import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
012
013public class OIDAssigner {
014
015
016  public static void main(String[] args) throws Exception {
017    new OIDAssigner().execute(args[0], args[1], args[2]);
018  }
019
020  private void execute(String oidSource, String folder, String version) throws IOException {
021   IniFile oids = new IniFile(oidSource);
022   File f = ManagedFileAccess.file(folder);
023   process(oids, f, version);
024  }
025
026  private void process(IniFile oids, File folder, String version) {
027    for (File f : folder.listFiles()) {
028      if (f.isDirectory()) {
029        process(oids, f, version);
030      } else if (f.getName().endsWith(".xml")) {
031        processFile(oids, f, version, FhirFormat.XML);
032      } else if (f.getName().endsWith(".json")) {
033        processFile(oids, f, version, FhirFormat.JSON);
034      }
035    }    
036  }
037
038  private void processFile(IniFile oids, File f, String version, FhirFormat fmt) {
039    switch (VersionUtilities.getMajMin(version)) {
040    case "1.0" : processFileR2(oids, f, fmt);
041    case "3.0" : processFileR3(oids, f, fmt);
042    case "4.0" : processFileR4(oids, f, fmt);
043    case "4.3" : processFileR4B(oids, f, fmt);
044    case "5.0" : processFileR5(oids, f, fmt);
045    }    
046  }
047
048  private void processFileR2(IniFile oids, File f, FhirFormat fmt) {
049    org.hl7.fhir.dstu2.formats.IParser parser = fmt == FhirFormat.JSON ? new  org.hl7.fhir.dstu2.formats.JsonParser() : new org.hl7.fhir.dstu2.formats.XmlParser();
050    try {
051      boolean save = false;
052      org.hl7.fhir.dstu2.model.Resource r = parser.parse(ManagedFileAccess.inStream(f));
053      if (r instanceof org.hl7.fhir.dstu2.model.ValueSet) { 
054        org.hl7.fhir.dstu2.model.ValueSet vs = (org.hl7.fhir.dstu2.model.ValueSet) r;
055        boolean hasOid = isOid(vs.getIdentifier());
056        if (!hasOid) {
057          String oid = getOid(oids, "ValueSet", vs.getUrl());
058          vs.setIdentifier(new org.hl7.fhir.dstu2.model.Identifier().setSystem("urn:ietf:rfc:3986").setValue("urn:oid:"+oid));
059          save = true;
060        }
061      }
062      if (r instanceof org.hl7.fhir.dstu2.model.ConceptMap) { 
063        org.hl7.fhir.dstu2.model.ConceptMap cm = (org.hl7.fhir.dstu2.model.ConceptMap) r;
064        boolean hasOid = isOid(cm.getIdentifier());
065        if (!hasOid) {
066          String oid = getOid(oids, "ConceptMap", cm.getUrl());
067          cm.setIdentifier(new org.hl7.fhir.dstu2.model.Identifier().setSystem("urn:ietf:rfc:3986").setValue("urn:oid:"+oid));
068          save = true;
069        }
070      }
071      if (r instanceof org.hl7.fhir.dstu2.model.StructureDefinition) { 
072        org.hl7.fhir.dstu2.model.StructureDefinition sd = (org.hl7.fhir.dstu2.model.StructureDefinition) r;
073        boolean hasOid = false;
074        for (org.hl7.fhir.dstu2.model.Identifier id : sd.getIdentifier()) {
075          if (isOid(id)) {
076            hasOid = true;
077          }
078        }
079        if (!hasOid) {
080          String oid = getOid(oids, "StructureDefinition", sd.getUrl());
081          sd.getIdentifier().add(new org.hl7.fhir.dstu2.model.Identifier().setSystem("urn:ietf:rfc:3986").setValue("urn:oid:"+oid));
082          save = true;
083        }
084      }
085      if (save) {
086        parser.setOutputStyle(org.hl7.fhir.dstu2.formats.IParser.OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(f), r);
087      }
088    } catch (Exception e) {
089      System.out.println("Erro processing "+f.getAbsolutePath()+": "+e.getMessage());
090    }    
091  }
092
093
094  private void processFileR3(IniFile oids, File f, FhirFormat fmt) {
095    org.hl7.fhir.dstu3.formats.IParser parser = fmt == FhirFormat.JSON ? new  org.hl7.fhir.dstu3.formats.JsonParser() : new org.hl7.fhir.dstu3.formats.XmlParser();
096    try {
097      boolean save = false;
098      org.hl7.fhir.dstu3.model.Resource r = parser.parse(ManagedFileAccess.inStream(f));
099      if (r instanceof org.hl7.fhir.dstu3.model.CodeSystem) { 
100        org.hl7.fhir.dstu3.model.CodeSystem cs = (org.hl7.fhir.dstu3.model.CodeSystem) r;
101        boolean hasOid = isOid(cs.getIdentifier());
102        if (!hasOid) {
103          String oid = getOid(oids, "CodeSystem", cs.getUrl());
104          cs.setIdentifier(new org.hl7.fhir.dstu3.model.Identifier().setSystem("urn:ietf:rfc:3986").setValue("urn:oid:"+oid));
105          save = true;
106        }
107      }
108      if (r instanceof org.hl7.fhir.dstu3.model.ValueSet) { 
109        org.hl7.fhir.dstu3.model.ValueSet vs = (org.hl7.fhir.dstu3.model.ValueSet) r;
110        boolean hasOid = false;
111        for (org.hl7.fhir.dstu3.model.Identifier id : vs.getIdentifier()) {
112          if (isOid(id)) {
113            hasOid = true;
114          }
115        }
116        if (!hasOid) {
117          String oid = getOid(oids, "ValueSet", vs.getUrl());
118          vs.getIdentifier().add(new org.hl7.fhir.dstu3.model.Identifier().setSystem("urn:ietf:rfc:3986").setValue("urn:oid:"+oid));
119          save = true;
120        }
121      }
122      if (r instanceof org.hl7.fhir.dstu3.model.ConceptMap) { 
123        org.hl7.fhir.dstu3.model.ConceptMap cm = (org.hl7.fhir.dstu3.model.ConceptMap) r;
124        boolean hasOid = isOid(cm.getIdentifier());
125        if (!hasOid) {
126          String oid = getOid(oids, "ConceptMap", cm.getUrl());
127          cm.setIdentifier(new org.hl7.fhir.dstu3.model.Identifier().setSystem("urn:ietf:rfc:3986").setValue("urn:oid:"+oid));
128          save = true;
129        }
130      }
131      if (r instanceof org.hl7.fhir.dstu3.model.StructureDefinition) { 
132        org.hl7.fhir.dstu3.model.StructureDefinition sd = (org.hl7.fhir.dstu3.model.StructureDefinition) r;
133        boolean hasOid = false;
134        for (org.hl7.fhir.dstu3.model.Identifier id : sd.getIdentifier()) {
135          if (isOid(id)) {
136            hasOid = true;
137          }
138        }
139        if (!hasOid) {
140          String oid = getOid(oids, "StructureDefinition", sd.getUrl());
141          sd.getIdentifier().add(new org.hl7.fhir.dstu3.model.Identifier().setSystem("urn:ietf:rfc:3986").setValue("urn:oid:"+oid));
142          save = true;
143        }
144      }
145      if (save) {
146        parser.setOutputStyle(org.hl7.fhir.dstu3.formats.IParser.OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(f), r);
147      }
148    } catch (Exception e) {
149      System.out.println("Erro processing "+f.getAbsolutePath()+": "+e.getMessage());
150    }    
151  }
152
153
154  private void processFileR4(IniFile oids, File f, FhirFormat fmt) {
155    org.hl7.fhir.r4.formats.IParser parser = fmt == FhirFormat.JSON ? new  org.hl7.fhir.r4.formats.JsonParser() : new org.hl7.fhir.r4.formats.XmlParser();
156    try {
157      boolean save = false;
158      org.hl7.fhir.r4.model.Resource r = parser.parse(ManagedFileAccess.inStream(f));
159      if (r instanceof org.hl7.fhir.r4.model.CodeSystem) { 
160        org.hl7.fhir.r4.model.CodeSystem cs = (org.hl7.fhir.r4.model.CodeSystem) r;
161        boolean hasOid = false;
162        for (org.hl7.fhir.r4.model.Identifier id : cs.getIdentifier()) {
163          if (isOid(id)) {
164            hasOid = true;
165          }
166        }
167        if (!hasOid) {
168          String oid = getOid(oids, "CodeSystem", cs.getUrl());
169          cs.getIdentifier().add(new org.hl7.fhir.r4.model.Identifier().setSystem("urn:ietf:rfc:3986").setValue("urn:oid:"+oid));
170          save = true;
171        }
172      }
173      if (r instanceof org.hl7.fhir.r4.model.ValueSet) { 
174        org.hl7.fhir.r4.model.ValueSet vs = (org.hl7.fhir.r4.model.ValueSet) r;
175        boolean hasOid = false;
176        for (org.hl7.fhir.r4.model.Identifier id : vs.getIdentifier()) {
177          if (isOid(id)) {
178            hasOid = true;
179          }
180        }
181        if (!hasOid) {
182          String oid = getOid(oids, "ValueSet", vs.getUrl());
183          vs.getIdentifier().add(new org.hl7.fhir.r4.model.Identifier().setSystem("urn:ietf:rfc:3986").setValue("urn:oid:"+oid));
184          save = true;
185        }
186      }
187      if (r instanceof org.hl7.fhir.r4.model.ConceptMap) { 
188        org.hl7.fhir.r4.model.ConceptMap cm = (org.hl7.fhir.r4.model.ConceptMap) r;
189        boolean hasOid = isOid(cm.getIdentifier());
190        if (!hasOid) {
191          String oid = getOid(oids, "ConceptMap", cm.getUrl());
192          cm.setIdentifier(new org.hl7.fhir.r4.model.Identifier().setSystem("urn:ietf:rfc:3986").setValue("urn:oid:"+oid));
193          save = true;
194        }
195      }
196      if (r instanceof org.hl7.fhir.r4.model.StructureDefinition) { 
197        org.hl7.fhir.r4.model.StructureDefinition sd = (org.hl7.fhir.r4.model.StructureDefinition) r;
198        boolean hasOid = false;
199        for (org.hl7.fhir.r4.model.Identifier id : sd.getIdentifier()) {
200          if (isOid(id)) {
201            hasOid = true;
202          }
203        }
204        if (!hasOid) {
205          String oid = getOid(oids, "StructureDefinition", sd.getUrl());
206          sd.getIdentifier().add(new org.hl7.fhir.r4.model.Identifier().setSystem("urn:ietf:rfc:3986").setValue("urn:oid:"+oid));
207          save = true;
208        }
209      }
210      if (save) {
211        parser.setOutputStyle(org.hl7.fhir.r4.formats.IParser.OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(f), r);
212      }
213    } catch (Exception e) {
214      System.out.println("Erro processing "+f.getAbsolutePath()+": "+e.getMessage());
215    }    
216  }
217
218  private void processFileR4B(IniFile oids, File f, FhirFormat fmt) {
219    org.hl7.fhir.r4b.formats.IParser parser = fmt == FhirFormat.JSON ? new  org.hl7.fhir.r4b.formats.JsonParser() : new org.hl7.fhir.r4b.formats.XmlParser();
220    try {
221      boolean save = false;
222      org.hl7.fhir.r4b.model.Resource r = parser.parse(ManagedFileAccess.inStream(f));
223      if (r instanceof org.hl7.fhir.r4b.model.CanonicalResource) { 
224        org.hl7.fhir.r4b.model.CanonicalResource cs = (org.hl7.fhir.r4b.model.CanonicalResource) r;
225        boolean hasOid = false;
226        for (org.hl7.fhir.r4b.model.Identifier id : cs.getIdentifier()) {
227          if (isOid(id)) {
228            hasOid = true;
229          }
230        }
231        if (!hasOid) {
232          String oid = getOid(oids, r.fhirType(), cs.getUrl());
233          cs.getIdentifier().add(new org.hl7.fhir.r4b.model.Identifier().setSystem("urn:ietf:rfc:3986").setValue("urn:oid:"+oid));
234          save = true;
235        }
236      }
237      if (save) {
238        parser.setOutputStyle(org.hl7.fhir.r4b.formats.IParser.OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(f), r);
239      }
240    } catch (Exception e) {
241      System.out.println("Erro processing "+f.getAbsolutePath()+": "+e.getMessage());
242    }    
243  }
244
245
246  private void processFileR5(IniFile oids, File f, FhirFormat fmt) {
247    org.hl7.fhir.r5.formats.IParser parser = fmt == FhirFormat.JSON ? new  org.hl7.fhir.r5.formats.JsonParser() : new org.hl7.fhir.r5.formats.XmlParser();
248    try {
249      boolean save = false;
250      org.hl7.fhir.r5.model.Resource r = parser.parse(ManagedFileAccess.inStream(f));
251      if (r instanceof org.hl7.fhir.r5.model.CanonicalResource) { 
252        org.hl7.fhir.r5.model.CanonicalResource cs = (org.hl7.fhir.r5.model.CanonicalResource) r;
253        boolean hasOid = false;
254        for (org.hl7.fhir.r5.model.Identifier id : cs.getIdentifier()) {
255          if (isOid(id)) {
256            hasOid = true;
257          }
258        }
259        if (!hasOid) {
260          String oid = getOid(oids, r.fhirType(), cs.getUrl());
261          cs.getIdentifier().add(new org.hl7.fhir.r5.model.Identifier().setSystem("urn:ietf:rfc:3986").setValue("urn:oid:"+oid));
262          save = true;
263        }
264      }
265      if (save) {
266        parser.setOutputStyle(org.hl7.fhir.r5.formats.IParser.OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(f), r);
267      }
268    } catch (Exception e) {
269      System.out.println("Erro processing "+f.getAbsolutePath()+": "+e.getMessage());
270    }    
271  }
272 
273  private boolean isOid(org.hl7.fhir.dstu2.model.Identifier id) {
274    return "urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:");
275  }
276
277  private boolean isOid(org.hl7.fhir.dstu3.model.Identifier id) {
278    return "urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:");
279  }
280
281  private boolean isOid(org.hl7.fhir.r4.model.Identifier id) {
282    return "urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:");
283  }
284
285  private boolean isOid(org.hl7.fhir.r4b.model.Identifier id) {
286    return "urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:");
287  }
288
289  private boolean isOid(org.hl7.fhir.r5.model.Identifier id) {
290    return "urn:ietf:rfc:3986".equals(id.getSystem()) && id.hasValue() && id.getValue().startsWith("urn:oid:");
291  }
292
293  private String getOid(IniFile oids, String rt, String url) {
294    String root = oids.getStringProperty("Roots", rt);
295    if (root == null) {
296      throw new Error("no OID.ini entry for "+rt);
297    }
298    String oid = oids.getStringProperty(rt, url);
299    if (oid != null) {
300      return oid;
301    }
302    int key = oids.getIntegerProperty("Key", rt);
303    key++;
304    oid = root+"."+key;
305    oids.setIntegerProperty("Key", rt, key, null);
306    oids.setStringProperty(rt, url, oid, null);
307    oids.save();
308    return oid;
309  }
310}