public void BuildEntityModel(ref EntityModel entityModel, IComponentPresentation cp, Localization localization)
        {
            using (new Tracer(entityModel, cp, localization))
            {
                IFieldSet contextExpressionsFieldSet;
                if (cp.ExtensionData == null || !cp.ExtensionData.TryGetValue(Constants.ContextExpressionsKey, out contextExpressionsFieldSet))
                {
                    // No Context Expressions found; nothing to do.
                    return;
                }

                ContextExpressionConditions conditions = new ContextExpressionConditions();
                IField includeField;
                if (contextExpressionsFieldSet.TryGetValue("Include", out includeField))
                {
                    conditions.Include = includeField.Values.ToArray();
                }
                IField excludeField;
                if (contextExpressionsFieldSet.TryGetValue("Exclude", out excludeField))
                {
                    conditions.Exclude = excludeField.Values.ToArray();
                }

                entityModel.SetExtensionData(Constants.ContextExpressionsKey, conditions);
            }
        }
Пример #2
0
        public void BuildEntityModel(ref EntityModel entityModel, IComponentPresentation cp, Localization localization)
        {
            using (new Tracer(entityModel, cp, localization))
            {
                IFieldSet contextExpressionsFieldSet;
                if (cp.ExtensionData == null || !cp.ExtensionData.TryGetValue(Constants.ContextExpressionsKey, out contextExpressionsFieldSet))
                {
                    // No Context Expressions found; nothing to do.
                    return;
                }

                ContextExpressionConditions conditions = new ContextExpressionConditions();
                IField includeField;
                if (contextExpressionsFieldSet.TryGetValue("Include", out includeField))
                {
                    conditions.Include = includeField.Values.ToArray();
                }
                IField excludeField;
                if (contextExpressionsFieldSet.TryGetValue("Exclude", out excludeField))
                {
                    conditions.Exclude = excludeField.Values.ToArray();
                }

                entityModel.SetExtensionData(Constants.ContextExpressionsKey, conditions);
            }
        }
        /// <summary>
        /// Determines whether a given Entity Model should be included based on the conditions specified on the Entity Model and the context.
        /// </summary>
        /// <param name="entity">The Entity Model to be evaluated.</param>
        /// <param name="localization">The context Localization</param>
        /// <returns><c>true</c> if the Entity should be included.</returns>
        public bool IncludeEntity(EntityModel entity, ILocalization localization)
        {
            using (new Tracer(entity))
            {
                object ceExtensionData;
                if (entity.ExtensionData == null || !entity.ExtensionData.TryGetValue(Constants.ContextExpressionsKey, out ceExtensionData))
                {
                    // No Context Expressions defined for Entity: just include it.
                    return(true);
                }
                ContextExpressionConditions ceConditions = (ContextExpressionConditions)ceExtensionData;

                IDictionary <string, object> contextClaims = GetCachedContextClaims(localization);

                if (!EvaluateContextExpressionClaims(ceConditions.Include, true, contextClaims))
                {
                    Log.Debug("Include Context Expression conditions are not satisfied; suppressing Entity [{0}].", entity);
                    return(false);
                }
                if (!EvaluateContextExpressionClaims(ceConditions.Exclude, false, contextClaims))
                {
                    Log.Debug("Exclude Context Expression conditions are not satisfied; suppressing Entity [{0}].", entity);
                    return(false);
                }

                Log.Debug("All resolved Context Expression conditions are satisfied; keeping Entity [{0}].", entity);
                return(true);
            }
        }
Пример #4
0
 /// <summary>
 /// Builds a strongly typed Entity Model based on a given DXA R2 Data Model.
 /// </summary>
 /// <param name="entityModel">The strongly typed Entity Model to build. Is <c>null</c> for the first Entity Model Builder in the pipeline.</param>
 /// <param name="entityModelData">The DXA R2 Data Model.</param>
 /// <param name="baseModelType">The base type for the Entity Model to build.</param>
 /// <param name="localization">The context <see cref="Localization"/>.</param>
 public void BuildEntityModel(ref EntityModel entityModel, EntityModelData entityModelData, Type baseModelType, Localization localization)
 {
     using (new Tracer(entityModel, entityModelData, baseModelType, localization))
     {
         IDictionary <string, object> extensionData = entityModel.ExtensionData;
         if (extensionData == null)
         {
             return;
         }
         object contextExpression;
         extensionData.TryGetValue("ContextExpressions", out contextExpression);
         if (contextExpression == null)
         {
             return;
         }
         ContentModelData            contextExpressionData = (ContentModelData)contextExpression;
         ContextExpressionConditions cxConditions          = new ContextExpressionConditions();
         if (contextExpressionData.ContainsKey("Include"))
         {
             if (contextExpressionData["Include"] is string[])
             {
                 cxConditions.Include = (string[])contextExpressionData["Include"];
             }
             else
             {
                 cxConditions.Include = new string[] { (string)contextExpressionData["Include"] };
             }
         }
         if (contextExpressionData.ContainsKey("Exclude"))
         {
             if (contextExpressionData["Exclude"] is string[])
             {
                 cxConditions.Exclude = (string[])contextExpressionData["Exclude"];
             }
             else
             {
                 cxConditions.Exclude = new string[] { (string)contextExpressionData["Exclude"] };
             }
         }
         extensionData.Remove("ContextExpressions");
         extensionData.Add(Constants.ContextExpressionsKey, cxConditions);
     }
 }