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.Extension;
010import org.hl7.fhir.r5.model.ExtensionHelper;
011import org.hl7.fhir.r5.model.OperationOutcome;
012import org.hl7.fhir.r5.model.OperationOutcome.IssueSeverity;
013import org.hl7.fhir.r5.model.OperationOutcome.OperationOutcomeIssueComponent;
014import org.hl7.fhir.r5.model.Resource;
015import org.hl7.fhir.r5.model.StringType;
016import org.hl7.fhir.r5.renderers.utils.RenderingContext;
017import org.hl7.fhir.r5.renderers.utils.BaseWrappers.ResourceWrapper;
018import org.hl7.fhir.r5.renderers.utils.Resolver.ResourceContext;
019import org.hl7.fhir.r5.utils.ToolingExtensions;
020import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
021import org.hl7.fhir.utilities.xhtml.XhtmlNode;
022
023public class OperationOutcomeRenderer extends ResourceRenderer {
024
025  public OperationOutcomeRenderer(RenderingContext context) {
026    super(context);
027  }
028
029  public OperationOutcomeRenderer(RenderingContext context, ResourceContext rcontext) {
030    super(context, rcontext);
031  }
032  
033  public boolean render(XhtmlNode x, Resource dr) throws FHIRFormatError, DefinitionException, IOException {
034    return render(x, (OperationOutcome) dr);
035  }
036
037  public boolean render(XhtmlNode x, OperationOutcome op) throws FHIRFormatError, DefinitionException, IOException {
038    boolean hasSource = false;
039    boolean success = true;
040    for (OperationOutcomeIssueComponent i : op.getIssue()) {
041      success = success && i.getSeverity() == IssueSeverity.INFORMATION;
042      hasSource = hasSource || ExtensionHelper.hasExtension(i, ToolingExtensions.EXT_ISSUE_SOURCE);
043    }
044    if (success)
045      x.para().tx("All OK");
046    if (op.getIssue().size() > 0) {
047      XhtmlNode tbl = x.table("grid"); // on the basis that we'll most likely be rendered using the standard fhir css, but it doesn't really matter
048      XhtmlNode tr = tbl.tr();
049      tr.td().b().tx("Severity");
050      tr.td().b().tx("Location");
051      tr.td().b().tx("Code");
052      tr.td().b().tx("Details");
053      tr.td().b().tx("Diagnostics");
054      if (hasSource)
055        tr.td().b().tx("Source");
056      for (OperationOutcomeIssueComponent i : op.getIssue()) {
057        tr = tbl.tr();
058        tr.td().addText(i.getSeverity().toString());
059        XhtmlNode td = tr.td();
060        boolean d = false;
061        for (StringType s : i.getLocation()) {
062          if (d)
063            td.tx(", ");
064          else
065            d = true;
066          td.addText(s.getValue());
067        }
068        tr.td().addText(i.getCode().getDisplay());
069        tr.td().addText(display(i.getDetails()));
070        smartAddText(tr.td(), i.getDiagnostics());
071        if (hasSource) {
072          Extension ext = ExtensionHelper.getExtension(i, ToolingExtensions.EXT_ISSUE_SOURCE);
073          tr.td().addText(ext == null ? "" : display(ext));
074        }
075      }
076    }
077    return true;
078
079  }
080
081  public void describe(XhtmlNode x, OperationOutcome oo) {
082    x.tx(display(oo));
083  }
084
085  public String display(OperationOutcome oo) {
086    return "todo";
087  }
088
089  @Override
090  public String display(ResourceWrapper r) throws UnsupportedEncodingException, IOException {
091    return "Not done yet";
092  }
093
094  @Override
095  public String display(Resource r) throws UnsupportedEncodingException, IOException {
096    return display((OperationOutcome) r);
097  }
098
099  public static String toString(OperationOutcome oo) {
100    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
101    for (OperationOutcomeIssueComponent issue : oo.getIssue()) {
102      b.append(issue.getSeverity().toCode()+": "+issue.getDetails().getText());
103    }
104    return b.toString();
105  }
106}