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.FHIRFormatError;
008import org.hl7.fhir.r5.model.DomainResource;
009import org.hl7.fhir.r5.model.Resource;
010import org.hl7.fhir.r5.model.StringType;
011import org.hl7.fhir.r5.model.CompartmentDefinition;
012import org.hl7.fhir.r5.model.CompartmentDefinition.CompartmentDefinitionResourceComponent;
013import org.hl7.fhir.r5.renderers.utils.RenderingContext;
014import org.hl7.fhir.r5.renderers.utils.BaseWrappers.ResourceWrapper;
015import org.hl7.fhir.r5.renderers.utils.Resolver.ResourceContext;
016import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
017import org.hl7.fhir.utilities.xhtml.XhtmlNode;
018import org.hl7.fhir.utilities.xhtml.XhtmlParser;
019
020public class CompartmentDefinitionRenderer extends ResourceRenderer {
021
022  public CompartmentDefinitionRenderer(RenderingContext context) {
023    super(context);
024  }
025
026  public CompartmentDefinitionRenderer(RenderingContext context, ResourceContext rcontext) {
027    super(context, rcontext);
028  }
029  
030  public boolean render(XhtmlNode x, Resource dr) throws FHIRFormatError, DefinitionException, IOException {
031    return render(x, (CompartmentDefinition) dr);
032  }
033
034  public boolean render(XhtmlNode x, CompartmentDefinition cpd) throws FHIRFormatError, DefinitionException, IOException {
035    StringBuilder in = new StringBuilder();
036    StringBuilder out = new StringBuilder();
037    for (CompartmentDefinitionResourceComponent cc: cpd.getResource()) {
038      CommaSeparatedStringBuilder rules = new CommaSeparatedStringBuilder();
039      if (!cc.hasParam()) {
040        out.append(" <li><a href=\"").append(cc.getCode().toLowerCase()).append(".html\">").append(cc.getCode()).append("</a></li>\r\n");
041      } else if (!rules.equals("{def}")) {
042        for (StringType p : cc.getParam())
043          rules.append(p.asStringValue());
044        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");
045      }
046    }
047    XhtmlNode xn;
048    xn = new XhtmlParser().parseFragment("<div><p>\r\nThe following resources may be in this compartment:\r\n</p>\r\n" +
049        "<table class=\"grid\">\r\n"+
050        " <tr><td><b>Resource</b></td><td><b>Inclusion Criteria</b></td></tr>\r\n"+
051        in.toString()+
052        "</table>\r\n"+
053        "<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" +
054        "<p>\r\n\r\n</p>\r\n" +
055        "<p>\r\nThe following resources are never in this compartment:\r\n</p>\r\n" +
056        "<ul>\r\n"+
057        out.toString()+
058        "</ul></div>\r\n");
059    x.getChildNodes().addAll(xn.getChildNodes());
060    return true;
061  }
062
063  public void describe(XhtmlNode x, CompartmentDefinition cd) {
064    x.tx(display(cd));
065  }
066
067  public String display(CompartmentDefinition cd) {
068    return cd.present();
069  }
070
071  @Override
072  public String display(Resource r) throws UnsupportedEncodingException, IOException {
073    return ((CompartmentDefinition) r).present();
074  }
075
076  public String display(ResourceWrapper r) throws UnsupportedEncodingException, IOException {
077    if (r.has("title")) {
078      return r.children("title").get(0).getBase().primitiveValue();
079    }
080    if (r.has("name")) {
081      return r.children("name").get(0).getBase().primitiveValue();
082    }
083    return "??";
084  }
085
086}