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);
        }
Example #2
0
        public static async Task <CdmManifestDefinition> FromData(CdmCorpusContext ctx, string docName, string jsonData, CdmFolderDefinition folder)
        {
            var obj = JsonConvert.DeserializeObject <Model>(jsonData);

            return(await FromObject(ctx, obj, folder));
        }
 /// <summary>
 /// Constructs a CdmAttributeGroupReference.
 /// </summary>
 /// <param name="ctx">The context.</param>
 /// <param name="attributeGroup">The attribute group to reference.</param>
 /// <param name="simpleReference">Whether this reference is a simple reference.</param>
 public CdmAttributeGroupReference(CdmCorpusContext ctx, dynamic attributeGroup, bool simpleReference)
     : base(ctx, (object)attributeGroup, simpleReference)
 {
     this.ObjectType = CdmObjectType.AttributeGroupRef;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="CdmLocalEntityDeclarationDefinition"/> class.
 /// </summary>
 /// <param name="ctx"> The context. </param>
 /// <param name="entityName"> The entity name. </param>
 public CdmLocalEntityDeclarationDefinition(CdmCorpusContext ctx, string entityName) : base(ctx)
 {
     this.ObjectType = CdmObjectType.LocalEntityDeclarationDef;
     this.EntityName = entityName;
 }
 /// <summary>
 /// Constructs a CdmConstantEntityDefinition.
 /// </summary>
 /// <param name="ctx">The context.</param>
 public CdmConstantEntityDefinition(CdmCorpusContext ctx, string constantEntityName)
     : base(ctx)
 {
     this.ObjectType         = CdmObjectType.ConstantEntityDef;
     this.ConstantEntityName = constantEntityName;
 }
Example #6
0
        /// <summary>
        /// Send a CDM request with the retry logic helper function.
        /// </summary>
        /// <param name="cdmRequest">The CDM Http request.</param>
        /// <param name="callback">The callback that gets executed after the request finishes.</param>
        /// <returns>The <see cref="Task"/>, representing CDM Http response.</returns>
        private async Task <CdmHttpResponse> SendAsyncHelper(CdmHttpRequest cdmRequest, Callback callback = null, CdmCorpusContext ctx = null)
        {
            string fullUrl;

            if (isApiEndpointSet)
            {
                fullUrl = Combine(this.apiEndpoint, cdmRequest.RequestedUrl);
            }
            else
            {
                fullUrl = cdmRequest.RequestedUrl;
            }

            // If the number of retries is 0, we only try once, otherwise we retry the specified number of times.
            for (int retryNumber = 0; retryNumber <= cdmRequest.NumberOfRetries; retryNumber++)
            {
                var requestMessage = new HttpRequestMessage(cdmRequest.Method, fullUrl);

                foreach (var item in cdmRequest.Headers)
                {
                    requestMessage.Headers.Add(item.Key, item.Value);
                }

                // GET requests might not have any content.
                if (cdmRequest.Content != null)
                {
                    requestMessage.Content = new StringContent(cdmRequest.Content, Encoding.UTF8, cdmRequest.ContentType);
                }

                CdmHttpResponse cdmHttpResponse = null;
                var             hasFailed       = false;
                try
                {
                    Task <HttpResponseMessage> request;

                    DateTimeOffset startTime = DateTimeOffset.UtcNow;

                    if (ctx != null)
                    {
                        Logger.Info(nameof(CdmHttpClient), ctx, $"Sending request {cdmRequest.RequestId}, request type: {requestMessage.Method}, request url: {cdmRequest.StripSasSig()}, retry number: {retryNumber}.", nameof(SendAsyncHelper));
                    }

                    // The check is added to fix a known issue in .net http client when reading HEAD request > 2GB.
                    // .net http client tries to write content even when the request is HEAD request.
                    if (cdmRequest.Method.Equals(HttpMethod.Head))
                    {
                        request = Task.Run(async() => await this.client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead));
                    }
                    else
                    {
                        request = Task.Run(async() => await this.client.SendAsync(requestMessage));
                    }

                    if (!request.Wait((TimeSpan)cdmRequest.Timeout))
                    {
                        if (ctx != null && cdmRequest.Timeout != null)
                        {
                            Logger.Info(nameof(CdmHttpClient), ctx, $"Request {cdmRequest.RequestId} timeout after {cdmRequest.Timeout?.Seconds} s.", nameof(SendAsyncHelper));
                        }

                        throw new CdmTimedOutException("Request timeout.");
                    }

                    HttpResponseMessage response = request.Result;

                    if (ctx != null)
                    {
                        DateTimeOffset endTime = DateTimeOffset.UtcNow;
                        Logger.Info(nameof(CdmHttpClient), ctx, $"Response for request {cdmRequest.RequestId} received, elapsed time: {endTime.Subtract(startTime).TotalMilliseconds} ms.", nameof(SendAsyncHelper));
                    }

                    if (response != null)
                    {
                        cdmHttpResponse = new CdmHttpResponse(response.StatusCode)
                        {
                            Reason       = response.ReasonPhrase,
                            Content      = response.Content,
                            IsSuccessful = response.IsSuccessStatusCode
                        };

                        foreach (var item in response.Headers)
                        {
                            cdmHttpResponse.ResponseHeaders.Add(item.Key, string.Join(",", item.Value));
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (ex is AggregateException aggrEx)
                    {
                        ex = aggrEx.InnerException;
                    }

                    hasFailed = true;

                    // Only throw an exception if another retry is not expected anymore.
                    if (callback == null || retryNumber == cdmRequest.NumberOfRetries)
                    {
                        if (retryNumber != 0)
                        {
                            throw new CdmNumberOfRetriesExceededException(ex.Message);
                        }
                        else
                        {
                            throw ex;
                        }
                    }
                }

                // Check whether we have a callback function set and whether this is not our last retry.
                if (callback != null && retryNumber != cdmRequest.NumberOfRetries)
                {
                    // Call the callback function with the retry numbers starting from 1.
                    var waitTime = callback(cdmHttpResponse, hasFailed, retryNumber + 1);

                    // Callback returned back that we do not want to retry anymore (probably successful request, client can set up what they want here).
                    if (waitTime == null)
                    {
                        return(cdmHttpResponse);
                    }
                    else
                    {
                        // Sleep time specified by the callback.
                        Thread.Sleep((int)waitTime.Value.TotalMilliseconds);
                    }
                }
                else
                {
                    // CDM Http Response exists, could be successful or bad (e.g. 403/404), it is up to caller to deal with it.
                    if (cdmHttpResponse != null)
                    {
                        return(cdmHttpResponse);
                    }
                    else
                    {
                        if (retryNumber == 0)
                        {
                            return(null);
                        }
                        else
                        {
                            // If response doesn't exist repeatedly, just throw that the number of retries has exceeded (we don't have any other information).
                            throw new CdmNumberOfRetriesExceededException();
                        }
                    }
                }
            }

            // Should never come here, but just in case throw this exception.
            throw new CdmNumberOfRetriesExceededException();
        }
 public CdmOperationAddAttributeGroup(CdmCorpusContext ctx) : base(ctx)
 {
     this.ObjectType = CdmObjectType.OperationAddAttributeGroupDef;
     this.Type       = CdmOperationType.AddAttributeGroup;
 }
Example #8
0
 /// <summary>
 /// Log to WARNING level.
 /// </summary>
 /// <param name="tag">The tag, usually the class which is calling the method.</param>
 /// <param name="ctx">The CDM corpus context.</param>
 /// <param name="message">The message.</param>
 /// <param name="path">The path, usually denotes the class and method calling this method.</param>
 public static void Warning(string tag, CdmCorpusContext ctx, string message, string path = null)
 {
     Log(CdmStatusLevel.Warning, ctx, tag, message, path, DefaultLogger.Warn);
 }
 /// <summary>
 /// Create a new empty state set
 /// </summary>
 public ProjectionAttributeStateSet(CdmCorpusContext Ctx)
 {
     this.Ctx    = Ctx;
     this.States = new List <ProjectionAttributeState>();
 }
Example #10
0
 /// <summary>
 /// Log to DEBUG level.
 /// </summary>
 /// <param name="tag">The tag, usually the class which is calling the method.</param>
 /// <param name="ctx">The CDM corpus context.</param>
 /// <param name="message">The message.</param>
 /// <param name="path">The path, usually denotes the class and method calling this method.</param>
 public static void Debug(string tag, CdmCorpusContext ctx, string message, string path = null)
 {
     Log(CdmStatusLevel.Progress, ctx, tag, message, path, DefaultLogger.Debug);
 }
Example #11
0
 /// <summary>
 /// Log to INFO level.
 /// </summary>
 /// <param name="tag">The tag, usually the class which is calling the method.</param>
 /// <param name="ctx">The CDM corpus context.</param>
 /// <param name="message">The message.</param>
 /// <param name="path">The path, usually denotes the class and method calling this method.</param>
 public static void Info(string tag, CdmCorpusContext ctx, string message, string path = null)
 {
     Log(CdmStatusLevel.Info, ctx, tag, message, path, DefaultLogger.Info);
 }
Example #12
0
 /// <summary>
 /// Log to ERROR level.
 /// </summary>
 /// <param name="tag">The tag, usually the class which is calling the method.</param>
 /// <param name="ctx">The CDM corpus context.</param>
 /// <param name="message">The message.</param>
 /// <param name="path">The path, usually denotes the class and method calling this method.</param>
 public static void Error(string tag, CdmCorpusContext ctx, string message, string path = null)
 {
     Log(CdmStatusLevel.Error, ctx, tag, message, path, DefaultLogger.Error);
 }
Example #13
0
 /// <summary>
 /// Constructs a CdmDefinitionCollection
 /// </summary>
 /// <param name="ctx">The context.</param>
 /// <param name="owner">The owner of the collection. Has to be a <see cref="CdmDocumentDefinition"/>.</param>
 public CdmDefinitionCollection(CdmCorpusContext ctx, CdmDocumentDefinition owner)
     : base(ctx, owner, CdmObjectType.EntityDef)
 {
 }
Example #14
0
 /// <summary>
 /// Constructs a CdmDocumentCollection by using the parent constructor and DocumentDef as the default type.
 /// </summary>
 /// <param name="ctx">The context.</param>
 /// <param name="owner">The folder that contains this collection.</param>
 public CdmDocumentCollection(CdmCorpusContext ctx, CdmFolderDefinition owner)
     : base(ctx, owner, CdmObjectType.DocumentDef)
 {
 }
Example #15
0
        public static CdmDocumentDefinition FromData(CdmCorpusContext ctx, string docName, string jsonData, CdmFolderDefinition folder)
        {
            var obj = JsonConvert.DeserializeObject <DocumentContent>(jsonData);

            return(FromObject(ctx, docName, folder.Namespace, folder.FolderPath, obj));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="CdmDataPartitionPatternDefinition"/> class.
 /// </summary>
 /// <param name="ctx">The context.</param>
 /// <param name="name">The name.</param>
 public CdmDataPartitionPatternDefinition(CdmCorpusContext ctx, string name) : base(ctx)
 {
     this.ObjectType = CdmObjectType.DataPartitionPatternDef;
     this.Name       = name;
 }
        public static CdmLocalEntityDeclarationDefinition FromData(CdmCorpusContext ctx, string prefixPath, JToken obj)
        {
            var localDec = ctx.Corpus.MakeObject <CdmLocalEntityDeclarationDefinition>(
                CdmObjectType.LocalEntityDeclarationDef,
                (string)obj["entityName"]);

            string entityPath = obj.Value <string>("entityPath");

            // Check for the old format, it has to be there then.
            if (entityPath == null)
            {
                entityPath = obj.Value <string>("entitySchema");

                if (entityPath == null)
                {
                    Logger.Error(nameof(LocalEntityDeclarationPersistence), ctx, "Couldn't find entity path or similar.", nameof(FromData));
                }
            }

            localDec.EntityPath = entityPath;

            if (!string.IsNullOrWhiteSpace(obj.Value <string>("lastChildFileModifiedTime")))
            {
                localDec.LastChildFileModifiedTime = DateTimeOffset.Parse(obj["lastChildFileModifiedTime"].ToString());
            }

            if (!string.IsNullOrWhiteSpace(obj.Value <string>("lastFileModifiedTime")))
            {
                localDec.LastFileModifiedTime = DateTimeOffset.Parse(obj["lastFileModifiedTime"].ToString());
            }

            if (!string.IsNullOrWhiteSpace(obj.Value <string>("lastFileStatusCheckTime")))
            {
                localDec.LastFileStatusCheckTime = DateTimeOffset.Parse(obj["lastFileStatusCheckTime"].ToString());
            }

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

            if (obj["exhibitsTraits"] != null)
            {
                Utils.AddListToCdmCollection(localDec.ExhibitsTraits, Utils.CreateTraitReferenceList(ctx, obj["exhibitsTraits"]));
            }

            if (obj["dataPartitions"] != null)
            {
                foreach (var dataPartition in obj["dataPartitions"])
                {
                    localDec.DataPartitions.Add(DataPartitionPersistence.FromData(ctx, dataPartition));
                }
            }

            if (obj["dataPartitionPatterns"] != null)
            {
                foreach (var pattern in obj["dataPartitionPatterns"])
                {
                    localDec.DataPartitionPatterns.Add(DataPartitionPatternPersistence.FromData(ctx, pattern));
                }
            }

            return(localDec);
        }
        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((ResolveContext)ctx, Tag, nameof(FromData), null, CdmLogCode.ErrPersistCardinalityPropMissing);
                }

                if (!CardinalitySettings.IsMinimumValid(minCardinality))
                {
                    Logger.Error((ResolveContext)ctx, Tag, nameof(FromData), null, CdmLogCode.ErrPersistInvalidMinCardinality, minCardinality);
                }

                if (!CardinalitySettings.IsMaximumValid(maxCardinality))
                {
                    Logger.Error((ResolveContext)ctx, Tag, nameof(FromData), null, CdmLogCode.ErrPersistInvalidMaxCardinality, maxCardinality);
                }

                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)
            {
                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);
        }
Example #19
0
        /// <summary>
        /// Send a CDM request with the retry logic.
        /// </summary>
        /// <param name="cdmRequest">The CDM Http request.</param>
        /// <param name="callback">The callback that gets executed after the request finishes.</param>
        /// <returns>The <see cref="Task"/>, representing CDM Http response.</returns>
        internal async Task <CdmHttpResponse> SendAsync(CdmHttpRequest cdmRequest, Callback callback = null, CdmCorpusContext ctx = null)
        {
            // Merge headers first.
            foreach (var item in this.Headers)
            {
                cdmRequest.Headers.Add(item.Key, item.Value);
            }

            try
            {
                var task = Task.Run(async() => await SendAsyncHelper(cdmRequest, callback, ctx));

                // Wait for all the requests to finish, if the time exceedes maximum timeout throw the CDM timed out exception.
                if (task.Wait((TimeSpan)cdmRequest.MaximumTimeout))
                {
                    return(task.Result);
                }
                else
                {
                    throw new CdmTimedOutException("Maximum timeout exceeded.");
                }
            }
            catch (AggregateException err)
            {
                throw err.InnerException;
            }
        }
Example #20
0
        /// <summary>
        /// A function to construct projection context and populate the resolved attribute set that ExtractResolvedAttributes method can then extract
        /// This function is the entry point for projection resolution.
        /// This function is expected to do the following 3 things:
        /// - Create an condition expression tree & default if appropriate
        /// - Create and initialize Projection Context
        /// - Process operations
        /// </summary>
        /// <param name="projDirective"></param>
        /// <param name="attrCtx"></param>
        /// <returns></returns>
        internal ProjectionContext ConstructProjectionContext(ProjectionDirective projDirective, CdmAttributeContext attrCtx)
        {
            ProjectionContext projContext = null;

            if (string.IsNullOrWhiteSpace(this.Condition))
            {
                // if no condition is provided, get default condition and persist
                this.Condition = ConditionExpression.GetDefaultConditionExpression(this.Operations, this.Owner);
            }
            // create an expression tree based on the condition
            ExpressionTree tree = new ExpressionTree();

            this.ConditionExpressionTreeRoot = tree.ConstructExpressionTree(this.Condition);
            if (this.ConditionExpressionTreeRoot == null)
            {
                Logger.Info(nameof(CdmProjection), this.Ctx, $"Optional expression missing. Implicit expression will automatically apply.", nameof(ConstructProjectionContext));
            }

            if (attrCtx != null)
            {
                // Add projection to context tree
                AttributeContextParameters acpProj = new AttributeContextParameters
                {
                    under         = attrCtx,
                    type          = CdmAttributeContextType.Projection,
                    Name          = this.FetchObjectDefinitionName(),
                    Regarding     = projDirective.OwnerRef,
                    IncludeTraits = false
                };
                CdmAttributeContext acProj = CdmAttributeContext.CreateChildUnder(projDirective.ResOpt, acpProj);

                AttributeContextParameters acpSource = new AttributeContextParameters
                {
                    under         = acProj,
                    type          = CdmAttributeContextType.Source,
                    Name          = "source",
                    Regarding     = null,
                    IncludeTraits = false
                };
                CdmAttributeContext acSource = CdmAttributeContext.CreateChildUnder(projDirective.ResOpt, acpSource);

                if (this.Source.FetchObjectDefinition <CdmObjectDefinition>(projDirective.ResOpt).ObjectType == CdmObjectType.ProjectionDef)
                {
                    // A Projection

                    projContext = ((CdmProjection)this.Source.ExplicitReference).ConstructProjectionContext(projDirective, acSource);
                }
                else
                {
                    // An Entity Reference

                    AttributeContextParameters acpSourceProjection = new AttributeContextParameters
                    {
                        under         = acSource,
                        type          = CdmAttributeContextType.Entity,
                        Name          = this.Source.NamedReference ?? this.Source.ExplicitReference.GetName(),
                        Regarding     = this.Source,
                        IncludeTraits = false
                    };
                    ResolvedAttributeSet ras = this.Source.FetchResolvedAttributes(projDirective.ResOpt, acpSourceProjection);

                    // Initialize the projection context

                    CdmCorpusContext ctx = (projDirective.Owner?.Ctx);

                    ProjectionAttributeStateSet pasSet = null;

                    // if polymorphic keep original source as previous state
                    Dictionary <string, List <ProjectionAttributeState> > polySourceSet = null;
                    if (projDirective.IsSourcePolymorphic)
                    {
                        polySourceSet = ProjectionResolutionCommonUtil.GetPolymorphicSourceSet(projDirective, ctx, this.Source, acpSourceProjection);
                    }

                    // now initialize projection attribute state
                    pasSet = ProjectionResolutionCommonUtil.InitializeProjectionAttributeStateSet(
                        projDirective,
                        ctx,
                        ras,
                        isSourcePolymorphic: projDirective.IsSourcePolymorphic,
                        polymorphicSet: polySourceSet);

                    projContext = new ProjectionContext(projDirective, ras.AttributeContext)
                    {
                        CurrentAttributeStateSet = pasSet
                    };
                }

                bool isConditionValid = false;
                if (this.ConditionExpressionTreeRoot != null)
                {
                    InputValues input = new InputValues()
                    {
                        noMaxDepth = projDirective.HasNoMaximumDepth,
                        isArray    = projDirective.IsArray,

                        referenceOnly = projDirective.IsReferenceOnly,
                        normalized    = projDirective.IsNormalized,
                        structured    = projDirective.IsStructured,

                        nextDepth = ++projDirective.CurrentDepth,
                        maxDepth  = projDirective.MaximumDepth,

                        minCardinality = projDirective.Cardinality?._MinimumNumber,
                        maxCardinality = projDirective.Cardinality?._MaximumNumber
                    };

                    isConditionValid = ExpressionTree.EvaluateExpressionTree(this.ConditionExpressionTreeRoot, input);
                }

                if (isConditionValid && this.Operations != null && this.Operations.Count > 0)
                {
                    // Just in case new operations were added programmatically, reindex operations
                    for (int i = 0; i < this.Operations.Count; i++)
                    {
                        this.Operations[i].Index = i + 1;
                    }

                    // Operation

                    AttributeContextParameters acpGenAttrSet = new AttributeContextParameters
                    {
                        under = attrCtx,
                        type  = CdmAttributeContextType.GeneratedSet,
                        Name  = "_generatedAttributeSet"
                    };
                    CdmAttributeContext acGenAttrSet = CdmAttributeContext.CreateChildUnder(projDirective.ResOpt, acpGenAttrSet);

                    AttributeContextParameters acpGenAttrRound0 = new AttributeContextParameters
                    {
                        under = acGenAttrSet,
                        type  = CdmAttributeContextType.GeneratedRound,
                        Name  = "_generatedAttributeRound0"
                    };
                    CdmAttributeContext acGenAttrRound0 = CdmAttributeContext.CreateChildUnder(projDirective.ResOpt, acpGenAttrRound0);

                    // Start with an empty list for each projection
                    ProjectionAttributeStateSet pasOperations = new ProjectionAttributeStateSet(projContext.CurrentAttributeStateSet.Ctx);
                    foreach (CdmOperationBase operation in this.Operations)
                    {
                        // Evaluate projections and apply to empty state
                        ProjectionAttributeStateSet newPasOperations = operation.AppendProjectionAttributeState(projContext, pasOperations, acGenAttrRound0);

                        // If the operations fails or it is not implemented the projection cannot be evaluated so keep previous valid state.
                        if (newPasOperations != null)
                        {
                            pasOperations = newPasOperations;
                        }
                    }

                    // Finally update the current state to the projection context
                    projContext.CurrentAttributeStateSet = pasOperations;
                }
                else
                {
                    // Pass Through - no operations to process
                }
            }

            return(projContext);
        }
Example #21
0
 public static CdmDocumentDefinition InstanceFromData(CdmCorpusContext ctx, dynamic obj)
 {
     return(CdmObjectBase.InstanceFromData <CdmDocumentDefinition, DocumentContent>(ctx, obj));
 }
Example #22
0
 /// <summary>
 /// Projection constructor
 /// </summary>
 /// <param name="ctx"></param>
 public CdmProjection(CdmCorpusContext ctx)
     : base(ctx)
 {
     this.ObjectType = CdmObjectType.ProjectionDef;
     this.Operations = new CdmOperationCollection(ctx, this);
 }
Example #23
0
 public CdmOperationAddTypeAttribute(CdmCorpusContext ctx) : base(ctx)
 {
     this.ObjectType = CdmObjectType.OperationAddTypeAttributeDef;
     this.Type       = CdmOperationType.AddTypeAttribute;
 }
Example #24
0
        public static CdmManifestDefinition FromObject(CdmCorpusContext ctx, string name, string nameSpace, string path, ManifestContent dataObj)
        {
            // Determine name of the manifest
            var manifestName = !string.IsNullOrEmpty(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 (string.IsNullOrEmpty(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 (!string.IsNullOrEmpty(dataObj.Schema))
            {
                manifest.Schema = dataObj.Schema;
            }
            if (DynamicObjectExtensions.HasProperty(dataObj, "JsonSchemaSemanticVersion") && !string.IsNullOrEmpty(dataObj.JsonSchemaSemanticVersion))
            {
                manifest.JsonSchemaSemanticVersion = dataObj.JsonSchemaSemanticVersion;
            }

            if (!string.IsNullOrEmpty(dataObj.DocumentVersion))
            {
                manifest.DocumentVersion = dataObj.DocumentVersion;
            }

            if (!string.IsNullOrEmpty(dataObj.ManifestName))
            {
                manifest.ManifestName = dataObj.ManifestName;
            }
            else if (!string.IsNullOrEmpty(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 = !string.IsNullOrEmpty(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);
                        }
                    }
                    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);
                }
            }

            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);
        }
Example #25
0
 /// <summary>
 /// Constructs a CdmDataTypeReference.
 /// </summary>
 /// <param name="ctx">The context.</param>
 /// <param name="dataType">The data type to reference.</param>
 /// <param name="simpleReference">Whether this reference is a simple reference.</param>
 public CdmDataTypeReference(CdmCorpusContext ctx, dynamic dataType, bool simpleReference)
     : base(ctx, (object)dataType, simpleReference)
 {
     this.ObjectType = CdmObjectType.DataTypeRef;
 }
Example #26
0
 /// <summary>
 /// Constructs a CdmAttributeContextReference.
 /// </summary>
 /// <param name="ctx">The context.</param>
 /// <param name="name">The name.</param>
 public CdmAttributeContextReference(CdmCorpusContext ctx, string name)
     : base(ctx, name, true)
 {
     this.ObjectType = CdmObjectType.AttributeContextRef;
 }
Example #27
0
        public static async Task <CdmManifestDefinition> FromObject(CdmCorpusContext ctx, Model obj, CdmFolderDefinition folder)
        {
            #region Prepare extensionDoc
            List <CdmTraitDefinition> extensionTraitDefList = new List <CdmTraitDefinition>();
            #endregion

            #region Set manifest fields
            CdmManifestDefinition manifest = ctx.Corpus.MakeObject <CdmManifestDefinition>(CdmObjectType.ManifestDef, obj.Name);

            // We need to set up folder path and namespace of a manifest to be able to retrieve that object.
            folder.Documents.Add(manifest);

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

            if (!manifest.Imports.Any((CdmImport importPresent) => importPresent.CorpusPath == "cdm:/foundations.cdm.json"))
            {
                manifest.Imports.Add("cdm:/foundations.cdm.json");
            }

            manifest.Explanation               = obj.Description;
            manifest.LastFileModifiedTime      = obj.ModifiedTime;
            manifest.LastChildFileModifiedTime = obj.LastChildFileModifiedTime;
            manifest.LastFileStatusCheckTime   = obj.LastFileStatusCheckTime;

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

            if (obj.Application != null)
            {
                var applicationTrait = ctx.Corpus.MakeRef <CdmTraitReference>(CdmObjectType.TraitRef, "is.managedBy", false);
                applicationTrait.IsFromProperty = true;

                var arg = ctx.Corpus.MakeObject <CdmArgumentDefinition>(CdmObjectType.ArgumentDef, "application");
                arg.Value = obj.Application;
                applicationTrait.Arguments.Add(arg);

                manifest.ExhibitsTraits.Add(applicationTrait);
            }

            if (obj.Version != null)
            {
                var versionTrait = ctx.Corpus.MakeRef <CdmTraitReference>(CdmObjectType.TraitRef, "is.modelConversion.modelVersion", false);

                var arg = ctx.Corpus.MakeObject <CdmArgumentDefinition>(CdmObjectType.ArgumentDef, "version");
                arg.Value = obj.Version;
                versionTrait.Arguments.Add(arg);

                manifest.ExhibitsTraits.Add(versionTrait);
            }

            if (obj.Culture != null)
            {
                var cultureTrait = ctx.Corpus.MakeRef <CdmTraitReference>(CdmObjectType.TraitRef, "is.partition.culture", false);
                cultureTrait.IsFromProperty = true;

                var arg = ctx.Corpus.MakeObject <CdmArgumentDefinition>(CdmObjectType.ArgumentDef, "culture");
                arg.Value = obj.Culture;
                cultureTrait.Arguments.Add(arg);

                manifest.ExhibitsTraits.Add(cultureTrait);
            }

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

            var referenceModels = new Dictionary <string, string>();

            if (obj.ReferenceModels != null)
            {
                var referenceModelsTrait = ctx.Corpus.MakeRef <CdmTraitReference>(CdmObjectType.TraitRef, "is.modelConversion.referenceModelMap", false);

                var arg = ctx.Corpus.MakeObject <CdmArgumentDefinition>(CdmObjectType.ArgumentDef, "referenceModelMap");
                arg.Value = JToken.FromObject(obj.ReferenceModels);
                referenceModelsTrait.Arguments.Add(arg);

                manifest.ExhibitsTraits.Add(referenceModelsTrait);

                foreach (var referenceModel in obj.ReferenceModels)
                {
                    referenceModels.Add(referenceModel.Id, referenceModel.Location);
                }
            }

            var entitySchemaByName = new Dictionary <string, string>();
            if (obj.Entities != null && obj.Entities.Count > 0)
            {
                foreach (var element in obj.Entities)
                {
                    CdmEntityDeclarationDefinition entity = null;

                    if ((string)element["$type"] == "LocalEntity")
                    {
                        entity = await LocalEntityDeclarationPersistence.FromData(ctx, folder, element.ToObject <LocalEntity>(), extensionTraitDefList, manifest);
                    }
                    else if ((string)element["$type"] == "ReferenceEntity")
                    {
                        var referenceEntity = element.ToObject <ReferenceEntity>();
                        if (!referenceModels.ContainsKey(referenceEntity.ModelId))
                        {
                            Logger.Error(nameof(ManifestPersistence), ctx, $"Model Id {referenceEntity.ModelId} from {referenceEntity.Name} not found in referenceModels.");

                            return(null);
                        }
                        entity = await ReferencedEntityDeclarationPersistence.FromData(ctx, referenceEntity, referenceModels[referenceEntity.ModelId]);
                    }
                    else
                    {
                        Logger.Error(nameof(ManifestPersistence), ctx, "There was an error while trying to parse entity type.");
                    }

                    if (entity != null)
                    {
                        manifest.Entities.Add(entity);
                        entitySchemaByName.Add(entity.EntityName, entity.EntityPath);
                    }
                    else
                    {
                        Logger.Error(nameof(ManifestPersistence), ctx, "There was an error while trying to parse entity type.");
                    }
                }
            }

            if (obj.Relationships != null && obj.Relationships.Count > 0)
            {
                foreach (var element in obj.Relationships)
                {
                    var relationship = await RelationshipPersistence.FromData(ctx, element, entitySchemaByName);

                    if (relationship != null)
                    {
                        manifest.Relationships.Add(relationship);
                    }
                    else
                    {
                        Logger.Warning(nameof(ManifestPersistence), ctx, "There was an issue while trying to read relationships from the model.json file.");
                    }
                }
            }

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

            var localExtensionTraitDefList = new List <CdmTraitDefinition>();
            ExtensionHelper.ProcessExtensionFromJson(ctx, obj, manifest.ExhibitsTraits, extensionTraitDefList, localExtensionTraitDefList);
            #endregion

            #region Use extensionDoc, finalize importDocs

            List <CdmImport> importDocs = await ExtensionHelper.StandardImportDetection(ctx, extensionTraitDefList, localExtensionTraitDefList);

            ExtensionHelper.AddImportDocsToManifest(ctx, importDocs, manifest);

            CreateExtensionDocAndAddToFolderAndImports(ctx, extensionTraitDefList, folder);
            #endregion

            return(manifest);
        }
Example #28
0
 public static CdmAttributeContextReference InstanceFromData(CdmCorpusContext ctx, dynamic obj)
 {
     return(CdmObjectBase.InstanceFromData <CdmAttributeContextReference, dynamic>(ctx, obj));
 }
 public CdmOperationExcludeAttributes(CdmCorpusContext ctx) : base(ctx)
 {
     this.ObjectType        = CdmObjectType.OperationExcludeAttributesDef;
     this.Type              = CdmOperationType.ExcludeAttributes;
     this.ExcludeAttributes = new List <string>();
 }
Example #30
0
 public CdmTypeAttributeDefinition(CdmCorpusContext ctx, string name)
     : base(ctx, name)
 {
     this.ObjectType         = CdmObjectType.TypeAttributeDef;
     this.TraitToPropertyMap = new TraitToPropertyMap(this);
 }