Example #1
0
        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);
        }
Example #2
0
        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);
        }
Example #3
0
        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);
        }