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.NamingSystem;
009import org.hl7.fhir.r5.model.NamingSystem.NamingSystemUniqueIdComponent;
010import org.hl7.fhir.r5.model.Resource;
011import org.hl7.fhir.r5.renderers.utils.BaseWrappers.ResourceWrapper;
012import org.hl7.fhir.r5.renderers.utils.RenderingContext;
013import org.hl7.fhir.r5.renderers.utils.Resolver.ResourceContext;
014import org.hl7.fhir.r5.terminologies.CodeSystemUtilities;
015import org.hl7.fhir.r5.utils.ToolingExtensions;
016import org.hl7.fhir.utilities.xhtml.XhtmlNode;
017
018public class NamingSystemRenderer extends ResourceRenderer {
019
020  public NamingSystemRenderer(RenderingContext context) {
021    super(context);
022  }
023
024  public NamingSystemRenderer(RenderingContext context, ResourceContext rcontext) {
025    super(context, rcontext);
026  }
027  
028  public boolean render(XhtmlNode x, Resource dr) throws FHIRFormatError, DefinitionException, IOException {
029    return render(x, (NamingSystem) dr);
030  }
031
032  public boolean render(XhtmlNode x, NamingSystem ns) throws FHIRFormatError, DefinitionException, IOException {
033    x.h3().tx("Summary");
034    XhtmlNode tbl = x.table("grid"); 
035    row(tbl, "Defining URL", ns.getUrl());
036    if (ns.hasVersion()) {
037      row(tbl, "Version", ns.getVersion());
038    }
039    if (ns.hasName()) {
040      row(tbl, "Name", gt(ns.getNameElement()));
041    }
042    if (ns.hasTitle()) {
043      row(tbl, "Title", gt(ns.getTitleElement()));
044    }
045    row(tbl, "Status", ns.getStatus().toCode());
046    if (ns.hasDescription()) {
047      addMarkdown(row(tbl, "Definition"), ns.getDescription());
048    }
049    if (ns.hasPublisher()) {
050      row(tbl, "Publisher", gt(ns.getPublisherElement()));
051    }
052    if (ns.hasExtension(ToolingExtensions.EXT_WORKGROUP)) {
053      renderCommitteeLink(row(tbl, "Committee"), ns);
054    }
055    if (CodeSystemUtilities.hasOID(ns)) {
056      row(tbl, "OID", CodeSystemUtilities.getOID(ns)).tx("("+translate("ns.summary", "for OID based terminology systems")+")");
057    }
058    if (ns.hasCopyright()) {
059      addMarkdown(row(tbl, "Copyright"), ns.getCopyright());
060    }
061    boolean hasPreferred = false;
062    boolean hasPeriod = false;
063    boolean hasComment = false;
064    for (NamingSystemUniqueIdComponent id : ns.getUniqueId()) {
065      hasPreferred = hasPreferred || id.hasPreferred();
066      hasPeriod = hasPeriod || id.hasPeriod();
067      hasComment = hasComment || id.hasComment();
068    }
069    x.h3().tx("Identifiers");
070    tbl = x.table("grid");
071    XhtmlNode tr = tbl.tr();
072    tr.td().b().tx(translate("ns.summary", "Type"));
073    tr.td().b().tx(translate("ns.summary", "Value"));
074    if (hasPreferred) {
075      tr.td().b().tx(translate("ns.summary", "Preferred"));
076    }
077    if (hasPeriod) {
078      tr.td().b().tx(translate("ns.summary", "Period"));
079    }
080    if (hasComment) {
081      tr.td().b().tx(translate("ns.summary", "Comment"));
082    }
083    for (NamingSystemUniqueIdComponent id : ns.getUniqueId()) {
084      tr = tbl.tr();
085      tr.td().tx(id.getType().getDisplay());
086      tr.td().tx(id.getValue());
087      if (hasPreferred) {
088        tr.td().tx(id.getPreferredElement().primitiveValue());
089      }
090      if (hasPeriod) {
091        tr.td().tx(display(id.getPeriod()));
092      }
093      if (hasComment) {
094        tr.td().tx(id.getComment());
095      }
096    }    
097    return false;
098  }
099
100  private XhtmlNode row(XhtmlNode tbl, String name) {
101    XhtmlNode tr = tbl.tr();
102    XhtmlNode td = tr.td();
103    td.tx(translate("ns.summary", name));
104    return tr.td();
105  }
106  private XhtmlNode row(XhtmlNode tbl, String name, String value) {
107    XhtmlNode td = row(tbl, name);
108    td.tx(value);
109    return td;
110  }
111
112  public void describe(XhtmlNode x, NamingSystem ns) {
113    x.tx(display(ns));
114  }
115
116  public String display(NamingSystem ns) {
117    return ns.present();
118  }
119
120  @Override
121  public String display(Resource r) throws UnsupportedEncodingException, IOException {
122    return ((NamingSystem) r).present();
123  }
124
125  public String display(ResourceWrapper r) throws UnsupportedEncodingException, IOException {
126    if (r.has("title")) {
127      return r.children("title").get(0).getBase().primitiveValue();
128    }
129    if (r.has("name")) {
130      return r.children("name").get(0).getBase().primitiveValue();
131    }
132    return "??";
133  }
134
135}