public override int SaveChanges()
        {
            DoBusinessRules();
            ObjectContext           ctx = ((IObjectContextAdapter)this).ObjectContext;
            List <ObjectStateEntry> objectStateEntryList =
                ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added
                                                             | EntityState.Modified
                                                             | EntityState.Deleted)
                .ToList();
            EntityState      state            = new EntityState();
            ObjectStateEntry objectStateEntry = null;

            if (objectStateEntryList.Count > 0)
            {
                objectStateEntry = objectStateEntryList.Where(p => p.Entity is BusinessRule).ToList()[0];
                state            = objectStateEntry.State;
            }
            SetDisplayValue(objectStateEntryList, state);
            if (objectStateEntry != null && state == EntityState.Modified)
            {
                var id = objectStateEntry.EntityKey.EntityKeyValues[0].Value.ToString();
                MakeUpdateJournalEntry(id, objectStateEntry.State.ToString(), objectStateEntryList.Where(p => p.Entity is BusinessRule).ToList()[0]);
            }
            if (objectStateEntry != null && state == EntityState.Deleted)
            {
                var id = objectStateEntry.EntityKey.EntityKeyValues[0].Value.ToString();
                ConditionContext brconditions = new ConditionContext();
                long?            longid       = Convert.ToInt64(id);
                var condlist = brconditions.Conditions.Where(p => p.RuleConditionsID.Value == longid);
                foreach (var cond in condlist)
                {
                    brconditions.Conditions.Remove(cond);
                }
                brconditions.SaveChanges();
                RuleActionContext brActions = new RuleActionContext();
                ActionArgsContext brArgs    = new ActionArgsContext();
                var actionlist = brActions.RuleActions.Where(p => p.RuleActionID.Value == longid);
                foreach (var action in actionlist)
                {
                    brActions.RuleActions.Remove(action);
                    var argslist = brArgs.ActionArgss.Where(p => p.ActionArgumentsID == action.Id);
                    foreach (var args in argslist)
                    {
                        brArgs.ActionArgss.Remove(args);
                    }
                    brArgs.SaveChanges();
                }
                brActions.SaveChanges();
            }
            var result = base.SaveChanges();

            if (objectStateEntryList.Count > 0)
            {
                if (objectStateEntry != null && state == EntityState.Added)
                {
                    var id = objectStateEntry.EntityKey.EntityKeyValues[0].Value.ToString();
                    MakeAddJournalEntry(id, state.ToString(), objectStateEntryList.Where(p => p.Entity is BusinessRule).ToList()[0]);
                }
            }
            return(result);
        }
        public static bool CheckRule <T>(this T obj1, BusinessRule br, string EntityName)
        {
            if (EntityName != br.EntityName)
            {
                return(false);
            }
            var  EntityInfo        = ModelReflector.Entities.FirstOrDefault(p => p.Name == EntityName);
            bool result            = true;
            var  ruleconditionsdb  = new ConditionContext().Conditions.Where(p => p.RuleConditionsID == br.Id);
            bool containsCondition = false;
            var  ruleactionsdb     = new RuleActionContext();
            var  ruleactions       = ruleactionsdb.RuleActions.Where(p => p.RuleActionID == br.Id && p.AssociatedActionTypeID == 1);

            string lstConditions = "";

            foreach (var condition in ruleconditionsdb)
            {
                containsCondition = true;
                var    PropName               = condition.PropertyName;
                var    ConditionOperator      = condition.Operator;
                var    ConditionValue         = condition.Value;
                object oldPropValue           = null;
                bool   isPropertyValueChanged = true;

                if (br.AssociatedBusinessRuleTypeID == 4)
                {
                    isPropertyValueChanged = IsPropertyValueChanged(obj1, EntityName, PropName);
                }

                if (PropName == "Id" && ConditionOperator == ">" && ConditionValue == "0")
                {
                    result = true && isPropertyValueChanged;
                }
                else
                {
                    string propDataType = "String";
                    var    PropInfo     = EntityInfo.Properties.FirstOrDefault(p => p.Name == PropName);
                    if (PropInfo != null)
                    {
                        propDataType = PropInfo.DataType;
                    }
                    var AssociationInfo = EntityInfo.Associations.FirstOrDefault(p => p.AssociationProperty == PropName);
                    if (PropName.StartsWith("[") && PropName.EndsWith("]"))
                    {
                        PropName = PropName.TrimStart("[".ToArray()).TrimEnd("]".ToArray());
                        if (PropName.Contains("."))
                        {
                            var targetProperties = PropName.Split(".".ToCharArray());
                            if (targetProperties.Length > 1)
                            {
                                AssociationInfo = EntityInfo.Associations.FirstOrDefault(p => p.AssociationProperty == targetProperties[0]);
                                if (AssociationInfo != null)
                                {
                                    var targetEntityInfo = ModelReflector.Entities.FirstOrDefault(p => p.Name == AssociationInfo.Target);
                                    PropInfo     = targetEntityInfo.Properties.FirstOrDefault(p => p.Name == targetProperties[1]);
                                    propDataType = PropInfo.DataType;
                                    var assocInfo = targetEntityInfo.Associations.FirstOrDefault(p => p.AssociationProperty == PropInfo.Name);
                                    if (assocInfo != null)
                                    {
                                        propDataType = "String";
                                    }
                                    PropName = targetProperties[0];
                                }
                            }
                        }
                    }
                    string targetValue = GetTargetConditionValue(obj1, ConditionValue, EntityName);
                    if (!string.IsNullOrEmpty(targetValue))
                    {
                        ConditionValue = targetValue;
                    }

                    string         DataType   = propDataType;
                    PropertyInfo[] properties = (obj1.GetType()).GetProperties().Where(p => p.PropertyType.FullName.StartsWith("System")).ToArray();
                    var            Property   = properties.FirstOrDefault(p => p.Name == PropName);
                    object         PropValue  = Property.GetValue(obj1, null);
                    oldPropValue = GetOldValueOfEntity(obj1, EntityName, PropName);
                    if (AssociationInfo != null)
                    {
                        if (PropValue != null)
                        {
                            PropValue = GetValueForAssociationProperty(AssociationInfo.Target, Convert.ToString(PropValue), PropInfo.Name);
                        }
                        //PropValue = GetDisplayValueForAssociation(AssociationInfo.Target, Convert.ToString(PropValue));
                        if (ConditionValue != null)
                        {
                            ConditionValue = GetValueForAssociationProperty(AssociationInfo.Target, Convert.ToString(ConditionValue), PropInfo.Name);
                        }
                        if (ruleactions != null && ruleactions.Count() > 0)
                        {
                            oldPropValue = GetDisplayValueForAssociation(AssociationInfo.Target, Convert.ToString(oldPropValue));
                        }

                        DataType = "Association";
                    }
                    if (ruleactions != null && ruleactions.Count() > 0)
                    {
                        bool isRuleValid = ValidateValueAgainstRule(oldPropValue, DataType, ConditionOperator, ConditionValue, Property, propDataType);
                        result = isRuleValid && isPropertyValueChanged;
                    }
                    else
                    {
                        bool isRuleValid = ValidateValueAgainstRule(PropValue, DataType, ConditionOperator, ConditionValue, Property, propDataType);
                        result = isRuleValid && isPropertyValueChanged;
                    }
                    if (br.AssociatedBusinessRuleTypeID == 4 && ConditionOperator == "Changes to anything")
                    {
                        result = isPropertyValueChanged;
                    }
                }
                lstConditions += Convert.ToString(result) + " " + condition.LogicalConnector + " ";
            }
            result = CheckRuleWithLogicalConnectors(lstConditions);

            if (!containsCondition)
            {
                return(false);
            }
            return(result);
        }
Example #3
0
        public IQueryable <T> FilterDropdown <T>(IUser User, IQueryable <T> list, string EntityName, string caller)
        {
            IQueryable <T> rejectedList     = list;
            bool           flag             = false;
            bool           flagIsElseAction = false;
            var            br = User.businessrules.Where(p => p.EntityName == EntityName);

            if (br != null && br.Count() > 0)
            {
                var ruleactiondb = new RuleActionContext();
                foreach (var _rule in br)
                {
                    foreach (var act in ruleactiondb.RuleActions.Where(p => p.RuleActionID == _rule.Id && p.AssociatedActionTypeID == 5))
                    {
                        ActionArgsContext actionargs = new ActionArgsContext();
                        var ruleconditionsdb         = new ConditionContext();
                        var arg = actionargs.ActionArgss.FirstOrDefault(p => p.ActionArgumentsID == act.Id && (p.ParameterName == caller || p.ParameterName == "All"));
                        if (arg != null)
                        {
                            foreach (var condition in ruleconditionsdb.Conditions.Where(p => p.RuleConditionsID == _rule.Id))
                            {
                                var PropName        = condition.PropertyName;
                                var EntityInfo      = ModelReflector.Entities.FirstOrDefault(p => p.Name == EntityName);
                                var PropInfo        = EntityInfo.Properties.FirstOrDefault(p => p.Name == PropName);
                                var AssociationInfo = EntityInfo.Associations.FirstOrDefault(p => p.AssociationProperty == PropName);

                                if (PropName.StartsWith("[") && PropName.EndsWith("]"))
                                {
                                    PropName = PropName.TrimStart("[".ToArray()).TrimEnd("]".ToArray());
                                    if (PropName.Contains("."))
                                    {
                                        var targetProperties = PropName.Split(".".ToCharArray());
                                        if (targetProperties.Length > 1)
                                        {
                                            AssociationInfo = EntityInfo.Associations.FirstOrDefault(p => p.AssociationProperty == targetProperties[0]);
                                            if (AssociationInfo != null)
                                            {
                                                EntityInfo = ModelReflector.Entities.FirstOrDefault(p => p.Name == AssociationInfo.Target);
                                                PropInfo   = EntityInfo.Properties.FirstOrDefault(p => p.Name == targetProperties[1]);
                                                PropName   = targetProperties[0];
                                            }
                                        }
                                    }
                                }

                                string         DataType   = PropInfo.DataType;
                                PropertyInfo[] properties = (typeof(T)).GetProperties().Where(p => p.PropertyType.FullName.StartsWith("System")).ToArray();
                                var            Property   = properties.FirstOrDefault(p => p.Name == PropName);
                                if (AssociationInfo != null)
                                {
                                    Type   controller                 = Type.GetType("GeneratorBase.MVC.Controllers." + AssociationInfo.Target + "Controller");
                                    object objController              = Activator.CreateInstance(controller, null);
                                    System.Reflection.MethodInfo mc   = controller.GetMethod("GetIdFromPropertyValue");
                                    object[]            MethodParams  = new object[] { PropInfo.Name, condition.Value };
                                    var                 obj           = mc.Invoke(objController, MethodParams);
                                    object              PropValue     = obj;
                                    IQueryable          query         = list;
                                    Type[]              exprArgTypes  = { query.ElementType };
                                    string              propToWhere   = condition.PropertyName;// filterCriteria.DropdownProperty;
                                    ParameterExpression p1            = Expression.Parameter(typeof(T), "p");
                                    MemberExpression    member        = Expression.PropertyOrField(p1, PropName);
                                    Type                targetType    = typeof(System.Int64?);
                                    dynamic             PropertyValue = PropValue;//Convert.ChangeType(PropValue, targetType);

                                    dynamic expr1 = Expression.Equal(member, Expression.Convert(Expression.Constant(PropertyValue), targetType));

                                    LambdaExpression     lambda     = Expression.Lambda <Func <T, bool> >(expr1, p1);
                                    MethodCallExpression methodCall = Expression.Call(typeof(Queryable), "Where", exprArgTypes, query.Expression, lambda);
                                    IQueryable           q          = query.Provider.CreateQuery(methodCall);
                                    //finalList = ((IQueryable<T>)q);
                                    rejectedList = rejectedList.Except((IQueryable <T>)q);
                                    flag         = true;
                                }
                                else
                                {
                                    object              PropValue    = condition.Value;// filterCriteria.PropertyValue;
                                    IQueryable          query        = list;
                                    Type[]              exprArgTypes = { query.ElementType };
                                    string              propToWhere  = condition.PropertyName;// filterCriteria.DropdownProperty;
                                    ParameterExpression p1           = Expression.Parameter(typeof(T), "p");
                                    MemberExpression    member       = Expression.PropertyOrField(p1, propToWhere);
                                    Type    targetType    = Property.PropertyType;
                                    dynamic PropertyValue = null;

                                    if (targetType.GetGenericArguments().Count() > 0)
                                    {
                                        if (targetType.GetGenericArguments()[0].Name == "DateTime" && condition.Value.ToLower().Contains("today"))
                                        {
                                            PropValue = ApplyRule.EvaluateDateTime("", condition.Value);
                                        }
                                    }

                                    if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Nullable <>))
                                    {
                                        if (String.IsNullOrEmpty(Convert.ToString(PropValue)))
                                        {
                                            PropertyValue = null;
                                        }
                                        else
                                        {
                                            PropertyValue = Convert.ChangeType(PropValue, targetType.GetGenericArguments()[0]);
                                        }
                                    }
                                    else
                                    {
                                        PropertyValue = Convert.ChangeType(PropValue, targetType);
                                    }
                                    LambdaExpression lambda = Expression.Lambda <Func <T, bool> >(Expression.Equal(member, Expression.Convert(Expression.Constant(PropertyValue), targetType)), p1);
                                    if (condition.Operator == ">")
                                    {
                                        lambda = Expression.Lambda <Func <T, bool> >(Expression.GreaterThan(member, Expression.Convert(Expression.Constant(PropertyValue), targetType)), p1);
                                    }
                                    if (condition.Operator == "<")
                                    {
                                        lambda = Expression.Lambda <Func <T, bool> >(Expression.LessThan(member, Expression.Convert(Expression.Constant(PropertyValue), targetType)), p1);
                                    }
                                    if (condition.Operator == "<=")
                                    {
                                        lambda = Expression.Lambda <Func <T, bool> >(Expression.LessThanOrEqual(member, Expression.Convert(Expression.Constant(PropertyValue), targetType)), p1);
                                    }
                                    if (condition.Operator == ">=")
                                    {
                                        lambda = Expression.Lambda <Func <T, bool> >(Expression.GreaterThanOrEqual(member, Expression.Convert(Expression.Constant(PropertyValue), targetType)), p1);
                                    }
                                    if (condition.Operator == "!=")
                                    {
                                        lambda = Expression.Lambda <Func <T, bool> >(Expression.NotEqual(member, Expression.Convert(Expression.Constant(PropertyValue), targetType)), p1);
                                    }
                                    MethodCallExpression methodCall = Expression.Call(typeof(Queryable), "Where", exprArgTypes, query.Expression, lambda);
                                    IQueryable           q          = query.Provider.CreateQuery(methodCall);
                                    //finalList = ((IQueryable<T>)q);
                                    rejectedList = rejectedList.Except((IQueryable <T>)q);
                                    flag         = true;
                                }
                            }
                        }
                        if (act.IsElseAction)
                        {
                            flagIsElseAction = true;
                        }
                    }
                }
            }
            if (flag)
            {
                if (flagIsElseAction)
                {
                    return(rejectedList);
                }
                return(list.Except(rejectedList));
            }
            else
            {
                return(list);
            }
        }