Example #1
0
 public static Import ToData(CdmImport instance, ResolveOptions resOpt, CopyOptions options)
 {
     return(new Import {
         Moniker = string.IsNullOrEmpty(instance.Moniker) ? null: instance.Moniker,
         CorpusPath = instance.CorpusPath
     });
 }
Example #2
0
 public static Import ToData(CdmImport instance, ResolveOptions resOpt, CopyOptions options)
 {
     return(new Import {
         Moniker = StringUtils.IsBlankByCdmStandard(instance.Moniker) ? null: instance.Moniker,
         CorpusPath = instance.CorpusPath
     });
 }
Example #3
0
        /// <summary>
        /// Adds the remaining definitions of extensions to extensionDoc.
        /// Then adds extensionDoc to the folder of the manifest and
        /// adds it's path to the list of imports
        /// </summary>
        /// <param name="ctx">The context</param>
        /// <param name="extensionDoc">The document where the definitions will be added. This document will be added to folder and it's path to importDocs</param>
        /// <param name="extensionTraitDefList">The list of definitions to be added to schema.</param>
        /// <param name="folder">The folder that contains the manifest and where the document containing the schema will be placed.</param>
        /// <param name="importDocs">The list of paths of documents containing schemas for the manifest.</param>
        private static void CreateExtensionDocAndAddToFolderAndImports(CdmCorpusContext ctx, CdmDocumentDefinition extensionDoc, CdmCollection <CdmTraitDefinition> extensionTraitDefList, CdmFolderDefinition folder, List <CdmImport> importDocs)
        {
            if (extensionTraitDefList.Count > 0)
            {
                // pull out the extension trait definitions into a new custom extension document
                extensionTraitDefList.Select((CdmTraitDefinition cdmTraitDef) => (CdmObjectDefinition)cdmTraitDef).ToList().ForEach((CdmObjectDefinition cdmObjectDef) =>
                {
                    extensionDoc.Definitions.Add(cdmObjectDef);
                });
                extensionDoc.Folder = folder;

                // import the cdm extensions into this new document that has the custom extensions
                CdmImport baseExtensionImport = ctx.Corpus.MakeObject <CdmImport>(CdmObjectType.Import);
                baseExtensionImport.CorpusPath = "cdm:/extensions/base.extension.cdm.json";
                extensionDoc.Imports.Add(baseExtensionImport);

                // add the extension doc to the folder, will wire everything together as needed
                folder.Documents.Add(extensionDoc);

                // import this new customer extension doc into the list of imports for the origial doc
                CdmImport extensionImport = ctx.Corpus.MakeObject <CdmImport>(CdmObjectType.Import);
                extensionImport.CorpusPath = ctx.Corpus.Storage.CreateRelativeCorpusPath(extensionDoc.AtCorpusPath, extensionDoc);
                importDocs.Add(extensionImport);
            }
        }
Example #4
0
        /// <summary>
        /// For all the definitions (name, type) we have found for extensions, search in CDM Standard Schema for definition files.
        /// If we find a definition in a CDM Standard Schema file, we add that file to importsList.
        /// At the same time, the found definition is removed from extensionTraitDefList.
        /// When this function returns, extensionTraitDefList only contains definitions that are not present in any CDM Standard Schema file,
        /// and a list of CDM Standard Schema files with relevant definitions is returned.
        /// </summary>
        /// <param name="ctx"> The context</param>
        /// <param name="extensionTraitDefList"> The list of all definitions for all found extensions. Function modifies this list by removing definitions found in CDM Standard Schema files.</param>
        /// <returns> A list of CDM Standard Schema files to import.</returns>
        public static async Task <List <CdmImport> > StandardImportDetection(CdmCorpusContext ctx, CdmCollection <CdmTraitDefinition> extensionTraitDefList)
        {
            List <CdmImport> importsList = new List <CdmImport>();

            // have to go from end to start because I might remove elements
            for (int traitIndex = extensionTraitDefList.Count - 1; traitIndex >= 0; traitIndex--)
            {
                CdmTraitDefinition extensionTraitDef = extensionTraitDefList[traitIndex];
                if (!TraitDefIsExtension(extensionTraitDef))
                {
                    Logger.Error(nameof(ExtensionHelper), ctx, $"Invalid extension trait name {extensionTraitDef.TraitName}, expected prefix {ExtensionTraitNamePrefix}.");

                    return(null);
                }

                string[] extensionBreakdown = RemoveExtensionTraitNamePrefix(extensionTraitDef.TraitName).Split(':');
                if (extensionBreakdown.Length > 1)
                {
                    string extensionName  = extensionBreakdown[0];
                    string fileName       = $"{extensionName}.extension.cdm.json";
                    string fileCorpusPath = $"cdm:/extensions/{fileName}";
                    CdmDocumentDefinition extensionDoc = await FetchDefDoc(ctx, fileName);

                    // If no document was found for that extensionName, the trait does not have a document with it's definition.
                    // Trait will be kept in extensionTraitDefList (a document with its definition will be created locally)
                    if (extensionDoc == null)
                    {
                        continue;
                    }

                    // There is a document with extensionName, now we search for the trait in the document.
                    // If we find it, we remove the trait from extensionTraitDefList and add the document to imports.
                    CdmTraitDefinition matchingTrait = extensionDoc.Definitions.AllItems.Find(
                        (definition) => definition.ObjectType == CdmObjectType.TraitDef && definition.GetName() == extensionTraitDef.TraitName)
                                                       as CdmTraitDefinition;
                    if (matchingTrait != null)
                    {
                        List <CdmParameterDefinition> parameterList = matchingTrait.Parameters.AllItems;
                        if (
                            extensionTraitDef.Parameters.AllItems.TrueForAll(
                                (CdmParameterDefinition extensionParameter) =>
                                parameterList.Exists(
                                    (CdmParameterDefinition defParameter) => defParameter.Name == extensionParameter.Name)
                                )
                            )
                        {
                            extensionTraitDefList.Remove(extensionTraitDefList[traitIndex]);
                            if (!importsList.Exists((CdmImport importDoc) => importDoc.CorpusPath == fileCorpusPath))
                            {
                                CdmImport importObject = ctx.Corpus.MakeObject <CdmImport>(CdmObjectType.Import);
                                importObject.CorpusPath = fileCorpusPath;
                                importsList.Add(importObject);
                            }
                        }
                    }
                }
            }
            return(importsList);
        }
        public void TestCdmImportCollectionAdd()
        {
            var document = CdmCollectionHelperFunctions.GenerateManifest("C:\\Nothing");

            document.IsDirty = false;
            Assert.AreEqual(false, document.IsDirty);
            var import      = new CdmImport(document.Ctx, "corpusPath", "moniker");
            var addedImport = document.Imports.Add(import);

            Assert.IsTrue(document.IsDirty);
            Assert.AreEqual(1, document.Imports.Count);
            Assert.AreEqual(import, addedImport);
            Assert.AreEqual(import, document.Imports[0]);
            Assert.AreEqual("corpusPath", import.CorpusPath);
            Assert.AreEqual("moniker", import.Moniker);
            Assert.AreEqual(document.Ctx, import.Ctx);
        }
Example #6
0
        public static CdmImport FromData(CdmCorpusContext ctx, Import obj)
        {
            if (obj == null)
            {
                throw new System.ArgumentNullException(nameof(obj));
            }

            CdmImport import     = ctx.Corpus.MakeObject <CdmImport>(CdmObjectType.Import);
            string    corpusPath = obj.CorpusPath;

            if (string.IsNullOrEmpty(corpusPath))
            {
                corpusPath = obj.URI;
            }
            import.CorpusPath = corpusPath;
            import.Moniker    = obj.Moniker;

            return(import);
        }