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