UserObject userobject = (UserObject)session.getAttribute("userobject");
String timeZone = userobject.getUserPref().getTimeZone();
int userId = userobject.getIndividualID();
int reportId = getTheId("reportId", request);
DynaActionForm adHocReportForm = (DynaActionForm)form;
ReportVO reportVO = null;
ReportFacade remote = getReportFacade();
// searchCriteria should always have at least one row.
SearchCriteriaVO[] searchCriteria = (SearchCriteriaVO[])adHocReportForm.get("searchCriteria");
String addRow = (String)adHocReportForm.get("addRow");
String removeRow = (String)adHocReportForm.get("removeRow");
// we may just be doing search criteria row manipulation.
if (addRow.equals("true")) {
searchCriteria = AdvancedSearchUtil.addRow(searchCriteria);
adHocReportForm.set("addRow", "false");
adHocReportForm.set("searchCriteria", searchCriteria);
reportVO = remote.getAdHocReport(userId, reportId);
reportVO.setSelectedFields(getSelectedFieldsWithNames((String)adHocReportForm.get("contentFields"), (String)adHocReportForm.get("contentOrders"), (String)adHocReportForm.get("contentFieldNames")));
request.setAttribute("pagedata", reportVO);
} else if (!removeRow.equals("false")) {
searchCriteria = AdvancedSearchUtil.removeRow(searchCriteria, removeRow);
adHocReportForm.set("removeRow", "false");
adHocReportForm.set("searchCriteria", searchCriteria);
reportVO = remote.getAdHocReport(userId, reportId);
reportVO.setSelectedFields(getSelectedFieldsWithNames((String)adHocReportForm.get("contentFields"), (String)adHocReportForm.get("contentOrders"), (String)adHocReportForm.get("contentFieldNames")));
request.setAttribute("pagedata", reportVO);
} else { // So it isn't search criteria row manipulation.
// if action is null, it means we are displaying the form for editing,
// otherwise we are going to save and possibly run the report.
String action = request.getParameter("action");
if (action == null) {
String showFields = (String)adHocReportForm.get("showFields");
String createNew = (String)adHocReportForm.get("createNew");
if (createNew != null && createNew.equals("true")) {
adHocReportForm.initialize(mapping);
}
reportVO = remote.getAdHocReport(userId, reportId);
// I guess showFields is used to determine if the user edited any of the fields, that are on the form
// but not yet in the database. So we know to pull the report stuff from the DB
// and then change the selected fields based on the current form.
// it is initially "false" in the struts config
if (!showFields.equals("true")) {
// if it isn't true we update the form with the info from the reportVO from the database.
adHocReportForm = (DynaActionForm)getAdHocReportFormFromReportVO(reportVO, adHocReportForm, request);
} else {
// otherwise we analyze the fields from the form and show that.
reportVO.setSelectedFields(getSelectedFieldsWithNames((String)adHocReportForm.get("contentFields"), (String)adHocReportForm.get("contentOrders"), (String)adHocReportForm.get("contentFieldNames")));
}
moduleId = reportVO.getModuleId();
request.setAttribute("pagedata", reportVO);
} else { // action is telling us to do something for real, check for errors.
ActionErrors errors = adHocReportForm.validate(mapping, request);
if (0 < errors.size()) {
saveErrors(request, errors);
reportVO = remote.getAdHocReport(userId, reportId);
moduleId = reportVO.getModuleId();
request.setAttribute("pagedata", reportVO);
} else { // the form passed validation
// So we need to build a reportVO based on the form.
// and either save a new one to the database (if we are duplicate)
// or update the existing one in the database.
reportVO = getAdHocReportVOFromForm(timeZone, adHocReportForm);
// I guess "copy" is used for duplicate? So if copy is true we will
// be creating a new one, otherwise we will be updating.
if (((Boolean)adHocReportForm.get("copy")).booleanValue()) {
reportId = remote.addAdHocReport(userId, reportVO);
reportVO.setReportId(reportId);
} else {
remote.updateAdHocReport(userId, reportVO);
}
if (action.equals("save")) {
moduleId = reportVO.getModuleId();
// It may be better to actually store of the Selects and
// whatever else outside the VO on the formbean, so we can
// not have to round trip here. But for now we need to get the
// reportVO from the EJB layer because it has the field definitions in it.
reportVO = remote.getAdHocReport(userId, reportId);