001package org.hl7.fhir.r5.renderers;
002
003import java.io.IOException;
004import java.io.UnsupportedEncodingException;
005import java.util.List;
006
007import org.hl7.fhir.exceptions.DefinitionException;
008import org.hl7.fhir.exceptions.FHIRException;
009import org.hl7.fhir.exceptions.FHIRFormatError;
010import org.hl7.fhir.r5.renderers.utils.RenderingContext;
011import org.hl7.fhir.r5.renderers.utils.ResourceWrapper;
012import org.hl7.fhir.r5.utils.EOperationOutcome;
013import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
014import org.hl7.fhir.utilities.xhtml.XhtmlNode;
015
016public class ParametersRenderer extends ResourceRenderer {
017
018  public ParametersRenderer(RenderingContext context) { 
019    super(context); 
020  } 
021 
022  
023  @Override
024  public String buildSummary(ResourceWrapper r) throws UnsupportedEncodingException, IOException {
025    List<ResourceWrapper> params = r.children("parameter");
026    if (params.size() < 8) {
027      CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
028      for (ResourceWrapper p : params) {
029        b.append(p.primitiveValue("name"));
030      }
031      return context.formatMessage(RenderingContext.PARS_SUMMARY_LIST, b.toString());
032    } else {
033      return context.formatMessage(RenderingContext.PARS_SUMMARY_SIZE, params.size());
034    }
035  }
036
037  public ParametersRenderer setMultiLangMode(boolean multiLangMode) {
038    this.multiLangMode = multiLangMode;
039    return this;
040  }
041
042  @Override
043  public void buildNarrative(RenderingStatus status, XhtmlNode x, ResourceWrapper r) throws FHIRFormatError, DefinitionException, IOException, FHIRException, EOperationOutcome {
044    renderResourceTechDetails(r, x);
045    x.h2().tx(context.formatPhrase(RenderingContext.GENERAL_PARS));
046    XhtmlNode tbl = x.table("grid");
047    params(status, tbl, r.children("parameter"), 0);
048  }
049
050  private void params(RenderingStatus status, XhtmlNode tbl, List<ResourceWrapper> list, int indent) throws FHIRFormatError, DefinitionException, FHIRException, IOException, EOperationOutcome {
051    for (ResourceWrapper p : list) {
052      XhtmlNode tr = tbl.tr();
053      XhtmlNode td = tr.td();
054      for (int i = 0; i < indent; i++) {
055        td.tx(XhtmlNode.NBSP);        
056      }
057      if (p.has("name")) {
058        td.tx(p.primitiveValue("name"));
059      } else {
060        td.tx("???");
061      }
062      if (p.has("value")) {
063        renderDataType(status, tr.td(), p.child("value"));
064      } else if (p.has("resource")) {
065        ResourceWrapper rw = p.child("resource");
066        td = tr.td();
067        XhtmlNode para = td.para();
068        para.tx(rw.fhirType()+"/"+rw.getId());
069        checkAddLink(para, rw.fhirType()+"_"+rw.getId());
070        checkAddLink(para, rw.fhirType()+"_"+rw.getScopedId());
071        checkAddLink(para, "hc"+rw.fhirType()+"_"+rw.getId());
072        checkAddLink(para, "hc"+rw.fhirType()+"_"+rw.getScopedId());
073        if (rw.has("meta")) {
074          ResourceWrapper m = rw.child("meta");
075          String ver = m.primitiveValue("version");
076          if (ver != null) {
077            checkAddLink(para, "hc"+rw.fhirType()+"_"+rw.getId()+"/"+ver);
078            checkAddLink(para, "hc"+rw.fhirType()+"_"+rw.getScopedId()+"/"+ver);            
079          }
080        }
081        XhtmlNode x = rw.getNarrative();
082        if (x != null) {
083          td.addChildren(x);
084        } else {
085          ResourceRenderer rr = RendererFactory.factory(rw, context);
086          rr.buildNarrative(status, td, rw);
087        }
088      } else if (p.has("part")) {
089        tr.td();
090        params(status, tbl, p.children("part"), indent+1);
091      }
092    }
093  }
094
095
096  private void checkAddLink(XhtmlNode para, String anchor) {
097    if (!context.hasAnchor(anchor)) {
098      context.addAnchor(anchor);
099      para.an(context.prefixAnchor(anchor)).tx(" ");
100    }
101
102    
103  }
104
105}