/**
 * 
 */
package com.exxeta.iss.sonar.esql.check;

import java.util.List;

import org.sonar.check.Rule;

import com.exxeta.iss.sonar.esql.api.tree.ProgramTree;
import com.exxeta.iss.sonar.esql.api.visitors.DoubleDispatchVisitorCheck;
import com.exxeta.iss.sonar.esql.api.visitors.EsqlFile;
import com.exxeta.iss.sonar.esql.api.visitors.IssueLocation;
import com.exxeta.iss.sonar.esql.api.visitors.LineIssue;
import com.exxeta.iss.sonar.esql.api.visitors.PreciseIssue;

/**
 * @author C50679
 *
 */
@Rule(key = "FilterNodeModifyMessage")
public class FilterNodeModifyMessageCheck  extends DoubleDispatchVisitorCheck {
	
	private static final String MESSAGE = "The filter node cannot modify the message";
	//private static final String MESSAGE2 = "The filter node may only have one return value";
	
	
	@Override
	public void visitProgram(ProgramTree tree) {
		EsqlFile file = getContext().getEsqlFile();
		List<String>  line = CheckUtils.readLines(file);
		int i = 0;
		
		String lines[] = line.stream().toArray(String[]::new);
		 int lineNumber = 1;
		 String startLine = lines[lineNumber - 1];
		 
		 
         String upperCaseTheLine = startLine.toUpperCase();
		
		boolean filter = false;
        if(CheckUtils.isCreateFilterModuleLine(upperCaseTheLine))
            filter = true;
        if(filter)
        {
            
            boolean done = false;
           // int lineNumber;
			int line1 = lineNumber - 1;
            boolean filterViolation = false;
            do
            {
                if(done || line1 >= lines.length - 1)
                    break;
                line1++;
                String currentLine = lines[line1].toUpperCase();
                i =line1+1;
                if(currentLine.toUpperCase().contains("SET "))
                {
                    int pos = currentLine.indexOf("=");
                    if(pos > 0)
                    {
                        String leftSide = currentLine.substring(0, pos);
                        if(!leftSide.startsWith("IF ") && (leftSide.contains("ROOT ") || leftSide.contains("ROOT.")))
                        {
                            done = true;
                            filterViolation = true;
                        }
                    }
                }
                
            } while(true);
        //  int i = 0;
            if(filterViolation)
            {
            	addIssue(new LineIssue(this, i,   MESSAGE ));
            	//addIssue(new PreciseIssue(this, new IssueLocation(tree,   MESSAGE )));
            }
            
         }
        
      }
}




@@ -143,23 +143,234 @@
 pi.tree.
-Program
+FieldReferenceTree;%0Aimport com.exxeta.iss.sonar.esql.api.tree.ProgramTree;%0Aimport com.exxeta.iss.sonar.esql.api.tree.statement.CreateModuleStatementTree;%0Aimport com.exxeta.iss.sonar.esql.api.tree.statement.SetStatement
 Tree;%0Aim
@@ -674,16 +674,64 @@
 seIssue;
+%0Aimport com.google.common.collect.ImmutableList;
 %0A%0A/**%0A *
@@ -963,18 +963,16 @@
 %22;%0A%09
-//
 private 
 stat
@@ -971,1680 +971,469 @@
 ate 
-static final String MESSAGE2 = %22The filter node may only have one return value%22;%0A%09%0A%09%0A%09@Override%0A%09public void visitProgram(ProgramTree tree) %7B%0A%09%09EsqlFile file = getContext().getEsqlFile();%0A%09%09List%3CString%3E  line = CheckUtils.readLines(file);%0A%09%09int i = 0;%0A%09%09%0A%09%09String lines%5B%5D = line.stream().toArray(String%5B%5D::new);%0A%09%09 int lineNumber = 1;%0A%09%09 String startLine = lines%5BlineNumber - 1%5D;%0A%09%09 %0A%09%09 %0A         String upperCaseTheLine = startLine.toUpperCase();
+boolean insideFilterModule;%0A%09%0A%09private List%3CString%3E rootElements = ImmutableList.of(%22Root%22, %22InputRoot%22, %22OutputRoot%22); %0A%09%0A%09%0A%09@Override%0A%09public void visitSetStatement(SetStatementTree tree) %7B%0A%09%09if (this.insideFilterModule)%7B%0A%09%09%09if (tree.variableReference() instanceof FieldReferenceTree)%7B
 %0A%09%09
-%0A
 %09%09
-boolean filter = false;%0A        if(CheckUtils.isCreateFilterModuleLine(upperCaseTheLine))%0A            filter = true;%0A        if(filter)%0A        %7B%0A            %0A            boolean done = false;%0A           // int lineNumber;%0A%09%09%09int line1 = lineNumber - 1;%0A            boolean filterViolation = false;%0A            do%0A            %7B%0A                if(done %7C%7C line1 %3E= lines.length - 1)%0A                    break;%0A                line1++;%0A                String currentLine = lines%5Bline1%5D.toUpperCase();%0A                i =line1+1;%0A                if(currentLine.toUpperCase().contains(%22SET %22))%0A                %7B%0A                    int pos = currentLine.indexOf(%22=%22);%0A                    if(pos %3E 0)%0A                    %7B%0A                        String leftSide = currentLine.substring(0, pos);%0A                        if(!leftSide.startsWith(%22IF %22) && (leftSide.contains(%22ROOT %22) %7C%7C leftSide.contains(%22ROOT.%22)))%0A                        %7B%0A                            done = true;%0A                            filterViolation = true;%0A                        %7D%0A                    %7D%0A                %7D%0A                %0A            %7D while(true);%0A        //  int i = 0;%0A            if(filterViolation)%0A            %7B%0A            
+FieldReferenceTree fieldReference = (FieldReferenceTree)tree.variableReference();%0A%09%09%09%09if (rootElements.contains(fieldReference.pathElement().name().name().text()))%7B%0A%09%09%09%09
 %09add
@@ -1458,17 +1458,20 @@
 e(this, 
-i
+tree
 ,   MESS
@@ -1482,147 +1482,64 @@
 ));%0A
-            %09//addIssue(new PreciseIssue(this, new IssueLocation(tree,   MESSAGE )));%0A            %7D%0A            %0A         %7D%0A        %0A      
+%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09%0A%09%09%7D%0A%09%09%0A%09%09super.visitSetStatement(tree);%0A%09
 %7D%0A%7D%0A

Use the parsed tree.