public static void MergeDataSources(DataSourceCollection fromDataSources, DashboardMerger dashboardMerger) { DataSourceCollection toDataSources = dashboardMerger.TargetDashboard.DataSources; foreach (IDashboardDataSource dataSource in fromDataSources) { AddDataSourceCopy(dataSource, dashboardMerger, (dataSourceCopy) => { toDataSources.Add(dataSourceCopy); }); } }
public static void MergeGroups(DashboardItemGroupCollection fromGroups, DashboardMerger dashboardMerger) { DashboardItemGroupCollection toGroups = dashboardMerger.TargetDashboard.Groups; IList <DashboardItem> newItems = dashboardMerger.NewItems; foreach (DashboardItemGroup group in fromGroups) { AddGroupCopy(group, dashboardMerger, (groupCopy) => { toGroups.Add(groupCopy); newItems.Add(groupCopy); }); } }
public static void MergeItems(DashboardItemCollection fromItems, DashboardMerger dashboardMerger) { DashboardItemCollection toItems = dashboardMerger.TargetDashboard.Items; IList <DashboardItem> newItems = dashboardMerger.NewItems; foreach (DashboardItem dashboardItem in fromItems) { AddItemCopy(dashboardItem, dashboardMerger, (dashboardItemCopy) => { toItems.Add(dashboardItemCopy); newItems.Add(dashboardItemCopy); }); } }
static void AddParamterCopy(DashboardParameter originalParameter, DashboardMerger dashboardMerger, Action <DashboardParameter> addParameterDelegate) { DashboardParameter parameterCopy = (DashboardParameter)originalParameter.Clone(); DashboardParameterCollection toParameters = dashboardMerger.TargetDashboard.Parameters; if (toParameters.Any(p => p.Name == parameterCopy.Name)) { if (ResolveParameterNamesConflict(parameterCopy, originalParameter.Name, dashboardMerger)) { addParameterDelegate(parameterCopy); } } else { addParameterDelegate(parameterCopy); } }
static void AddGroupCopy(DashboardItemGroup originalGroup, DashboardMerger dashboardMerger, Action <DashboardItemGroup> addGroupDelegate) { DashboardItemGroupCollection toGroups = dashboardMerger.TargetDashboard.Groups; DashboardItemGroup groupCopy = CreateGroupCopy(originalGroup); if (toGroups.Any(g => g.ComponentName == originalGroup.ComponentName)) { if (ResolveGroupNamesConflict(groupCopy, originalGroup.ComponentName, toGroups, dashboardMerger.GroupNamesMap)) { addGroupDelegate(groupCopy); } } else { addGroupDelegate(groupCopy); } }
static void AddDataSourceCopy(IDashboardDataSource dataSource, DashboardMerger dashboardMerger, Action <IDashboardDataSource> addDataSourceDelegate) { DataSourceCollection toDataSources = dashboardMerger.TargetDashboard.DataSources; IDictionary <string, string> dataSourceNamesMap = dashboardMerger.DataSourceNamesMap; IDashboardDataSource dataSourceCopy = CreateDataSourceCopy(dataSource); if (dataSourceCopy != null) { if (toDataSources.Any(d => d.ComponentName == dataSourceCopy.ComponentName)) { if (ResolveNamesConflict(dataSourceCopy, toDataSources, dataSourceNamesMap)) { addDataSourceDelegate(dataSourceCopy); } } else { addDataSourceDelegate(dataSourceCopy); } } }
// The method that performs the merge. void MergeDashboard(DashboardToolbarItemClickEventArgs args) { // Invoke a file dialog to select mutiple dashboards to merge. // If one or several selected dashboards have tabbed layout and cannot be merged, a message is shown. OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Dashboard files (*.xml)|*.xml"; openFileDialog.Multiselect = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { dashboardDesigner.Dashboard.BeginUpdate(); try { List <string> rejectedDashboard = new List <string>(); foreach (string fileName in openFileDialog.FileNames) { using (Dashboard dashboard = new Dashboard()) { dashboard.LoadFromXml(fileName); // The DashboardMerger instance is the key object that performs the merge. DashboardMerger dashboardMerger = new DashboardMerger(dashboardDesigner.Dashboard); if (!dashboardMerger.MergeDashboard(dashboard)) { rejectedDashboard.Add(Path.GetFileName(fileName)); } } } if (rejectedDashboard.Count > 0) { MessageBox.Show(String.Format("Cannot merge the following dashboard(s): {0}{1}", Environment.NewLine, String.Join(Environment.NewLine, rejectedDashboard))); } if ((openFileDialog.FileNames.Length - rejectedDashboard.Count) > 0) { dashboardChanged = true; } } finally { dashboardDesigner.Dashboard.EndUpdate(); dashboardDesigner.ReloadData(); } } }
static void AddItemCopy(DashboardItem originalItem, DashboardMerger dashboardMerger, Action <DashboardItem> addItemDelegate) { DashboardItemCollection toItems = dashboardMerger.TargetDashboard.Items; IDictionary <string, string> dataSourceNamesMap = dashboardMerger.DataSourceNamesMap; DataSourceCollection existingDataSources = dashboardMerger.TargetDashboard.DataSources; DashboardItem dashboardItemCopy = originalItem.CreateCopy(); bool shouldAddItem = false; if (toItems.Any(item => item.ComponentName == originalItem.ComponentName)) { if (ResolveDashboardItemNameConflict(dashboardItemCopy, originalItem.ComponentName, toItems, dashboardMerger.DashboardItemNamesMap)) { shouldAddItem = true; } } else { dashboardItemCopy.ComponentName = originalItem.ComponentName; shouldAddItem = true; } if (shouldAddItem) { DataDashboardItem dataDashboardItem = dashboardItemCopy as DataDashboardItem; if (dataDashboardItem != null && dataDashboardItem.DataSource != null) { string newDataSourceName = String.Empty; if (!dataSourceNamesMap.TryGetValue(dataDashboardItem.DataSource.ComponentName, out newDataSourceName)) { newDataSourceName = dataDashboardItem.DataSource.ComponentName; } dataDashboardItem.DataSource = existingDataSources[newDataSourceName]; } addItemDelegate(dashboardItemCopy); } }
public static void MergeParameters(DashboardParameterCollection fromParameters, DashboardMerger dashboardMerger) { DashboardParameterCollection toParameters = dashboardMerger.TargetDashboard.Parameters; foreach (DashboardParameter parameter in fromParameters) { AddParamterCopy(parameter, dashboardMerger, (parameterCopy) => { toParameters.Add(parameterCopy); }); } }
static bool ResolveParameterNamesConflict(DashboardParameter parameterCopy, string originalName, DashboardMerger dashboardMerger) { // Provide your parameter name confilict resolution logic here parameterCopy.Name = NamesGenerator.GenerateName("RenamedParameter", 1, dashboardMerger.TargetDashboard.Parameters.Select(p => p.Name)); IEnumerable <DataDashboardItem> dataDashboardItems = dashboardMerger.TargetDashboard.Items.Where(item => item is DataDashboardItem).Cast <DataDashboardItem>(); string originalNamePattern = String.Format("?{0}", originalName); string copyNamePattern = String.Format("?{0}", parameterCopy.Name); foreach (DataDashboardItem item in dataDashboardItems) { if (!String.IsNullOrEmpty(item.FilterString) && item.FilterString.Contains(originalNamePattern)) { item.FilterString = item.FilterString.Replace(originalNamePattern, copyNamePattern); } } foreach (IDashboardDataSource dataSource in dashboardMerger.TargetDashboard.DataSources) { UpdateDataSourceParametersNames(dataSource, originalNamePattern, copyNamePattern); } return(true); }
public static void MergeLayout(DashboardLayoutGroup layoutRoot, string newPageName, DashboardMerger dashboardMerger) { TabContainerDashboardItem tabContainer = dashboardMerger.TabContainer; Dashboard targetDashboard = dashboardMerger.TargetDashboard; IDictionary <string, string> dashboardItemNamesMap = dashboardMerger.DashboardItemNamesMap; IDictionary <string, string> groupNamesMap = dashboardMerger.GroupNamesMap; IEnumerable <DashboardItem> newItems = dashboardMerger.NewItems; DashboardTabPage newTabPage = tabContainer.CreateTabPage(); DashboardLayoutTabPage layoutPage = new DashboardLayoutTabPage(newTabPage); foreach (DashboardLayoutNode node in layoutRoot.GetNodesRecursive()) { if (node.DashboardItem != null) { DashboardItemGroup group = node.DashboardItem as DashboardItemGroup; if (group != null) { string groupComponentName = group.ComponentName; string newGroupComponentName = String.Empty; if (!groupNamesMap.TryGetValue(group.ComponentName, out newGroupComponentName)) { newGroupComponentName = group.ComponentName; } node.DashboardItem = newItems.Single(itm => itm.ComponentName == newGroupComponentName); } else { DashboardItem item = node.DashboardItem; string newItemName = String.Empty; if (!dashboardItemNamesMap.TryGetValue(item.ComponentName, out newItemName)) { newItemName = item.ComponentName; } node.DashboardItem = newItems.Single(itm => itm.ComponentName == newItemName); } } } layoutPage.ChildNodes.Add(layoutRoot); foreach (DashboardItem item in newItems) { if (item.ParentContainer == null) { item.ParentContainer = newTabPage; } else { IDashboardItemContainer container = item.ParentContainer; if (container is DashboardItemGroup) { string newGroupName = String.Empty; if (!groupNamesMap.TryGetValue(container.ComponentName, out newGroupName)) { newGroupName = container.ComponentName; } item.ParentContainer = targetDashboard.Groups[newGroupName]; } else { item.ParentContainer = newTabPage; } } } DashboardLayoutTabContainer layoutTabContainer = targetDashboard.LayoutRoot.FindRecursive(tabContainer); layoutTabContainer.ChildNodes.Add(layoutPage); newTabPage.Name = newPageName; }