001package org.hl7.fhir.r5.ips;
002
003import java.io.IOException;
004import java.util.HashMap;
005import java.util.List;
006import java.util.Map;
007
008import org.hl7.fhir.r5.ips.IPSRenderer.InternalTemplateEngine;
009import org.hl7.fhir.r5.model.Bundle;
010import org.hl7.fhir.r5.model.Composition;
011import org.hl7.fhir.r5.model.Composition.SectionComponent;
012import org.hl7.fhir.r5.model.DomainResource;
013import org.hl7.fhir.r5.model.Reference;
014import org.hl7.fhir.r5.terminologies.client.ITerminologyClient;
015import org.hl7.fhir.utilities.xhtml.NodeType;
016import org.hl7.fhir.utilities.xhtml.XhtmlComposer;
017import org.hl7.fhir.utilities.xhtml.XhtmlNode;
018
019public class IPSRenderer {
020
021  public class InternalTemplateEngine implements ITemplateImplementer {
022
023    @Override
024    public String buildPage(Map<String, String> headers, String content) {
025      // TODO Auto-generated method stub
026      return null;
027    }
028
029  }
030
031  private interface ITemplateImplementer {
032    public String buildPage(Map<String, String> headers, String content);
033  }
034  private ITerminologyClient tx;
035  private String folder; // for images etc
036  private Map<String, byte[]> binaries; // from the pubpack
037  private ITemplateImplementer templater;
038  private Map<String, String> headers;
039
040  public IPSRenderer(ITerminologyClient tx, String folder, Map<String, byte[]> binaries, ITemplateImplementer templater) {
041    super();
042    this.tx = tx;
043    this.folder = folder;
044    this.binaries = binaries;
045    this.templater = templater;
046  }
047  
048  public IPSRenderer(ITerminologyClient tx, String folder, Map<String, byte[]> binaries) {
049    super();
050    this.tx = tx;
051    this.folder = folder;
052    this.binaries = binaries;
053    this.templater = new InternalTemplateEngine();
054  }
055
056  public String render(Bundle document) throws IOException {
057    headers = new HashMap<>();
058    XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
059    generate(x, document);
060    return templater.buildPage(headers, new XhtmlComposer(false, true).compose(x));
061  }
062
063  private void generate(XhtmlNode x, Bundle document) {
064    Composition cmp = (Composition) document.getEntryFirstRep().getResource();
065    int sectionDepth = findSectionDepth(cmp.getSection());
066    XhtmlNode table = x.table("grid");
067    
068    // row 1: header
069    XhtmlNode tr = table.tr();
070    XhtmlNode td = tr.td().colspan(1+sectionDepth);
071    td.b().tx("Provided");
072    td = tr.td().colspan(1+sectionDepth);
073    td.b().tx("Generated");
074    
075    // row 2: Subject
076    DomainResource subject = findResource(document, cmp.getSubjectFirstRep());
077    tr = table.tr();
078    td = tr.td().colspan(1+sectionDepth);
079    // genNarrative("subject", subject, td);
080    td = tr.td().colspan(1+sectionDepth);
081    td.b().tx("Generated");
082    
083    
084    
085    
086  }
087
088  private DomainResource findResource(Bundle document, Reference subject) {
089    // TODO Auto-generated method stub
090    return null;
091  }
092
093  private int findSectionDepth(List<SectionComponent> list) {
094    int i = 1;
095    for (SectionComponent sect : list) {
096      i = Integer.max(i, 1+findSectionDepth(sect.getSection()));
097    }
098    return i;
099  }
100}