Skip to content

Commit

Permalink
supported entity type filtration for entities not included into Entit…
Browse files Browse the repository at this point in the history
…yContainer\EntitySet (like "<EntityType Name="crmbaseentity" Abstract="true"/>" in Dynamics CRM)
  • Loading branch information
AndrewK2 committed Oct 26, 2023
1 parent 583155c commit e98e36c
Showing 1 changed file with 49 additions and 38 deletions.
87 changes: 49 additions & 38 deletions EDMXTrimmer/EDMXTrimmer/EdmxTrimmer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,31 +132,48 @@ private void RemoveAllEntitiesExcept(
List<XmlNode> entitySets,
List<XmlNode> entityTypes)
{
string regex = EntitySearchTermsToRegularExpression(entitiesToKeep);
var entitiesKeep = entitySets.Where(n => Regex.IsMatch(n.Attributes[ATTRIBUTE_NAME].Value, regex)).ToList();
var (entitySetsToKeep, entityTypeNamesToKeep) = FilterByEntity(entitiesToKeep, entitySets, entityTypes, true);

RemoveEntitySets(entitySets, entitiesKeep);
RemoveEntityTypes(entityTypes, entitiesKeep);
RemoveEntitySets(entitySets, entitySetsToKeep);
RemoveEntityTypes(entityTypes, entityTypeNamesToKeep);
}

private void RemoveExcludedEntities(
List<string> entitiesToExclude,
List<XmlNode> entitySets,
List<XmlNode> entityTypes)
{
string regex = EntitySearchTermsToRegularExpression(entitiesToExclude);
var entitiesKeep = entitySets.Where(n => !Regex.IsMatch(n.Attributes[ATTRIBUTE_NAME].Value, regex)).ToList();
var (entitySetsToKeep, entityTypeNamesToKeep) = FilterByEntity(entitiesToExclude, entitySets, entityTypes, false);

RemoveEntitySets(entitySets, entitiesKeep);
RemoveEntityTypes(entityTypes, entitiesKeep);
RemoveEntitySets(entitySets, entitySetsToKeep);
RemoveEntityTypes(entityTypes, entityTypeNamesToKeep);
}

private string EntitySearchTermsToRegularExpression(List<string> entitiesToKeep)
private (List<XmlNode> EntitySetsToKeep, IReadOnlyCollection<string> EntityTypeNamesToKeep) FilterByEntity(IEnumerable<string> filteringEntities, IEnumerable<XmlNode> entitySets, IEnumerable<XmlNode> entityTypes, bool includeFiltered)
{
List<string> listRegularExpression = entitiesToKeep.Select(s => EntitySearchTermToRegularExpression(s)).ToList();
string regex = String.Join("|", listRegularExpression.ToArray());
var nameRegex = EntitySearchTermsToRegularExpression(filteringEntities);

var entitySetsNodes = entitySets
.Where(n => Regex.IsMatch(n.Attributes[ATTRIBUTE_NAME].Value, nameRegex) ? includeFiltered : !includeFiltered)
.ToList();

var entityTypeNames = entitySetsNodes
.Select(n => GetEntityTypeWithoutNamespace(n, TAG_ENTITY_TYPE))
.Concat(entityTypes
.Where(node => Regex.IsMatch(node.Attributes[ATTRIBUTE_NAME].Value, nameRegex) ? includeFiltered : !includeFiltered)
.Select(node => GetEntityTypeWithoutNamespace(node, ATTRIBUTE_NAME))
)
.Distinct()
.ToList();

return (entitySetsNodes, entityTypeNames);
}

return regex;
private string EntitySearchTermsToRegularExpression(IEnumerable<string> entitiesToKeep)
{
var parts = entitiesToKeep.Select(EntitySearchTermToRegularExpression);

return String.Join("|", parts);
}

private string EntitySearchTermToRegularExpression(string searchTerm)
Expand All @@ -171,7 +188,7 @@ private string EntitySearchTermToRegularExpression(string searchTerm)
return regex;
}

private void RemoveEntitySets(List<XmlNode> entitySets, List<XmlNode> entitiesKeep)
private void RemoveEntitySets(IEnumerable<XmlNode> entitySets, List<XmlNode> entitiesKeep)
{
// Remove entities not required (EntitySet)
entitySets.Except(entitiesKeep).ToList().ForEach(n => n.ParentNode.RemoveChild(n));
Expand All @@ -186,26 +203,20 @@ private void RemoveEntitySets(List<XmlNode> entitySets, List<XmlNode> entitiesKe
});
}

private void RemoveEntityTypes(List<XmlNode> entityTypes, List<XmlNode> entitiesKeep)
private void RemoveEntityTypes(IReadOnlyCollection<XmlNode> entityTypes, IReadOnlyCollection<string> entitiesNamesToKeep)
{
List<String> entityTypesFound = new List<string>();
entitiesKeep.ForEach(n => {
var entityType = GetEntityTypeWithoutNamespace(n);
entityTypesFound.Add(entityType);
});

// Remove all navigation properties
this._xmlDocument.GetElementsByTagName(TAG_NAVIGATION_PROPERTY).Cast<XmlNode>()
.Where(navProp => !entityTypesFound.Any(entityType => EntityExists(navProp, entityType))).ToList()
.Where(navProp => !entitiesNamesToKeep.Any(entityType => EntityExists(navProp, entityType))).ToList()
.ForEach(n => n.ParentNode.RemoveChild(n));

// Remove entity not required (EntityType)
var entityTypesToKeep = entityTypes.Where(n => entityTypesFound.Contains(n.Attributes[ATTRIBUTE_NAME].Value)).ToList();
var entityTypesToKeep = entityTypes.Where(n => entitiesNamesToKeep.Contains(n.Attributes[ATTRIBUTE_NAME].Value)).ToList();
entityTypes.Except(entityTypesToKeep).ToList().ForEach(n => n.ParentNode.RemoveChild(n));

// Remove all Actions
this._xmlDocument.GetElementsByTagName(TAG_ACTION).Cast<XmlNode>()
.Where(action => !entityTypesFound.Any(entityType => action.ChildNodes.Cast<XmlNode>().
.Where(action => !entitiesNamesToKeep.Any(entityType => action.ChildNodes.Cast<XmlNode>().
Any(childNode => EntityExists(childNode, entityType)))).ToList()
.ForEach(n => n.ParentNode.RemoveChild(n));

Expand Down Expand Up @@ -240,26 +251,14 @@ private void RemoveEntityTypes(List<XmlNode> entityTypes, List<XmlNode> entities

return;

string GetEntityTypeWithoutNamespace(XmlNode n) {
var entityType = n.Attributes[TAG_ENTITY_TYPE]?.Value;

if(ENTITYNAMESPACE_ALIAS != null) {
var replaced = entityType.Replace(ENTITYNAMESPACE_ALIAS, "");
if(replaced != entityType) {
return replaced;
}
}

return entityType.Replace(ENTITYNAMESPACE, "");
}

IEnumerable<string> GetEntityTypesFromNodeChildren(XmlNode typeNode, string nodeName) =>
IReadOnlyCollection<string> GetEntityTypesFromNodeChildren(XmlNode typeNode, string nodeName) =>
typeNode
.ChildNodes
.Cast<XmlNode>()
.Where(prop => prop.Name.Equals(nodeName))
.Select(RemoveNamespace)
.Where(name => name != null);
.Where(name => name != null)
.ToList();

string RemoveNamespace(XmlNode xmlNode) {
var enumType = xmlNode.Attributes[ATTRIBUTE_TYPE]?.Value;
Expand All @@ -277,6 +276,18 @@ string RemoveNamespace(XmlNode xmlNode) {
return null;
}
}

private string GetEntityTypeWithoutNamespace(XmlNode n, string attributeName) {
var entityType = n.Attributes[attributeName]?.Value;

if(ENTITYNAMESPACE_ALIAS != null) {
var replaced = entityType.Replace(ENTITYNAMESPACE_ALIAS, "");
if(replaced != entityType) {
return replaced;
}
}
return entityType.Replace(ENTITYNAMESPACE, "");
}

private void RemovePrimaryAnnotations()
{
Expand Down

0 comments on commit e98e36c

Please sign in to comment.