001package org.hl7.fhir.r5.renderers;
002
003import java.io.IOException;
004import java.io.UnsupportedEncodingException;
005import java.util.ArrayList;
006import java.util.List;
007
008import org.hl7.fhir.exceptions.DefinitionException;
009import org.hl7.fhir.exceptions.FHIRException;
010import org.hl7.fhir.exceptions.FHIRFormatError;
011import org.hl7.fhir.r5.extensions.ExtensionDefinitions;
012import org.hl7.fhir.r5.model.NamingSystem;
013import org.hl7.fhir.r5.model.NamingSystem.NamingSystemUniqueIdComponent;
014import org.hl7.fhir.r5.renderers.utils.RenderingContext;
015import org.hl7.fhir.r5.renderers.utils.ResourceWrapper;
016import org.hl7.fhir.r5.terminologies.CodeSystemUtilities;
017import org.hl7.fhir.r5.utils.EOperationOutcome;
018
019import org.hl7.fhir.utilities.MarkedToMoveToAdjunctPackage;
020import org.hl7.fhir.utilities.xhtml.XhtmlNode;
021
022@MarkedToMoveToAdjunctPackage
023public class NamingSystemRenderer extends ResourceRenderer {
024
025
026  public NamingSystemRenderer(RenderingContext context) { 
027    super(context); 
028  } 
029 
030  @Override
031  public void buildNarrative(RenderingStatus status, XhtmlNode x, ResourceWrapper r) throws FHIRFormatError, DefinitionException, IOException, FHIRException, EOperationOutcome {
032    if (r.isDirect()) { 
033      renderResourceTechDetails(r, x);
034      genSummaryTable(status, x, (NamingSystem) r.getBase());
035      render(status, x, (NamingSystem) r.getBase());      
036    } else {
037      // the intention is to change this in the future
038      x.para().tx("NamingSystemRenderer only renders native resources directly");
039    }
040  }
041
042  @Override
043  public String buildSummary(ResourceWrapper r) throws UnsupportedEncodingException, IOException {
044    return canonicalTitle(r);
045  }
046
047  
048
049  public void render(RenderingStatus status, XhtmlNode x, NamingSystem ns) throws FHIRFormatError, DefinitionException, IOException {
050    x.h3().tx(context.formatPhrase(RenderingContext.GENERAL_SUMM));
051    XhtmlNode tbl = x.table("grid", false); 
052    row(tbl, (context.formatPhrase(RenderingContext.GENERAL_DEFINING_URL)), ns.getUrl());
053    if (ns.hasVersion()) {
054      row(tbl, (context.formatPhrase(RenderingContext.GENERAL_VER)), ns.getVersion());
055    }
056    if (ns.hasName()) {
057      row(tbl, (context.formatPhrase(RenderingContext.GENERAL_NAME)), gt(ns.getNameElement()));
058    }
059    if (ns.hasTitle()) {
060      row(tbl, (context.formatPhrase(RenderingContext.GENERAL_TITLE)), gt(ns.getTitleElement()));
061    }
062    row(tbl, (context.formatPhrase(RenderingContext.GENERAL_STATUS)), ns.getStatus().toCode());
063    if (ns.hasDescription()) {
064      addMarkdown(row(tbl, (context.formatPhrase(RenderingContext.GENERAL_DEFINITION))), ns.getDescription());
065    }
066    if (ns.hasPublisher()) {
067      row(tbl, (context.formatPhrase(RenderingContext.CANON_REND_PUBLISHER)), gt(ns.getPublisherElement()));
068    }
069    if (ns.hasExtension(ExtensionDefinitions.EXT_WORKGROUP)) {
070      renderCommitteeLink(row(tbl, "Committee"), ns);
071    }
072    if (CodeSystemUtilities.hasOID(ns)) {
073      row(tbl, context.formatPhrase(RenderingContext.GENERAL_OID)).tx(context.formatPhrase(RenderingContext.CODE_SYS_FOR_OID, CodeSystemUtilities.getOID(ns)));
074    }
075    if (ns.hasCopyright()) {
076      addMarkdown(row(tbl, (context.formatPhrase(RenderingContext.GENERAL_COPYRIGHT))), ns.getCopyright());
077    }
078    List<NamingSystem> nsl = new ArrayList<>();
079    nsl.add(ns);
080    renderList(x, nsl);
081  }
082  
083  public void renderList(XhtmlNode x, List<NamingSystem> nsl) {
084
085    boolean hasPreferred = false;
086    boolean hasPeriod = false;
087    boolean hasComment = false;
088    for (NamingSystem ns : nsl) {
089      for (NamingSystemUniqueIdComponent id : ns.getUniqueId()) {
090        hasPreferred = hasPreferred || id.hasPreferred();
091        hasPeriod = hasPeriod || id.hasPeriod();
092        hasComment = hasComment || id.hasComment();
093      }
094    }
095    x.h3().tx(context.formatPhrase(RenderingContext.NAME_SYS_IDEN));
096    XhtmlNode tbl = x.table("grid", false);
097    XhtmlNode tr = tbl.tr();
098    tr.td().b().tx((context.formatPhrase(RenderingContext.GENERAL_TYPE)));
099    tr.td().b().tx((context.formatPhrase(RenderingContext.GENERAL_VALUE)));
100    if (hasPreferred) {
101      tr.td().b().tx((context.formatPhrase(RenderingContext.GENERAL_PREFERRED)));
102    }
103    if (hasPeriod) {
104      tr.td().b().tx((context.formatPhrase(RenderingContext.NAME_SYS_PER)));
105    }
106    if (hasComment) {
107      tr.td().b().tx((context.formatPhrase(RenderingContext.GENERAL_COMMENT)));
108    }
109    for (NamingSystem ns : nsl) {
110      for (NamingSystemUniqueIdComponent id : ns.getUniqueId()) {
111        tr = tbl.tr();
112        tr.td().tx(id.getType().getDisplay());
113        tr.td().tx(id.getValue());
114        if (hasPreferred) {
115          tr.td().tx(id.getPreferredElement().primitiveValue());
116        }
117        if (hasPeriod) {
118          tr.td().tx(displayDataType(id.getPeriod()));
119        }
120        if (hasComment) {
121          tr.td().tx(id.getComment());
122        }
123      } 
124    }
125  }
126  
127
128  private XhtmlNode row(XhtmlNode tbl, String name) {
129    XhtmlNode tr = tbl.tr();
130    XhtmlNode td = tr.td();
131    td.tx((name));
132    return tr.td();
133  }
134  private XhtmlNode row(XhtmlNode tbl, String name, String value) {
135    XhtmlNode td = row(tbl, name);
136    td.tx(value);
137    return td;
138  }
139
140  public void describe(XhtmlNode x, NamingSystem ns) {
141    x.tx(display(ns));
142  }
143
144  public String display(NamingSystem ns) {
145    return ns.present();
146  }
147
148}