findingsToRemove.clear();
// Remove any findings from the scan being deleted and
// update the first finding for reporting purposes
boolean changeFirstFinding = false;
Finding newFirstFinding = null;
Calendar earliestTime = null;
for (Finding finding : vuln.getFindings()) {
if (finding == null || finding.getScan() == null
|| finding.getScan().getId() == null) {
continue;
}
if (newFirstFinding == null || earliestTime == null ||
(finding.getScan().getImportTime() != null
&& finding.getScan().getImportTime().before(earliestTime))) {
newFirstFinding = finding;
earliestTime = finding.getScan().getImportTime();
}
if (finding.getScan().getId().equals(scan.getId())) {
finding.setVulnerability(null);
findingsToRemove.add(finding);
if (finding.isFirstFindingForVuln()) {
changeFirstFinding = true;
}
}
}
// Should avoid any problems related to removing items from a collection
// while iterating through it.
vuln.getFindings().removeAll(findingsToRemove);
if (changeFirstFinding && newFirstFinding != null) {
if (newFirstFinding.getVulnerability() != null) {
newFirstFinding.getVulnerability().setSurfaceLocation(
newFirstFinding.getSurfaceLocation());
}
newFirstFinding.setFirstFindingForVuln(true);
log.debug("Updating number new vulnerabilities for Scan with ID " +
newFirstFinding.getScan().getId());
newFirstFinding.getScan().setNumberNewVulnerabilities(
newFirstFinding.getScan().getNumberNewVulnerabilities() + 1);
vuln.setOpenTime(newFirstFinding.getScan().getImportTime());
}
// now if the vuln has no findings, delete it
if (vuln.getFindings().size() == 0) {
vulnsToRemove.add(vuln);