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      // it seems very inlikely this will change
033      x.para().tx("CompartmentDefinitionRenderer only renders native resources directly");
034    }
035  }
036
037  @Override
038  public String buildSummary(ResourceWrapper r) throws UnsupportedEncodingException, IOException {
039    return canonicalTitle(r);
040  }
041
042  public void render(RenderingStatus status, XhtmlNode x, CompartmentDefinition cpd) throws FHIRFormatError, DefinitionException, IOException {
043    StringBuilder in = new StringBuilder();
044    StringBuilder out = new StringBuilder();
045    for (CompartmentDefinitionResourceComponent cc: cpd.getResource()) {
046      CommaSeparatedStringBuilder rules = new CommaSeparatedStringBuilder();
047      if (!cc.hasParam()) {
048        out.append(" <li><a href=\"").append(cc.getCode().toLowerCase()).append(".html\">").append(cc.getCode()).append("</a></li>\r\n");
049      } else if (!rules.equals("{def}")) {
050        for (StringType p : cc.getParam())
051          rules.append(p.asStringValue());
052        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");
053      }
054    }
055    XhtmlNode xn;
056    xn = new XhtmlParser().parseFragment("<div><p>\r\nThe following resources may be in this compartment:\r\n</p>\r\n" +
057        "<table class=\"grid\">\r\n"+
058        " <tr><td><b>Resource</b></td><td><b>Inclusion Criteria</b></td></tr>\r\n"+
059        in.toString()+
060        "</table>\r\n"+
061        "<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" +
062        "<p>\r\n\r\n</p>\r\n" +
063        "<p>\r\nThe following resources are never in this compartment:\r\n</p>\r\n" +
064        "<ul>\r\n"+
065        out.toString()+
066        "</ul></div>\r\n");
067    x.addChildNodes(xn.getChildNodes());
068  }
069
070  public void describe(XhtmlNode x, CompartmentDefinition cd) {
071    x.tx(display(cd));
072  }
073
074  public String display(CompartmentDefinition cd) {
075    return cd.present();
076  }
077
078}