private void doItButton_Click(object sender, EventArgs e)
{
if (!addRadioButton.Checked && propertiesCheckedListBox.CheckedItems.Count == 0 && source2CheckedListBox.CheckedItems.Count == 0 && source3CheckedListBox.CheckedItems.Count == 0) throw new Exception("No source selected");
if (destinationCheckedListBox.CheckedItems.Count == 0) throw new Exception("No destination selected");
int reportCnt = 0, itemCnt = 0;
List<Report> reportsToSave = new List<Report>();
foreach (var destinationObject in destinationCheckedListBox.CheckedItems.OfType<PropertyItem>().Where(i => i.Object != null))
{
itemCnt++;
//Source is Report Model
if (_source is ReportModel)
{
ReportModel modelSource = _source as ReportModel;
if (addRadioButton.Checked)
{
//Add models to reports
Report report = destinationObject.Object as Report;
ReportModel modelDestination = (ReportModel)Helper.Clone(modelSource);
report.Models.Add(modelDestination);
ReportSource newSource = report.Sources.FirstOrDefault(i => i.MetaSourceGUID == modelSource.Source.MetaSourceGUID);
if (newSource != null) modelDestination.SourceGUID = newSource.GUID;
report.InitReferences();
modelDestination.GUID = Guid.NewGuid().ToString();
modelDestination.Name = Helper.GetUniqueName(modelSource.Name, (from i in report.Models select i.Name).ToList());
foreach (var item in modelDestination.Elements) item.GUID = Guid.NewGuid().ToString();
foreach (var item in modelDestination.Restrictions)
{
string oldGUID = item.GUID;
item.GUID = Guid.NewGuid().ToString();
modelDestination.Restriction = modelDestination.Restriction.Replace(oldGUID, item.GUID);
}
if (!reportsToSave.Contains(report)) reportsToSave.Add(report);
}
else
{
//Update models
ReportModel model = destinationObject.Object as ReportModel;
if (!reportsToSave.Contains(model.Report)) reportsToSave.Add(model.Report);
foreach (var property in propertiesCheckedListBox.CheckedItems.OfType<PropertyItem>().Where(i => i.Object != null))
{
PropertyDescriptor descriptor = property.Object as PropertyDescriptor;
if (descriptor != null) descriptor.SetValue(model, descriptor.GetValue(_source));
model.InitReferences();
}
//elements
foreach (var elementItem in source2CheckedListBox.CheckedItems.OfType<PropertyItem>().Where(i => i.Object != null))
{
ReportElement elementSource = elementItem.Object as ReportElement;
//Select the best element to replace...
ReportElement elementBefore = model.Elements.FirstOrDefault(i => i.MetaColumnGUID == elementSource.MetaColumnGUID && i.PivotPosition == elementSource.PivotPosition && i.DisplayNameEl == elementSource.DisplayNameEl);
if (elementBefore == null) elementBefore = model.Elements.FirstOrDefault(i => i.MetaColumnGUID == elementSource.MetaColumnGUID && i.PivotPosition == elementSource.PivotPosition);
if (elementBefore == null) elementBefore = model.Elements.FirstOrDefault(i => i.MetaColumnGUID == elementSource.MetaColumnGUID);
int position = -1;
if (elementBefore != null)
{
position = model.Elements.IndexOf(elementBefore);
model.Elements.Remove(elementBefore);
}
ReportElement elementDestination = (ReportElement)Helper.Clone(elementSource);
elementDestination.GUID = (elementBefore != null ? elementBefore.GUID : Guid.NewGuid().ToString());
if (position != -1) model.Elements.Insert(position, elementDestination);
else model.Elements.Add(elementDestination);
model.InitReferences();
}
//restrictions
bool restrictionTextCopied = source3CheckedListBox.CheckedIndices.Contains(1);
bool aggRestrictionTextCopied = source3CheckedListBox.CheckedIndices.Contains(2);
//copy restriction texts...
if (restrictionTextCopied) model.Restriction = ((ReportModel)_source).Restriction;
if (aggRestrictionTextCopied) model.AggregateRestriction = ((ReportModel)_source).AggregateRestriction;
foreach (var restrictionItem in source3CheckedListBox.CheckedItems.OfType<PropertyItem>().Where(i => i.Object != null))
{
ReportRestriction restrictionSource = restrictionItem.Object as ReportRestriction;
bool isAggregate = !((ReportModel)_source).Restrictions.Contains(restrictionSource);
List<ReportRestriction> restrictions = (isAggregate ? model.AggregateRestrictions : model.Restrictions);
//Select the best restriction to replace...
ReportRestriction restrictionBefore = restrictions.FirstOrDefault(i => i.MetaColumnGUID == restrictionSource.MetaColumnGUID && i.PivotPosition == restrictionSource.PivotPosition && i.DisplayNameEl == restrictionSource.DisplayNameEl);
if (restrictionBefore == null) restrictionBefore = restrictions.FirstOrDefault(i => i.MetaColumnGUID == restrictionSource.MetaColumnGUID && i.PivotPosition == restrictionSource.PivotPosition);
if (restrictionBefore == null) restrictionBefore = restrictions.FirstOrDefault(i => i.MetaColumnGUID == restrictionSource.MetaColumnGUID);
if (restrictionBefore != null) restrictions.Remove(restrictionBefore);
ReportRestriction restrictionDestination = (ReportRestriction)Helper.Clone(restrictionSource);
restrictionDestination.GUID = (restrictionBefore != null ? restrictionBefore.GUID : Guid.NewGuid().ToString());
restrictions.Add(restrictionDestination);
string restrictionText = (isAggregate ? model.AggregateRestriction : model.Restriction);
if (restrictionText == null) restrictionText = "";
if (aggRestrictionTextCopied || restrictionTextCopied) restrictionText = restrictionText.Replace(restrictionSource.GUID, restrictionDestination.GUID);
else if (restrictionBefore != null) restrictionText = restrictionText.Replace(restrictionBefore.GUID, restrictionDestination.GUID);
if (!restrictionText.Contains(restrictionDestination.GUID))
{
if (!string.IsNullOrWhiteSpace(restrictionText)) restrictionText += "\r\nAND ";
restrictionText += string.Format("[{0}]", restrictionDestination.GUID);
}
if (isAggregate) model.AggregateRestriction = restrictionText;
else model.Restriction = restrictionText;
model.InitReferences();
}
}
}
//Source is Report Restriction
else if (_source is ReportRestriction)
{
ReportModel modelSource = _source as ReportModel;
if (addRadioButton.Checked)
{
//Add restriction to model
ReportModel model = destinationObject.Object as ReportModel;
bool isAggregate = !((ReportRestriction)_source).Model.Restrictions.Contains(_source);
if (!reportsToSave.Contains(model.Report)) reportsToSave.Add(model.Report);
ReportRestriction restrictionDestination = (ReportRestriction)Helper.Clone(_source);
restrictionDestination.GUID = Guid.NewGuid().ToString();
if (isAggregate) model.AggregateRestrictions.Add(restrictionDestination);
else model.Restrictions.Add(restrictionDestination);
string restrictionText = (isAggregate ? model.AggregateRestriction : model.Restriction);
if (!string.IsNullOrWhiteSpace(restrictionText)) restrictionText += "\r\nAND ";
restrictionText += string.Format("[{0}]", restrictionDestination.GUID);
if (isAggregate) model.AggregateRestriction = restrictionText;
else model.Restriction = restrictionText;
model.InitReferences();
}
else
{
//Update restrictions
ReportRestriction restriction = destinationObject.Object as ReportRestriction;
if (!reportsToSave.Contains(restriction.Model.Report)) reportsToSave.Add(restriction.Model.Report);
foreach (var property in propertiesCheckedListBox.CheckedItems.OfType<PropertyItem>().Where(i => i.Object != null))
{
PropertyDescriptor descriptor = property.Object as PropertyDescriptor;
if (descriptor != null)
{
//Special case for enum values
if (restriction.IsEnum && descriptor.Name == "EnumValue")
{
restriction.EnumValues = ((ReportRestriction)_source).EnumValues.ToList();
}
else descriptor.SetValue(restriction, descriptor.GetValue(_source));
}
}
}
}
//Source is Report Element
else if (_source is ReportElement)
{
ReportModel modelSource = _source as ReportModel;
if (addRadioButton.Checked)
{
//Add elements to model
ReportModel model = destinationObject.Object as ReportModel;
if (!reportsToSave.Contains(model.Report)) reportsToSave.Add(model.Report);
ReportElement elementDestination = (ReportElement)Helper.Clone(_source);
elementDestination.GUID = Guid.NewGuid().ToString();
model.Elements.Add(elementDestination);
model.InitReferences();
}
else
{
//Update elements
ReportElement element = destinationObject.Object as ReportElement;
if (!reportsToSave.Contains(element.Model.Report)) reportsToSave.Add(element.Model.Report);
foreach (var property in propertiesCheckedListBox.CheckedItems.OfType<PropertyItem>().Where(i => i.Object != null))
{
PropertyDescriptor descriptor = property.Object as PropertyDescriptor;
if (descriptor != null) descriptor.SetValue(element, descriptor.GetValue(_source));
}
}
}
//Source is Report View
else if (_source is ReportView)
{
ReportView viewSource = _source as ReportView;
if (addRadioButton.Checked)
{
//Add views to a view
ReportView view = destinationObject.Object as ReportView;
if (!reportsToSave.Contains(view.Report)) reportsToSave.Add(view.Report);
ReportView viewDestination = (ReportView)Helper.Clone(_source);
var views = view.Views;
if (string.IsNullOrEmpty(view.Name))
{
//dummy view, add it to the report
views = view.Report.Views;
}
viewDestination.Name = Helper.GetUniqueName(viewSource.Name, (from i in views select i.Name).ToList());
views.Add(viewDestination);
view.Report.InitReferences();
viewDestination.GUID = Guid.NewGuid().ToString();
viewDestination.ReinitGUIDChildren();
}
else
{
//Update views
ReportView view = destinationObject.Object as ReportView;
if (!reportsToSave.Contains(view.Report)) reportsToSave.Add(view.Report);
foreach (var property in propertiesCheckedListBox.CheckedItems.OfType<PropertyItem>().Where(i => i.Object != null))
{
PropertyDescriptor descriptor = property.Object as PropertyDescriptor;
if (descriptor != null)
{
if (descriptor.Name == "GeneralParameters")
{
foreach (var item in viewSource.GeneralParameters)
{
var param = view.GeneralParameters.FirstOrDefault(i => i.Name == item.Name);
if (param != null) param.Value = item.Value;
else view.Parameters.Add(new Parameter() { Name = item.Name, Value = item.Value });
}
}
else if (descriptor.Name == "DataTableParameters")
{
foreach (var item in viewSource.DataTableParameters)
{
var param = view.DataTableParameters.FirstOrDefault(i => i.Name == item.Name);
if (param != null) param.Value = item.Value;
else view.Parameters.Add(new Parameter() { Name = item.Name, Value = item.Value });
}
}
else if (descriptor.Name == "CSS")
{
foreach (var item in viewSource.CSS)
{
var param = view.CSS.FirstOrDefault(i => i.Name == item.Name);
if (param != null) param.Value = item.Value;
else view.CSS.Add(new Parameter() { Name = item.Name, Value = item.Value });
}
}
else if (descriptor.Name == "NVD3Parameters")
{
foreach (var item in viewSource.NVD3Parameters)
{
var param = view.NVD3Parameters.FirstOrDefault(i => i.Name == item.Name);
if (param != null) param.Value = item.Value;
else view.NVD3Parameters.Add(new Parameter() { Name = item.Name, Value = item.Value });
}
}
else if (descriptor.Name == "PdfConverter")
{
view.PdfConverter = null;
view.PdfConfigurations = viewSource.PdfConfigurations.ToList();
}
else if (descriptor.Name == "ExcelConverter")
{
view.ExcelConverter = null;
view.ExcelConfigurations = viewSource.ExcelConfigurations.ToList();
}
else
{
if (descriptor.Name == "ChartConfigurationXml") view.ResetChartConfiguration();
descriptor.SetValue(view, descriptor.GetValue(_source));
}
}
}
//parameters
foreach (var item in source2CheckedListBox.CheckedItems.OfType<PropertyItem>().Where(i => i.Object != null))
{
Parameter sourceParameter = item.Object as Parameter;
Parameter parameter = view.Parameters.FirstOrDefault(i => i.Name == sourceParameter.Name);
if (parameter != null) parameter.Value = sourceParameter.Value;
else view.Parameters.Add(new Parameter() { Name = sourceParameter.Name, Value = sourceParameter.Value });
}
//CSS
foreach (var item in source3CheckedListBox.CheckedItems.OfType<PropertyItem>().Where(i => i.Object != null))
{
Parameter sourceCSS = item.Object as Parameter;
Parameter css = view.CSS.FirstOrDefault(i => i.Name == sourceCSS.Name);
if (css != null) css.Value = sourceCSS.Value;
else view.CSS.Add(new Parameter() { Name = sourceCSS.Name, Value = sourceCSS.Value });
}
}
}
//Source is Report Task
else if (_source is ReportTask)
{
ReportTask taskSource = _source as ReportTask;
if (addRadioButton.Checked)
{
//Add tasks to report
Report report = destinationObject.Object as Report;
if (!reportsToSave.Contains(report)) reportsToSave.Add(report);
ReportTask taskDestination = (ReportTask)Helper.Clone(_source);
taskDestination.GUID = Guid.NewGuid().ToString();
taskDestination.Name = Helper.GetUniqueName(taskSource.Name, (from i in report.Tasks select i.Name).ToList());
report.Tasks.Add(taskDestination);
report.InitReferences();
}
else
{
//Update tasks
ReportTask task = destinationObject.Object as ReportTask;
if (!reportsToSave.Contains(task.Report)) reportsToSave.Add(task.Report);
foreach (var property in propertiesCheckedListBox.CheckedItems.OfType<PropertyItem>().Where(i => i.Object != null))
{
PropertyDescriptor descriptor = property.Object as PropertyDescriptor;
if (descriptor != null)
{
descriptor.SetValue(task, descriptor.GetValue(_source));
}
}
}
}
//Source is Report Output
else if (_source is ReportOutput)
{
ReportOutput outputSource = _source as ReportOutput;
if (addRadioButton.Checked)
{
//Add outputs to report
Report report = destinationObject.Object as Report;
if (!reportsToSave.Contains(report)) reportsToSave.Add(report);
ReportOutput outputDestination = (ReportOutput)Helper.Clone(_source);
outputDestination.GUID = Guid.NewGuid().ToString();
outputDestination.Name = Helper.GetUniqueName(outputSource.Name, (from i in report.Outputs select i.Name).ToList());
report.Outputs.Add(outputDestination);
//assign view to the report, by name ??
var newView = report.Views.FirstOrDefault(i => i.Name == outputSource.View.Name);
if (newView == null) newView = report.Views.FirstOrDefault();
if (newView != null) outputDestination.ViewGUID = newView.GUID;
report.InitReferences();
}
else
{
//Update outputs
ReportOutput output = destinationObject.Object as ReportOutput;
if (!reportsToSave.Contains(output.Report)) reportsToSave.Add(output.Report);
foreach (var property in propertiesCheckedListBox.CheckedItems.OfType<PropertyItem>().Where(i => i.Object != null))
{
PropertyDescriptor descriptor = property.Object as PropertyDescriptor;
if (descriptor != null)
{
if (descriptor.Name == "Restrictions")
{
output.Restrictions = (List<ReportRestriction>)Helper.Clone(outputSource.Restrictions);
}
else
{
descriptor.SetValue(output, descriptor.GetValue(_source));
}
}
}
}
}
//Source is Report TasksFolder
else if (_source is TasksFolder)
{
//Update report tasksScript
Report reportDestination = destinationObject.Object as Report;
if (!reportsToSave.Contains(reportDestination)) reportsToSave.Add(reportDestination);
reportDestination.TasksScript = _report.TasksScript;
}
}
foreach (var report in reportsToSave)
{
if (report == _report) IsReportModified = true;
else
{
try
{
report.SaveToFile();
reportCnt++;
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Error saving report {0}:\r\n{1}", report.FilePath, ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
DialogResult = DialogResult.OK;
Close();
string message = string.Format("{0} item(s) modified.", itemCnt);
if (reportCnt > 0) message += string.Format("\r\n{0} report(s) modified and saved.", reportCnt);
MessageBox.Show(message, "Smart Copy", MessageBoxButtons.OK, MessageBoxIcon.Information);
}