Exemple #1
0
        private static bool ConditionIsTrue <T>(T entity, ConditionExpression condition) where T : Entity
        {
            // Date Time Details: https://community.dynamics.com/crm/b/gonzaloruiz/archive/2012/07/29/date-and-time-operators-in-crm-explained

            int      days;
            bool     value;
            DateTime date;

            AssertExpectedNumberOfValues(condition);

            var name = condition.GetQualifiedAttributeName();

            switch (condition.Operator)
            {
            case ConditionOperator.Equal:
                value = Compare(entity, name, condition.Values[0]) == 0;
                break;

            case ConditionOperator.NotEqual:
                value = Compare(entity, name, condition.Values[0]) != 0;
                break;

            case ConditionOperator.GreaterThan:
                value = Compare(entity, name, condition.Values[0]) > 0;
                break;

            case ConditionOperator.LessThan:
                value = Compare(entity, name, condition.Values[0]) < 0;
                break;

            case ConditionOperator.GreaterEqual:
                value = Compare(entity, name, condition.Values[0]) >= 0;
                break;

            case ConditionOperator.LessEqual:
                value = Compare(entity, name, condition.Values[0]) <= 0;
                break;

            case ConditionOperator.Like:
                var str = GetString(entity, name);
                if (str == null)
                {
                    value = condition.Values[0] == null;
                }
                else
                {
                    var likeCondition = (string)condition.Values[0];
                    // http://stackoverflow.com/questions/5417070/c-sharp-version-of-sql-like
                    value = new Regex(@"\A" + new Regex(@"\.|\$|\^|\{|\[|\(|\||\)|\*|\+|\?|\\").Replace(likeCondition.ToUpper(), ch => @"\" + ch).Replace('_', '.').Replace("%", ".*") + @"\z", RegexOptions.Singleline).IsMatch(str.ToUpper());
                }
                break;

            case ConditionOperator.NotLike:
                value = !ConditionIsTrue(entity, new ConditionExpression(condition.EntityName, condition.AttributeName, ConditionOperator.Like));
                break;

            case ConditionOperator.In:
                value = condition.Values.Any(v => v.Equals(ConvertCrmTypeToBasicComparable(entity, name)));
                break;

            case ConditionOperator.NotIn:
                value = !condition.Values.Any(v => v.Equals(ConvertCrmTypeToBasicComparable(entity, name)));
                break;

            //case ConditionOperator.Between:
            //    break;
            //case ConditionOperator.NotBetween:
            //    break;
            case ConditionOperator.Null:
                value = Compare(entity, name, null) == 0;
                break;

            case ConditionOperator.NotNull:
                value = Compare(entity, name, null) != 0;
                break;

            case ConditionOperator.Yesterday:
                value = IsBetween(entity, condition, DateTime.UtcNow.Date.AddDays(-1), DateTime.UtcNow.Date);
                break;

            case ConditionOperator.Today:
                value = IsBetween(entity, condition, DateTime.UtcNow.Date, DateTime.UtcNow.Date.AddDays(1));
                break;

            case ConditionOperator.Tomorrow:
                value = IsBetween(entity, condition, DateTime.UtcNow.Date.AddDays(1), DateTime.UtcNow.Date.AddDays(2));
                break;

            case ConditionOperator.Last7Days:
                condition.Operator = ConditionOperator.LastXDays;
                condition.Values.Add(7);
                value = ConditionIsTrue(entity, condition);
                break;

            case ConditionOperator.Next7Days:
                condition.Operator = ConditionOperator.NextXDays;
                condition.Values.Add(7);
                value = ConditionIsTrue(entity, condition);
                break;

            //case ConditionOperator.LastWeek:
            //    break;
            //case ConditionOperator.ThisWeek:
            //    break;
            //case ConditionOperator.NextWeek:
            //    break;
            //case ConditionOperator.LastMonth:
            //    break;
            //case ConditionOperator.ThisMonth:
            //    break;
            //case ConditionOperator.NextMonth:
            //    break;
            case ConditionOperator.On:
                date = condition.GetDateTimeValueFromDateOrString().Date;
                var attributeDate = entity.GetAttributeValue <DateTime?>(name);
                value = attributeDate.HasValue &&
                        date == attributeDate.Value.Date;
                break;

            case ConditionOperator.OnOrBefore:
                date = condition.GetDateTimeValueFromDateOrString().Date;
                if (date != DateTime.MaxValue)
                {
                    date = date.AddDays(1);
                }
                value = IsBetween(entity, condition, DateTime.MinValue, date);
                break;

            case ConditionOperator.OnOrAfter:
                date  = condition.GetDateTimeValueFromDateOrString().Date;
                value = IsBetween(entity, condition, date, DateTime.MaxValue);
                break;

            //    break;
            //case ConditionOperator.LastYear:
            //    break;
            //case ConditionOperator.ThisYear:
            //    break;
            //case ConditionOperator.NextYear:
            //    break;
            //case ConditionOperator.LastXHours:
            //    break;
            //case ConditionOperator.NextXHours:
            //    break;
            case ConditionOperator.LastXDays:
                days = condition.GetIntValueFromIntOrString();
                if (days <= 0)
                {
                    throw CrmExceptions.GetConditionValueGreaterThan0Exception();
                }

                value = IsBetween(entity, condition, DateTime.UtcNow.Date.AddDays(-1d * days), DateTime.UtcNow.AddDays(1).Date);
                break;

            case ConditionOperator.NextXDays:
                days = condition.GetIntValueFromIntOrString();
                if (days <= 0)
                {
                    throw CrmExceptions.GetConditionValueGreaterThan0Exception();
                }
                value = IsBetween(entity, condition, DateTime.UtcNow, DateTime.UtcNow.Date.AddDays(days + 1));
                break;

            //case ConditionOperator.LastXWeeks:
            //    break;
            //case ConditionOperator.NextXWeeks:
            //    break;
            //case ConditionOperator.LastXMonths:
            //    break;
            //case ConditionOperator.NextXMonths:
            //    break;
            //case ConditionOperator.LastXYears:
            //    break;
            //case ConditionOperator.NextXYears:
            //    break;
            //case ConditionOperator.EqualUserId:
            //    break;
            //case ConditionOperator.NotEqualUserId:
            //    break;
            //case ConditionOperator.EqualBusinessId:
            //    break;
            //case ConditionOperator.NotEqualBusinessId:
            //    break;
            //case ConditionOperator.ChildOf:
            //    break;
            //case ConditionOperator.Mask:
            //    break;
            //case ConditionOperator.NotMask:
            //    break;
            //case ConditionOperator.MasksSelect:
            //    break;
            //case ConditionOperator.Contains:
            //    break;
            //case ConditionOperator.DoesNotContain:
            //    break;
            //case ConditionOperator.EqualUserLanguage:
            //    break;
            //case ConditionOperator.NotOn:
            //    break;
            //case ConditionOperator.OlderThanXMonths:
            //    break;
            case ConditionOperator.BeginsWith:
                var beginsWithStr = GetString(entity, name);
                if (beginsWithStr == null)
                {
                    value = condition.Values[0] == null;
                }
                else
                {
                    value = beginsWithStr.StartsWith((string)condition.Values[0]);
                }
                break;

            case ConditionOperator.DoesNotBeginWith:
                condition.Operator = ConditionOperator.BeginsWith;
                value = !ConditionIsTrue(entity, condition);
                break;

            case ConditionOperator.EndsWith:
                var endsWithStr = GetString(entity, name);
                if (endsWithStr == null)
                {
                    value = condition.Values[0] == null;
                }
                else
                {
                    value = endsWithStr.EndsWith((string)condition.Values[0]);
                }
                break;

            case ConditionOperator.DoesNotEndWith:
                condition.Operator = ConditionOperator.EndsWith;
                value = !ConditionIsTrue(entity, condition);
                break;

            //case ConditionOperator.ThisFiscalYear:
            //    break;
            //case ConditionOperator.ThisFiscalPeriod:
            //    break;
            //case ConditionOperator.NextFiscalYear:
            //    break;
            //case ConditionOperator.NextFiscalPeriod:
            //    break;
            //case ConditionOperator.LastFiscalYear:
            //    break;
            //case ConditionOperator.LastFiscalPeriod:
            //    break;
            //case ConditionOperator.LastXFiscalYears:
            //    break;
            //case ConditionOperator.LastXFiscalPeriods:
            //    break;
            //case ConditionOperator.NextXFiscalYears:
            //    break;
            //case ConditionOperator.NextXFiscalPeriods:
            //    break;
            //case ConditionOperator.InFiscalYear:
            //    break;
            //case ConditionOperator.InFiscalPeriod:
            //    break;
            //case ConditionOperator.InFiscalPeriodAndYear:
            //    break;
            //case ConditionOperator.InOrBeforeFiscalPeriodAndYear:
            //    break;
            //case ConditionOperator.InOrAfterFiscalPeriodAndYear:
            //    break;
            //case ConditionOperator.EqualUserTeams:
            //    break;
            default:
                throw new NotImplementedException(condition.Operator.ToString());
            }
            return(value);
        }