public ModelScalarType CreateModelScalarType( string complexTypeName, string complexTypeExternalName, string propertyName, string propertyExternalName, string scalarTypeName, ModelScalarType suggestedType, short?maxLentgth, bool?fixedLength, bool?nullable, byte?precision, byte?scale, ModelErrors errors ) { if (suggestedType != null) { var key = (complexTypeExternalName ?? complexTypeName) + "." + (propertyExternalName ?? propertyName) + ":" + scalarTypeName; return(this._RulesModelScalarType.HandleRules(key, suggestedType, this.GenerateRules)); } else { var result = new ModelScalarType { Name = scalarTypeName, MaxLength = maxLentgth, FixedLength = fixedLength, Nullable = nullable, Precision = precision, Scale = scale }; #warning CreateModelScalarType var key = (complexTypeExternalName ?? complexTypeName) + "." + (propertyExternalName ?? propertyName) + ":" + scalarTypeName; return(this._RulesModelScalarType.HandleRules(key, result, this.GenerateRules)); } }
public void BuildSchema( MappingModelSchema mappingModelSchema, ModelErrors errors ) { this.BuildEntities(mappingModelSchema, errors); this.BuildComplexTypes(mappingModelSchema, errors); }
/// <summary> /// magic /// </summary> /// <param name="errors">errors</param> public void Build( ModelErrors errors ) { if (this.MappingModelRepository is null) { throw new ArgumentNullException(nameof(this.MappingModelRepository)); } var repositorySource = this.MappingModelRepository.Source ?? throw new ArgumentNullException(nameof(this.MappingModelRepository.Source)); var repositoryTarget = this.MappingModelRepository.Target ?? throw new ArgumentNullException(nameof(this.MappingModelRepository.Target)); if (repositoryTarget.ModelSchema is null) { repositoryTarget.CreateModelSchema(null); } this.NamingServiceTarget = this.MappingModelRepository.Target.GetNamingService(this.MappingModelRepository); var lstMappingModelSchema = this.MappingModelRepository.ModelSchemaMappings; if (lstMappingModelSchema.Count == 0) { this.MappingModelRepository.CreateMappingModelSchema( null, repositorySource.ModelSchema, repositoryTarget.ModelSchema, this.EnabledForCreatedMappings || this.EnabledForCreatedMappingModelSchema, true, this.GetComment()); } { var lstMappingModelSchemaEnabled = lstMappingModelSchema.Where(_ => _.Enabled).ToList(); foreach (var mappingModelSchema in lstMappingModelSchemaEnabled) { var knownEntityMappingSourceName = new HashSet <string>(); foreach (var entityMapping in mappingModelSchema.EntityMappings) { knownEntityMappingSourceName.Add(entityMapping.SourceName); } this._KnownEntityMappingSourceName = knownEntityMappingSourceName; var knownComplexTypeMappingSourceName = new HashSet <string>(); foreach (var complexTypeMapping in mappingModelSchema.ComplexTypeMappings) { knownComplexTypeMappingSourceName.Add(complexTypeMapping.SourceName); } this._KnownComplexTypeMappingSourceName = knownComplexTypeMappingSourceName; } } { var lstMappingModelSchemaEnabled = lstMappingModelSchema.Where(_ => _.Enabled).ToList(); foreach (var mappingModelSchemaEnabled in lstMappingModelSchemaEnabled) { this.BuildSchema(mappingModelSchemaEnabled, errors); } } //this.BuildSchema(this.MappingModelRepository.MappingModelSchema, source.ModelSchema, target.ModelSchema, errors); //errors.AddErrorOrThrow(new ModelErrorInfo("", "")); }
public void MappingModelBuilder_1_Test() { var testCfg = TestCfg.Get(); var srcPath = System.IO.Path.Combine(testCfg.SolutionFolder, @"test\ProjectOnlineModelSchema.json"); IServiceCollection services = new ServiceCollection(); services.AddLogging((builder) => { builder.AddDebug(); }); services.AddOfaSchlupferModel(); services.AddOfaSchlupferEntity(); //services.AddHttpClient((builder) => { }); var serviceProvider = services.BuildServiceProvider(); using (var scope = serviceProvider.CreateScope()) { var scopedServiceProvider = scope.ServiceProvider; var modelRoot = scopedServiceProvider.GetRequiredService <ModelRoot>(); var modelRepositorySource = modelRoot.CreateRepository("Source", null); var modelRepositoryTarget = modelRoot.CreateRepository("Target", null); var mappingModelRepository = new MappingModelRepository(); mappingModelRepository = modelRoot.CreateMapping("SourceTarget", modelRepositorySource, modelRepositoryTarget); var jsonContent = System.IO.File.ReadAllText(srcPath); var modelSchemaSource = ModelJsonUtility.DeserializeModelFromJson <ModelSchema>(jsonContent, null); Assert.NotNull(modelSchemaSource); modelRepositorySource.ModelSchema = modelSchemaSource; var modelSchemaTarget = new ModelSchema(); modelRepositoryTarget.ModelSchema = modelSchemaTarget; var mappingModelBuilder = new MappingModelBuilder(); mappingModelBuilder.MappingModelRepository = mappingModelRepository; var errors = new ModelErrors(); mappingModelBuilder.Build(errors); if (errors.HasErrors()) { output.WriteLine(errors.ToString()); } Assert.False(errors.HasErrors()); { var serializeSettings = new Newtonsoft.Json.JsonSerializerSettings(); serializeSettings.TypeNameAssemblyFormatHandling = Newtonsoft.Json.TypeNameAssemblyFormatHandling.Simple; serializeSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto; var schemaAsJson = Newtonsoft.Json.JsonConvert.SerializeObject(modelRoot, Newtonsoft.Json.Formatting.Indented, serializeSettings); try { string outputPath = System.IO.Path.Combine(testCfg.SolutionFolder, @"test\temp\MappingModelBuilder_1_Test.json"); System.IO.File.WriteAllText(outputPath, schemaAsJson); } catch { throw; } } } }
public ModelComplexType CreateModelComplexType( string complexTypeName, string complexTypeExternalName, ModelErrors errors) { var result = new ModelComplexType { Name = complexTypeName, ExternalName = complexTypeExternalName ?? complexTypeName }; return(this._RulesModelComplexType.HandleRules(result.ExternalName, result, this.GenerateRules)); }
public static bool HasErrors(this ModelErrors errors) { if ((object)errors == null) { return(false); } if ((object)(errors.Errors) == null) { return(false); } return(errors.Errors.Count > 0); }
public ModelEntity CreateModelEntity( string entityName, ModelEntityKind modelEntityKind, ModelErrors errors) { var result = new ModelEntity { Name = entityName, ExternalName = entityName, Kind = modelEntityKind }; return(this._RulesModelEntity.HandleRules(result.ExternalName, result, this.GenerateRules)); }
public ModelProperty CreateModelProperty( string complexTypeName, string complexTypeExternalName, string propertyName, string propertyExternalName, ModelErrors errors ) { var result = new ModelProperty { Name = propertyName, ExternalName = propertyExternalName ?? propertyName }; var key = (complexTypeExternalName ?? complexTypeName) + "." + result.ExternalName; return(this._RulesModelProperty.HandleRules(key, result, this.GenerateRules)); }
public ModelIndex CreateModelIndex( string complexTypeName, string complexTypeExternalName, string indexName, string indexExternalName, //CsdlPropertyModel Property, ModelErrors errors ) { var result = new ModelIndex { Name = indexName, ExternalName = indexExternalName ?? indexName }; var key = (complexTypeExternalName ?? complexTypeName) + "." + result.ExternalName; return(this._RulesModelIndex.HandleRules(key, result, this.GenerateRules)); }
public static void AddErrorOrThrow(this ModelErrors errors, ModelErrorInfo modelErrorInfo, Func <string, Exception> generator = null) { if ((object)errors == null) { if (generator == null) { throw new ModelException(string.Empty, new ModelErrors(modelErrorInfo)); } else { throw (generator(modelErrorInfo.ToString())); } } else { errors.Errors?.Add(modelErrorInfo); } }
public static void AddErrorOrThrow(this ModelErrors errors, string msg, string location, Func <ModelErrorInfo, Exception> generator = null) { if ((object)errors == null) { if (generator == null) { throw new ModelException(string.Empty, new ModelErrors(new ModelErrorInfo(msg, location))); } else { throw (generator(new ModelErrorInfo(msg, location))); } } else { errors.Errors?.Add(new ModelErrorInfo(msg, location)); } }
public ModelIndexProperty CreateModelIndexProperty( string complexTypeName, string complexTypeExternalName, string indexName, string indexExternalName, string propertyName, string propertyExternalName, //CsdlPropertyModel Property, ModelErrors errors ) { #warning here CreateModelIndexProperty think of var result = new ModelIndexProperty { Name = propertyName, ExternalName = propertyExternalName ?? propertyName }; var key = (complexTypeExternalName ?? complexTypeName) + "." + result.ExternalName; return(this._RulesModelIndexProperty.HandleRules(key, result, this.GenerateRules)); }
public ResolveNameNotUniqueException(string message, ModelErrors modelErrors) : base(message, modelErrors) { }
public ModelException(string message, ModelErrors modelErrors) : base(message) { }
//public virtual void ResolveNameSource(ModelErrors errors) { } public abstract void ResolveNameSource(ModelErrors errors);
//public virtual void ResolveNameTarget(ModelErrors errors) { } public abstract void ResolveNameTarget(ModelErrors errors);
protected void ResolveNameTargetHelper <T>(T owner, Func <T, TMappingKey, List <TMappingValue> > findByKey, ModelErrors errors) where T : class { if (owner is null) { return; } if (((object)this._Target == null) && ((object)this._TargetName != null)) { var lstFound = findByKey(owner, this._TargetName); if (lstFound.Count == 1) { this._Target = lstFound[0]; this._TargetName = null; } else if (lstFound.Count == 0) { errors.AddErrorOrThrow($"Target {this._TargetName} not found", this.Name?.ToString(), ResolveNameNotFoundException.Factory); } else { errors.AddErrorOrThrow($"Target {this._TargetName} found #{lstFound.Count} times.", this.Name?.ToString(), ResolveNameNotUniqueException.Factory); } } }
public void BuildEntities( MappingModelSchema mappingModelSchema, ModelErrors errors ) { ModelSchema modelSchemaSource = mappingModelSchema.Source; ModelSchema modelSchemaTarget = mappingModelSchema.Target; var lstEntityMappings = mappingModelSchema.EntityMappings.Where(_ => _.Enabled).ToList(); foreach (var entityMapping in lstEntityMappings) { if (entityMapping.Target is null) { var targetName = entityMapping.TargetName; string extenalName = targetName; string typeName = null; #warning SOON thinkof what's in the mapping. if (this.NamingServiceTarget.EntitiesHaveFakeComplexTypes) { typeName = targetName; } modelSchemaTarget.CreateEntity(targetName, extenalName, typeName); } } foreach (var entitySource in modelSchemaSource.Entities) { if (this._KnownEntityMappingSourceName.Contains(entitySource.Name)) { continue; } (string targetName, string extenalName, string typeName, string typeExternalName) = this.NamingServiceTarget.SuggestEntityName(entitySource.Name); if (typeName is null && this.NamingServiceTarget.EntitiesHaveFakeComplexTypes) { typeName = targetName; } var entityMapping = mappingModelSchema.CreateEntityMapping( null, entitySource.Name, targetName, this.EnabledForCreatedMappings || this.EnabledForCreatedMappingModelEntity, true, this.GetComment() ); if (entityMapping.Enabled) { if (entityMapping.Target is null) { modelSchemaTarget.CreateEntity(entityMapping.TargetName, extenalName, typeName); } } if (this._KnownComplexTypeMappingSourceName.Contains(entitySource.EntityTypeName)) { // if there is a rule ... ok } else { var complexTypeSource = entitySource.EntityType; if (complexTypeSource is null) { #warning SOON error?? errors.AddErrorOrThrow($"EntityType {entitySource.EntityTypeName} does not exists.", entitySource.Name); } else { var complexTypeMapping = mappingModelSchema.CreateComplexTypeMapping( null, complexTypeSource.Name, typeName, this.EnabledForCreatedMappings || this.EnabledForCreatedMappingModelComplexType, true, this.GetComment() ); this._KnownComplexTypeMappingSourceName.Add(complexTypeSource.Name); if (complexTypeMapping.Enabled && !string.IsNullOrEmpty(complexTypeMapping.TargetName)) { if (complexTypeMapping.Target is null) { modelSchemaTarget.CreateComplexType(complexTypeMapping.TargetName ?? typeName, typeExternalName); } if ((!(complexTypeMapping.Source is null)) && (!(complexTypeMapping.Target is null)) ) { this.BuildComplexTypeProperties(complexTypeMapping, errors); this.BuildComplexTypeIndexes(complexTypeMapping, errors); } else { if (complexTypeMapping.Source is null) { errors.AddErrorOrThrow($"complexTypeMapping.Source {complexTypeMapping.SourceName} is null", $"complexTypeMapping.Name: {complexTypeMapping.Name}"); } if (complexTypeMapping.Target is null) { errors.AddErrorOrThrow($"complexTypeMapping.Target {complexTypeMapping.TargetName} is null", $"complexTypeMapping.Name: {complexTypeMapping.Name}"); } } } } } }
public override void ResolveNameTarget(ModelErrors errors) => this.ResolveNameTargetHelper(this.Owner, (owner, name) => owner.Target.Properties.FindByKey(name), errors);
public static ModelErrors GetIgnorance() => (_Ignorance ?? (_Ignorance = new ModelErrors(true)));
public override void ResolveNameSource(ModelErrors errors) => this.ResolveNameSourceHelper(this.Owner, (owner, name) => owner.Source.Properties.FindByKey(name), errors);
public abstract ModelSchema GetModelSchema(MetaModelBuilder metaModelBuilder, ModelErrors errors);