コード例 #1
0
        public bool ValidateOperand2BasedOnOperand1Type(AttributeTypeCode operand1Type, string operand2, bool multiOperand2Values)
        {
            DataTypeValidator validator = new DataTypeValidator();

            string[] operand2Values = operand2.Split('^');

            switch (operand1Type)
            {
            case AttributeTypeCode.State:
            case AttributeTypeCode.Status:
            case AttributeTypeCode.Integer:
            case AttributeTypeCode.Picklist:
                return(validator.IsInteger(operand2Values));

            case AttributeTypeCode.Double:
                return(validator.IsDouble(operand2Values));

            case AttributeTypeCode.Decimal:
                return(validator.IsDecimal(operand2Values));

            case AttributeTypeCode.Lookup:
                return(validator.IsLookup(operand2Values));

            case AttributeTypeCode.DateTime:
                return(validator.IsDate(operand2Values));

            case AttributeTypeCode.String:
            case AttributeTypeCode.Memo:
                return(true);

            default:
                throw new InvalidCastException("Unrecognized Operand1 type");
            }
        }
コード例 #2
0
        /// <summary>
        /// Based on the operand and operator types, generate the proper If statement.
        /// </summary>
        /// <param name="operand1"></param>
        /// <param name="operatorValue"></param>
        /// <param name="operand2"></param>
        /// <param name="operand1Type"></param>
        /// <returns></returns>
        private string GenerateIfStatement(string operand1, int operatorValue, string operand2, AttributeTypeCode operand1Type)
        {
            string operatorSymbol = "";
            string ifStatement    = "";
            string operand2JsArray;

            switch (operatorValue)
            {
            case 497060000:
                operatorSymbol = "==";
                break;

            case 497060001:
                operatorSymbol = "!=";
                break;

            case 497060002:
                operatorSymbol = "<";
                break;

            case 497060006:
                operatorSymbol = "<=";
                break;

            case 497060003:
                operatorSymbol = ">";
                break;

            case 497060004:
                operatorSymbol = ">=";
                break;

            case 497060005:     // contains
                ifStatement = $"if (Boolean(getFieldValue(\"{operand1}\")) &&getFieldValue(\"{operand1}\")!=\"\" )";
                return(ifStatement);

            case 497060007:     // doesn't contain
                ifStatement = $"if (!Boolean(getFieldValue(\"{operand1}\")))";
                return(ifStatement);

            case 497060008:     //in
                operand2JsArray = GenerateJSArray(operand2, operand1Type);
                ifStatement     = $"if ({operand2JsArray}.includes(getFieldValue(\"{operand1}\")))";
                return(ifStatement);

            case 497060009:     // not in
                operand2JsArray = GenerateJSArray(operand2, operand1Type);
                ifStatement     = $"if (!{operand2JsArray}.includes(getFieldValue(\"{operand1}\")))";
                return(ifStatement);

            default:
                throw new InvalidOperationException($"Unrecognized Operator Value: {operatorValue}");
            }

            DataTypeValidator validator = new DataTypeValidator();

            switch (operand1Type)
            {
            case AttributeTypeCode.DateTime:
                //make sure operand 2 is valid date
                if (validator.IsDate(operand2))
                {
                    string date1 = $"new Date(getFieldValue(\"{operand1}\"))";
                    string date2 = $"new Date(\"{operand2}\")";
                    ifStatement = $"if ({date1} {operatorSymbol} {date2})";
                }
                else
                {
                    throw new InvalidCastException("Operand 2 Should be a formatted as a datetime");
                }
                break;

            case AttributeTypeCode.Boolean:
                if (validator.IsBoolean(operand2))
                {
                    ifStatement = $"if (getFieldValue(\"{operand1}\") {operatorSymbol} {operand2})";
                }
                else
                {
                    throw new InvalidCastException("Operand 2 Should be a boolean (true or false)");
                }
                break;

            case AttributeTypeCode.State:
            case AttributeTypeCode.Status:
            case AttributeTypeCode.Integer:
            case AttributeTypeCode.Picklist:
                if (validator.IsInteger(operand2))
                {
                    ifStatement = $"if (getFieldValue(\"{operand1}\") {operatorSymbol} {operand2})";
                }
                else
                {
                    throw new InvalidCastException("Operand 2 Should be an integer");
                }
                break;

            case AttributeTypeCode.Double:
                if (validator.IsDouble(operand2))
                {
                    ifStatement = $"if (getFieldValue(\"{operand1}\") {operatorSymbol} {operand2})";
                }
                else
                {
                    throw new InvalidCastException("Operand 2 Should be an double");
                }
                break;

            case AttributeTypeCode.Decimal:
                if (validator.IsDecimal(operand2))
                {
                    ifStatement = $"if (getFieldValue(\"{operand1}\") {operatorSymbol} {operand2})";
                }
                else
                {
                    throw new InvalidCastException("Operand 2 Should be decimal");
                }
                break;

            case AttributeTypeCode.Lookup:
                if (validator.IsLookup(operand2))
                {
                    ifStatement = $"if (getFieldValue(\"{operand1}\") {operatorSymbol} \"{operand2}\")";
                }
                else
                {
                    throw new InvalidCastException("Operand 2 Should be a GUID");
                }
                break;

            default:     // everything else is handled as string
                ifStatement = $"if (getFieldValue(\"{operand1}\") {operatorSymbol} \"{operand2}\")";
                break;
            }

            return(ifStatement);
        }