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.FHIRException;
008import org.hl7.fhir.exceptions.FHIRFormatError;
009import org.hl7.fhir.r5.model.CanonicalType;
010import org.hl7.fhir.r5.model.Enumeration;
011import org.hl7.fhir.r5.model.Enumerations.FHIRTypes;
012import org.hl7.fhir.r5.model.Enumerations.VersionIndependentResourceTypesAll;
013import org.hl7.fhir.r5.model.Extension;
014import org.hl7.fhir.r5.model.OperationDefinition;
015import org.hl7.fhir.r5.model.OperationDefinition.OperationDefinitionParameterComponent;
016import org.hl7.fhir.r5.model.OperationDefinition.OperationParameterScope;
017import org.hl7.fhir.r5.model.Resource;
018import org.hl7.fhir.r5.model.StructureDefinition;
019import org.hl7.fhir.r5.renderers.utils.RenderingContext;
020import org.hl7.fhir.r5.renderers.utils.RenderingContext.KnownLinkType;
021import org.hl7.fhir.r5.renderers.utils.ResourceWrapper;
022import org.hl7.fhir.r5.utils.EOperationOutcome;
023import org.hl7.fhir.r5.utils.ToolingExtensions;
024import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
025import org.hl7.fhir.utilities.StandardsStatus;
026import org.hl7.fhir.utilities.Utilities;
027import org.hl7.fhir.utilities.xhtml.XhtmlNode; 
028 
029public class OperationDefinitionRenderer extends TerminologyRenderer { 
030 
031
032  public OperationDefinitionRenderer(RenderingContext context) { 
033    super(context); 
034  } 
035 
036  @Override
037  public void buildNarrative(RenderingStatus status, XhtmlNode x, ResourceWrapper r) throws FHIRFormatError, DefinitionException, IOException, FHIRException, EOperationOutcome {
038    if (r.isDirect()) {
039      renderResourceTechDetails(r, x);  
040      genSummaryTable(status, x, (OperationDefinition) r.getBase());    
041      render(status, x, (OperationDefinition) r.getBase());      
042    } else {
043      throw new Error("OperationDefinitionRenderer only renders native resources directly");
044    }
045  }
046   
047  @Override
048  public String buildSummary(ResourceWrapper r) throws UnsupportedEncodingException, IOException {
049    return canonicalTitle(r);
050  }
051
052  public void render(RenderingStatus status, XhtmlNode x, OperationDefinition opd) throws IOException, FHIRException, EOperationOutcome { 
053    if (context.isShowSummaryTable()) { 
054      x.h2().addText(opd.getName()); 
055      x.para().addText(Utilities.capitalize(opd.getKind().toString())+": "+opd.getName());     
056      x.para().tx(context.formatPhrase(RenderingContext.OP_DEF_OFFIC)+" "); 
057      x.pre().tx(opd.getUrl()); 
058      addMarkdown(x, opd.getDescription());} 
059 
060    if (opd.getSystem()) 
061      x.para().tx(context.formatPhrase(RenderingContext.OP_DEF_URLS, opd.getCode())); 
062    for (Enumeration<VersionIndependentResourceTypesAll> c : opd.getResource()) { 
063      if (opd.getType()) 
064        x.para().tx(context.formatPhrase(RenderingContext.OP_DEF_URL, c.getCode()+"/$"+opd.getCode())); 
065      if (opd.getInstance()) 
066        x.para().tx(context.formatPhrase(RenderingContext.OP_DEF_URL, c.getCode()+"/[id]/$"+opd.getCode())); 
067    } 
068 
069    if (opd.hasInputProfile()) { 
070      XhtmlNode p = x.para(); 
071      p.tx(context.formatPhrase(RenderingContext.OP_DEF_INPAR)); 
072      StructureDefinition sd = context.getContext().fetchResource(StructureDefinition.class, opd.getInputProfile(), opd); 
073      if (sd == null) { 
074        p.pre().tx(opd.getInputProfile());         
075      } else { 
076        p.ah(context.prefixLocalHref(sd.getWebPath())).tx(sd.present());                  
077      }       
078    } 
079    if (opd.hasOutputProfile()) { 
080      XhtmlNode p = x.para(); 
081      p.tx(context.formatPhrase(RenderingContext.OP_DEF_OUTPAR)); 
082      StructureDefinition sd = context.getContext().fetchResource(StructureDefinition.class, opd.getOutputProfile(), opd); 
083      if (sd == null) { 
084        p.pre().tx(opd.getOutputProfile());         
085      } else { 
086        p.ah(context.prefixLocalHref(sd.getWebPath())).tx(sd.present());                  
087      }       
088    } 
089    x.para().tx(context.formatPhrase(RenderingContext.GENERAL_PARS)); 
090    XhtmlNode tbl = x.table( "grid"); 
091    XhtmlNode tr = tbl.tr(); 
092    tr.td().b().tx(context.formatPhrase(RenderingContext.OP_DEF_USE)); 
093    tr.td().b().tx(context.formatPhrase(RenderingContext.GENERAL_NAME)); 
094    tr.td().b().tx(context.formatPhrase(RenderingContext.OP_DEF_SCO)); 
095    tr.td().b().tx(context.formatPhrase(RenderingContext.GENERAL_CARDINALITY)); 
096    tr.td().b().tx(context.formatPhrase(RenderingContext.GENERAL_TYPE)); 
097    tr.td().b().tx(context.formatPhrase(RenderingContext.GENERAL_BINDING)); 
098    tr.td().b().tx(context.formatPhrase(RenderingContext.GENERAL_DOCUMENTATION)); 
099    for (OperationDefinitionParameterComponent p : opd.getParameter()) { 
100      genOpParam(tbl, "", p, opd); 
101    } 
102    addMarkdown(x, opd.getComment()); 
103  } 
104 
105  public void describe(XhtmlNode x, OperationDefinition opd) { 
106    x.tx(display(opd)); 
107  } 
108 
109  public String display(OperationDefinition opd) { 
110    return opd.present(); 
111  } 
112 
113  @Override 
114  public String display(Resource r) throws UnsupportedEncodingException, IOException { 
115    return ((OperationDefinition) r).present(); 
116  } 
117 
118  private void genOpParam(XhtmlNode tbl, String path, OperationDefinitionParameterComponent p, Resource opd) throws EOperationOutcome, FHIRException, IOException { 
119    XhtmlNode tr; 
120    tr = tbl.tr(); 
121    tr.td().addText(p.getUse().toString()); 
122    XhtmlNode td = tr.td(); 
123    td.addText(path+p.getName()); 
124    StandardsStatus ss = ToolingExtensions.getStandardsStatus(p); 
125    genStandardsStatus(td, ss); 
126    td = tr.td(); 
127    if (p.hasScope()) { 
128      CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(); 
129      for (Enumeration<OperationParameterScope> s : p.getScope()) { 
130        b.append(s.getCode()); 
131      } 
132      td.tx(b.toString()); 
133    } 
134    tr.td().addText(Integer.toString(p.getMin())+".."+p.getMax()); 
135    td = tr.td(); 
136    String actualType = translateTypeToVersion(p.getTypeElement()); 
137    StructureDefinition sd = actualType != null ? context.getWorker().fetchTypeDefinition(actualType) : null; 
138    if (sd == null) 
139      td.tx(p.hasType() ? actualType : ""); 
140    else if (sd.getAbstract() && p.hasExtension(ToolingExtensions.EXT_ALLOWED_TYPE)) { 
141      boolean first = true; 
142      for (Extension ex : p.getExtensionsByUrl(ToolingExtensions.EXT_ALLOWED_TYPE)) { 
143        if (first) first = false; else td.tx(" | "); 
144        String s = ex.getValue().primitiveValue(); 
145        StructureDefinition sdt = context.getWorker().fetchTypeDefinition(s); 
146        if (sdt == null) 
147          td.tx(p.hasType() ? actualType : ""); 
148        else 
149          td.ah(context.prefixLocalHref(sdt.getWebPath())).tx(s);          
150      } 
151    } else 
152      td.ah(context.prefixLocalHref(sd.getWebPath())).tx(actualType); 
153    if (p.hasTargetProfile()) { 
154      td.tx(" ("); 
155      boolean first = true; 
156      for (CanonicalType tp : p.getTargetProfile()) { 
157        if (first) { first = false;} else {td.tx(", ");}; 
158        StructureDefinition sdt = context.getWorker().fetchTypeDefinition(tp.asStringValue()); 
159        if (sdt == null || !sdt.hasWebPath()) { 
160          td.code().tx(tp.asStringValue()); 
161        } else { 
162          td.ah(context.prefixLocalHref(sdt.getWebPath()), tp.asStringValue()).tx(sdt.present()); 
163        } 
164      } 
165      td.tx(")"); 
166    } 
167    if (p.hasSearchType()) { 
168      td.br(); 
169      td.tx("("); 
170      td.ah(context.prefixLocalHref(context.getLink(KnownLinkType.SPEC) == null ? "search.html#"+p.getSearchType().toCode() : Utilities.pathURL(context.getLink(KnownLinkType.SPEC), "search.html#"+p.getSearchType().toCode()))).tx(p.getSearchType().toCode());        
171      td.tx(")"); 
172    } 
173    td = tr.td(); 
174    if (p.hasBinding() && p.getBinding().hasValueSet()) { 
175      AddVsRef(p.getBinding().getValueSet(), td, opd); 
176      td.tx(" ("+p.getBinding().getStrength().getDisplay()+")"); 
177    } 
178    addMarkdown(tr.td(), p.getDocumentation()); 
179    if (!p.hasType()) { 
180      for (OperationDefinitionParameterComponent pp : p.getPart()) { 
181        genOpParam(tbl, path+p.getName()+".", pp, opd); 
182      } 
183    } 
184  } 
185   
186  public static final String EXT_OPDEF_ORIGINAL_TYPE = "http://hl7.org/fhir/4.0/StructureDefinition/extension-OperationDefinition.parameter.type"; 
187 
188  private String translateTypeToVersion(Enumeration<FHIRTypes> src) { 
189    if (src.hasExtension(EXT_OPDEF_ORIGINAL_TYPE)) { 
190      return src.getExtensionString(EXT_OPDEF_ORIGINAL_TYPE); 
191    } else { 
192      return src.asStringValue(); 
193    } 
194  } 
195 
196 
197}