use of org.hl7.fhir.validation.instance.utils.EntrySummary in project org.hl7.fhir.core by hapifhir.
the class BundleValidator method checkAllInterlinked.
private void checkAllInterlinked(List<ValidationMessage> errors, List<Element> entries, NodeStack stack, Element bundle, boolean isError) {
List<EntrySummary> entryList = new ArrayList<>();
int i = 0;
for (Element entry : entries) {
Element r = entry.getNamedChild(RESOURCE);
if (r != null) {
EntrySummary e = new EntrySummary(i, entry, r);
entryList.add(e);
// System.out.println("Found entry "+e.dbg());
}
i++;
}
for (EntrySummary e : entryList) {
Set<String> references = findReferences(e.getEntry());
for (String ref : references) {
Element tgt = resolveInBundle(entries, ref, e.getEntry().getChildValue(FULL_URL), e.getResource().fhirType(), e.getResource().getIdBase());
if (tgt != null) {
EntrySummary t = entryForTarget(entryList, tgt);
if (t != null) {
if (t != e) {
// System.out.println("Entry "+e.getIndex()+" refers to "+t.getIndex()+" by ref '"+ref+"'");
e.getTargets().add(t);
} else {
// System.out.println("Entry "+e.getIndex()+" refers to itself by '"+ref+"'");
}
}
}
}
}
Set<EntrySummary> visited = new HashSet<>();
visitLinked(visited, entryList.get(0));
boolean foundRevLinks;
do {
foundRevLinks = false;
for (EntrySummary e : entryList) {
if (!visited.contains(e)) {
// System.out.println("Not visited "+e.getIndex()+" - check for reverse links");
boolean add = false;
for (EntrySummary t : e.getTargets()) {
if (visited.contains(t)) {
add = true;
}
}
if (add) {
warning(errors, IssueType.INFORMATIONAL, e.getEntry().line(), e.getEntry().col(), stack.addToLiteralPath(ENTRY + '[' + (i + 1) + ']'), isExpectedToBeReverse(e.getResource().fhirType()), I18nConstants.BUNDLE_BUNDLE_ENTRY_REVERSE, (e.getEntry().getChildValue(FULL_URL) != null ? "'" + e.getEntry().getChildValue(FULL_URL) + "'" : ""));
// System.out.println("Found reverse links for "+e.getIndex());
foundRevLinks = true;
visitLinked(visited, e);
}
}
}
} while (foundRevLinks);
i = 0;
for (EntrySummary e : entryList) {
Element entry = e.getEntry();
if (isError) {
rule(errors, IssueType.INFORMATIONAL, entry.line(), entry.col(), stack.addToLiteralPath(ENTRY + '[' + (i + 1) + ']'), visited.contains(e), I18nConstants.BUNDLE_BUNDLE_ENTRY_ORPHAN, (entry.getChildValue(FULL_URL) != null ? "'" + entry.getChildValue(FULL_URL) + "'" : ""));
} else {
warning(errors, IssueType.INFORMATIONAL, entry.line(), entry.col(), stack.addToLiteralPath(ENTRY + '[' + (i + 1) + ']'), visited.contains(e), I18nConstants.BUNDLE_BUNDLE_ENTRY_ORPHAN, (entry.getChildValue(FULL_URL) != null ? "'" + entry.getChildValue(FULL_URL) + "'" : ""));
}
i++;
}
}
Aggregations