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