/*
 * Sonar ESQL Plugin
 * Copyright (C) 2013-2018 Thomas Pohl and EXXETA AG
 * http://www.exxeta.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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.LineIssue;

/**
 * This java class is created to implement the logic for checking there should be one blank line between procedure and function.
 * @author C50679
 *
 */
@Rule(key = "InsertBlankLineBetweenFuncProc")
public class InsertBlankLineBetweenFuncProcCheck extends DoubleDispatchVisitorCheck{

	private static final String MESSAGE = "Insert one blank line between functions and procedures.";


  @Override
	public void visitProgram(ProgramTree tree) {
		EsqlFile file = getContext().getEsqlFile();
		List<String>  lines = CheckUtils.readLines(file);
		
		int linecounter = 0;
		for (String line : lines) {
			
			linecounter = linecounter + 1;
			
			
			if(isEndStatement(line)){
				
			
				if(linecounter<lines.size()){
					String nextLine = lines.get(linecounter);
				
					if(!nextLine.trim().isEmpty()){
						addIssue(new LineIssue(this, linecounter, MESSAGE));
					}
				}
			}
		} 
  }
	
	public static boolean isEndStatement(String s) {
		String withoutSpace = s.replace(" ", "").toUpperCase();
		return withoutSpace.contains("END;");
	}

}

@@ -712,54 +712,109 @@
 ort 
-java.util.List;%0A%0Aimport org.sonar.check.Rule;%0A
+org.sonar.check.Rule;%0A%0Aimport com.exxeta.iss.sonar.esql.api.tree.statement.CreateModuleStatementTree;
 %0Aimp
@@ -852,23 +852,35 @@
 pi.tree.
-Program
+statement.Statement
 Tree;%0Aim
@@ -1001,73 +1001,93 @@
 ors.
-EsqlFile;%0Aimport com.exxeta.iss.sonar.esql.api.visitors.LineIssue
+LineIssue;%0Aimport com.exxeta.iss.sonar.esql.tree.impl.statement.CreateRoutineTreeImpl
 ;%0A%0A/
@@ -1475,18 +1475,19 @@
 es.%22;%0A%0A%0A
-  
+%09%0A%09
 @Overrid
@@ -1510,256 +1510,176 @@
 isit
-Program(ProgramTree tree) %7B%0A%09%09EsqlFile file = getContext().getEsqlFile();%0A%09%09List%3CString%3E  lines = CheckUtils.readLines(file);%0A%09%09%0A%09%09int linecounter = 0;%0A%09%09for (String line : lines) %7B%0A%09%09%09%0A%09%09%09linecounter = linecounter + 1;%0A%09%09%09%0A%09%09%09%0A%09%09%09if(isEndS
+CreateModuleStatement(CreateModuleStatementTree tree) %7B%0A%09%09StatementTree previousStatement = null;%0A%09%09for (StatementTree statement : tree.moduleStatementsList().s
 tatement
 (lin
@@ -1678,150 +1678,208 @@
 ment
+s
 (
-line
 ))
+ 
 %7B%0A%09%09%09
-%09%0A%09%09%09%0A%09%09%09%09if(linecounter%3Clines.size())%7B%0A%09%09%09%09%09String nextLine = lines.get(linecounter);%0A%09%09%09%09
+if (statement instanceof CreateRoutineTreeImpl && previousStatement instanceof CreateRoutineTreeImpl) %7B
 %0A%09%09%09%09
-%09
 if
+ 
 (!
-nextLine.trim().isEmpty())
+(previousStatement.lastToken().endLine()%3Cstatement.firstToken().line()-1)) 
 %7B%0A
-%09
 %09%09%09%09
@@ -1912,19 +1912,37 @@
 is, 
-linecounter
+previousStatement.lastToken()
 , ME
@@ -1958,20 +1958,18 @@
 %09%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09%7D%0A%09%09%09
 %7D%0A%09%09
@@ -1968,166 +1968,296 @@
 %0A%09%09%09
-%7D%0A%09%09%7D %0A  %7D%0A%09%0A%09public static boolean isEndStatement(String s) %7B%0A%09%09String withoutSpace = s.replace(%22 %22, %22%22).toUpperCase();%0A%09%09return withoutSpace.contains(%22END;%22
+previousStatement=statement;%0A%09%09%7D%0A%09%09if (previousStatement instanceof CreateRoutineTreeImpl && !(previousStatement.lastToken().endLine() %3C tree.endKeyword().line()-1)) %7B%0A%09%09%09addIssue(new LineIssue(this, previousStatement.lastToken(), MESSAGE));%0A%09%09%7D%0A%09%09%0A%09%09super.visitCreateModuleStatement(tree
 );%0A%09

Improve InsertBlankLineBetweenFuncProcCheck