Files
ayanova7/source/WinFormApp/ReportEditor.cs
2018-06-29 19:47:36 +00:00

569 lines
20 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using log4net;
using GZTW.AyaNova.BLL;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.UserDesigner;
using System.IO;
using System.ComponentModel.Design;
using DevExpress.XtraReports.UserDesigner.Native;
using System.Drawing.Design;
namespace AyaNova
{
public partial class ReportEditor : Form
{
// Create a logger for use in this class
//case 1039 private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public ReportEditor(Report rpt, ReportDataSet DataSource)
{
InitializeComponent();
xrDesignPanel1.AddCommandHandler(new SaveCommandHandler(xrDesignPanel1));
Util.Localize(this);
mReport = rpt;
if (DataSource != null)
mData = DataSource;
else
throw (new ApplicationException("DataSource==null (rpt=" + rpt.Name + "[" + rpt.ReportKey + "]) in ReportEditor"));
}
// __ _ _ ___ ____ __ __ __ __ __ ___ ___
// / _)( )( )/ __)(_ _)/ \( \/ ) / _)/ \( \( _)
// ( (_ )()( \__ \ )( ( () )) ( ( (_( () )) ) )) _)
// \__) \__/ (___/ (__) \__/(_/\/\_) \__)\__/(___/(___)
//
#region global vars
private Report mReport;
private ReportDataSet mData;
//variable to hold user form settings
//UIUserFormSetting mFormSetting;
bool bClosingHandled = false;
XtraReport mxr = null;
#endregion
#region Load / close
private void ReportEditor_Load(object sender, EventArgs e)
{
this.Icon = Resource1.ReportTemplate16icon;
//See if user has any rights to be here...
//(this is done here to ensure that no matter what happens if a user has no rights they can't get here
//useful in case we miss a way of getting here from elsewhere in the program)
if (AyaBizUtils.Right("Object.Report") < (int)SecurityLevelTypes.ReadWrite)//Less than read/write because many may have read, but few may be able to edit reports
{
//Inform them of their wicked ways and boot them out of here...
//log.Warn("NotAuthorized - exiting form");
MessageBox.Show(string.Format(
Util.LocaleText.GetLocalizedText("Error.Security.NotAuthorizedToRetrieve"),
Util.LocaleText.GetLocalizedText("O.Report")));
this.Close();
return;
}
//Case 238
XtraReport.FilterControlProperties += new FilterControlPropertiesEventHandler(OnFilterControlProperties);
Util.LoadFormCustomization("ReportEditor", this, null, false);
InitializeReport(false);
commandBarItem46.Enabled = true;
}
private void ReportEditor_FormClosing(object sender, FormClosingEventArgs e)
{
if (!bClosingHandled)
{
//Save record if necessary
//User may opt to not cancel exit
if (!RecordUpdate(RecordActionType.PromptToSave))
{
e.Cancel = true;
return;
}
}
Util.SaveFormCustomization("ReportEditor", this, null, false);
}
#endregion
#region designer events
bool bReportLayoutIsDirty = false;
private void xrDesignPanel1_ReportStateChanged(object sender, DevExpress.XtraReports.UserDesigner.ReportStateEventArgs e)
{
// this.Text=e.ReportState.ToString();
bReportLayoutIsDirty = (e.ReportState == ReportState.Changed);
commandBarItem33.Enabled = bReportLayoutIsDirty;
}
#endregion
#region Initialize report
/// <summary>
/// Gets report from business object
/// sets it in the UI and prepares UI for editing
/// </summary>
private void InitializeReport(bool ReportIsANewDuplicate)
{
//Case 257 added parameter ReportIsANewDuplicate to indicate that though report isnew is true
//it's not actually new in the sense that's important here
//case 1039 //log.Debug("InitializeReport");
Cursor.Current = Cursors.WaitCursor;
if (mReport.IsNew && mReport.ReportSize == 0)
{
//case 1039 //log.Debug("InitializeReport: New report");
mxr = new XtraReport();
}
else
{
//case 1039 //log.Debug("InitializeReport: Load report");
mxr = new XtraReport();
//Case 232
//mxr.LoadState(mReport.GetReportContent());
mxr.LoadLayout(mReport.GetReportContent());
}
#region Case 232 set data source
//Case 232
//complete change of how data source is set
//mxr.DataSource=this.mData;
if (mData.Tables.Count == 1)
{
//single table new or existing report
//It's a summary report or it's new so just bind to the report as normal
mxr.DataSource = this.mData;
//Set the datamember to the first table
//in a summary this is the only table
//in a detailed with multiple tables this is AFAIK the root table
mxr.DataMember = this.mData.Tables[0].TableName;
}
else
{
if (mReport.IsNew && !ReportIsANewDuplicate)//Case 257
{
//multi table new report, add detailereportband and set data to it
//report header
mxr.Bands.Add(new ReportHeaderBand());
//detail
DetailBand dtTop = new DetailBand();
dtTop.Height = 0;
mxr.Bands.Add(dtTop);
//reportdetail
DetailReportBand dtReportDetail = new DetailReportBand();
mxr.Bands.Add(dtReportDetail);
dtReportDetail.Bands.Add(new DetailBand());
dtReportDetail.DataSource = this.mData;
dtReportDetail.DataMember = this.mData.Tables[0].TableName;
}
else
{
//It's an existing multitable report
//remove the datasource
//from the main report and set it on the detailereport band
mxr.DataSource = null;
DetailReportBand detailReport = mxr.Bands[BandKind.DetailReport] as DetailReportBand;
if (detailReport != null)
detailReport.DataSource = this.mData;
}
}
#endregion
//case 1468
mxr.ScriptReferences = AyaBizUtils.GetBizObjectLibraryDllPaths();
mxr.DesignerLoaded += new DesignerLoadedEventHandler(r_DesignerLoaded);
if (AyaBizUtils.Trial)
Util.WatermarkReport(mxr, true);
//mxr.ExportOptions.Email.RecipientAddress = strEmail;
xrDesignPanel1.OpenReport(mxr); //24 or 30 second delay right here when initialize is called
this.Text = mReport.Name;
}
#endregion
#region record update handling
//Used to signal to main form that there are changes
//which could affect it if true
private bool mbChangesMade = false;
public bool ChangesMade
{
get
{
return mbChangesMade;
}
}
/// <summary>
/// Update record and quit if requested
///
/// </summary>
/// <returns>True if handled, false if not handled</returns>
private bool RecordUpdate(RecordActionType SaveType)
{
Cursor.Current = Cursors.WaitCursor;
//if (log.IsDebugEnabled) //case 1039 //log.Debug("SaveHandler(Action=" + SaveType.ToString() + ")");
switch (SaveType)
{
case RecordActionType.DeleteAndExit:
if (Util.PromptForDelete() == DialogResult.Yes)
{
//Delete, then exit
try
{
Report.DeleteItem(mReport.ID);
}
catch (Exception ex)
{
Util.ReportSQLError(ex);
return false;
}
bClosingHandled = true;
mbChangesMade = true;
this.Close();
return true;
}
else return false;
case RecordActionType.SaveAndExit:
if (bReportLayoutIsDirty)
{
MemoryStream mStream = new MemoryStream();
if (AyaBizUtils.Trial) Util.WatermarkReport(mxr, false);
//Case 232
//xrDesignPanel1.Report.SaveState(mStream);
xrDesignPanel1.Report.SaveLayout(mStream);
mReport.SetReportContent(mStream);
//Save if necessary and exit
if (this.mReport.IsSavable)
{
mReport.Save();
xrDesignPanel1.ReportState = ReportState.Saved;
bClosingHandled = true;
mbChangesMade = true;
this.Close();
return true;
}
if (mReport.IsDirty)//dirty and unsaveable due to broken rules
{
if (Util.PromptForBrokenRulesCancelSave() == DialogResult.Yes)
{
bClosingHandled = true;
this.Close();
return true;
}
else
return false;
}
}
//not dirty so just exit
bClosingHandled = true;
this.Close();
return true;
case RecordActionType.SaveOnly:
if (bReportLayoutIsDirty)
{
MemoryStream mStream = new MemoryStream();
if (AyaBizUtils.Trial) Util.WatermarkReport(mxr, false);
//Case 232
//xrDesignPanel1.Report.SaveState(mStream);
xrDesignPanel1.Report.SaveLayout(mStream);
mReport.SetReportContent(mStream);
if (mReport.IsSavable)
{
mReport = (Report)mReport.Save();
xrDesignPanel1.ReportState = ReportState.Saved;
mbChangesMade = true;
return true;
}
}
return true;
case RecordActionType.PromptToSave:
//Prompt to save and save if
//required
if (bReportLayoutIsDirty || mReport.IsDirty)
{
DialogResult dr = Util.PromptForSave();
if (dr == DialogResult.Cancel)
{
//Cancel
return false;
}
if (dr == DialogResult.Yes)
{
//Save before exit
MemoryStream mStream = new MemoryStream();
if (AyaBizUtils.Trial) Util.WatermarkReport(mxr, false);
//Case 232
//xrDesignPanel1.Report.SaveState(mStream);
xrDesignPanel1.Report.SaveLayout(mStream);
mReport.SetReportContent(mStream);
if (mReport.IsSavable)
{
mReport.Save();
xrDesignPanel1.ReportState = ReportState.Saved;
mbChangesMade = true;
return true;
}
if (mReport.IsDirty)//dirty and unsaveable due to broken rules
{
if (Util.PromptForBrokenRulesCancelSave() == DialogResult.Yes)
{
return true;
}
else
return false;
}
}
}
return true;
}
return false;
}
#endregion
#region customize design components
private void r_DesignerLoaded(object sender, DesignerLoadedEventArgs e)
{
IMenuCommandService CommandServ = e.DesignerHost.GetService(typeof(IMenuCommandService)) as IMenuCommandService;
MenuCommand cmd = CommandServ.FindCommand(UICommands.Closing);
if (cmd != null)
CommandServ.RemoveCommand(cmd);
//Case 238
XRDesignPanel panel = e.DesignerHost.GetService(typeof(XRDesignPanel)) as XRDesignPanel;
//Hide Wizard if multi table report data source
//since the wizard doesn't work with it anyway
//CommandVisibility viz= panel.GetCommandVisibility(ReportCommand.VerbReportWizard);
if (mData.Tables.Count > 1)
panel.SetCommandVisibility(ReportCommand.VerbReportWizard, CommandVisibility.None);
fieldListDockPanel1.ShowParametersNode = false;
//case 1410
IToolboxService ts = (IToolboxService)e.DesignerHost.GetService(typeof(IToolboxService));
List<ToolboxItem> itemsToRemove = new List<ToolboxItem>();
foreach (ToolboxItem item in ts.GetToolboxItems())
{
if (ShouldRemoveItem(item.TypeName))
itemsToRemove.Add(item);
}
foreach (ToolboxItem item in itemsToRemove)
{
ts.RemoveToolboxItem(item);
}
}
//case 1410
private bool ShouldRemoveItem(string typeName)
{
return typeName.Contains("XRPivotGrid") || typeName.Contains("XRCrossBandLine") ||
typeName.Contains("XRChart") || typeName.Contains("XRSubreport") || typeName.Contains("XRCrossBandBox");
}
//Case 238
void OnFilterControlProperties(object sender, FilterControlPropertiesEventArgs e)
{
if (e.Control is DevExpress.XtraReports.UI.XtraReport)
{
if (e.Properties.Contains("DataSource"))
e.Properties.Remove("DataSource");
if (e.Properties.Contains("DataAdapter"))
e.Properties.Remove("DataAdapter");
if (e.Properties.Contains("DataMember"))
e.Properties.Remove("DataMember");
if (e.Properties.Contains("DataSourceSchema"))
e.Properties.Remove("DataSourceSchema");
if (e.Properties.Contains("XmlDataPath"))
e.Properties.Remove("XmlDataPath");
}
}
#endregion
#region menu items
//Save
private void commandBarItem33_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//case 1039 //log.Debug("mnuSaveLayout_Click");
// MessageBox.Show("ReportCommand.VerbReportWizard visibility is currently: " + this.xrDesignPanel1.GetCommandVisibility(ReportCommand.VerbReportWizard).ToString());
RecordUpdate(RecordActionType.SaveOnly);
}
//Save as
private void commandBarItem40_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//case 1039 //log.Debug("mnuSaveLayoutAs_Click");
if (!RecordUpdate(RecordActionType.PromptToSave))
return;
ReportDesignerSaveAsDialog d = new ReportDesignerSaveAsDialog();
d.SelectedName = Util.LocaleText.GetLocalizedText("UI.Label.CopyOfText") + " " + this.mReport.Name;
d.ReportKey = mReport.ReportKey;
DialogResult result = d.ShowDialog();
if (result == DialogResult.Cancel) return;
//create new duplicate copy of existing report object
Report rptOld = this.mReport;
try
{
mReport = GZTW.AyaNova.BLL.Report.NewItem();
mReport.Name = d.SelectedName;
mReport.ReportKey = rptOld.ReportKey;
mReport.ReportSize = rptOld.ReportSize;
mReport.SetReportContent(rptOld.GetReportContent());
//Case 257 added boolean to indicate it's a copy of an existing report
InitializeReport(true);
xrDesignPanel1.ReportState = ReportState.Changed;
}
catch (Exception ex)
{
//log.Error("mnuSaveLayoutAs_Click", ex);
MessageBox.Show(ex.Message + "\r\n" + ex.Source);
}
}
//Delete report
private void commandBarItem46_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//case 1039 //log.Debug("mnuDelete_Click");
RecordUpdate(RecordActionType.DeleteAndExit);
}
#endregion
private void mnuDeleteReport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
RecordUpdate(RecordActionType.DeleteAndExit);
}
//------------------------------------
}
#region Custom command handler to disable save prompt etc
public class SaveCommandHandler : ICommandHandler
{
XRDesignPanel panel;
public SaveCommandHandler(XRDesignPanel panel)
{
this.panel = panel;
}
public virtual void HandleCommand(ReportCommand command, object[] args, ref bool handled)
{
if (!CanHandleCommand(command)) return;
// Save a report.
//Save();
// Set handled to true to avoid the standard saving procedure to be called.
handled = true;
}
public virtual bool CanHandleCommand(ReportCommand command)
{
// This handler is used for SaveFile, SaveFileAs and Closing commands.
return command == ReportCommand.SaveFile ||
command == ReportCommand.SaveFileAs ||
command == ReportCommand.Closing;
}
//void Save()
//{
// // Write your custom saving here.
// // ...
// // For instance:
// panel.Report.SaveLayout("c:\\report1.repx");
// // Prevent the "Report has been changed" dialog from being shown.
// panel.ReportState = ReportState.Saved;
//}
}
#endregion
}