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