public static CdmDocumentDefinition FromObject(CdmCorpusContext ctx, string name, string nameSpace, string path, DocumentContent obj)
        {
            var doc = ctx.Corpus.MakeObject <CdmDocumentDefinition>(CdmObjectType.DocumentDef, name);

            doc.FolderPath = path;
            doc.Namespace  = nameSpace;

            if (!string.IsNullOrEmpty(obj.Schema))
            {
                doc.Schema = obj.Schema;
            }
            if (DynamicObjectExtensions.HasProperty(obj, "JsonSchemaSemanticVersion") && !string.IsNullOrEmpty(obj.JsonSchemaSemanticVersion))
            {
                doc.JsonSchemaSemanticVersion = obj.JsonSchemaSemanticVersion;
            }
            if (obj.Imports != null)
            {
                foreach (var importObj in obj.Imports)
                {
                    doc.Imports.Add(ImportPersistence.FromData(ctx, importObj));
                }
            }

            if (obj.Definitions != null)
            {
                for (int i = 0; i < obj.Definitions.Count; i++)
                {
                    dynamic d = obj.Definitions[i];
                    if (d["dataTypeName"] != null)
                    {
                        doc.Definitions.Add(DataTypePersistence.FromData(ctx, d));
                    }
                    else if (d["purposeName"] != null)
                    {
                        doc.Definitions.Add(PurposePersistence.FromData(ctx, d));
                    }
                    else if (d["attributeGroupName"] != null)
                    {
                        doc.Definitions.Add(AttributeGroupPersistence.FromData(ctx, d));
                    }
                    else if (d["traitName"] != null)
                    {
                        doc.Definitions.Add(TraitPersistence.FromData(ctx, d));
                    }
                    else if (d["entityShape"] != null)
                    {
                        doc.Definitions.Add(ConstantEntityPersistence.FromData(ctx, d));
                    }
                    else if (d["entityName"] != null)
                    {
                        doc.Definitions.Add(EntityPersistence.FromData(ctx, d));
                    }
                }
            }

            return(doc);
        }
        private static dynamic GetEntityReference(CdmCorpusContext ctx, JToken obj)
        {
            dynamic entity = null;

            if (obj["entityReference"] is JValue)
            {
                entity = obj["entityReference"];
            }
            else if (obj["entityReference"]?["entityShape"] != null)
            {
                entity = ConstantEntityPersistence.FromData(ctx, obj["entityReference"]);
            }
            else
            {
                entity = EntityPersistence.FromData(ctx, obj["entityReference"]);
            }
            return(entity);
        }
Exemple #3
0
        public static CdmManifestDefinition FromObject(CdmCorpusContext ctx, string name, string nameSpace, string path, ManifestContent dataObj)
        {
            // Determine name of the manifest
            var manifestName = !StringUtils.IsBlankByCdmStandard(dataObj.ManifestName) ? dataObj.ManifestName : dataObj.FolioName;

            // We haven't found the name in the file, use one provided in the call but without the suffixes
            if (StringUtils.IsBlankByCdmStandard(manifestName))
            {
                manifestName = name.Replace(PersistenceLayer.ManifestExtension, "").Replace(PersistenceLayer.FolioExtension, "");
            }

            var manifest = ctx.Corpus.MakeObject <CdmManifestDefinition>(CdmObjectType.ManifestDef, manifestName);

            manifest.Name        = name; // this is the document name which is assumed by constructor to be related to the the manifestName, but may not be
            manifest.FolderPath  = path;
            manifest.Namespace   = nameSpace;
            manifest.Explanation = dataObj.Explanation;

            if (!StringUtils.IsBlankByCdmStandard(dataObj.Schema))
            {
                manifest.Schema = dataObj.Schema;
            }
            if (DynamicObjectExtensions.HasProperty(dataObj, "JsonSchemaSemanticVersion") && !StringUtils.IsBlankByCdmStandard(dataObj.JsonSchemaSemanticVersion))
            {
                manifest.JsonSchemaSemanticVersion = dataObj.JsonSchemaSemanticVersion;
            }

            if (!StringUtils.IsBlankByCdmStandard(dataObj.DocumentVersion))
            {
                manifest.DocumentVersion = dataObj.DocumentVersion;
            }

            if (!StringUtils.IsBlankByCdmStandard(dataObj.ManifestName))
            {
                manifest.ManifestName = dataObj.ManifestName;
            }
            else if (!StringUtils.IsBlankByCdmStandard(dataObj.FolioName))
            {
                // Might be populated in the case of folio.cdm.json or manifest.cdm.json file.
                manifest.ManifestName = dataObj.FolioName;
            }

            Utils.AddListToCdmCollection(manifest.ExhibitsTraits, Utils.CreateTraitReferenceList(ctx, dataObj.ExhibitsTraits));

            if (dataObj.Imports != null)
            {
                foreach (var importObj in dataObj.Imports)
                {
                    manifest.Imports.Add(ImportPersistence.FromData(ctx, importObj));
                }
            }

            if (dataObj.Definitions != null)
            {
                for (int i = 0; i < dataObj.Definitions.Count; i++)
                {
                    dynamic d = dataObj.Definitions[i];
                    if (d["dataTypeName"] != null)
                    {
                        manifest.Definitions.Add(DataTypePersistence.FromData(ctx, d));
                    }
                    else if (d["purposeName"] != null)
                    {
                        manifest.Definitions.Add(PurposePersistence.FromData(ctx, d));
                    }
                    else if (d["attributeGroupName"] != null)
                    {
                        manifest.Definitions.Add(AttributeGroupPersistence.FromData(ctx, d));
                    }
                    else if (d["traitName"] != null)
                    {
                        manifest.Definitions.Add(TraitPersistence.FromData(ctx, d));
                    }
                    else if (d["entityShape"] != null)
                    {
                        manifest.Definitions.Add(ConstantEntityPersistence.FromData(ctx, d));
                    }
                    else if (d["entityName"] != null)
                    {
                        manifest.Definitions.Add(EntityPersistence.FromData(ctx, d));
                    }
                }
            }

            if (dataObj.LastFileStatusCheckTime != null)
            {
                manifest.LastFileStatusCheckTime = DateTimeOffset.Parse(dataObj.LastFileStatusCheckTime);
            }

            if (dataObj.LastFileModifiedTime != null)
            {
                manifest.LastFileModifiedTime = DateTimeOffset.Parse(dataObj.LastFileModifiedTime);
            }

            if (dataObj.LastChildFileModifiedTime != null)
            {
                manifest.LastChildFileModifiedTime = DateTimeOffset.Parse(dataObj.LastChildFileModifiedTime);
            }


            if (dataObj.Entities != null)
            {
                var fullPath = !StringUtils.IsBlankByCdmStandard(nameSpace) ? $"{nameSpace}:{path}" : path;
                foreach (var entityObj in dataObj.Entities)
                {
                    CdmEntityDeclarationDefinition entity = null;

                    if (entityObj["type"] != null)
                    {
                        if (entityObj["type"].ToString() == EntityDeclarationDefinitionType.LocalEntity)
                        {
                            entity = LocalEntityDeclarationPersistence.FromData(ctx, fullPath, entityObj);
                        }
                        else if (entityObj["type"].ToString() == EntityDeclarationDefinitionType.ReferencedEntity)
                        {
                            entity = ReferencedEntityDeclarationPersistence.FromData(ctx, fullPath, entityObj);
                        }
                        else
                        {
                            Logger.Error(ctx, Tag, nameof(FromObject), null, CdmLogCode.ErrPersistEntityDeclarationMissing, (string)entityObj["entityName"]);
                        }
                    }
                    else
                    {
                        // We see old structure of entity declaration, check for entity schema/declaration.
                        if (entityObj["entitySchema"] != null)
                        {
                            // Local entity declaration used to use entity schema.
                            entity = LocalEntityDeclarationPersistence.FromData(ctx, fullPath, entityObj);
                        }
                        else
                        {
                            // While referenced entity declaration used to use entity declaration.
                            entity = ReferencedEntityDeclarationPersistence.FromData(ctx, fullPath, entityObj);
                        }
                    }

                    manifest.Entities.Add(entity);
                }

                // Checks if incremental trait is needed from foundations.cdm.json
                ImportFoundationsIfIncrementalPartitionTraitExist(manifest);
            }

            if (dataObj.Relationships != null)
            {
                foreach (var rel in dataObj.Relationships)
                {
                    manifest.Relationships.Add(E2ERelationshipPersistence.FromData(ctx, rel));
                }
            }

            if (dataObj.SubManifests != null)
            {
                foreach (var subManifest in dataObj.SubManifests)
                {
                    manifest.SubManifests.Add(ManifestDeclarationPersistence.FromData(ctx, subManifest));
                }
            }
            // Might be populated in the case of folio.cdm.json or manifest.cdm.json file.
            else if (dataObj.SubFolios != null)
            {
                foreach (var subFolio in dataObj.SubFolios)
                {
                    manifest.SubManifests.Add(ManifestDeclarationPersistence.FromData(ctx, subFolio));
                }
            }

            return(manifest);
        }
Exemple #4
0
        public static CdmDocumentDefinition FromObject(CdmCorpusContext ctx, string name, string nameSpace, string path, DocumentContent obj)
        {
            var doc = ctx.Corpus.MakeObject <CdmDocumentDefinition>(CdmObjectType.DocumentDef, name);

            doc.FolderPath = path;
            doc.Namespace  = nameSpace;

            if (!StringUtils.IsBlankByCdmStandard(obj.Schema))
            {
                doc.Schema = obj.Schema;
            }

            if (!StringUtils.IsBlankByCdmStandard(obj.DocumentVersion))
            {
                doc.DocumentVersion = obj.DocumentVersion;
            }

            if (obj.Imports != null)
            {
                foreach (var importObj in obj.Imports)
                {
                    doc.Imports.Add(ImportPersistence.FromData(ctx, importObj));
                }
            }

            if (obj.Definitions != null)
            {
                for (int i = 0; i < obj.Definitions.Count; i++)
                {
                    dynamic d = obj.Definitions[i];
                    if (d["dataTypeName"] != null)
                    {
                        doc.Definitions.Add(DataTypePersistence.FromData(ctx, d));
                    }
                    else if (d["purposeName"] != null)
                    {
                        doc.Definitions.Add(PurposePersistence.FromData(ctx, d));
                    }
                    else if (d["attributeGroupName"] != null)
                    {
                        doc.Definitions.Add(AttributeGroupPersistence.FromData(ctx, d));
                    }
                    else if (d["traitName"] != null)
                    {
                        doc.Definitions.Add(TraitPersistence.FromData(ctx, d));
                    }
                    else if (d["traitGroupName"] != null)
                    {
                        doc.Definitions.Add(TraitGroupPersistence.FromData(ctx, d));
                    }
                    else if (d["entityShape"] != null)
                    {
                        doc.Definitions.Add(ConstantEntityPersistence.FromData(ctx, d));
                    }
                    else if (d["entityName"] != null)
                    {
                        doc.Definitions.Add(EntityPersistence.FromData(ctx, d));
                    }
                }
            }

            var isResolvedDoc = false;

            if (doc.Definitions.Count == 1 && doc.Definitions[0] is CdmEntityDefinition entity)
            {
                var resolvedTrait = entity.ExhibitsTraits.Item("has.entitySchemaAbstractionLevel");
                // Tries to figure out if the document is in resolved form by looking for the schema abstraction trait
                // or the presence of the attribute context.
                isResolvedDoc = resolvedTrait != null && string.Equals((resolvedTrait as CdmTraitReference).Arguments[0].Value, "resolved");
                isResolvedDoc = isResolvedDoc || entity.AttributeContext != null;
            }

            if (!StringUtils.IsBlankByCdmStandard(obj.JsonSchemaSemanticVersion))
            {
                doc.JsonSchemaSemanticVersion = obj.JsonSchemaSemanticVersion;
                if (CompareJsonSemanticVersion(ctx, doc.JsonSchemaSemanticVersion) > 0)
                {
                    if (isResolvedDoc)
                    {
                        Logger.Warning(ctx, Tag, nameof(FromObject), null, CdmLogCode.WarnPersistUnsupportedJsonSemVer, JsonSemanticVersion, doc.JsonSchemaSemanticVersion);
                    }
                    else
                    {
                        Logger.Error(ctx, Tag, nameof(FromObject), null, CdmLogCode.ErrPersistUnsupportedJsonSemVer, JsonSemanticVersion, doc.JsonSchemaSemanticVersion);
                    }
                }
            }
            else
            {
                Logger.Warning(ctx, Tag, nameof(FromObject), null, CdmLogCode.WarnPersistJsonSemVerMandatory);
            }

            return(doc);
        }
Exemple #5
0
        public static CdmDocumentDefinition FromObject(CdmCorpusContext ctx, string name, string nameSpace, string path, DocumentContent obj)
        {
            var doc = ctx.Corpus.MakeObject <CdmDocumentDefinition>(CdmObjectType.DocumentDef, name);

            doc.FolderPath = path;
            doc.Namespace  = nameSpace;

            if (!string.IsNullOrEmpty(obj.Schema))
            {
                doc.Schema = obj.Schema;
            }

            if (!string.IsNullOrEmpty(obj.DocumentVersion))
            {
                doc.DocumentVersion = obj.DocumentVersion;
            }

            if (obj.Imports != null)
            {
                foreach (var importObj in obj.Imports)
                {
                    doc.Imports.Add(ImportPersistence.FromData(ctx, importObj));
                }
            }

            if (obj.Definitions != null)
            {
                for (int i = 0; i < obj.Definitions.Count; i++)
                {
                    dynamic d = obj.Definitions[i];
                    if (d["dataTypeName"] != null)
                    {
                        doc.Definitions.Add(DataTypePersistence.FromData(ctx, d));
                    }
                    else if (d["purposeName"] != null)
                    {
                        doc.Definitions.Add(PurposePersistence.FromData(ctx, d));
                    }
                    else if (d["attributeGroupName"] != null)
                    {
                        doc.Definitions.Add(AttributeGroupPersistence.FromData(ctx, d));
                    }
                    else if (d["traitName"] != null)
                    {
                        doc.Definitions.Add(TraitPersistence.FromData(ctx, d));
                    }
                    else if (d["entityShape"] != null)
                    {
                        doc.Definitions.Add(ConstantEntityPersistence.FromData(ctx, d));
                    }
                    else if (d["entityName"] != null)
                    {
                        doc.Definitions.Add(EntityPersistence.FromData(ctx, d));
                    }
                }
            }

            var isResolvedDoc = false;

            if (doc.Definitions.Count == 1 && doc.Definitions[0] is CdmEntityDefinition entity)
            {
                var resolvedTrait = entity.ExhibitsTraits.Item("has.entitySchemaAbstractionLevel");
                // Tries to figure out if the document is in resolved form by looking for the schema abstraction trait
                // or the presence of the attribute context.
                isResolvedDoc = resolvedTrait != null && string.Equals(resolvedTrait.Arguments[0].Value, "resolved");
                isResolvedDoc = isResolvedDoc || entity.AttributeContext != null;
            }

            if (!string.IsNullOrEmpty(obj.JsonSchemaSemanticVersion))
            {
                doc.JsonSchemaSemanticVersion = obj.JsonSchemaSemanticVersion;
                if (CompareJsonSemanticVersion(ctx, doc.JsonSchemaSemanticVersion) > 0)
                {
                    var message = $"This ObjectModel version supports json semantic version {JsonSemanticVersion} at maximum. Trying to load a document with version {doc.JsonSchemaSemanticVersion}.";
                    if (isResolvedDoc)
                    {
                        Logger.Warning(nameof(DocumentPersistence), ctx, message, nameof(FromData));
                    }
                    else
                    {
                        Logger.Error(nameof(DocumentPersistence), ctx, message, nameof(FromData));
                    }
                }
            }
            else
            {
                Logger.Warning(nameof(DocumentPersistence), ctx, "jsonSemanticVersion is a required property of a document.", nameof(FromData));
            }

            return(doc);
        }