public TransactionResult runQuery(EntityManager em) throws Exception {
BugzillaReporter reporter = BugReporterFactory.getDefaultReporter();
List<Integer> issueIds = PersistenceUtils.executeQuery(em, QUERY, null);
javax.persistence.Query reportQuery = em.createNamedQuery("Report.findByIssueId");
for (Integer issueId : issueIds) {
Report rootReport = null;
reportQuery.setParameter("issueId", issueId);
Issue issue = reporter.getIssue(issueId);
List<Report> reports = reportQuery.getResultList();
//find report referenced from IZ
if (issue == null) {
SYNCHRONIZATION_LOGGER.log(Level.SEVERE, "Impossible to find issue #{0}", issueId.toString());
continue;
}
Integer reporterSubmitId = issue.getReporterSubmitId();
if (reporterSubmitId != null) {
for (Report report : reports) {
if (reporterSubmitId.equals(report.getId())) {
rootReport = report;
break;
}
}
if (rootReport == null) {
//workaround for old links from issuezilla
if (reporterSubmitId < 146000) {
Exceptions exc = em.getReference(Exceptions.class, reporterSubmitId);
if (exc != null) {
rootReport = exc.getReportId();
} else {
SYNCHRONIZATION_LOGGER.log(Level.SEVERE, "Impossible to find root report {0}", reporterSubmitId.toString());
continue;
}
}
}
} else {
// the root report was not filled using exception reporter
rootReport = reports.iterator().next();
}
reports.remove(rootReport);
// move all exceptions to the referenced report
for (Report report : reports) {
SYNCHRONIZATION_LOGGER.log(Level.INFO, "merging report {0} to report {1}", new Object[]{report.getId().toString(), rootReport.getId().toString()});
report.preloadSubmitCollection(em);
for (Submit sub : report.getSubmitCollection()) {
sub.setReportId(rootReport);
em.merge(sub);
}
ReportComment rc = new ReportComment();
rc.generateId();
rc.setReportId(report);
String message = String.format(MARKED_AS_DUPLICATE, rootReport.getId());
rc.setComment(message);
rc.setNbuserId(getReporterUser(em));
em.persist(rc);
}