示例#1
0
        public static CdmEntityDefinition FromData(CdmCorpusContext ctx, JToken obj)
        {
            var entity = ctx.Corpus.MakeObject <CdmEntityDefinition>(CdmObjectType.EntityDef, (string)obj["entityName"]);

            entity.ExtendsEntity = EntityReferencePersistence.FromData(ctx, obj["extendsEntity"]);
            entity.ExtendsEntityResolutionGuidance = AttributeResolutionGuidancePersistence.FromData(ctx, obj["extendsEntityResolutionGuidance"]);

            if (obj["explanation"] != null)
            {
                entity.Explanation = (string)obj["explanation"];
            }

            Utils.AddListToCdmCollection(entity.ExhibitsTraits, Utils.CreateTraitReferenceList(ctx, obj["exhibitsTraits"]));
            if (obj["attributeContext"] != null)
            {
                entity.AttributeContext = AttributeContextPersistence.FromData(ctx, obj["attributeContext"]);
            }

            Utils.AddListToCdmCollection(entity.Attributes, Utils.CreateAttributeList(ctx, obj["hasAttributes"]));
            entity.SourceName  = (string)obj["sourceName"];
            entity.DisplayName = (string)obj["displayName"];
            entity.Description = (string)obj["description"];
            entity.Version     = (string)obj["version"];
            entity.CdmSchemas  = obj["cdmSchemas"]?.ToObject <List <string> >();

            return(entity);
        }
示例#2
0
 /// <summary>
 /// Creates a CDM object from a JSON object
 /// </summary>
 public static dynamic CreateConstant(CdmCorpusContext ctx, dynamic obj)
 {
     if (obj == null)
     {
         return(null);
     }
     if (obj is JValue)
     {
         return(obj.Value);
     }
     else if (obj is JObject)
     {
         if (obj["purpose"] != null || obj["dataType"] != null || obj["entity"] != null)
         {
             if (obj["dataType"] != null)
             {
                 return(TypeAttributePersistence.FromData(ctx, obj));
             }
             else if (obj["entity"] != null)
             {
                 return(EntityAttributePersistence.FromData(ctx, obj));
             }
             else
             {
                 return(null);
             }
         }
         else if (obj["purposeReference"] != null)
         {
             return(PurposeReferencePersistence.FromData(ctx, obj));
         }
         else if (obj["traitReference"] != null)
         {
             return(TraitReferencePersistence.FromData(ctx, obj));
         }
         else if (obj["dataTypeReference"] != null)
         {
             return(DataTypeReferencePersistence.FromData(ctx, obj));
         }
         else if (obj["entityReference"] != null)
         {
             return(EntityReferencePersistence.FromData(ctx, obj));
         }
         else if (obj["attributeGroupReference"] != null)
         {
             return(AttributeGroupReferencePersistence.FromData(ctx, obj));
         }
         else
         {
             return(obj);
         }
     }
     else
     {
         return(obj);
     }
 }
        public static CdmConstantEntityDefinition FromData(CdmCorpusContext ctx, JToken obj)
        {
            var constantEntity = ctx.Corpus.MakeObject <CdmConstantEntityDefinition>(CdmObjectType.ConstantEntityDef, (string)obj["constantEntityName"]);

            if (obj["explanation"] != null)
            {
                constantEntity.Explanation = (string)obj["explanation"];
            }
            if (obj["constantValues"] != null)
            {
                constantEntity.ConstantValues = obj["constantValues"].ToObject <List <List <string> > >();
            }
            constantEntity.EntityShape = EntityReferencePersistence.FromData(ctx, obj["entityShape"]);
            return(constantEntity);
        }
示例#4
0
        public static CdmEntityAttributeDefinition FromData(CdmCorpusContext ctx, JToken obj)
        {
            var entityAttribute = ctx.Corpus.MakeObject <CdmEntityAttributeDefinition>(CdmObjectType.EntityAttributeDef, (string)obj["name"]);

            if (obj["explanation"] != null)
            {
                entityAttribute.Explanation = (string)obj["explanation"];
            }

            entityAttribute.Entity = EntityReferencePersistence.FromData(ctx, obj["entity"]);

            entityAttribute.Purpose = PurposeReferencePersistence.FromData(ctx, obj["purpose"]);
            Utils.AddListToCdmCollection(entityAttribute.AppliedTraits, Utils.CreateTraitReferenceList(ctx, obj["appliedTraits"]));
            entityAttribute.ResolutionGuidance = AttributeResolutionGuidancePersistence.FromData(ctx, obj["resolutionGuidance"]);
            return(entityAttribute);
        }
示例#5
0
        public static CdmEntityAttributeDefinition FromData(CdmCorpusContext ctx, JToken obj)
        {
            var entityAttribute = ctx.Corpus.MakeObject <CdmEntityAttributeDefinition>(CdmObjectType.EntityAttributeDef, (string)obj["name"]);

            entityAttribute.Description = Utils.PropertyFromDataToString(obj["description"]);
            entityAttribute.DisplayName = Utils.PropertyFromDataToString(obj["displayName"]);
            entityAttribute.Explanation = Utils.PropertyFromDataToString(obj["explanation"]);

            entityAttribute.Cardinality = Utils.CardinalitySettingsFromData(obj["cardinality"], entityAttribute);

            entityAttribute.IsPolymorphicSource = (bool?)obj["isPolymorphicSource"];

            bool isProjection = obj["entity"] != null &&
                                !(obj["entity"] is JValue) &&
                                obj["entity"]["source"] != null;

            if (isProjection)
            {
                CdmEntityReference inlineEntityRef = ctx.Corpus.MakeObject <CdmEntityReference>(CdmObjectType.EntityRef, null);
                inlineEntityRef.ExplicitReference = ProjectionPersistence.FromData(ctx, obj["entity"]);
                entityAttribute.Entity            = inlineEntityRef;
            }
            else
            {
                entityAttribute.Entity = EntityReferencePersistence.FromData(ctx, obj["entity"]);
            }

            entityAttribute.Purpose = PurposeReferencePersistence.FromData(ctx, obj["purpose"]);
            Utils.AddListToCdmCollection(entityAttribute.AppliedTraits, Utils.CreateTraitReferenceList(ctx, obj["appliedTraits"]));
            // ignore resolution guidance if the entity is a projection
            if (obj["resolutionGuidance"] != null && isProjection)
            {
                Logger.Error((ResolveContext)ctx, Tag, nameof(FromData), null, CdmLogCode.ErrPersistEntityAttrUnsupported, entityAttribute.Name);
            }
            else
            {
                entityAttribute.ResolutionGuidance = AttributeResolutionGuidancePersistence.FromData(ctx, obj["resolutionGuidance"]);
            }
            return(entityAttribute);
        }
        public static CdmAttributeContext FromData(CdmCorpusContext ctx, dynamic obj)
        {
            if (obj == null)
            {
                return(null);
            }

            CdmAttributeContext attributeContext = ctx.Corpus.MakeObject <CdmAttributeContext>(CdmObjectType.AttributeContextDef, obj.Value <string>("name"), false);

            attributeContext.Type = MapTypeNameToEnum(obj.Value <string>("type"));
            if (obj.Value <string>("parent") != null)
            {
                attributeContext.Parent = AttributeContextReferencePersistence.FromData(ctx, obj.Value <string>("parent"));
            }
            string explanation = obj.Value <string>("explanation");

            if (!string.IsNullOrEmpty(explanation))
            {
                attributeContext.Explanation = explanation;
            }
            if (obj.Value <string>("definition") != null)
            {
                switch (attributeContext.Type)
                {
                case CdmAttributeContextType.Entity:
                case CdmAttributeContextType.EntityReferenceExtends:
                    attributeContext.Definition = EntityReferencePersistence.FromData(ctx, obj.Value <string>("definition"));
                    break;

                case CdmAttributeContextType.AttributeGroup:
                    attributeContext.Definition = AttributeGroupReferencePersistence.FromData(ctx, obj.Value <string>("definition"));
                    break;

                case CdmAttributeContextType.AddedAttributeSupporting:
                case CdmAttributeContextType.AddedAttributeIdentity:
                case CdmAttributeContextType.AddedAttributeExpansionTotal:
                case CdmAttributeContextType.AddedAttributeSelectedType:
                case CdmAttributeContextType.AttributeDefinition:
                    attributeContext.Definition = AttributeReferencePersistence.FromData(ctx, obj.Value <string>("definition"));
                    break;
                }
            }
            // i know the trait collection names look wrong. but I wanted to use the def baseclass
            Utils.AddListToCdmCollection(attributeContext.ExhibitsTraits, Utils.CreateTraitReferenceList(ctx, obj.Value <JToken>("appliedTraits")));
            if (obj.Value <JToken>("contents") != null)
            {
                for (int i = 0; i < obj.Value <JToken>("contents").Count; i++)
                {
                    JToken ct = obj.Value <JToken>("contents")[i];
                    if (ct is JValue)
                    {
                        attributeContext.Contents.Add(AttributeReferencePersistence.FromData(ctx, (string)ct));
                    }
                    else
                    {
                        attributeContext.Contents.Add(FromData(ctx, ct));
                    }
                }
            }
            return(attributeContext);
        }
示例#7
0
        public static CdmProjection FromData(CdmCorpusContext ctx, JToken obj)
        {
            if (obj == null)
            {
                return(null);
            }

            CdmProjection projection = ctx.Corpus.MakeObject <CdmProjection>(CdmObjectType.ProjectionDef);

            CdmEntityReference source = EntityReferencePersistence.FromData(ctx, obj["source"]);

            if (obj["explanation"] != null)
            {
                projection.Explanation = (string)obj["explanation"];
            }

            projection.Condition       = obj["condition"]?.ToString();
            projection.RunSequentially = (bool?)obj["runSequentially"];

            if (obj["operations"] != null)
            {
                List <JObject> operationJsons = obj["operations"]?.ToObject <List <JObject> >();
                foreach (JObject operationJson in operationJsons)
                {
                    string type = (string)operationJson["$type"];
                    switch (type)
                    {
                    case "addCountAttribute":
                        CdmOperationAddCountAttribute addCountAttributeOp = OperationAddCountAttributePersistence.FromData(ctx, operationJson);
                        projection.Operations.Add(addCountAttributeOp);
                        break;

                    case "addSupportingAttribute":
                        CdmOperationAddSupportingAttribute addSupportingAttributeOp = OperationAddSupportingAttributePersistence.FromData(ctx, operationJson);
                        projection.Operations.Add(addSupportingAttributeOp);
                        break;

                    case "addTypeAttribute":
                        CdmOperationAddTypeAttribute addTypeAttributeOp = OperationAddTypeAttributePersistence.FromData(ctx, operationJson);
                        projection.Operations.Add(addTypeAttributeOp);
                        break;

                    case "excludeAttributes":
                        CdmOperationExcludeAttributes excludeAttributesOp = OperationExcludeAttributesPersistence.FromData(ctx, operationJson);
                        projection.Operations.Add(excludeAttributesOp);
                        break;

                    case "arrayExpansion":
                        CdmOperationArrayExpansion arrayExpansionOp = OperationArrayExpansionPersistence.FromData(ctx, operationJson);
                        projection.Operations.Add(arrayExpansionOp);
                        break;

                    case "combineAttributes":
                        CdmOperationCombineAttributes combineAttributesOp = OperationCombineAttributesPersistence.FromData(ctx, operationJson);
                        projection.Operations.Add(combineAttributesOp);
                        break;

                    case "renameAttributes":
                        CdmOperationRenameAttributes renameAttributesOp = OperationRenameAttributesPersistence.FromData(ctx, operationJson);
                        projection.Operations.Add(renameAttributesOp);
                        break;

                    case "replaceAsForeignKey":
                        CdmOperationReplaceAsForeignKey replaceAsForeignKeyOp = OperationReplaceAsForeignKeyPersistence.FromData(ctx, operationJson);
                        projection.Operations.Add(replaceAsForeignKeyOp);
                        break;

                    case "includeAttributes":
                        CdmOperationIncludeAttributes includeAttributesOp = OperationIncludeAttributesPersistence.FromData(ctx, operationJson);
                        projection.Operations.Add(includeAttributesOp);
                        break;

                    case "addAttributeGroup":
                        CdmOperationAddAttributeGroup addAttributeGroupOp = OperationAddAttributeGroupPersistence.FromData(ctx, operationJson);
                        projection.Operations.Add(addAttributeGroupOp);
                        break;

                    default:
                        Logger.Error(nameof(ProjectionPersistence), ctx, $"Invalid operation type '{type}'.", nameof(FromData));
                        break;
                    }
                }
            }

            projection.Source = source;

            return(projection);
        }
示例#8
0
        public static Projection ToData(CdmProjection instance, ResolveOptions resOpt, CopyOptions options)
        {
            if (instance == null)
            {
                return(null);
            }

            dynamic source = null;

            if (instance.Source != null && instance.Source.GetType() == typeof(string))
            {
                source = instance.Source;
            }
            else if (instance.Source != null && !string.IsNullOrWhiteSpace(instance.Source.NamedReference) && instance.Source.ExplicitReference == null)
            {
                source = instance.Source.NamedReference;
            }
            else if (instance.Source != null && instance.Source.GetType() == typeof(CdmEntityReference))
            {
                source = EntityReferencePersistence.ToData(instance.Source, resOpt, options);
            }

            List <OperationBase> operations = null;

            if (instance.Operations != null && instance.Operations.Count > 0)
            {
                operations = new List <OperationBase>();
                foreach (CdmOperationBase operation in instance.Operations)
                {
                    switch (operation.ObjectType)
                    {
                    case CdmObjectType.OperationAddCountAttributeDef:
                        OperationAddCountAttribute addCountAttributeOp = OperationAddCountAttributePersistence.ToData(operation as CdmOperationAddCountAttribute, resOpt, options);
                        operations.Add(addCountAttributeOp);
                        break;

                    case CdmObjectType.OperationAddSupportingAttributeDef:
                        OperationAddSupportingAttribute addSupportingAttributeOp = OperationAddSupportingAttributePersistence.ToData(operation as CdmOperationAddSupportingAttribute, resOpt, options);
                        operations.Add(addSupportingAttributeOp);
                        break;

                    case CdmObjectType.OperationAddTypeAttributeDef:
                        OperationAddTypeAttribute addTypeAttributeOp = OperationAddTypeAttributePersistence.ToData(operation as CdmOperationAddTypeAttribute, resOpt, options);
                        operations.Add(addTypeAttributeOp);
                        break;

                    case CdmObjectType.OperationExcludeAttributesDef:
                        OperationExcludeAttributes excludeAttributesOp = OperationExcludeAttributesPersistence.ToData(operation as CdmOperationExcludeAttributes, resOpt, options);
                        operations.Add(excludeAttributesOp);
                        break;

                    case CdmObjectType.OperationArrayExpansionDef:
                        OperationArrayExpansion arrayExpansionOp = OperationArrayExpansionPersistence.ToData(operation as CdmOperationArrayExpansion, resOpt, options);
                        operations.Add(arrayExpansionOp);
                        break;

                    case CdmObjectType.OperationCombineAttributesDef:
                        OperationCombineAttributes combineAttributesOp = OperationCombineAttributesPersistence.ToData(operation as CdmOperationCombineAttributes, resOpt, options);
                        operations.Add(combineAttributesOp);
                        break;

                    case CdmObjectType.OperationRenameAttributesDef:
                        OperationRenameAttributes renameAttributesOp = OperationRenameAttributesPersistence.ToData(operation as CdmOperationRenameAttributes, resOpt, options);
                        operations.Add(renameAttributesOp);
                        break;

                    case CdmObjectType.OperationReplaceAsForeignKeyDef:
                        OperationReplaceAsForeignKey replaceAsForeignKeyOp = OperationReplaceAsForeignKeyPersistence.ToData(operation as CdmOperationReplaceAsForeignKey, resOpt, options);
                        operations.Add(replaceAsForeignKeyOp);
                        break;

                    case CdmObjectType.OperationIncludeAttributesDef:
                        OperationIncludeAttributes includeAttributesOp = OperationIncludeAttributesPersistence.ToData(operation as CdmOperationIncludeAttributes, resOpt, options);
                        operations.Add(includeAttributesOp);
                        break;

                    case CdmObjectType.OperationAddAttributeGroupDef:
                        OperationAddAttributeGroup addAttributeGroupOp = OperationAddAttributeGroupPersistence.ToData(operation as CdmOperationAddAttributeGroup, resOpt, options);
                        operations.Add(addAttributeGroupOp);
                        break;

                    default:
                        OperationBase baseOp = new OperationBase();
                        baseOp.Type = OperationTypeConvertor.OperationTypeToString(CdmOperationType.Error);
                        operations.Add(baseOp);
                        break;
                    }
                }
            }

            return(new Projection
            {
                Explanation = instance.Explanation,
                Source = source,
                Operations = operations,
                Condition = instance.Condition,
                RunSequentially = instance.RunSequentially
            });
        }
        public static CdmEntityAttributeDefinition FromData(CdmCorpusContext ctx, JToken obj)
        {
            var entityAttribute = ctx.Corpus.MakeObject <CdmEntityAttributeDefinition>(CdmObjectType.EntityAttributeDef, (string)obj["name"]);

            entityAttribute.Description = Utils.PropertyFromDataToString(obj["description"]);
            entityAttribute.DisplayName = Utils.PropertyFromDataToString(obj["displayName"]);
            entityAttribute.Explanation = Utils.PropertyFromDataToString(obj["explanation"]);

            if (obj["cardinality"] != null)
            {
                string minCardinality = null;
                if (obj["cardinality"]["minimum"] != null)
                {
                    minCardinality = (string)obj["cardinality"]["minimum"];
                }

                string maxCardinality = null;
                if (obj["cardinality"]["maximum"] != null)
                {
                    maxCardinality = (string)obj["cardinality"]["maximum"];
                }

                if (string.IsNullOrWhiteSpace(minCardinality) || string.IsNullOrWhiteSpace(maxCardinality))
                {
                    Logger.Error(nameof(EntityAttributePersistence), ctx, $"Both minimum and maximum are required for the Cardinality property.", nameof(FromData));
                }

                if (!CardinalitySettings.IsMinimumValid(minCardinality))
                {
                    Logger.Error(nameof(EntityAttributePersistence), ctx, $"Invalid minimum cardinality {minCardinality}.", nameof(FromData));
                }

                if (!CardinalitySettings.IsMaximumValid(maxCardinality))
                {
                    Logger.Error(nameof(EntityAttributePersistence), ctx, $"Invalid maximum cardinality {maxCardinality}.", nameof(FromData));
                }

                if (!string.IsNullOrWhiteSpace(minCardinality) &&
                    !string.IsNullOrWhiteSpace(maxCardinality) &&
                    CardinalitySettings.IsMinimumValid(minCardinality) &&
                    CardinalitySettings.IsMinimumValid(maxCardinality))
                {
                    entityAttribute.Cardinality = new CardinalitySettings(entityAttribute)
                    {
                        Minimum = minCardinality,
                        Maximum = maxCardinality
                    };
                }
            }

            entityAttribute.IsPolymorphicSource = (bool?)obj["isPolymorphicSource"];

            bool isProjection = obj["entity"] != null &&
                                !(obj["entity"] is JValue) &&
                                obj["entity"]["source"] != null;

            if (isProjection)
            {
                CdmProjection projection = ProjectionPersistence.FromData(ctx, obj["entity"]);
                projection.Owner = entityAttribute;

                CdmEntityReference inlineEntityRef = ctx.Corpus.MakeObject <CdmEntityReference>(CdmObjectType.EntityRef, null);
                inlineEntityRef.ExplicitReference = projection;
                entityAttribute.Entity            = inlineEntityRef;
            }
            else
            {
                entityAttribute.Entity = EntityReferencePersistence.FromData(ctx, obj["entity"]);
            }

            entityAttribute.Purpose = PurposeReferencePersistence.FromData(ctx, obj["purpose"]);
            Utils.AddListToCdmCollection(entityAttribute.AppliedTraits, Utils.CreateTraitReferenceList(ctx, obj["appliedTraits"]));
            // ignore resolution guidance if the entity is a projection
            if (obj["resolutionGuidance"] != null && isProjection)
            {
                Logger.Error(nameof(EntityAttributePersistence), ctx, $"The EntityAttribute {entityAttribute.Name} is projection based. Resolution guidance is not supported with a projection.");
            }
            else
            {
                entityAttribute.ResolutionGuidance = AttributeResolutionGuidancePersistence.FromData(ctx, obj["resolutionGuidance"]);
            }
            return(entityAttribute);
        }