/// <summary>Handles the specified statement.</summary>
    /// <param name="statement">The statement.</param>
    /// <param name="scope">The scope.</param>
    /// <returns>Returns the string.</returns>
    public override StatementDescriptor Handle(IStatement statement, AutoTemplateScope scope)
    {
      var ifStatement = statement as IIfStatement;
      if (ifStatement == null)
      {
        return null;
      }

      var condition = ifStatement.Condition;
      if (condition == null)
      {
        return null;
      }

      var expressionDescriptor = ExpressionTemplateBuilder.Handle(condition, scope.ScopeParameters);
      if (expressionDescriptor != null)
      {
        return new StatementDescriptor(scope, string.Format("if ({0}) {{ $END$ }}", expressionDescriptor.Template), expressionDescriptor.TemplateVariables);
      }

      var result = new StatementDescriptor(scope)
      {
        Template = string.Format("if ({0}) {{ $END$ }}", condition.GetText())
      };

      string variableName;
      if (scope.ScopeParameters.TryGetValue("VariableName", out variableName))
      {
        result.Template = result.Template.Replace(variableName, "$VariableName$");
      }

      return result;
    }
        /// <summary>
        /// Creates a statement based on the prepared statement.
        /// </summary>
        /// <param name="statementCreationArgs">The statement creation args.</param>
        /// <returns></returns>
        public StatementDescriptor CreatePrepared(StatementCreationArgs statementCreationArgs)
        {
            if (statementCreationArgs.PreparedStatementId == null)
            {
                throw new EPException("invalid statement arguments - missing prepared statement id");
            }

            var preparedStatement = GetPreparedStatement(statementCreationArgs.PreparedStatementId);

            if (preparedStatement != null)
            {
                var administrator       = ServiceProvider.EPAdministrator;
                var statementDescriptor = new StatementDescriptor();
                var statement           = administrator.Create(
                    preparedStatement,
                    statementCreationArgs.StatementName,
                    statementDescriptor);

                var publishers = _eventPublisherFactories
                                 .Select(factory => factory.CreatePublisher(new EventPublisherArgs(ServiceProvider.EPRuntime, statement)));

                if (StatementCreated != null)
                {
                    StatementCreated(this, new StatementCreationEventArgs(this, statement));
                }

                statementDescriptor.Id        = statement.Name;
                statementDescriptor.URIs      = publishers.Select(publisher => publisher.URI.ToString()).ToArray();
                statementDescriptor.EventType = statement.EventType.ToXElement();
                return(statementDescriptor);
            }

            throw new EPException("prepared statement does not exist");
        }
    /// <summary>Handles the specified statement.</summary>
    /// <param name="statement">The statement.</param>
    /// <param name="scope">The scope.</param>
    /// <returns>Returns the string.</returns>
    public override StatementDescriptor Handle(IStatement statement, AutoTemplateScope scope)
    {
      var tryStatement = statement as ITryStatement;
      if (tryStatement == null)
      {
        return null;
      }

      var result = new StatementDescriptor(scope)
      {
        Template = string.Format("try {{ $END$ }}")
      };

      foreach (var catchClause in tryStatement.Catches)
      {
        var type = catchClause.ExceptionType;
        if (type == null)
        {
          result.Template += " catch { }";
          continue;
        }

        var typeName = type.GetLongPresentableName(tryStatement.Language);
        result.Template += string.Format(" catch ({0}) {{ }}", typeName);
      }

      return result;
    }
    /// <summary>Handles the specified statement.</summary>
    /// <param name="statement">The statement.</param>
    /// <param name="scope">The scope.</param>
    /// <returns>Returns the string.</returns>
    public override StatementDescriptor Handle(IStatement statement, AutoTemplateScope scope)
    {
      var result = new StatementDescriptor(scope)
      {
        Template = string.Format("try {{ $END$ }} finally {{ }}")
      };

      return result;
    }
        /// <summary>
        /// Creates a statement based off the pattern that is presented.
        /// </summary>
        /// <param name="creationArgs">The on expression.</param>
        /// <returns></returns>
        public StatementDescriptor CreatePattern(StatementCreationArgs creationArgs)
        {
            var administrator       = ServiceProvider.EPAdministrator;
            var statementDescriptor = new StatementDescriptor();
            var statement           = administrator.CreatePattern(
                creationArgs.StatementText,
                creationArgs.StatementName,
                statementDescriptor);

            var publishers = _eventPublisherFactories
                             .Select(factory => factory.CreatePublisher(new EventPublisherArgs(ServiceProvider.EPRuntime, statement)));

            if (StatementCreated != null)
            {
                StatementCreated(this, new StatementCreationEventArgs(this, statement));
            }

            statementDescriptor.Id        = statement.Name;
            statementDescriptor.URIs      = publishers.Select(publisher => publisher.URI.ToString()).ToArray();
            statementDescriptor.EventType = statement.EventType.ToXElement();
            return(statementDescriptor);
        }
    /// <summary>Handles the specified statement.</summary>
    /// <param name="statement">The statement.</param>
    /// <param name="scope">The scope.</param>
    /// <returns>Returns the string.</returns>
    public override StatementDescriptor Handle(IStatement statement, AutoTemplateScope scope)
    {
      var declarationStatement = statement as IDeclarationStatement;
      if (declarationStatement == null)
      {
        return null;
      }

      var localVariableDeclarations = declarationStatement.VariableDeclarations;
      if (localVariableDeclarations.Count != 1)
      {
        return null;
      }

      var localVariableDeclaration = localVariableDeclarations.FirstOrDefault();
      if (localVariableDeclaration == null)
      {
        return null;
      }

      var expressionInitializer = localVariableDeclaration.Initializer as IExpressionInitializer;
      if (expressionInitializer == null)
      {
        return null;
      }

      var value = expressionInitializer.Value;
      if (value != null)
      {
        var expressionDescriptor = ExpressionTemplateBuilder.Handle(value, scope.ScopeParameters);
        if (expressionDescriptor != null)
        {
          var typeName = string.Empty;
          if (localVariableDeclaration.IsVar)
          {
            typeName += "var";
          }
          else
          {
            var type = localVariableDeclaration.Type;
            if (type != null && !type.IsUnknown && type.IsResolved)
            {
              typeName += type.GetPresentableName(localVariableDeclaration.Language);
            }
            else
            {
              var scalarTypeName = localVariableDeclaration.ScalarTypeName;
              if (scalarTypeName != null)
              {
                typeName += scalarTypeName.QualifiedName;
              }
              else
              {
                return null;
              }
            }
          }

          expressionDescriptor.Template = string.Format("{0} $name$ {1} {2};", typeName, localVariableDeclaration.AssignmentSign.GetTokenType().TokenRepresentation, expressionDescriptor.Template);
          expressionDescriptor.TemplateVariables["name"] = "suggestVariableName()";

          return new StatementDescriptor(scope, expressionDescriptor.Template, expressionDescriptor.TemplateVariables);
        }
      }

      var result = new StatementDescriptor(scope)
      {
        Template = declarationStatement.GetText()
      };

      string variableName;
      if (scope.ScopeParameters.TryGetValue("VariableName", out variableName))
      {
        result.Template = result.Template.Replace(variableName, "$VariableName$");
      }

      return result;
    }
    /// <summary>Handles the specified statement.</summary>
    /// <param name="statement">The statement.</param>
    /// <param name="scope">The scope.</param>
    /// <returns>Returns the string.</returns>
    public override StatementDescriptor Handle(IStatement statement, AutoTemplateScope scope)
    {
      var parent = statement.Parent;
      if (parent == null)
      {
        return null;
      }

      var foreachStatement = parent as IForeachStatement;
      if (foreachStatement == null)
      {
        return null;
      }

      var collection = foreachStatement.Collection;
      if (collection == null)
      {
        return null;
      }

      var iteratorDeclaration = foreachStatement.IteratorDeclaration;
      if (iteratorDeclaration == null)
      {
        return null;
      }

      var iterator = string.Empty;
      if (iteratorDeclaration.IsVar)
      {
        iterator += "var";
      }
      else
      {
        var type = iteratorDeclaration.Type;
        if (type == null || type.IsUnknown || !type.IsResolved)
        {
          return null;
        }

        iterator += type.GetLongPresentableName(statement.Language);
      }

      iterator += " $iterator$";

      var expressionDescriptor = ExpressionTemplateBuilder.Handle(collection, scope.ScopeParameters);
      if (expressionDescriptor != null)
      {
        var r = new StatementDescriptor(scope, string.Format("foreach ({0} in {1}) {{ $END$ }}", iterator, expressionDescriptor.Template), expressionDescriptor.TemplateVariables);
        r.TemplateVariables["iterator"] = "suggestVariableName()";
        return r;
      }

      var result = new StatementDescriptor(scope)
      {
        Template = string.Format("foreach ({0} in {1}) {{ $END$ }}", iterator, collection.GetText())
      };

      string variableName;
      if (scope.ScopeParameters.TryGetValue("VariableName", out variableName))
      {
        result.Template = result.Template.Replace(variableName, "$VariableName$");
      }

      result.TemplateVariables["iterator"] = "suggestVariableName()";

      return result;
    }
 /// <summary>
 /// Initializes a new instance of the <see cref="CatalystStatement"/> class.
 /// </summary>
 /// <param name="adapter"></param>
 /// <param name="statementDescriptor">The statement descriptor.</param>
 /// <param name="userObject">The user object.</param>
 public CatalystStatement(Catalyst adapter, StatementDescriptor statementDescriptor, object userObject)
 {
     Adapter             = adapter;
     StatementDescriptor = statementDescriptor;
     UserObject          = userObject;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="CatalystStatement"/> class.
 /// </summary>
 /// <param name="adapter"></param>
 /// <param name="statementDescriptor">The statement descriptor.</param>
 public CatalystStatement(Catalyst adapter, StatementDescriptor statementDescriptor)
 {
     Adapter             = adapter;
     StatementDescriptor = statementDescriptor;
 }