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