Esempio n. 1
0
        public string GetParameterValue(Entity Target)
        {
            if (Target.Contains(AttributeName))
            {
                if (Target[AttributeName] is EntityReference)
                {
                    // Lookup condition is based on GUID
                    return(Conditional.HasCondition ? Conditional.GetResult(Target.GetAttributeValue <EntityReference>(AttributeName).Id) : Target.GetAttributeValue <EntityReference>(AttributeName).Name);
                }
                else if (Target[AttributeName] is OptionSetValue)
                {
                    // Conditional OptionSetValue is based on the integer value
                    return(Conditional.HasCondition ? Conditional.GetResult(Target.GetAttributeValue <OptionSetValue>(AttributeName).Value.ToString()) : Target.FormattedValues[AttributeName]);
                }
                else if (Target[AttributeName] is bool)
                {
                    // Note: Boolean values ignore the match value, they just use the attribute value itself as the condition
                    return(Conditional.HasCondition ? Conditional.GetResult(Target.GetAttributeValue <bool>(AttributeName)) : Target.FormattedValues[AttributeName]);
                }
                else if (Target[AttributeName] is DateTime)
                {
                    // If there is a format AND a condition, apply formatting first, then evaluate condition as a string
                    // If there is a condition without any format, evaluate condition as DateTime
                    return(String.IsNullOrEmpty(StringFormatter) ? Conditional.GetResult(Target.GetAttributeValue <DateTime>(AttributeName)) : Conditional.GetResult(Target.GetAttributeValue <DateTime>(AttributeName).ToString(StringFormatter)));
                }
                else if (Target[AttributeName] is Money)
                {
                    return(Conditional.HasCondition ? Conditional.GetResult(Target.GetAttributeValue <Money>(AttributeName).Value) : Target.GetAttributeValue <Money>(AttributeName).Value.ToString(StringFormatter));
                }
                else if (Target[AttributeName] is int)
                {
                    return(Conditional.HasCondition ? Conditional.GetResult(Target.GetAttributeValue <double>(AttributeName)) : Target.GetAttributeValue <double>(AttributeName).ToString(StringFormatter));
                }
                else if (Target[AttributeName] is decimal)
                {
                    return(Conditional.HasCondition ? Conditional.GetResult(Target.GetAttributeValue <decimal>(AttributeName)) : Target.GetAttributeValue <decimal>(AttributeName).ToString(StringFormatter));
                }
                else if (Target[AttributeName] is double)
                {
                    return(Conditional.HasCondition ? Conditional.GetResult(Target.GetAttributeValue <double>(AttributeName)) : Target.GetAttributeValue <double>(AttributeName).ToString(StringFormatter));
                }
                else if (Target[AttributeName] is string)
                {
                    return(Conditional.GetResult(Target[AttributeName].ToString()));
                }
            }
            else if (AttributeName.Equals("rand"))
            {
                string length       = "";
                string stringStyle  = "upper";
                int    stringLength = 5;               // Seems like reasonable default

                if (StringFormatter.Contains('?'))
                {
                    length      = StringFormatter.Split('?')[0];
                    stringStyle = StringFormatter.Split('?')[1].ToLower();
                }
                else
                {
                    length = StringFormatter;
                }

                if (!Int32.TryParse(length, out stringLength))
                {
                    stringLength = 5;
                }

                string stringValues = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                if (stringStyle == "mix")
                {
                    stringValues = stringValues + stringValues.ToLower();
                }
                else if (stringStyle == "lower")
                {
                    stringValues = stringValues.ToLower();
                }

                Random rnd = new Random();
                return(String.Join("", Enumerable.Range(0, stringLength).Select(n => stringValues[rnd.Next(stringValues.Length)])));
            }

            return(DefaultValue);
        }
        public string GetParameterValue(Entity target)
        {
            if (target.Contains(AttributeName))
            {
                switch (target[AttributeName])
                {
                case EntityReference _:
                    // Lookup condition is based on GUID
                    return(Conditional.HasCondition ? Conditional.GetResult(target.GetAttributeValue <EntityReference>(AttributeName).Id) : target.GetAttributeValue <EntityReference>(AttributeName).Name);

                case OptionSetValue _:
                    // Conditional OptionSetValue is based on the integer value
                    return(Conditional.HasCondition ? Conditional.GetResult(target.GetAttributeValue <OptionSetValue>(AttributeName).Value.ToString()) : target.FormattedValues[AttributeName]);

                case bool _:
                    // Note: Boolean values ignore the match value, they just use the attribute value itself as the condition
                    return(Conditional.HasCondition ? Conditional.GetResult(target.GetAttributeValue <bool>(AttributeName)) : target.FormattedValues[AttributeName]);

                case DateTime _:
                    // If there is a format AND a condition, apply formatting first, then evaluate condition as a string
                    // If there is a condition without any format, evaluate condition as DateTime
                    return(string.IsNullOrEmpty(StringFormatter) ? Conditional.GetResult(target.GetAttributeValue <DateTime>(AttributeName)) : Conditional.GetResult(target.GetAttributeValue <DateTime>(AttributeName).ToString(StringFormatter)));

                case Money _:
                    return(Conditional.HasCondition ? Conditional.GetResult(target.GetAttributeValue <Money>(AttributeName).Value) : target.GetAttributeValue <Money>(AttributeName).Value.ToString(StringFormatter));

                case int _:
                    return(Conditional.HasCondition ? Conditional.GetResult(target.GetAttributeValue <double>(AttributeName)) : target.GetAttributeValue <double>(AttributeName).ToString(StringFormatter));

                case decimal _:
                    return(Conditional.HasCondition ? Conditional.GetResult(target.GetAttributeValue <decimal>(AttributeName)) : target.GetAttributeValue <decimal>(AttributeName).ToString(StringFormatter));

                case double _:
                    return(Conditional.HasCondition ? Conditional.GetResult(target.GetAttributeValue <double>(AttributeName)) : target.GetAttributeValue <double>(AttributeName).ToString(StringFormatter));

                case string _:
                    return(Conditional.GetResult(target[AttributeName].ToString()));
                }
            }
            else if (AttributeName.Equals("rand"))
            {
                string length;
                var    stringStyle = "upper";

                if (StringFormatter.Contains('?'))
                {
                    length      = StringFormatter.Split('?')[0];
                    stringStyle = StringFormatter.Split('?')[1].ToLower();
                }
                else
                {
                    length = StringFormatter;
                }

                if (!int.TryParse(length, out var stringLength))
                {
                    stringLength = 5;
                }

                var stringValues = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

                if (stringStyle == "mix")
                {
                    stringValues = stringValues + stringValues.ToLower();
                }
                else if (stringStyle == "lower")
                {
                    stringValues = stringValues.ToLower();
                }

                return(string.Join("", Enumerable.Range(0, stringLength).Select(n => stringValues[Random.Next(stringValues.Length)])));
            }

            return(DefaultValue);
        }