Exemple #1
0
        private static HashSet <string> FetchTraitNamedReferences(CdmTraitCollection traits)
        {
            HashSet <string> namedReferences = new HashSet <string>();

            traits.AllItems.ForEach(trait => namedReferences.Add(trait.NamedReference));

            return(namedReferences);
        }
        private static HashSet <string> FetchTraitNamedReferences(CdmTraitCollection traits)
        {
            HashSet <string> namedReferences = new HashSet <string>();

            foreach (var trait in traits)
            {
                namedReferences.Add(trait.NamedReference);
            }
            return(namedReferences);
        }
Exemple #3
0
        public static async Task <CdmReferencedEntityDeclarationDefinition> FromData(CdmCorpusContext ctx, ReferenceEntity obj, string location)
        {
            var referencedEntity = ctx.Corpus.MakeObject <CdmReferencedEntityDeclarationDefinition>(CdmObjectType.ReferencedEntityDeclarationDef, obj.Name);

            referencedEntity.EntityName = obj.Name;

            var corpusPath = ctx.Corpus.Storage.AdapterPathToCorpusPath(location);

            if (corpusPath == null)
            {
                Logger.Error((ResolveContext)ctx, Tag, nameof(FromData), null, CdmLogCode.ErrPersistModelJsonRefEntityInvalidLocation, location, referencedEntity.EntityName);
                return(null);
            }

            referencedEntity.EntityPath              = $"{corpusPath}/{obj.Source}";
            referencedEntity.Explanation             = obj.Description;
            referencedEntity.LastFileModifiedTime    = obj.LastFileModifiedTime;
            referencedEntity.LastFileStatusCheckTime = obj.LastFileStatusCheckTime;

            await Utils.ProcessAnnotationsFromData(ctx, obj, referencedEntity.ExhibitsTraits);

            if (obj.IsHidden == true)
            {
                var isHiddenTrait = ctx.Corpus.MakeRef <CdmTraitReference>(CdmObjectType.TraitRef, "is.hidden", true);
                isHiddenTrait.IsFromProperty = true;
                referencedEntity.ExhibitsTraits.Add(isHiddenTrait);
            }

            var trait = ctx.Corpus.MakeRef <CdmTraitReference>(CdmObjectType.TraitRef, "is.propertyContent.multiTrait", false);

            trait.IsFromProperty = true;
            var argument = ctx.Corpus.MakeObject <CdmArgumentDefinition>(CdmObjectType.ArgumentDef, "modelId");

            argument.Value = obj.ModelId;
            trait.Arguments.Add(argument);
            referencedEntity.ExhibitsTraits.Add(trait);

            var extensionTraitDefList = new List <CdmTraitDefinition>();
            var extensionTraits       = new CdmTraitCollection(ctx, referencedEntity);

            ExtensionHelper.ProcessExtensionFromJson(ctx, obj, extensionTraits, extensionTraitDefList);

            if (extensionTraitDefList.Count > 0)
            {
                Logger.Warning(ctx, Tag, nameof(FromData), null, CdmLogCode.WarnPersistCustomExtNotSupported, referencedEntity.EntityName);
            }

            return(referencedEntity);
        }
Exemple #4
0
        private void GetTraitCollection(CdmTraitCollection traitCollection)
        {
            foreach (CdmTraitReferenceBase trait in traitCollection)
            {
                string attribTraits = trait.NamedReference;
                bldr.AppendLine(attribTraits);

                if (trait is CdmTraitReference)
                {
                    foreach (CdmArgumentDefinition args in (trait as CdmTraitReference).Arguments)
                    {
                        GetArgumentValues(args);
                    }
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// Processes extensions from an object which was obtained from a "model.json" file.
        /// From every extension found, it's value (name, value) is added to traitRefSet,
        /// and it's definition (name, type) is added to extensionTraitDefList.
        /// </summary>
        /// <param name="ctx"> The context </param>
        /// <param name="sourceObject"> The object obtained from "model.json" file.</param>
        /// <param name="traitRefSet"> The list of extensions found, in the form of (name & value).</param>
        /// <param name="extensionTraitDefList"> The list of definitions. For each extension, it's definition is added to this list (name & type).</param>
        /// <param name="localExtensionTraitDefList"> Same as extensionTraitDefList but limited to extensions inside one document.</param>
        public static void ProcessExtensionFromJson(
            CdmCorpusContext ctx,
            MetadataObject sourceObject,
            CdmTraitCollection traitRefSet,
            List <CdmTraitDefinition> extensionTraitDefList,
            List <CdmTraitDefinition> localExtensionTraitDefList = null)
        {
            var extensions = sourceObject.ExtensionFields;

            foreach (JProperty extensionAsJProperty in extensions.Children())
            {
                string             traitName         = AddExtensionTraitNamePrefix(extensionAsJProperty.Name);
                CdmTraitDefinition extensionTraitDef = extensionTraitDefList.Find((CdmTraitDefinition trait) => trait.TraitName == traitName);
                bool traitExists = extensionTraitDef != null;
                if (!traitExists)
                {
                    extensionTraitDef = ctx.Corpus.MakeObject <CdmTraitDefinition>(CdmObjectType.TraitDef, traitName);
                    extensionTraitDef.ExtendsTrait = ctx.Corpus.MakeObject <CdmTraitReference>(CdmObjectType.TraitRef, "is.extension", true);
                }

                CdmTraitReference extensionTraitRef = ctx.Corpus.MakeObject <CdmTraitReference>(CdmObjectType.TraitRef, traitName);
                var extensionValue = extensionAsJProperty.Value;
                var extensionType  = extensionValue.Type;

                var shouldLookIntoProperties = extensionType == JTokenType.Object;

                if (shouldLookIntoProperties)
                {
                    var extensionValueAsJObject = extensionValue as JObject;
                    // extensionProperty.Name is here the equivalent of extensionProperty from TS project
                    foreach (JProperty extensionProperty in extensionValueAsJObject.Children())
                    {
                        // for every extensionProperty will have to save the (name, value) pair into extensionArgument,
                        // which will be saved in extensionTraitRef (the entity that will contain the data)
                        // (name, type) will be saved in extensionParameter,
                        // which will be saved in extensionTraitDef (the definition of the data, that can be saved in a schema file)
                        JToken extensionPropertyValue = extensionProperty.Value;

                        if (extensionPropertyValue == null)
                        {
                            continue;
                        }

                        CdmArgumentDefinition extensionArgument = ctx.Corpus.MakeObject <CdmArgumentDefinition>(
                            CdmObjectType.ArgumentDef,
                            extensionProperty.Name
                            );
                        CdmParameterDefinition extensionParameter = extensionTraitDef.Parameters.AllItems.Find(
                            (CdmParameterDefinition parameter) => parameter.Name == extensionProperty.Name);
                        bool parameterExists = extensionParameter != null;
                        if (!parameterExists)
                        {
                            extensionParameter             = ctx.Corpus.MakeObject <CdmParameterDefinition>(CdmObjectType.ParameterDef, extensionProperty.Name);
                            extensionParameter.DataTypeRef = ctx.Corpus.MakeObject <CdmDataTypeReference>(CdmObjectType.DataTypeRef,
                                                                                                          ConvertJTokenTypeToExpectedString(extensionPropertyValue.Type),
                                                                                                          true);
                        }

                        if (extensionPropertyValue is JValue extensionPropertyValueAsJValue)
                        {
                            extensionArgument.Value = extensionPropertyValueAsJValue.Value;
                        }
                        else
                        {
                            extensionArgument.Value = extensionPropertyValue;
                        }

                        extensionTraitRef.Arguments.Add(extensionArgument);
                        if (!parameterExists)
                        {
                            extensionTraitDef.Parameters.Add(extensionParameter);
                        }
                    }
                }
                else
                {
                    CdmArgumentDefinition  extensionArgument  = ctx.Corpus.MakeObject <CdmArgumentDefinition>(CdmObjectType.ArgumentDef, traitName);
                    CdmParameterDefinition extensionParameter = extensionTraitDef.Parameters.AllItems.Find((CdmParameterDefinition parameter) => parameter.Name == traitName);
                    bool parameterExists = extensionParameter != null;
                    if (!parameterExists)
                    {
                        extensionParameter             = ctx.Corpus.MakeObject <CdmParameterDefinition>(CdmObjectType.ParameterDef, traitName);
                        extensionParameter.DataTypeRef = ctx.Corpus.MakeObject <CdmDataTypeReference>(
                            CdmObjectType.DataTypeRef,
                            ConvertJTokenTypeToExpectedString(extensionType),
                            true);
                    }

                    if (extensionValue is JValue extensionValuePrimite)
                    {
                        extensionArgument.Value = extensionValuePrimite.Value;
                    }
                    else
                    {
                        extensionArgument.Value = extensionValue;
                    }

                    extensionTraitRef.Arguments.Add(extensionArgument);
                    if (!parameterExists)
                    {
                        extensionTraitDef.Parameters.Add(extensionParameter);
                    }
                }

                if (!traitExists)
                {
                    extensionTraitDefList.Add(extensionTraitDef);
                }

                if (localExtensionTraitDefList != null)
                {
                    localExtensionTraitDefList.Add(extensionTraitDef);
                }

                traitRefSet.Add(extensionTraitRef);
            }
        }
        internal static async Task ProcessTraitsAndAnnotationsToData(CdmCorpusContext ctx, MetadataObject obj, CdmTraitCollection traits)
        {
            if (traits == null)
            {
                return;
            }

            var annotations = new List <Annotation>();
            var extensions  = new List <JToken>();

            foreach (var trait in traits)
            {
                if (ExtensionHelper.TraitRefIsExtension(trait))
                {
                    ExtensionHelper.ProcessExtensionTraitToObject(trait, obj);

                    continue;
                }
                if (trait.NamedReference == "is.modelConversion.otherAnnotations")
                {
                    foreach (var annotation in trait.Arguments[0].Value)
                    {
                        if (annotation is JObject jAnnotation)
                        {
                            annotations.Add(jAnnotation.ToObject <Annotation>());
                        }
                        else if (annotation is Annotation)
                        {
                            annotations.Add(annotation);
                        }
                        else
                        {
                            Logger.Warning(nameof(Utils), ctx, "Unsupported annotation type.");
                        }
                    }
                }
                else if (
                    !ignoredTraits.Contains(trait.NamedReference) &&
                    !trait.NamedReference.StartsWith("is.dataFormat") &&
                    !(modelJsonPropertyTraits.Contains(trait.NamedReference) && trait.IsFromProperty))
                {
                    var extension = CdmFolder.TraitReferencePersistence.ToData(trait, null, null);
                    extensions.Add(JToken.FromObject(extension, JsonSerializationUtil.JsonSerializer));
                }
            }

            if (annotations.Count > 0)
            {
                obj.Annotations = annotations;
            }

            if (extensions.Count > 0)
            {
                obj.Traits = extensions;
            }
        }
        internal static async Task ProcessAnnotationsFromData(CdmCorpusContext ctx, MetadataObject obj, CdmTraitCollection traits)
        {
            var multiTraitAnnotations = new List <Annotation>();

            if (obj.Annotations != null)
            {
                foreach (var element in obj.Annotations)
                {
                    if (!ShouldAnnotationGoIntoASingleTrait(element.Name))
                    {
                        multiTraitAnnotations.Add(element);
                    }
                    else
                    {
                        var innerTrait = ctx.Corpus.MakeObject <CdmTraitReference>(CdmObjectType.TraitRef, ConvertAnnotationToTrait(element.Name));
                        innerTrait.Arguments.Add(await ArgumentPersistence.FromData(ctx, element));
                        traits.Add(innerTrait);
                    }
                }

                if (multiTraitAnnotations.Count > 0)
                {
                    var trait = ctx.Corpus.MakeRef <CdmTraitReference>(CdmObjectType.TraitRef, "is.modelConversion.otherAnnotations", false);
                    trait.IsFromProperty = false;
                    var annotationsArgument = new CdmArgumentDefinition(ctx, "annotations")
                    {
                        Value = multiTraitAnnotations
                    };
                    trait.Arguments.Add(annotationsArgument);
                    traits.Add(trait);
                }
            }

            if (obj.Traits != null)
            {
                foreach (var trait in obj.Traits)
                {
                    var traitInstance = CdmFolder.TraitReferencePersistence.FromData(ctx, JToken.FromObject(trait));
                    traits.Add(traitInstance);
                }
            }
        }
Exemple #8
0
        internal static void ProcessTraitsAndAnnotationsToData(CdmCorpusContext ctx, MetadataObject obj, CdmTraitCollection traits)
        {
            if (traits == null)
            {
                return;
            }

            var annotations = new List <Annotation>();
            var extensions  = new List <JToken>();

            foreach (var trait in traits)
            {
                if (ExtensionHelper.TraitRefIsExtension(trait))
                {
                    // Safe to cast since extensions can only be trait refs, not trait group refs
                    ExtensionHelper.ProcessExtensionTraitToObject(trait as CdmTraitReference, obj);
                    continue;
                }

                if (trait.NamedReference == "is.modelConversion.otherAnnotations")
                {
                    // Safe to cast since "is.modelConversion.otherAnnotations" is a trait, not trait group
                    foreach (var annotation in (trait as CdmTraitReference).Arguments[0].Value)
                    {
                        if (annotation is JObject jAnnotation)
                        {
                            annotations.Add(jAnnotation.ToObject <Annotation>());
                        }
                        else if (annotation is NameValuePair)
                        {
                            Annotation element = new Annotation()
                            {
                                Name  = annotation.Name,
                                Value = annotation.Value
                            };
                            annotations.Add(element);
                        }
                        else
                        {
                            Logger.Warning(ctx, Tag, nameof(ProcessTraitsAndAnnotationsToData), null, CdmLogCode.WarnAnnotationTypeNotSupported);
                        }
                    }
                }
                else if (
                    !ignoredTraits.Contains(trait.NamedReference) &&
                    !trait.NamedReference.StartsWith("is.dataFormat") &&
                    !(modelJsonPropertyTraits.Contains(trait.NamedReference) && trait is CdmTraitReference && (trait as CdmTraitReference).IsFromProperty))
                {
                    var extension = trait is CdmTraitGroupReference?
                                    CdmFolder.TraitGroupReferencePersistence.ToData(trait as CdmTraitGroupReference, null, null) :
                                        CdmFolder.TraitReferencePersistence.ToData(trait as CdmTraitReference, null, null);

                    extensions.Add(JToken.FromObject(extension, JsonSerializationUtil.JsonSerializer));
                }
            }

            if (annotations.Count > 0)
            {
                obj.Annotations = annotations;
            }

            if (extensions.Count > 0)
            {
                obj.Traits = extensions;
            }
        }