001package org.hl7.fhir.convertors.misc.utg;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileNotFoundException;
006import java.io.FileOutputStream;
007import java.io.IOException;
008import java.util.Calendar;
009import java.util.Date;
010
011import org.hl7.fhir.r4.formats.IParser;
012import org.hl7.fhir.r4.formats.IParser.OutputStyle;
013import org.hl7.fhir.r4.formats.JsonParser;
014import org.hl7.fhir.r4.formats.XmlParser;
015import org.hl7.fhir.r4.model.Bundle;
016import org.hl7.fhir.r4.model.Bundle.BundleType;
017import org.hl7.fhir.r4.model.CodeSystem;
018import org.hl7.fhir.r4.model.CodeSystem.CodeSystemContentMode;
019import org.hl7.fhir.r4.model.Period;
020import org.hl7.fhir.r4.model.Provenance;
021import org.hl7.fhir.r4.model.Provenance.ProvenanceAgentComponent;
022import org.hl7.fhir.r4.model.Resource;
023import org.hl7.fhir.utilities.Utilities;
024import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
025
026@SuppressWarnings("checkstyle:systemout")
027public class UTGCaseSensitivePopulator {
028
029  public static void main(String[] args) throws FileNotFoundException, IOException {
030    new UTGCaseSensitivePopulator().process(ManagedFileAccess.file(args[0]));
031
032  }
033
034  private void process(File root) throws FileNotFoundException, IOException {
035    Bundle bundle = new Bundle();
036    bundle.setType(BundleType.COLLECTION);
037    bundle.setId("hxutg1-1-0-12");
038    
039    scanFolders(root, bundle);
040    new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(Utilities.path(root.getAbsolutePath(), "input", "sourceOfTruth", "history", "utgrel1hx-1-0-12.json")), bundle);
041    System.out.println("Done");
042  }
043
044  private void scanFolders(File folder, Bundle bundle) throws FileNotFoundException, IOException {
045    Calendar today = Calendar.getInstance();
046    Date dt = today.getTime();
047    today.set(Calendar.HOUR_OF_DAY, 0);
048    Date d = today.getTime();
049    System.out.println("Scan "+folder.getAbsolutePath());
050        
051    for (File f : folder.listFiles()) {
052      if (f.isDirectory() && !f.getName().equals("retired")) {
053        scanFolders(f, bundle);
054      } else if (f.getName().endsWith(".xml")) {
055        processFile(f, bundle, new XmlParser(), d, dt);
056      } else if (f.getName().endsWith(".json")) {
057        processFile(f, bundle, new JsonParser(), d, dt);
058      }
059    }
060    
061  }
062
063  private void processFile(File f, Bundle bundle, IParser parser, Date d, Date dt) {
064    try {
065      Resource r = parser.parse(ManagedFileAccess.inStream(f));
066      if (r instanceof CodeSystem) {
067        if (processCodeSystem((CodeSystem) r, bundle, d, dt)) {
068          parser.setOutputStyle(OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(f), r);
069        }
070      }
071    } catch (Exception e) {
072    }
073    
074  }
075
076  private boolean processCodeSystem(CodeSystem cs, Bundle bundle, Date d, Date dt) {
077    if (cs.hasCaseSensitive()) {
078      return false;
079    }
080    if (!cs.getUrl().startsWith("http://terminology.hl7.org") || cs.getContent() == CodeSystemContentMode.NOTPRESENT) {
081      return false;
082    }
083    cs.setCaseSensitive(true);
084    Provenance p = new Provenance();
085    p.setId("cs-286-"+cs.getId());
086    p.addTarget().setReference("CodeSystem/"+cs.getId());
087    p.setOccurred(new Period());
088    p.getOccurredPeriod().setEnd(d);
089    p.setRecorded(dt);
090    p.addReason().setText("Populate Missing caseSensitive property;UP-286").addCoding().setSystem("http://terminology.hl7.org/CodeSystem/v3-ActReason").setCode("METAMGT");
091    p.getActivity().addCoding().setSystem("http://terminology.hl7.org/CodeSystem/v3-DataOperation").setCode("UPDATE");
092    ProvenanceAgentComponent agent = p.addAgent();
093    agent.getType().addCoding().setSystem("http://terminology.hl7.org/CodeSystem/provenance-participant-type").setCode("author");
094    agent.getWho().setDisplay("Grahame Grieve");
095    agent = p.addAgent();
096    agent.getType().addCoding().setSystem("http://terminology.hl7.org/CodeSystem/provenance-participant-type").setCode("custodian");
097    agent.getWho().setDisplay("Vocabulary WG");
098    
099    bundle.addEntry().setFullUrl("http://terminology.hl7.org/fhir/Provenance/cs-286-"+cs.getId()).setResource(p);
100    
101    return true;
102  }
103
104}