private bool IsBinaryOperatorEntityIsNullCheck(BinaryOperator binaryOperator)
 {
     return ((binaryOperator.Member == MethodNames.Equal) || (binaryOperator.Member == MethodNames.NotEqual)) &&
         (((binaryOperator.RightOperand is Literal) && (((Literal)binaryOperator.RightOperand).Value == null)) ||
         ((binaryOperator.LeftOperand is Literal) && (((Literal)binaryOperator.LeftOperand).Value == null)));
 }
 private bool IsBinaryOperatorComplexEntityContrain(BinaryOperator binaryOperator)
 {
     return ((binaryOperator.Member == MethodNames.Or) || (binaryOperator.Member == MethodNames.And)) &&
         ((binaryOperator.LeftOperand is EntityConstrain) || (binaryOperator.RightOperand is EntityConstrain) ||
         ((binaryOperator.LeftOperand is BinaryOperator) && (IsBinaryOperatorComplexEntityContrain((BinaryOperator)binaryOperator.LeftOperand))) ||
         ((binaryOperator.RightOperand is BinaryOperator) && (IsBinaryOperatorComplexEntityContrain((BinaryOperator)binaryOperator.RightOperand))));
 }
        private void VisitEntityIsNullCheck(BinaryOperator binaryOperator)
        {
            if (binaryOperator.Member == MethodNames.Equal)
            {
                EntityConstrain entityConstrain;
                _commandText.Append("NOT EXISTS {");
                VisitComponent(_currentEntityAccessor.Peek().About);
                _commandText.Append(" ");
                if ((binaryOperator.RightOperand is Literal) && (((Literal)binaryOperator.RightOperand).Value == null))
                {
                    entityConstrain = _currentEntityAccessor.Peek().Elements.OfType<EntityConstrain>().Where(item => item.Value == binaryOperator.LeftOperand).First();
                    VisitComponent(entityConstrain.Predicate);
                    _commandText.Append(" ");
                    VisitComponent(binaryOperator.LeftOperand);
                }
                else
                {
                    entityConstrain = _currentEntityAccessor.Peek().Elements.OfType<EntityConstrain>().Where(item => item.Value == binaryOperator.RightOperand).First();
                    VisitComponent(entityConstrain.Predicate);
                    _commandText.Append(" ");
                    VisitComponent(binaryOperator.RightOperand);
                }

                _commandText.Append("}");
                _visitedComponents.Remove(entityConstrain);
            }
            else
            {
                _cancelLast = true;
            }
        }
        private void VisitComplexEntityContrain(BinaryOperator binaryOperator)
        {
            string operatorString;
            switch (binaryOperator.Member)
            {
                case MethodNames.Or:
                    operatorString = "||";
                    break;
                case MethodNames.And:
                    operatorString = "&&";
                    break;
                default:
                    throw new NotImplementedException(String.Format("Binary operator '{0}' is not supported.", binaryOperator.Member));
            }

            switch (operatorString)
            {
                case "||":
                    {
                        _commandText.Append("EXISTS { ");
                        VisitComponent(binaryOperator.LeftOperand);
                        _commandText.Append(" } || EXISTS { ");
                        VisitComponent(binaryOperator.RightOperand);
                        _commandText.Append("} ");
                        break;
                    }

                case "&&":
                    {
                        _commandText.Append("{ ");
                        VisitComponent(binaryOperator.LeftOperand);
                        VisitComponent(binaryOperator.RightOperand);
                        _commandText.Append("} ");
                        break;
                    }
            }
        }
        /// <summary>Visit a binary operator.</summary>
        /// <param name="binaryOperator">Binary operator to be visited.</param>
        protected override void VisitBinaryOperator(BinaryOperator binaryOperator)
        {
            if (IsBinaryOperatorComplexEntityContrain(binaryOperator))
            {
                VisitComplexEntityContrain(binaryOperator);
            }
            else if (IsBinaryOperatorEntityIsNullCheck(binaryOperator))
            {
                VisitEntityIsNullCheck(binaryOperator);
            }
            else
            {
                string operatorString;
                switch (binaryOperator.Member)
                {
                    case MethodNames.Add:
                        operatorString = "+";
                        break;
                    case MethodNames.Substract:
                        operatorString = "-";
                        break;
                    case MethodNames.Multiply:
                        operatorString = "*";
                        break;
                    case MethodNames.Divide:
                        operatorString = "/";
                        break;
                    case MethodNames.Equal:
                        operatorString = "=";
                        break;
                    case MethodNames.GreaterThan:
                        operatorString = ">";
                        break;
                    case MethodNames.GreaterThanOrEqual:
                        operatorString = ">=";
                        break;
                    case MethodNames.LessThan:
                        operatorString = "<";
                        break;
                    case MethodNames.LessThanOrEqual:
                        operatorString = "<=";
                        break;
                    case MethodNames.NotEqual:
                        operatorString = "!=";
                        break;
                    case MethodNames.Or:
                        operatorString = "||";
                        break;
                    case MethodNames.And:
                        operatorString = "&&";
                        break;
                    default:
                        throw new NotImplementedException(String.Format("Binary operator '{0}' is not supported.", binaryOperator.Member));
                }

                VisitComponent(binaryOperator.LeftOperand);
                bool leftOperandCanceled = _cancelLast;
                _cancelLast = false;
                int startIndex = _commandText.Length;
                if (!leftOperandCanceled)
                {
                    _commandText.AppendFormat(" {0} ", operatorString);
                }

                VisitComponent(binaryOperator.RightOperand);
                bool rightOperandCanceled = _cancelLast;
                _cancelLast = false;
                int length = _commandText.Length - startIndex;
                if (rightOperandCanceled)
                {
                    _commandText = _commandText.Remove(startIndex, length);
                }

                _cancelLast = leftOperandCanceled && rightOperandCanceled;
            }
        }
 /// <summary>Visit a binary operator.</summary>
 /// <param name="binaryOperator">Binary operator to be visited.</param>
 protected abstract void VisitBinaryOperator(BinaryOperator binaryOperator);