-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System.Linq; | ||
using OSPSuite.Utility.Extensions; | ||
|
||
namespace OSPSuite.Core.Domain.Services | ||
{ | ||
public interface IContainerMergeTask | ||
{ | ||
void MergeContainers(IContainer targetContainer, IContainer containerToMerge); | ||
void AddOrMergeContainer(IContainer parentContainer, IContainer containerToMerge); | ||
|
||
/// <summary> | ||
/// If the entity exists, it will be removed and replace by the new one, otherwise simply added | ||
/// </summary> | ||
void AddOrReplaceInContainer(IContainer container, IEntity entityToAddOrReplace); | ||
} | ||
|
||
public class ContainerMergeTask : IContainerMergeTask | ||
{ | ||
public void AddOrMergeContainer(IContainer parentContainer, IContainer containerToMerge) | ||
{ | ||
var existingContainer = parentContainer.Container(containerToMerge.Name); | ||
if (existingContainer == null) | ||
parentContainer.Add(containerToMerge); | ||
else | ||
MergeContainers(existingContainer, containerToMerge); | ||
} | ||
|
||
/// <summary> | ||
/// If the entity exists, it will be removed and replace by the new one, otherwise simply added | ||
/// </summary> | ||
public void AddOrReplaceInContainer(IContainer container, IEntity entityToAddOrReplace) | ||
{ | ||
var existingChild = container.GetSingleChildByName(entityToAddOrReplace.Name); | ||
if (existingChild != null) | ||
container.RemoveChild(existingChild); | ||
|
||
container.Add(entityToAddOrReplace); | ||
} | ||
|
||
public void MergeContainers(IContainer targetContainer, IContainer containerToMerge) | ||
{ | ||
updateContainerProperties(targetContainer, containerToMerge); | ||
var allChildrenContainerToMerge = containerToMerge.GetChildren<IContainer>(x => !x.IsAnImplementationOf<IDistributedParameter>()).ToList(); | ||
var allChildrenEntitiesToMerge = containerToMerge.GetChildren<IEntity>().Except(allChildrenContainerToMerge).ToList(); | ||
|
||
//First we do all containers (except distributed parameters which are to be seen as entities) recursively | ||
allChildrenContainerToMerge.Each(x => | ||
{ | ||
var targetChildrenContainer = targetContainer.Container(x.Name); | ||
//does not exist, we add it | ||
if (targetChildrenContainer == null) | ||
AddOrReplaceInContainer(targetContainer, x); | ||
//it exists, we need to merge | ||
else | ||
MergeContainers(targetChildrenContainer, x); | ||
}); | ||
|
||
//then we add or replace all non containers entities | ||
allChildrenEntitiesToMerge.Each(x => AddOrReplaceInContainer(targetContainer, x)); | ||
} | ||
|
||
private void updateContainerProperties(IContainer targetContainer, IContainer containerToMerge) | ||
{ | ||
targetContainer.Mode = containerToMerge.Mode; | ||
containerToMerge.Tags.Each(targetContainer.AddTag); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters