}
public long save(Report report) throws CruxException {
if (report != null) {
long id = report.getId();
Report foundReport = null;
ArrayList<ReportDesign> oldDesignList = null;
ArrayList<RowAliasFilter> oldRowFilter = null;
ArrayList<ColumnFilter> oldColumnFilter = null;
if (id != 0) {
try {
foundReport = this.findById(id);
} catch (CruxException e) {
throw new CruxException(
"Report you are trying to edit does not exists in database",
e);
}
oldDesignList = new ArrayList<ReportDesign>(
foundReport.getDesigns());
oldRowFilter = new ArrayList<RowAliasFilter>(
foundReport.getRowAliasFilters());
oldColumnFilter = new ArrayList<ColumnFilter>(
foundReport.getColumnFilters());
foundReport.getDesigns().clear();
foundReport.getRowAliasFilters().clear();
foundReport.getColumnFilters().clear();
for (ReportDesign design : oldDesignList) {
try {
transaction.begin();
session.delete(design);
transaction.commit();
} catch (JDBCException e) {
transaction.rollback();
throw new CruxException(e.getSQLException()
.getMessage(), e);
}
}
for (RowAliasFilter rowFilter : oldRowFilter) {
try {
transaction.begin();
session.delete(rowFilter);
transaction.commit();
} catch (JDBCException e) {
transaction.rollback();
throw new CruxException(e.getSQLException()
.getMessage(), e);
}
}
for (ColumnFilter columnFilter : oldColumnFilter) {
try {
transaction.begin();
session.delete(columnFilter);
transaction.commit();
} catch (JDBCException e) {
transaction.rollback();
throw new CruxException(e.getSQLException()
.getMessage(), e);
}
}
ArrayList<ReportDesign> newDesignList = new ArrayList<ReportDesign>(
report.getDesigns());
for (ReportDesign design : newDesignList) {
design.setReport(foundReport);
}
ArrayList<RowAliasFilter> newRowFilter = new ArrayList<RowAliasFilter>(
report.getRowAliasFilters());
for (RowAliasFilter rowFilter : newRowFilter) {
rowFilter.setReport(foundReport);
}
ArrayList<ColumnFilter> newColumnFilter = new ArrayList<ColumnFilter>(
report.getColumnFilters());
for (ColumnFilter columnFilter : newColumnFilter) {
columnFilter.setReport(foundReport);
}
foundReport.setColumnFilters(newColumnFilter);
foundReport.setRowAliasFilters(newRowFilter);
foundReport.setDesigns(newDesignList);
foundReport.setName(report.getName());
foundReport.setReportType(report.getReportType());
foundReport.setUser(report.getUser());
foundReport.setDashboard(report.getDashboard());
foundReport.setNumRecordsPerPage(report.getNumRecordsPerPage());
return saveReport(foundReport);
}
return saveReport(report);
} else {