internal static void CommitChanges() { var dependencyIds = new List <string>(); if (_currentIds.Any()) { Console.WriteLine($"{_currentIds.Count} deleted object(s) found. Cleaning the model:"); } else { return; } foreach (var currentId in _currentIds) { var element = _workspace.Model.GetElement(currentId); //check on dependencies (they are not loaded by the scanner) if (NamedIdentity.TryGetTypeFromExternalAssemblyByElementName(element.Name, out var dependency)) { dependencyIds.Add(currentId); continue; } switch (element) { case SoftwareSystem softwareSystem: Console.WriteLine($"\tRemoving SoftwareSystem {softwareSystem.Name}"); _workspace.Model.SoftwareSystems.Remove(softwareSystem); break; case Person person: Console.WriteLine($"\tRemoving Person {person.Name}"); _workspace.Model.People.Remove(person); break; case Container container: Console.WriteLine($"\tRemoving Container {container.Name}"); ((SoftwareSystem)container.Parent).Containers.Remove(container); break; case Component component: Console.WriteLine($"\tRemoving Component {component.Name}"); ((Container)component.Parent).Components.Remove(component); break; } } var styles = _workspace.Views.Configuration.Styles; foreach (var currentRelationShipTag in _currentRelationShipTags) { Console.WriteLine($"\tRemoving RelationshipStyle {currentRelationShipTag}"); var relStyle = styles.Relationships.Find(x => x.Tag == currentRelationShipTag); styles.Relationships.Remove(relStyle); } foreach (var currentElementTag in _currentElementTags.Where(x => x != "Element")) { Console.WriteLine($"\tRemoving ElementStyle {currentElementTag}"); var elStyle = styles.Elements.Find(x => x.Tag == currentElementTag); styles.Elements.Remove(elStyle); } //remove all the dependencies foreach (var currentId in _currentIds) { if (dependencyIds.Contains(currentId)) { continue; } // // Cleanup views foreach (var landscapeViewItem in _workspace.Views.SystemLandscapeViews) { var foundLandscapeView = landscapeViewItem.Elements.FirstOrDefault(x => x.Id == currentId); if (foundLandscapeView != null) { Console.WriteLine($"\tRemoving SystemLandscapeView Element {foundLandscapeView.Element.Name}"); landscapeViewItem.Elements.Remove(foundLandscapeView); } } var contextView = _workspace.Views.SystemContextViews.FirstOrDefault(x => x.SoftwareSystemId == currentId); if (contextView != null) { Console.WriteLine($"\tRemoving SystemContextView {contextView.Name}"); _workspace.Views.SystemContextViews.Remove(contextView); } foreach (var contextViewItem in _workspace.Views.SystemContextViews) { var foundContextView = contextViewItem.Elements.FirstOrDefault(x => x.Id == currentId); if (foundContextView != null) { Console.WriteLine($"\tRemoving SystemContextView Element {foundContextView.Element.Name}"); contextViewItem.Elements.Remove(foundContextView); } } var containerView = _workspace.Views.ContainerViews.FirstOrDefault(x => x.SoftwareSystemId == currentId); if (containerView != null) { Console.WriteLine($"\tRemoving ContainerView {containerView.Name}"); _workspace.Views.ContainerViews.Remove(containerView); } foreach (var containerViewItem in _workspace.Views.ContainerViews) { var foundContainerView = containerViewItem.Elements.FirstOrDefault(x => x.Id == currentId); if (foundContainerView != null) { Console.WriteLine($"\tRemoving ContainerView Element {foundContainerView.Element.Name}"); containerViewItem.Elements.RemoveWhere((e) => e.Id == currentId); } } var componentView = _workspace.Views.ComponentViews.FirstOrDefault(x => x.ContainerId == currentId); if (componentView != null) { Console.WriteLine($"\tRemoving ComponentView {componentView.Name}"); _workspace.Views.ComponentViews.Remove(componentView); } foreach (var componentViewItem in _workspace.Views.ComponentViews) { var foundComponentView = componentViewItem.Elements.FirstOrDefault(x => x.Id == currentId); if (foundComponentView != null) { Console.WriteLine($"\tRemoving ComponentView Element {foundComponentView.Element.Name}"); componentViewItem.Elements.RemoveWhere((e) => e.Id == currentId); } } // //cleanup relations var relations = _workspace.Model.Relationships.Where(x => x.DestinationId == currentId); foreach (var relationship in relations) { Console.WriteLine($"\tRemoving Relationship {relationship}"); foreach (var softwareSystem in _workspace.Model.SoftwareSystems) { if (softwareSystem.Relationships.Contains(relationship)) { softwareSystem.Relationships.Remove(relationship); } foreach (var container in softwareSystem.Containers) { if (container.Relationships.Contains(relationship)) { container.Relationships.Remove(relationship); } foreach (var component in container.Components) { if (component.Relationships.Contains(relationship)) { container.Relationships.Remove(relationship); } } } } } } }