001package org.hl7.fhir.r5.renderers;
002
003import java.io.IOException;
004import java.io.UnsupportedEncodingException;
005
006import org.hl7.fhir.exceptions.DefinitionException;
007import org.hl7.fhir.exceptions.FHIRException;
008import org.hl7.fhir.exceptions.FHIRFormatError;
009import org.hl7.fhir.r5.model.CompartmentDefinition;
010import org.hl7.fhir.r5.model.CompartmentDefinition.CompartmentDefinitionResourceComponent;
011import org.hl7.fhir.r5.model.StringType;
012import org.hl7.fhir.r5.renderers.utils.RenderingContext;
013import org.hl7.fhir.r5.renderers.utils.ResourceWrapper;
014import org.hl7.fhir.r5.utils.EOperationOutcome;
015import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
016import org.hl7.fhir.utilities.xhtml.XhtmlNode;
017import org.hl7.fhir.utilities.xhtml.XhtmlParser;
018
019public class CompartmentDefinitionRenderer extends ResourceRenderer {
020
021  public CompartmentDefinitionRenderer(RenderingContext context) { 
022    super(context); 
023  } 
024 
025  @Override
026  public void buildNarrative(RenderingStatus status, XhtmlNode x, ResourceWrapper r) throws FHIRFormatError, DefinitionException, IOException, FHIRException, EOperationOutcome {
027    if (r.isDirect()) {
028      renderResourceTechDetails(r, x);
029      genSummaryTable(status, x, (CompartmentDefinition) r.getBase());
030      render(status, x, (CompartmentDefinition) r.getBase());      
031    } else {
032      throw new Error("CompartmentDefinitionRenderer only renders native resources directly");
033    }
034  }
035
036  @Override
037  public String buildSummary(ResourceWrapper r) throws UnsupportedEncodingException, IOException {
038    return canonicalTitle(r);
039  }
040
041  public void render(RenderingStatus status, XhtmlNode x, CompartmentDefinition cpd) throws FHIRFormatError, DefinitionException, IOException {
042    StringBuilder in = new StringBuilder();
043    StringBuilder out = new StringBuilder();
044    for (CompartmentDefinitionResourceComponent cc: cpd.getResource()) {
045      CommaSeparatedStringBuilder rules = new CommaSeparatedStringBuilder();
046      if (!cc.hasParam()) {
047        out.append(" <li><a href=\"").append(cc.getCode().toLowerCase()).append(".html\">").append(cc.getCode()).append("</a></li>\r\n");
048      } else if (!rules.equals("{def}")) {
049        for (StringType p : cc.getParam())
050          rules.append(p.asStringValue());
051        in.append(" <tr><td><a href=\"").append(cc.getCode().toLowerCase()).append(".html\">").append(cc.getCode()).append("</a></td><td>").append(rules.toString()).append("</td></tr>\r\n");
052      }
053    }
054    XhtmlNode xn;
055    xn = new XhtmlParser().parseFragment("<div><p>\r\nThe following resources may be in this compartment:\r\n</p>\r\n" +
056        "<table class=\"grid\">\r\n"+
057        " <tr><td><b>Resource</b></td><td><b>Inclusion Criteria</b></td></tr>\r\n"+
058        in.toString()+
059        "</table>\r\n"+
060        "<p>\r\nA resource is in this compartment if the nominated search parameter (or chain) refers to the patient resource that defines the compartment.\r\n</p>\r\n" +
061        "<p>\r\n\r\n</p>\r\n" +
062        "<p>\r\nThe following resources are never in this compartment:\r\n</p>\r\n" +
063        "<ul>\r\n"+
064        out.toString()+
065        "</ul></div>\r\n");
066    x.getChildNodes().addAll(xn.getChildNodes());
067  }
068
069  public void describe(XhtmlNode x, CompartmentDefinition cd) {
070    x.tx(display(cd));
071  }
072
073  public String display(CompartmentDefinition cd) {
074    return cd.present();
075  }
076
077}