|
| 1 | +using Dataverse.ConfigurationMigrationTool.Console.Features.Import.Model; |
| 2 | +using Dataverse.ConfigurationMigrationTool.Console.Features.Import.Validators.Rules.EntitySchemas.FieldSchemas; |
| 3 | +using Dataverse.ConfigurationMigrationTool.Console.Features.Import.Validators.Rules.EntitySchemas.RelationshipSchemas; |
| 4 | +using Dataverse.ConfigurationMigrationTool.Console.Features.Shared; |
| 5 | +using Microsoft.Xrm.Sdk.Metadata; |
| 6 | + |
| 7 | +namespace Dataverse.ConfigurationMigrationTool.Console.Features.Import.Validators.Rules.EntitySchemas |
| 8 | +{ |
| 9 | + public class EntitySchemaValidator : IValidator<EntitySchema> |
| 10 | + { |
| 11 | + private readonly IMetadataService _metadataService; |
| 12 | + private readonly IReadOnlyCollection<IFieldSchemaValidationRule> _fieldSchemaRules; |
| 13 | + private readonly IReadOnlyCollection<IRelationshipSchemaValidationRule> _relationshipSchemaRules; |
| 14 | + public EntitySchemaValidator(IMetadataService metadataService, |
| 15 | + IEnumerable<IFieldSchemaValidationRule> fieldSchemaRules, |
| 16 | + IEnumerable<IRelationshipSchemaValidationRule> relationshipSchemaRules) |
| 17 | + { |
| 18 | + _metadataService = metadataService; |
| 19 | + _fieldSchemaRules = fieldSchemaRules.ToArray(); |
| 20 | + _relationshipSchemaRules = relationshipSchemaRules.ToArray(); |
| 21 | + } |
| 22 | + |
| 23 | + public async Task<ValidationResult> Validate(EntitySchema value) |
| 24 | + { |
| 25 | + var failures = new List<ValidationFailure>(); |
| 26 | + var Result = new ValidationResult |
| 27 | + { |
| 28 | + Failures = failures |
| 29 | + }; |
| 30 | + var entityMd = await _metadataService.GetEntity(value.Name); |
| 31 | + if (entityMd == null) |
| 32 | + { |
| 33 | + failures.Add(new ValidationFailure |
| 34 | + { |
| 35 | + Message = $"Entity {value.Name} does not exist in target environment" |
| 36 | + }); |
| 37 | + return Result; |
| 38 | + } |
| 39 | + foreach (var fieldSchema in value?.Fields?.Field) |
| 40 | + { |
| 41 | + var fieldResult = await ValidateFieldSchema(fieldSchema, entityMd); |
| 42 | + if (!fieldResult.IsSuccess) |
| 43 | + { |
| 44 | + failures.Add(new ValidationFailure |
| 45 | + { |
| 46 | + Message = fieldResult.ErrorMessage, |
| 47 | + PropertyBound = $"{value.Name}.{fieldSchema.Name}" |
| 48 | + }); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + foreach (var relationshipSchema in value?.Relationships?.Relationship) |
| 53 | + { |
| 54 | + var relationshipResult = await ValidateRelationshipSchema(relationshipSchema, entityMd); |
| 55 | + if (!relationshipResult.IsSuccess) |
| 56 | + { |
| 57 | + failures.Add(new ValidationFailure |
| 58 | + { |
| 59 | + Message = relationshipResult.ErrorMessage, |
| 60 | + PropertyBound = $"{value.Name}.{relationshipSchema.Name}" |
| 61 | + }); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return Result; |
| 66 | + } |
| 67 | + private async Task<RuleResult> ValidateFieldSchema(FieldSchema fieldSchema, EntityMetadata entityMetadata) |
| 68 | + { |
| 69 | + var attributeMetadata = entityMetadata.Attributes.FirstOrDefault(amd => amd.LogicalName == fieldSchema.Name); |
| 70 | + if (attributeMetadata == null) |
| 71 | + { |
| 72 | + return RuleResult.Failure($"Attribute '{fieldSchema.Name}' for Entity {entityMetadata.LogicalName} does not have exist in target environment"); |
| 73 | + } |
| 74 | + foreach (var fieldSchemaRule in _fieldSchemaRules) |
| 75 | + { |
| 76 | + |
| 77 | + var fieldValidationResult = await fieldSchemaRule.Validate(fieldSchema, attributeMetadata); |
| 78 | + if (!fieldValidationResult.IsSuccess) |
| 79 | + { |
| 80 | + return fieldValidationResult; |
| 81 | + } |
| 82 | + } |
| 83 | + return RuleResult.Success(); |
| 84 | + } |
| 85 | + private async Task<RuleResult> ValidateRelationshipSchema(RelationshipSchema relationshipSchema, EntityMetadata entityMetadata) |
| 86 | + { |
| 87 | + var relationShipMetadata = await _metadataService.GetRelationShipM2M(relationshipSchema.Name); |
| 88 | + if (relationShipMetadata == null) |
| 89 | + { |
| 90 | + return RuleResult.Failure($"ManyToMany Relationship Table {relationshipSchema.Name} does not exist in target environment or it's not a M2M relationship."); |
| 91 | + } |
| 92 | + foreach (var schemaRule in _relationshipSchemaRules) |
| 93 | + { |
| 94 | + |
| 95 | + var fieldValidationResult = await schemaRule.Validate(entityMetadata.LogicalName, relationshipSchema, relationShipMetadata); |
| 96 | + if (!fieldValidationResult.IsSuccess) |
| 97 | + { |
| 98 | + return fieldValidationResult; |
| 99 | + } |
| 100 | + } |
| 101 | + return RuleResult.Success(); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments