public static async Task <CdmDocumentDefinition> FromData(CdmCorpusContext ctx, LocalEntity obj, List <CdmTraitDefinition> extensionTraitDefList, List <CdmTraitDefinition> localExtensionTraitDefList) { var docName = $"{obj.Name}.cdm.json"; var document = ctx.Corpus.MakeObject <CdmDocumentDefinition>(CdmObjectType.DocumentDef, docName); // import at least foundations document.Imports.Add("cdm:/foundations.cdm.json"); var entity = await EntityPersistence.FromData(ctx, obj, extensionTraitDefList, localExtensionTraitDefList); if (entity == null) { Logger.Error(ctx, Tag, nameof(FromData), null, CdmLogCode.ErrPersistModelJsonEntityConversionError); return(null); } if (obj.Imports != null) { foreach (var import in obj.Imports) { if (import.CorpusPath?.Equals("cdm:/foundations.cdm.json") == true) { // don't add foundations twice continue; } document.Imports.Add(CdmFolder.ImportPersistence.FromData(ctx, import)); } } document.Definitions.Add(entity); return(document); }
public static async Task<CdmDocumentDefinition> FromData(CdmCorpusContext ctx, LocalEntity obj, List<CdmTraitDefinition> extensionTraitDefList, List<CdmTraitDefinition> localExtensionTraitDefList) { var docName = $"{obj.Name}.cdm.json"; var document = ctx.Corpus.MakeObject<CdmDocumentDefinition>(CdmObjectType.DocumentDef, docName); // import at least foundations document.Imports.Add("cdm:/foundations.cdm.json"); var entity = await EntityPersistence.FromData(ctx, obj, extensionTraitDefList, localExtensionTraitDefList); if (entity == null) { Logger.Error(nameof(DocumentPersistence), ctx, "There was an error while trying to convert a model.json entity to the CDM entity."); return null; } if (obj.Imports != null) { foreach (var import in obj.Imports) { if (import.CorpusPath?.Equals("cdm:/foundations.cdm.json") == true) { // don't add foundations twice continue; } document.Imports.Add(CdmFolder.ImportPersistence.FromData(ctx, import)); } } document.Definitions.Add(entity); return document; }
public static async Task <LocalEntity> ToData(dynamic documentObjectOrPath, CdmManifestDefinition manifest, ResolveOptions resOpt, CopyOptions options, CdmCorpusContext ctx) { if (documentObjectOrPath is string) { if (await ctx.Corpus.FetchObjectAsync <CdmEntityDefinition>(documentObjectOrPath, manifest) is CdmEntityDefinition cdmEntity) { var entity = await EntityPersistence.ToData(cdmEntity, resOpt, options, ctx); if (cdmEntity.Owner != null && cdmEntity.Owner is CdmDocumentDefinition document) { if (document.Imports.Count > 0) { entity.Imports = new List <Import>(); foreach (var element in document.Imports) { var import = CdmFolder.ImportPersistence.ToData(element, resOpt, options); // the corpus path in the imports are relative to the document where it was defined. // when saving in model.json the documents are flattened to the manifest level // so it is necessary to recalculate the path to be relative to the manifest. var absolutePath = ctx.Corpus.Storage.CreateAbsoluteCorpusPath(import.CorpusPath, document); if (!string.IsNullOrEmpty(document.Namespace) && absolutePath.StartsWith(document.Namespace + ":")) { absolutePath = absolutePath.Substring(document.Namespace.Length + 1); } import.CorpusPath = ctx.Corpus.Storage.CreateRelativeCorpusPath(absolutePath, manifest); entity.Imports.Add(import); } } } else { Logger.Warning(ctx, Tag, nameof(ToData), manifest.AtCorpusPath, CdmLogCode.WarnPersistEntityMissing, cdmEntity.GetName()); } return(entity); } else { Logger.Error(ctx, Tag, nameof(ToData), manifest.AtCorpusPath, CdmLogCode.ErrPersistCdmEntityFetchError); return(null); } } return(null); }
public static async Task <LocalEntity> ToData(dynamic documentObjectOrPath, CdmManifestDefinition manifest, ResolveOptions resOpt, CopyOptions options, CdmCorpusContext ctx) { if (documentObjectOrPath is string) { if (await ctx.Corpus.FetchObjectAsync <CdmEntityDefinition>(documentObjectOrPath) is CdmEntityDefinition cdmEntity) { var entity = await EntityPersistence.ToData(cdmEntity, resOpt, options, ctx); if (cdmEntity.Owner != null && cdmEntity.Owner is CdmDocumentDefinition document) { if (document.Imports.Count > 0) { entity.Imports = new List <Import>(); foreach (var element in document.Imports) { var import = CdmFolder.ImportPersistence.ToData(element, resOpt, options); // the corpus path in the imports are relative to the document where it was defined. // when saving in model.json the documents are flattened to the manifest level // so it is necessary to recalculate the path to be relative to the manifest. var absolutePath = ctx.Corpus.Storage.CreateAbsoluteCorpusPath(import.CorpusPath, document); import.CorpusPath = ctx.Corpus.Storage.CreateRelativeCorpusPath(absolutePath, manifest); entity.Imports.Add(import); } } } else { Logger.Warning(nameof(DocumentPersistence), ctx, $"Entity {cdmEntity.GetName()} is not inside a document or its owner is not a document."); } return(entity); } else { Logger.Error(nameof(DocumentPersistence), ctx, "There was an error while trying to fetch cdm entity doc."); return(null); } } return(null); }
public static async Task <LocalEntity> ToData(dynamic documentObjectOrPath, ResolveOptions resOpt, CopyOptions options, CdmCorpusContext ctx) { if (documentObjectOrPath is string) { string absCorpusPath = ctx.Corpus.Storage.CreateAbsoluteCorpusPath(documentObjectOrPath); if (await ctx.Corpus.FetchObjectAsync <CdmEntityDefinition>(absCorpusPath) is CdmEntityDefinition cdmEntity) { var entity = await EntityPersistence.ToData(cdmEntity, resOpt, options, ctx); if (cdmEntity.Owner != null && cdmEntity.Owner is CdmDocumentDefinition document) { if (document.Imports.Count > 0) { entity.Imports = new List <Import>(); foreach (var element in document.Imports) { entity.Imports.Add(CdmFolder.ImportPersistence.ToData(element, resOpt, options)); } } } else { Logger.Warning(nameof(DocumentPersistence), ctx, $"Entity {cdmEntity.GetName()} is not inside a document or its owner is not a document."); } return(entity); } else { Logger.Error(nameof(DocumentPersistence), ctx, "There was an error while trying to fetch cdm entity doc."); return(null); } } return(null); }