001package org.hl7.fhir.r5.conformance;
002
003import java.io.BufferedOutputStream;
004import java.io.File;
005import java.io.FileInputStream;
006import java.io.FileOutputStream;
007import java.io.IOException;
008import java.io.InputStream;
009import java.io.OutputStream;
010import java.net.URL;
011import java.net.URLConnection;
012import java.util.ArrayList;
013import java.util.Collections;
014import java.util.Comparator;
015import java.util.Date;
016import java.util.List;
017import java.util.UUID;
018import java.util.zip.ZipEntry;
019import java.util.zip.ZipInputStream;
020
021import org.hl7.fhir.exceptions.DefinitionException;
022import org.hl7.fhir.exceptions.FHIRFormatError;
023import org.hl7.fhir.r5.conformance.CapabilityStatementUtilities.CapabilityStatementRestSorter;
024import org.hl7.fhir.r5.conformance.ProfileComparer.ProfileComparison;
025import org.hl7.fhir.r5.context.IWorkerContext;
026import org.hl7.fhir.r5.context.SimpleWorkerContext;
027import org.hl7.fhir.r5.formats.JsonParser;
028import org.hl7.fhir.r5.formats.XmlParser;
029import org.hl7.fhir.r5.formats.IParser.OutputStyle;
030import org.hl7.fhir.r5.model.BackboneElement;
031import org.hl7.fhir.r5.model.Base;
032import org.hl7.fhir.r5.model.CanonicalType;
033import org.hl7.fhir.r5.model.CapabilityStatement;
034import org.hl7.fhir.r5.model.CapabilityStatement.CapabilityStatementRestComponent;
035import org.hl7.fhir.r5.model.CapabilityStatement.CapabilityStatementRestResourceComponent;
036import org.hl7.fhir.r5.model.CapabilityStatement.CapabilityStatementRestResourceOperationComponent;
037import org.hl7.fhir.r5.model.CapabilityStatement.CapabilityStatementRestResourceSearchParamComponent;
038import org.hl7.fhir.r5.model.CapabilityStatement.CapabilityStatementRestSecurityComponent;
039import org.hl7.fhir.r5.model.CapabilityStatement.ResourceInteractionComponent;
040import org.hl7.fhir.r5.model.CapabilityStatement.SystemInteractionComponent;
041import org.hl7.fhir.r5.model.Enumerations.PublicationStatus;
042import org.hl7.fhir.r5.model.Enumerations.RestfulCapabilityMode;
043import org.hl7.fhir.r5.model.Extension;
044import org.hl7.fhir.r5.utils.KeyGenerator;
045import org.hl7.fhir.r5.utils.OperationOutcomeUtilities;
046import org.hl7.fhir.r5.model.CodeableConcept;
047import org.hl7.fhir.r5.model.Coding;
048import org.hl7.fhir.r5.model.DomainResource;
049import org.hl7.fhir.r5.model.Element;
050import org.hl7.fhir.r5.model.Enumeration;
051import org.hl7.fhir.r5.model.OperationOutcome;
052import org.hl7.fhir.r5.model.Property;
053import org.hl7.fhir.r5.model.StringType;
054import org.hl7.fhir.r5.model.StructureDefinition;
055import org.hl7.fhir.r5.model.ValueSet;
056import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
057import org.hl7.fhir.utilities.MarkDownProcessor;
058import org.hl7.fhir.utilities.Utilities;
059import org.hl7.fhir.utilities.MarkDownProcessor.Dialect;
060import org.hl7.fhir.utilities.TextFile;
061import org.hl7.fhir.utilities.validation.ValidationMessage;
062import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity;
063import org.hl7.fhir.utilities.validation.ValidationMessage.IssueType;
064import org.hl7.fhir.utilities.validation.ValidationMessage.Source;
065import org.hl7.fhir.utilities.xhtml.NodeType;
066import org.hl7.fhir.utilities.xhtml.XhtmlComposer;
067import org.hl7.fhir.utilities.xhtml.XhtmlDocument;
068import org.hl7.fhir.utilities.xhtml.XhtmlNode;
069import org.hl7.fhir.utilities.xhtml.XhtmlParser;
070
071public class CapabilityStatementUtilities {
072
073  public class CapabilityStatementComparisonOutput {
074    public CapabilityStatementComparisonOutput() {
075      subset = new CapabilityStatement();
076      keygen.genId(subset);
077      subset.setDate(new Date());
078      subset.setName("intersection of "+selfName+" and "+otherName);
079      subset.setStatus(PublicationStatus.DRAFT);
080      
081      superset = new CapabilityStatement();
082      keygen.genId(superset);
083      superset.setDate(subset.getDate());
084      superset.setName("union of "+selfName+" and "+otherName);
085      superset.setStatus(PublicationStatus.DRAFT);
086    }
087    private CapabilityStatement superset;
088    private CapabilityStatement subset;
089    private OperationOutcome outcome;
090    private List<ValidationMessage> messages = new ArrayList<>();
091    public CapabilityStatement getSuperset() {
092      return superset;
093    }
094    public CapabilityStatement getSubset() {
095      return subset;
096    }
097    public OperationOutcome getOutcome() {
098      return outcome;
099    }
100    public List<ValidationMessage> getMessages() {
101      return messages;
102    }
103  }
104
105  private IWorkerContext context;
106  private String selfName;
107  private String otherName;
108  private CapabilityStatementComparisonOutput output;
109  private XhtmlDocument html;
110  private MarkDownProcessor markdown = new MarkDownProcessor(Dialect.COMMON_MARK);
111  private String folder;
112  private KeyGenerator keygen;
113//  private String css; 
114
115  public CapabilityStatementUtilities(SimpleWorkerContext context, String folder, KeyGenerator keygen) throws IOException {
116    super();
117    this.context = context;
118    this.folder = folder;
119    this.keygen = keygen;
120    if (!new File(Utilities.path(folder, "conparison-zip-marker.bin")).exists()) {
121      String f = Utilities.path(folder, "comparison.zip");
122      download("https://www.fhir.org/archive/comparison.zip", f);
123      unzip(f, folder);
124    }
125  }
126  
127  /**
128   * Size of the buffer to read/write data
129   */
130  private static final int BUFFER_SIZE = 4096;
131  /**
132   * Extracts a zip file specified by the zipFilePath to a directory specified by
133   * destDirectory (will be created if does not exists)
134   * @param zipFilePath
135   * @param destDirectory
136   * @throws IOException
137   */
138  public void unzip(String zipFilePath, String destDirectory) throws IOException {
139      File destDir = new File(destDirectory);
140      if (!destDir.exists()) {
141          destDir.mkdir();
142      }
143      ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
144      ZipEntry entry = zipIn.getNextEntry();
145      // iterates over entries in the zip file
146      while (entry != null) {
147          String filePath = destDirectory + File.separator + entry.getName();
148          if (!entry.isDirectory()) {
149              // if the entry is a file, extracts it
150              extractFile(zipIn, filePath);
151          } else {
152              // if the entry is a directory, make the directory
153              File dir = new File(filePath);
154              dir.mkdir();
155          }
156          zipIn.closeEntry();
157          entry = zipIn.getNextEntry();
158      }
159      zipIn.close();
160  }
161  /**
162   * Extracts a zip entry (file entry)
163   * @param zipIn
164   * @param filePath
165   * @throws IOException
166   */
167  private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
168      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
169      byte[] bytesIn = new byte[BUFFER_SIZE];
170      int read = 0;
171      while ((read = zipIn.read(bytesIn)) != -1) {
172          bos.write(bytesIn, 0, read);
173      }
174      bos.close();
175  }
176
177  public void saveToFile() throws IOException {
178    String s = new XhtmlComposer(true, true).compose(html);
179    TextFile.stringToFile(s, Utilities.path(folder, "index.html"));
180  }
181  
182  /**
183   * Compares 2 capability statements to see if self is a subset of other
184   * 
185   * e.g. is a system implementing "+otherName+" also an implementation of self?
186   * 
187   * the output is a series of messages identifying ways in which it is not, 
188   * or warning messages where the algorithm is unable to determine the 
189   * relationship
190   * 
191   * Note that there are aspects of this question that are not computable.
192   * the 
193   * 
194   * @param self
195   * @param other
196   * @return
197   * @throws IOException 
198   * @throws FHIRFormatError 
199   * @throws DefinitionException 
200   */
201  public CapabilityStatementComparisonOutput isCompatible(String selfName, String otherName, CapabilityStatement self,  CapabilityStatement other) throws DefinitionException, FHIRFormatError, IOException {
202    this.selfName = selfName;
203    this.otherName = otherName;
204    this.output = new CapabilityStatementComparisonOutput();
205    XhtmlNode x = startHtml();
206
207    information(x, IssueType.INVARIANT, self.getUrl(), "Comparing "+selfName+" to "+otherName+", to see if a server that implements "+otherName+" also implements "+selfName+"");
208    information(x, IssueType.INVARIANT, self.getUrl(), "  "+selfName+": "+self.getUrl()+"|"+self.getVersion());
209    information(x, IssueType.INVARIANT, self.getUrl(), "  "+otherName+": "+other.getUrl()+"|"+other.getVersion());
210
211    if (self.getRest().size() != 1 || self.getRestFirstRep().getMode() != RestfulCapabilityMode.SERVER)
212      fatal(x, IssueType.INVARIANT, self.getUrl()+"#rest", "The CapabilityStatement Comparison tool can only compare CapabilityStatements with a single server component");
213    if (other.getRest().size() != 1 || other.getRestFirstRep().getMode() != RestfulCapabilityMode.SERVER)
214      fatal(x, IssueType.INVARIANT, other.getUrl()+"#rest", "The CapabilityStatement Comparison tool can only compare CapabilityStatements with a single server component");
215
216    if (self.getRest().size() == 1 && other.getRest().size() == 1) {
217      XhtmlNode tbl = startTable(x, self, other);
218      compareRest(tbl, self.getUrl(), self.getRest().get(0), other.getRest().get(0), output.subset.addRest().setMode(RestfulCapabilityMode.SERVER), output.superset.addRest().setMode(RestfulCapabilityMode.SERVER)); 
219    }
220    if (folder != null)
221      saveToFile();
222    output.outcome = OperationOutcomeUtilities.createOutcome(output.messages);
223    sortCapabilityStatement(output.subset);
224    sortCapabilityStatement(output.superset);
225    return output;
226  }
227
228  private void compareRest(XhtmlNode tbl, String path, CapabilityStatementRestComponent self, CapabilityStatementRestComponent other, CapabilityStatementRestComponent intersection, CapabilityStatementRestComponent union) throws DefinitionException, FHIRFormatError, IOException {
229    compareSecurity(tbl, path, self, other, intersection, union);
230
231    // check resources
232    List<CapabilityStatementRestResourceComponent> ol = new ArrayList<>();
233    List<CapabilityStatementRestResourceComponent> olr = new ArrayList<>();
234    ol.addAll(other.getResource());
235    for (CapabilityStatementRestResourceComponent r : self.getResource()) {
236      CapabilityStatementRestResourceComponent o = null;
237      for (CapabilityStatementRestResourceComponent t : ol) {
238        if (t.getType().equals(r.getType())) {
239          o = t;
240          break;
241        }
242      }
243      XhtmlNode tr = tbl.tr();
244      tr.style("background-color: #dddddd");
245      tr.td().b().addText(r.getType());
246      tr.td().tx("Present");
247     
248
249      if (o == null) {
250        union.addResource(r);
251        XhtmlNode p = tr.td().para("Absent");
252        XhtmlNode td = tr.td();
253        String s = getConfStatus(r);
254        if (Utilities.existsInList(s,  "SHALL", "SHOULD")) {
255          error(td, IssueType.NOTFOUND, path+".resource.where(type = '"+r.getType()+"')", selfName+" specifies the resource "+r.getType()+" as "+s+" but "+otherName+" does not cover it");
256          p.style("background-color: #ffe6e6; border: 1px solid #ff1a1a; margin-width: 10px");
257        }
258
259        tr = tbl.tr();
260        tr.td().para().tx(XhtmlNode.NBSP+" - Conformance");
261        genConf(tr.td().para(), r, null);
262        tr.td().style("background-color: #eeeeee");
263        tr.td();
264
265        tr = tbl.tr();
266        tr.td().para().tx(XhtmlNode.NBSP+" - Profile");
267        genProfile(tr.td().para(), r, null);
268        tr.td().style("background-color: #eeeeee");
269        tr.td();
270
271        tr = tbl.tr();
272        tr.td().para().tx(XhtmlNode.NBSP+" - Interactions");
273        genInt(tr.td(), r, null, false);
274        tr.td().style("background-color: #eeeeee");
275        tr.td();
276
277        tr = tbl.tr();
278        tr.td().para().tx(XhtmlNode.NBSP+" - Search Parameters");
279        genSP(tr.td(), r, null, false);
280        tr.td().style("background-color: #eeeeee");
281        tr.td();
282
283      } else { 
284        olr.add(o);
285        tr.td().tx("Present");
286        tr.td().nbsp();
287        compareResource(path+".resource.where(type = '"+r.getType()+"')", r, o, tbl, intersection.addResource().setType(r.getType()), union.addResource().setType(r.getType()));
288      }
289    }
290    for (CapabilityStatementRestResourceComponent t : ol) {
291      XhtmlNode tr = tbl.tr();
292      if (!olr.contains(t)) {
293        union.addResource(t);
294        tr.td().addText(t.getType());
295        XhtmlNode td = tr.td();
296        td.style("background-color: #eeeeee").para("Absent");
297        td = tr.td();
298        genConf(td, t, null);
299        genProfile(td, t, null);
300        genInt(td, t, null, false);
301        genSP(td, t, null, false);
302        if (isProhibited(t)) {
303          error(td, IssueType.INVARIANT, path+".resource", selfName+" does not specify the resource "+t.getType()+" but "+otherName+" prohibits it");
304        }
305      }    
306    }
307    // check interactions
308    // check search parameters
309    // check operation
310    // check compartments
311  }
312
313  private void genConf(XhtmlNode x, CapabilityStatementRestResourceComponent r, CapabilityStatementRestResourceComponent other) {
314    String s = getConfStatus(r);
315    String so = other != null ? getConfStatus(other) : null;
316    x.add(same(s == null ? "(not specified)" : s, (s == null && so == null) || (s != null && s.equals(so))));   
317  }
318
319  private void genProfile(XhtmlNode x, CapabilityStatementRestResourceComponent r, CapabilityStatementRestResourceComponent other) {
320    String s = getProfile(r);
321    if (s == null)
322      s = "(not specified)";
323    String so = other == null ? null : getProfile(other) == null ? getProfile(other) : "(not specified)";
324    x.add(same(s, (s == null && so == null) || (s != null && s.equals(so))));
325  }
326  
327  private String getProfile(CapabilityStatementRestResourceComponent r) {
328    if (r.hasSupportedProfile() && r.getSupportedProfile().size() == 1)
329      return r.getSupportedProfile().get(0).asStringValue();
330    return r.getProfile();
331  }
332
333  private void genInt(XhtmlNode td, CapabilityStatementRestResourceComponent r, CapabilityStatementRestResourceComponent other, boolean errorIfNoMatch) {
334    boolean first = true;
335    for (ResourceInteractionComponent i : r.getInteraction()) {
336      if (first) first = false; else td.tx(", ");
337      if (exists(other, i)) {
338        td.code().span("background-color: #bbff99; border: 1px solid #44cc00; margin-width: 10px", null).tx(i.getCode().toCode());
339      } else if (errorIfNoMatch){
340        td.code().span("background-color: #ffe6e6; border: 1px solid #ff1a1a; margin-width: 10px", null).tx(i.getCode().toCode());
341      } else {
342        td.code(i.getCode().toCode());
343      }
344    }    
345  }
346
347  private boolean exists(CapabilityStatementRestResourceComponent other, ResourceInteractionComponent i) {
348    if (other == null)
349      return false;
350    for (ResourceInteractionComponent t : other.getInteraction())
351      if (t.getCode().equals(i.getCode()))
352        return true;
353    return false;
354  }
355
356  private void genSP(XhtmlNode td, CapabilityStatementRestResourceComponent r, CapabilityStatementRestResourceComponent other, boolean errorIfNoMatch) {
357    boolean first = true;
358    for (CapabilityStatementRestResourceSearchParamComponent i : r.getSearchParam()) {
359      if (first) first = false; else td.tx(", ");
360      if (exists(other, i)) {
361        td.code().span("background-color: #bbff99; border: 1px solid #44cc00; margin-width: 10px", null).tx(i.getName());
362      } else if (errorIfNoMatch){
363        td.code().span("background-color: #ffe6e6; border: 1px solid #ff1a1a; margin-width: 10px", null).tx(i.getName());
364      } else {
365        td.code(i.getName());
366      }
367    }    
368  }
369
370  private boolean exists(CapabilityStatementRestResourceComponent other, CapabilityStatementRestResourceSearchParamComponent i) {
371    if (other == null)
372      return false;
373    for (CapabilityStatementRestResourceSearchParamComponent t : other.getSearchParam())
374      if (t.getName().equals(i.getName()))
375        return true;
376    return false;
377  }
378
379  public void compareSecurity(XhtmlNode tbl, String path, CapabilityStatementRestComponent self, CapabilityStatementRestComponent other, CapabilityStatementRestComponent intersection, CapabilityStatementRestComponent union) {
380    XhtmlNode tr = tbl.tr();
381    tr.td().b().addText("Security");
382    tr.td().para(gen(self.getSecurity()));
383    tr.td().para(gen(other.getSecurity()));
384    XhtmlNode td = tr.td();
385    
386    if (self.hasSecurity() && !other.hasSecurity()) 
387      error(td, IssueType.CONFLICT, path+".security", selfName+" specifies some security requirements ("+gen(self.getSecurity())+") but "+otherName+" doesn't");
388    else if (!self.hasSecurity() && other.hasSecurity()) 
389      error(td, IssueType.CONFLICT, path+".security", selfName+" does not specify security requirements but "+otherName+" does ("+gen(self.getSecurity())+")");
390    else if (self.hasSecurity() && other.hasSecurity())
391      compareSecurity(td, path+".security", self.getSecurity(), other.getSecurity(), intersection.getSecurity(), union.getSecurity());
392  }  
393
394  private void compareResource(String path, CapabilityStatementRestResourceComponent self, CapabilityStatementRestResourceComponent other, XhtmlNode tbl,
395      CapabilityStatementRestResourceComponent intersection, CapabilityStatementRestResourceComponent union) throws DefinitionException, FHIRFormatError, IOException {
396    XhtmlNode tr = tbl.tr();
397    tr.td().para().tx(XhtmlNode.NBSP+" - Conformance");
398    genConf(tr.td(), self, other);
399    genConf(tr.td(), other, self);
400    tr.td().nbsp();
401
402    tr = tbl.tr();
403    tr.td().para().tx(XhtmlNode.NBSP+" - Profile");
404    genProfile(tr.td(), self, other);
405    genProfile(tr.td(), other, self);
406    compareProfiles(tr.td(), path, getProfile(self), getProfile(other), self.getType(), intersection, union);
407    
408    // compare the interactions
409    compareResourceInteractions(path, self, other, tbl, intersection, union);
410    compareResourceSearchParams(path, self, other, tbl, intersection, union);
411    // compare the search parameters
412    // compare the operations
413
414    // compare the profile?
415
416  }
417
418  private void compareProfiles(XhtmlNode td, String path, String urlL, String urlR, String type,
419      CapabilityStatementRestResourceComponent intersection, CapabilityStatementRestResourceComponent union) throws DefinitionException, FHIRFormatError, IOException {
420    if (urlL == null) {
421      urlL = "http://hl7.org/fhir/StructureDefinition/"+type;
422    }
423    if (urlR == null) {
424        urlR = "http://hl7.org/fhir/StructureDefinition/"+type; 
425    }
426    StructureDefinition sdL = context.fetchResource(StructureDefinition.class, urlL);
427    StructureDefinition sdR = context.fetchResource(StructureDefinition.class, urlR);
428    if (sdL == null)
429      error(td, IssueType.NOTFOUND, path, "Unable to resolve "+urlL);
430    if (sdR == null)
431      error(td, IssueType.NOTFOUND, path, "Unable to resolve "+urlR);
432      
433    if (sdL != null && sdR != null && sdL != sdR) {
434      // ok they are different... 
435      if (sdR.getUrl().equals(sdL.getBaseDefinition())) {
436        information(td, null, path, "The profile specified by "+selfName+" is inherited from the profile specified by "+otherName);
437        intersection.setProfile(sdL.getUrl());
438        union.setProfile(sdR.getUrl());
439      } else if (sdL.getUrl().equals(sdR.getBaseDefinition())) {
440        information(td, null, path, "The profile specified by "+otherName+" is inherited from the profile specified by "+selfName);
441        intersection.setProfile(sdR.getUrl());
442        union.setProfile(sdL.getUrl());
443      } else if (folder != null) {
444        try {
445          ProfileComparer pc = new ProfileComparer(context, keygen, folder);
446          pc.setId("api-ep."+type);
447          pc.setTitle("Comparison - "+selfName+" vs "+otherName);
448          pc.setLeftName(selfName+": "+sdL.present());
449          pc.setLeftLink(sdL.getUserString("path"));
450          pc.setRightName(otherName+": "+sdR.present());
451          pc.setRightLink(sdR.getUserString("path"));
452          pc.compareProfiles(sdL, sdR);
453          System.out.println("Generate Comparison between "+pc.getLeftName()+" and "+pc.getRightName());
454          pc.generate();
455          
456          for (ProfileComparison cmp : pc.getComparisons()) {
457            new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(folder, cmp.getSubset().fhirType()+"-"+cmp.getSubset().getId()+".xml")), cmp.getSubset());
458            new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(folder, cmp.getSubset().fhirType()+"-"+cmp.getSubset().getId()+".json")), cmp.getSubset());
459            new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(folder, cmp.getSuperset().fhirType()+"-"+cmp.getSuperset().getId()+".xml")), cmp.getSuperset());
460            new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(folder, cmp.getSuperset().fhirType()+"-"+cmp.getSuperset().getId()+".json")), cmp.getSuperset());
461          }
462          for (ValueSet vs : pc.getValuesets())  {
463            new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(folder, vs.fhirType()+"-"+vs.getId()+".xml")), vs);
464            new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(folder, vs.fhirType()+"-"+vs.getId()+".json")), vs);
465          }
466
467          intersection.setProfile(pc.getComparisons().get(0).getSubset().getUrl());
468          union.setProfile(pc.getComparisons().get(0).getSuperset().getUrl());
469          td.ah(pc.getId()+".html").tx("Comparison...");
470          td.tx(pc.getErrCount()+" "+Utilities.pluralize("error", pc.getErrCount()));
471        } catch (Exception e) {
472          e.printStackTrace();
473          error(td, IssueType.EXCEPTION, path, "Error comparing profiles: "+e.getMessage());
474        }
475      } else {
476        information(td, null, path, "Use the validator to compare the profiles");
477      }
478    }
479  }
480
481  private void compareResourceInteractions(String path, CapabilityStatementRestResourceComponent self, CapabilityStatementRestResourceComponent other, XhtmlNode tbl,
482      CapabilityStatementRestResourceComponent intersection, CapabilityStatementRestResourceComponent union) {
483    XhtmlNode tr = tbl.tr();
484    tr.td().para().tx(XhtmlNode.NBSP+" - Interactions");
485    genInt(tr.td(), self, other, true);
486    genInt(tr.td(), other, self, false);
487    XhtmlNode td = tr.td();
488    List<ResourceInteractionComponent> ol = new ArrayList<>();
489    List<ResourceInteractionComponent> olr = new ArrayList<>();
490    ol.addAll(other.getInteraction());
491    for (ResourceInteractionComponent r : self.getInteraction()) {
492      ResourceInteractionComponent o = null;
493      for (ResourceInteractionComponent t : ol) {
494        if (t.getCode().equals(r.getCode())) {
495          o = t;
496          break;
497        }
498      }
499      union.addInteraction(r);
500      if (o == null) {
501        error(td, IssueType.NOTFOUND, path+".interaction.where(code = '"+r.getCode()+"')", selfName+" specifies the interaction "+r.getCode()+" but "+otherName+" does not");        
502      } else { 
503        intersection.addInteraction(r);
504        olr.add(o);
505      }
506    }
507    for (ResourceInteractionComponent t : ol) {
508      union.addInteraction(t);
509      if (!olr.contains(t) && isProhibited(t))
510        error(td, IssueType.CONFLICT, path+".interaction", selfName+" does not specify the interaction "+t.getCode()+" but "+otherName+" prohibits it");        
511    }        
512  }
513
514  private void compareResourceSearchParams(String path, CapabilityStatementRestResourceComponent self, CapabilityStatementRestResourceComponent other, XhtmlNode tbl,
515      CapabilityStatementRestResourceComponent intersection, CapabilityStatementRestResourceComponent union) {
516    XhtmlNode tr = tbl.tr();
517    tr.td().para().tx(XhtmlNode.NBSP+" - Search Params");
518    genSP(tr.td(), self, other, true);
519    genSP(tr.td(), other, self, false);
520    XhtmlNode td = tr.td();
521
522    List<CapabilityStatementRestResourceSearchParamComponent> ol = new ArrayList<>();
523    List<CapabilityStatementRestResourceSearchParamComponent> olr = new ArrayList<>();
524    ol.addAll(other.getSearchParam());
525    for (CapabilityStatementRestResourceSearchParamComponent r : self.getSearchParam()) {
526      CapabilityStatementRestResourceSearchParamComponent o = null;
527      for (CapabilityStatementRestResourceSearchParamComponent t : ol) {
528        if (t.getName().equals(r.getName())) {
529          o = t;
530          break;
531        }
532      }
533      union.addSearchParam(r);
534      if (o == null) {
535        error(td, IssueType.NOTFOUND, path+".searchParam.where(name = '"+r.getName()+"')", selfName+" specifies the search parameter "+r.getName()+" but "+otherName+" does not");        
536      } else { 
537        intersection.addSearchParam(r);
538        olr.add(o);
539      }
540    }
541    for (CapabilityStatementRestResourceSearchParamComponent t : ol) {
542      if (!olr.contains(t))
543        union.addSearchParam(t);
544      if (!olr.contains(t) && isProhibited(t))
545        error(td, IssueType.CONFLICT, path+"", selfName+" does not specify the search parameter "+t.getName()+" but "+otherName+" prohibits it");        
546    }    
547
548  }
549
550  private String getConfStatus(Element t) {
551    return t.hasExtension("http://hl7.org/fhir/StructureDefinition/capabilitystatement-expectation")  ?  
552        t.getExtensionString("http://hl7.org/fhir/StructureDefinition/capabilitystatement-expectation") : null;
553  }
554
555  private boolean isShouldOrShall(Element t) {
556    return t.hasExtension("http://hl7.org/fhir/StructureDefinition/capabilitystatement-expectation") && 
557        ("SHALL".equals(t.getExtensionString("http://hl7.org/fhir/StructureDefinition/capabilitystatement-expectation")) || "SHOULD".equals(t.getExtensionString("http://hl7.org/fhir/StructureDefinition/capabilitystatement-expectation")));
558  }
559
560  private boolean isProhibited(Element t) {
561    return t.hasExtension("http://hl7.org/fhir/StructureDefinition/capabilitystatement-expectation") && "SHALL NOT".equals(t.getExtensionString("http://hl7.org/fhir/StructureDefinition/capabilitystatement-expectation"));
562  }
563
564  private void compareSecurity(XhtmlNode td, String path, CapabilityStatementRestSecurityComponent self, CapabilityStatementRestSecurityComponent other, 
565      CapabilityStatementRestSecurityComponent intersection, CapabilityStatementRestSecurityComponent union) {
566    if (self.getCors() && !other.getCors()) 
567      error(td, IssueType.CONFLICT, path+".security.cors", selfName+" specifies CORS but "+otherName+" doesn't");
568    else if (!self.getCors() && other.getCors()) 
569      error(td, IssueType.CONFLICT, path+".security.cors", selfName+" does not specify CORS but "+otherName+" does");
570    if (self.getCors() || other.getCors())
571      union.setCors(true);
572    if (self.getCors() && other.getCors())
573      union.setCors(true);
574
575    List<CodeableConcept> ol = new ArrayList<>();
576    List<CodeableConcept> olr = new ArrayList<>();
577    ol.addAll(other.getService());
578    for (CodeableConcept cc : self.getService()) {
579      CodeableConcept o = null;
580      for (CodeableConcept t : ol) {
581        if (isMatch(t, cc)) {
582          o = t;
583          break;
584        }
585      }
586      union.getService().add(cc);
587      if (o == null) {
588        error(td, IssueType.CONFLICT, path+".security.cors", selfName+" specifies the security option "+gen(cc)+" but "+otherName+" does not");        
589      } else { 
590        intersection.getService().add(cc);
591        olr.add(o);
592      }
593    }
594    for (CodeableConcept cc : ol) {
595      if (!olr.contains(cc)) {
596        union.getService().add(cc);
597        error(td, IssueType.CONFLICT, path+".security.cors", selfName+" does not specify the security option "+gen(cc)+" but "+otherName+" does");
598      }
599    }    
600  }
601
602  private boolean isMatch(CodeableConcept self, CodeableConcept other) {
603    for (Coding s : self.getCoding())
604      for (Coding o : other.getCoding())
605        if (isMatch(s, o))
606          return true;
607    return false;
608  }
609
610  private boolean isMatch(Coding s, Coding o) {
611    return s.hasCode() && s.getCode().equals(o.getCode()) && s.hasSystem() && s.getSystem().equals(o.getSystem());
612  }
613
614  private String gen(CapabilityStatementRestSecurityComponent security) {
615    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
616    for (CodeableConcept cc : security.getService()) 
617      b.append(gen(cc));
618    if (security.getCors())
619      b.append("(CORS)");
620    if (Utilities.noString(b.toString()))
621      return "(none specified)";
622    return b.toString();
623  }
624
625  private String gen(CodeableConcept cc) {
626    if (cc.hasText())
627      return cc.getText();
628    if (cc.hasCoding())
629      return gen(cc.getCoding().get(0));
630    return "??";
631  }
632
633  private String gen(Coding coding) {
634    if (coding.hasDisplay())
635      return coding.getDisplay();
636    if (coding.hasCode())
637      return coding.getCode();
638    return "???";
639  }
640
641
642  private XhtmlNode startHtml() {
643    html = new XhtmlDocument();
644    XhtmlNode doc = html.addTag("html");
645    XhtmlNode head = doc.addTag("head");
646    head.addTag("title").addText("Comparison of "+selfName+" to "+otherName);
647    head.addTag("link").setAttribute("rel", "stylesheet").setAttribute("href", "fhir.css");
648    XhtmlNode body = doc.addTag("body").style("background-color: white");
649
650    body.h1().addText("Comparison of "+selfName+" to "+otherName);
651    return body;    
652  }
653
654  private void addMarkdown(XhtmlNode x, String text) throws FHIRFormatError, IOException, DefinitionException {
655    if (text != null) {
656      // 1. custom FHIR extensions
657      while (text.contains("[[[")) {
658        String left = text.substring(0, text.indexOf("[[["));
659        String link = text.substring(text.indexOf("[[[")+3, text.indexOf("]]]"));
660        String right = text.substring(text.indexOf("]]]")+3);
661        String url = link;
662        String[] parts = link.split("\\#");
663        StructureDefinition p = context.fetchResource(StructureDefinition.class, parts[0]);
664        if (p == null)
665          p = context.fetchTypeDefinition(parts[0]);
666        if (p == null)
667          p = context.fetchResource(StructureDefinition.class, link);
668        if (p != null) {
669          url = p.getUserString("path");
670          if (url == null)
671            url = p.getUserString("filename");
672        } else
673          throw new DefinitionException("Unable to resolve markdown link "+link);
674
675        text = left+"["+link+"]("+url+")"+right;
676      }
677
678      // 2. markdown
679      String s = markdown.process(Utilities.escapeXml(text), "narrative generator");
680      XhtmlParser p = new XhtmlParser();
681      XhtmlNode m;
682      try {
683        m = p.parse("<div>"+s+"</div>", "div");
684      } catch (org.hl7.fhir.exceptions.FHIRFormatError e) {
685        throw new FHIRFormatError(e.getMessage(), e);
686      }
687      x.getChildNodes().addAll(m.getChildNodes());
688    }
689  }
690
691
692  private XhtmlNode startTable(XhtmlNode x, CapabilityStatement self, CapabilityStatement other) {
693    XhtmlNode tbl = x.table("grid");    
694    XhtmlNode tr = tbl.tr();
695    tr.td().b().nbsp();
696    tr.td().b().addText(selfName);
697    tr.td().b().addText(otherName);
698    tr.td().b().addText("Comparison");
699    return tbl;
700  }
701
702  private void download(String address, String filename) throws IOException {
703    URL url = new URL(address);
704    URLConnection c = url.openConnection();
705    InputStream s = c.getInputStream();
706    FileOutputStream f = new FileOutputStream(filename);
707    transfer(s, f, 1024);
708    f.close();   
709  }
710
711
712  public static void transfer(InputStream in, OutputStream out, int buffer) throws IOException {
713    byte[] read = new byte[buffer]; // Your buffer size.
714    while (0 < (buffer = in.read(read)))
715      out.write(read, 0, buffer);
716  }
717
718  private void fatal(XhtmlNode x, IssueType type, String path, String message) {
719    output.messages.add(new ValidationMessage(Source.ProfileComparer, type, path, message, IssueSeverity.FATAL));
720    XhtmlNode ul;
721    if ("ul".equals(x.getName())) {
722      ul = x;
723    } else {
724      ul = null;
725      for (XhtmlNode c : x.getChildNodes()) {
726        if ("ul".equals(c.getName())) {
727          ul = c;
728        }
729      }
730      if (ul == null) {
731        ul = x.ul();
732      }
733    } 
734    ul.li().b().style("color: maroon").addText(message);
735  }
736
737  private void error(XhtmlNode x, IssueType type, String path, String message) {
738    output.messages.add(new ValidationMessage(Source.ProfileComparer, type, path, message, IssueSeverity.ERROR));
739    XhtmlNode ul;
740    if ("ul".equals(x.getName())) {
741      ul = x;
742    } else {
743      ul = null;
744      for (XhtmlNode c : x.getChildNodes()) {
745        if ("ul".equals(c.getName())) {
746          ul = c;
747        }
748      }
749      if (ul == null) {
750        ul = x.ul();
751      }
752    } 
753    ul.li().b().style("color: maroon").addText(message);
754  }
755
756  private void information(XhtmlNode x, IssueType type, String path, String message) {
757    if (type != null)
758      output.messages.add(new ValidationMessage(Source.ProfileComparer, type, path, message, IssueSeverity.INFORMATION));
759    XhtmlNode ul;
760    if ("ul".equals(x.getName())) {
761      ul = x;
762    } else {
763      ul = null;
764      for (XhtmlNode c : x.getChildNodes()) {
765        if ("ul".equals(c.getName())) {
766          ul = c;
767        }
768      }
769      if (ul == null) {
770        ul = x.ul();
771      }
772    } 
773    ul.li().addText(message);
774  }
775
776  private XhtmlNode same(String text, boolean test) {
777    XhtmlNode span = new XhtmlNode(NodeType.Element, "span");
778    if (test)
779      span.style("background-color: #bbff99; border: 1px solid #44cc00; margin-width: 10px");
780    span.tx(text);
781    return span;
782  }
783
784  private int compareStr(String l, String r) {
785    if (l == null) {
786      if (r == null)
787        return (r == null) ? 0 : -1;
788    }
789    if (r == null)
790      return 1;
791    return l.compareTo(r);
792  }
793
794  public class CapabilityStatementRestSorter implements Comparator<CapabilityStatementRestComponent> {
795    @Override
796    public int compare(CapabilityStatementRestComponent l, CapabilityStatementRestComponent r) {
797      return compareStr(l.hasMode() ? l.getMode().toCode() : null, r.hasMode() ? r.getMode().toCode() : null);
798    }
799  }
800
801  public class CapabilityStatementRestResourceSorter implements Comparator<CapabilityStatementRestResourceComponent> {
802    @Override
803    public int compare(CapabilityStatementRestResourceComponent l, CapabilityStatementRestResourceComponent r) {
804      return compareStr(l.getType(), r.getType());
805    }
806  }
807
808  public class CapabilityStatementRestResourceInteractionSorter implements Comparator<ResourceInteractionComponent> {
809    @Override
810    public int compare(ResourceInteractionComponent l, ResourceInteractionComponent r) {
811      return compareStr(l.hasCode() ? l.getCode().toCode() : null, r.hasCode() ? r.getCode().toCode() : null);
812    }
813  }
814
815  public class SystemInteractionComponentSorter implements Comparator<SystemInteractionComponent> {
816    @Override
817    public int compare(SystemInteractionComponent l, SystemInteractionComponent r) {
818      return compareStr(l.hasCode() ? l.getCode().toCode() : null, r.hasCode() ? r.getCode().toCode() : null);
819    }
820  }
821
822  public class SearchParamSorter implements Comparator<CapabilityStatementRestResourceSearchParamComponent> {
823    @Override
824    public int compare(CapabilityStatementRestResourceSearchParamComponent l, CapabilityStatementRestResourceSearchParamComponent r) {
825      return compareStr(l.getName(), r.getName());
826    }
827  }
828
829  public class OperationSorter implements Comparator<CapabilityStatementRestResourceOperationComponent> {
830    @Override
831    public int compare(CapabilityStatementRestResourceOperationComponent l, CapabilityStatementRestResourceOperationComponent r) {
832      return compareStr(l.getName(), r.getName());
833    }
834  }
835  
836  public class CanonicalTypeSorter implements Comparator<CanonicalType> {
837    @Override
838    public int compare(CanonicalType l, CanonicalType r) {
839      return compareStr(l.getValue(), r.getValue());
840    }
841  }
842
843  public class StringTypeSorter implements Comparator<StringType> {
844    @Override
845    public int compare(StringType l, StringType r) {
846      return compareStr(l.getValue(), r.getValue());
847    }
848  }
849
850  public class EnumSorter implements Comparator<Enumeration> {
851    @Override
852    public int compare(Enumeration l, Enumeration r) {
853      return compareStr(l.getCode(), r.getCode());
854    }
855  }
856
857  public class ExtensionSorter implements Comparator<Extension> {
858    @Override
859    public int compare(Extension l, Extension r) {
860      return compareStr(l.getUrl(), r.getUrl());
861    }
862  }
863
864  public class CodingSorter implements Comparator<Coding> {
865    @Override
866    public int compare(Coding l, Coding r) {
867      return compareStr(""+l.getSystem()+"#"+l.getCode(), ""+r.getSystem()+"#"+r.getCode());
868    }    
869  }
870  public class CodeableConceptSorter implements Comparator<CodeableConcept> {
871    @Override
872    public int compare(CodeableConcept l, CodeableConcept r) {
873      Collections.sort(l.getCoding(), new CodingSorter());      
874      Collections.sort(r.getCoding(), new CodingSorter());      
875      return compareStr(cgen(l), cgen(r));
876    }
877
878    private String cgen(CodeableConcept cc) {
879      if (cc.hasCoding())
880        return ""+cc.getCodingFirstRep().getSystem()+"#"+cc.getCodingFirstRep().getCode();
881      return "~"+cc.getText();
882    }    
883  }
884
885  private void sortCapabilityStatement(CapabilityStatement cs) {
886    sortExtensions(cs);
887    Collections.sort(cs.getRest(), new CapabilityStatementRestSorter());
888    for (CapabilityStatementRestComponent csr : cs.getRest()) {
889      Collections.sort(csr.getSecurity().getService(), new CodeableConceptSorter());
890      Collections.sort(csr.getCompartment(), new CanonicalTypeSorter());
891      Collections.sort(csr.getResource(), new CapabilityStatementRestResourceSorter());
892      for (CapabilityStatementRestResourceComponent r : csr.getResource()) {
893        Collections.sort(r.getSupportedProfile(), new CanonicalTypeSorter());
894        Collections.sort(r.getInteraction(), new CapabilityStatementRestResourceInteractionSorter());
895        Collections.sort(r.getReferencePolicy(), new EnumSorter());
896        Collections.sort(r.getSearchInclude(), new StringTypeSorter());
897        Collections.sort(r.getSearchRevInclude(), new StringTypeSorter());
898        Collections.sort(r.getSearchParam(), new SearchParamSorter());
899        Collections.sort(r.getOperation(), new OperationSorter());
900      }
901      Collections.sort(csr.getInteraction(), new SystemInteractionComponentSorter());
902      Collections.sort(csr.getSearchParam(), new SearchParamSorter());
903      Collections.sort(csr.getOperation(), new OperationSorter());
904    }
905  }
906  
907  private void sortExtensions(DomainResource dr) {
908    Collections.sort(dr.getExtension(), new ExtensionSorter());
909    for (Property p : dr.children()) {
910      for (Base b : p.getValues()) {
911        if (b instanceof Element)
912          sortExtensions((Element) b);
913      }
914    }
915  }
916
917  private void sortExtensions(Element e) {
918    Collections.sort(e.getExtension(), new ExtensionSorter());         
919    for (Property p : e.children()) {
920      for (Base b : p.getValues()) {
921        if (b instanceof Element)
922          sortExtensions((Element) b);
923      }
924    }
925  }
926
927}