From 74f3a1540b1df4b7c8ad8d6c9a8e5accd1be4b3a Mon Sep 17 00:00:00 2001 From: Jonas Rapp Date: Sun, 24 Nov 2024 17:53:19 +0100 Subject: [PATCH] Nonduplication relationships plus DBML fixes --- LCG-UDG/Generation/GenerationUtils.cs | 87 +++++++++++++++++---------- LCG-UDG/Settings/TemplateDBML.cs | 14 +---- 2 files changed, 58 insertions(+), 43 deletions(-) diff --git a/LCG-UDG/Generation/GenerationUtils.cs b/LCG-UDG/Generation/GenerationUtils.cs index 74c7981..40ccff5 100644 --- a/LCG-UDG/Generation/GenerationUtils.cs +++ b/LCG-UDG/Generation/GenerationUtils.cs @@ -10,11 +10,12 @@ public class GenerationUtils { public static bool GenerateFiles(List selectedentities, Settings settings, IConstantFileWriter fileWriter) { + var addedrelationships = new List(); var commonentity = GetCommonEntity(selectedentities, settings); var suffix = Settings.FileSuffix(settings.TemplateFormat); if (commonentity != null) { - var entity = GetClass(commonentity, null, settings); + var entity = GetClass(commonentity, null, settings, addedrelationships); var fileName = commonentity.GetNameTechnical(settings.FileName, settings) + suffix; fileWriter.WriteBlock(settings, entity, fileName); } @@ -22,13 +23,13 @@ public static bool GenerateFiles(List selectedentities, Set { foreach (var group in settings.Groups.Where(g => selectedentities.Any(e => e.Group == g))) { - var groupstr = GetGroup(group, selectedentities.Where(e => e.Group == group).ToList(), settings); + var groupstr = GetGroup(group, selectedentities.Where(e => e.Group == group).ToList(), settings, addedrelationships); fileWriter.WriteBlock(settings, groupstr, group.Name); } } foreach (var entitymetadata in selectedentities.Where(e => e.Group == null)) { - var entity = GetClass(entitymetadata, commonentity, settings); + var entity = GetClass(entitymetadata, commonentity, settings, addedrelationships); var fileName = entitymetadata.GetNameTechnical(settings.FileName, settings) + suffix; fileWriter.WriteBlock(settings, entity, fileName); } @@ -36,7 +37,7 @@ public static bool GenerateFiles(List selectedentities, Set { var relationships = selectedentities.SelectMany(e => e.Relationships.Where(r => r.IsSelected)); relationships = relationships.GroupBy(r => r.LogicalName).Select(r => r.FirstOrDefault()); // This will make it distinct by LogicalName - var allrelationshipsstring = GetRelationships(relationships, settings); + var allrelationshipsstring = GetRelationships(relationships, settings, addedrelationships); fileWriter.WriteBlock(settings, allrelationshipsstring, "Relationships" + suffix); } return fileWriter.Finalize(settings); @@ -101,14 +102,14 @@ private static EntityMetadataProxy GetCommonEntity(List sel return result; } - private static string GetGroup(EntityGroup group, List selectedentities, Settings settings) + private static string GetGroup(EntityGroup group, List selectedentities, Settings settings, List addedrelationships) { var template = settings.TemplateSettings.Template; var entities = new StringBuilder(); foreach (var entitymetadata in selectedentities) { - var entity = GetClass(entitymetadata, null, settings); + var entity = GetClass(entitymetadata, null, settings, addedrelationships); entities.AppendLine(entity); } var color = group.ColorToString ?? ""; @@ -122,7 +123,7 @@ private static string GetGroup(EntityGroup group, List sele .Replace("{entities}", entities.ToString()); } - private static string GetClass(EntityMetadataProxy entitymetadata, EntityMetadataProxy commonentity, Settings settings) + private static string GetClass(EntityMetadataProxy entitymetadata, EntityMetadataProxy commonentity, Settings settings, List addedrelationships) { var template = settings.TemplateSettings.Template; var name = entitymetadata.GetNameTechnical(settings.ConstantName, settings); @@ -147,7 +148,7 @@ private static string GetClass(EntityMetadataProxy entitymetadata, EntityMetadat .Replace("{remarks}", remarks) .Replace("'", "\"") .Replace("{attributes}", GetAttributes(entitymetadata, commonentity, settings)) - .Replace("{relationships}", GetRelationships(entitymetadata, settings)) + .Replace("{relationships}", GetRelationships(entitymetadata, settings, addedrelationships)) .Replace("{optionsets}", GetOptionSets(entitymetadata, settings)); return entity; } @@ -221,30 +222,34 @@ private static int OrderByRequiredLevel(AttributeMetadataProxy attribute) } } - private static string GetRelationships(EntityMetadataProxy entitymetadata, Settings settings) + private static string GetRelationships(EntityMetadataProxy entitymetadata, Settings settings, List addedrelationships) { + if (settings.TemplateSettings.Template.AddAllRelationshipsAfterEntities) + { + return string.Empty; + } var relationships = new List(); var relation = string.Empty; if (settings.RelationShips && entitymetadata.Relationships != null) { - foreach (var relationship in entitymetadata.Relationships.Where(r => r.Parent != entitymetadata && r.IsSelected).Distinct()) + foreach (var relationship in entitymetadata.Relationships.Where(r => r.Parent != entitymetadata && r.IsSelected && !addedrelationships.Contains(r.LogicalName)).Distinct()) { relation = GetRelationShip(relationship, settings, (relationship.Metadata.RelationshipType == RelationshipType.ManyToManyRelationship) ? settings.TemplateSettings.ManyManyRelationshipPrefix : - settings.TemplateSettings.ManyOneRelationshipPrefix); + settings.TemplateSettings.ManyOneRelationshipPrefix, addedrelationships); // N : 1 and 1 : N are the same, so we only add one of them if (!relationships.Contains(relation)) { relationships.Add(relation); } } - foreach (var relationship in entitymetadata.Relationships.Where(r => r.Parent == entitymetadata && r.IsSelected).Distinct()) + foreach (var relationship in entitymetadata.Relationships.Where(r => r.Parent == entitymetadata && r.IsSelected && !addedrelationships.Contains(r.LogicalName)).Distinct()) { relation = GetRelationShip(relationship, settings, (relationship.Metadata.RelationshipType == RelationshipType.ManyToManyRelationship) ? settings.TemplateSettings.ManyManyRelationshipPrefix : - settings.TemplateSettings.OneManyRelationshipPrefix); + settings.TemplateSettings.OneManyRelationshipPrefix, addedrelationships); // N : 1 and 1 : N are the same, so we only add one of them if (!relationships.Contains(relation)) { @@ -261,15 +266,15 @@ private static string GetRelationships(EntityMetadataProxy entitymetadata, Setti return result; } - private static string GetRelationships(IEnumerable relationshipslist, Settings settings) + private static string GetRelationships(IEnumerable relationshipslist, Settings settings, List addedrelationships) { var relationships = new List(); - foreach (var relationship in relationshipslist) + foreach (var relationship in relationshipslist.Where(r => !addedrelationships.Contains(r.LogicalName))) { relationships.Add(GetRelationShip(relationship, settings, (relationship.Metadata.RelationshipType == RelationshipType.ManyToManyRelationship) ? settings.TemplateSettings.ManyManyRelationshipPrefix : - settings.TemplateSettings.OneManyRelationshipPrefix)); + settings.TemplateSettings.OneManyRelationshipPrefix, addedrelationships)); } var result = string.Join("\r\n", relationships); return result; @@ -395,7 +400,7 @@ private static string GetAttribute(AttributeMetadataProxy attributemetadata, Set return attribute; } - private static string GetRelationShip(RelationshipMetadataProxy relationship, Settings settings, string prefix) + private static string GetRelationShip(RelationshipMetadataProxy relationship, Settings settings, string prefix, List addedrelationships) { var template = settings.TemplateSettings.Template; if (relationship.Child == null || relationship.Parent == null) @@ -443,31 +448,49 @@ private static string GetRelationShip(RelationshipMetadataProxy relationship, Se .Replace("{referencingName}", referencingName) .Replace("{referencedAttribute}", relationship.LookupName) .Replace("{entity2}", relationship.Child.GetNameTechnical(settings.ConstantName, settings)) - .Replace("{relationtype}", GetRelationUMLNotation(relationship, settings.RelationShipSize)) + .Replace("{relationtype}", GetRelationUMLNotation(relationship, settings.RelationShipSize, settings.TemplateFormat)) .Replace("{lookup}", settings.RelationshipLabels ? relationship.LookupAttribute?.GetNameTechnical(settings) : "") .Replace("{summary}", summary) .Replace("'", "\"") .TrimEnd(' ', ':'); + if (!string.IsNullOrWhiteSpace(relation)) + { + addedrelationships.Add(relationship.LogicalName); + } return relation; } - private static string GetRelationUMLNotation(RelationshipMetadataProxy relationship, int size) + private static string GetRelationUMLNotation(RelationshipMetadataProxy relationship, int size, TemplateFormat templateFormat) { - var sizestr = new string('-', size); - if (relationship.Metadata is ManyToManyRelationshipMetadata) + switch (templateFormat) { - return "}" + sizestr + "{"; - } - else if (relationship.Metadata is OneToManyRelationshipMetadata) - { - if (relationship.LookupAttribute?.Metadata.RequiredLevel.Value == AttributeRequiredLevel.ApplicationRequired || - relationship.LookupAttribute?.Metadata.RequiredLevel.Value == AttributeRequiredLevel.SystemRequired) - { - return "||" + sizestr + "{"; - } - return sizestr + "{"; + case TemplateFormat.PlantUML: + var sizestr = new string('-', size); + if (relationship.Metadata is ManyToManyRelationshipMetadata) + { + return "}" + sizestr + "{"; + } + else if (relationship.Metadata is OneToManyRelationshipMetadata) + { + if (relationship.LookupAttribute?.Metadata.RequiredLevel.Value == AttributeRequiredLevel.ApplicationRequired || + relationship.LookupAttribute?.Metadata.RequiredLevel.Value == AttributeRequiredLevel.SystemRequired) + { + return "||" + sizestr + "{"; + } + return sizestr + "{"; + } + return sizestr; + + case TemplateFormat.DBML: + if (relationship.Metadata is ManyToManyRelationshipMetadata) + { + return "<>"; + } + return ">"; + + default: + return string.Empty; } - return sizestr; } private static string GetOptionSet(AttributeMetadataProxy attributemetadata, Settings settings) diff --git a/LCG-UDG/Settings/TemplateDBML.cs b/LCG-UDG/Settings/TemplateDBML.cs index 216d6e0..9753156 100644 --- a/LCG-UDG/Settings/TemplateDBML.cs +++ b/LCG-UDG/Settings/TemplateDBML.cs @@ -11,20 +11,12 @@ public TemplateDBML() DataContainer = "{data}"; EntityGroup = "\n{entities}\n"; EntityContainer = "\nTable {entityname} {\n{attributes}\n}\n{relationships}\n\n"; - Attribute = "{attribute} {type} [note: '{logicalname}']"; - - Relationship = ""; - - // allow for multiple relationships + Attribute = "{attribute} {type}{typedetails} [note: '{logicalname}']"; + PrimaryKeyName = "{attribute}"; + PrimaryAttributeName = "{attribute}"; RelationshipN_1 = "Ref:{entity2}.{referencingName} > {entity1}.{referencedName}"; Relationship1_N = "Ref:{entity1}.{referencedName} > {entity2}.{referencingName}"; - - // N:N, many-to-many relationship shown by a join table RelationshipN_N = "\n\nTable {schemaname} {\n {referencingName} Uniqueidentifier [ref: > {entity2}.{referencingName}]\n {referencedName} Uniqueidentifier [ref: > {entity1}.{referencedName}]\n}\n"; - - PrimaryKeyName = "{attribute}"; - PrimaryAttributeName = "{attribute}"; - OptionSet = "Enum {name}\n{\n{values}\n}"; OptionSetValue = "{name}\n"; TableAttributeNameClashSuffix = string.Empty;