package com.brg.analyse;

import com.brg.ServiceProvider;
import com.brg.domain.RuleValueBundle;

public class ValidateRuleHelper {
    private  DatabaseService databaseService;
    private RuleValueBundle ruleValueBundle;

    public ValidateRuleHelper(RuleValueBundle ruleValueBundle) {
        databaseService = ServiceProvider.getInstance().getAnalyseService().getDatabaseService();
        this.ruleValueBundle = ruleValueBundle;
    }

    /**
    * Check which type must by validate
     **/
    public boolean RuleType(String type) {
        boolean value = false;

        switch (type) {
            case "Attribute": value = AttributeValidate();
                break;
            case "Tuple": value = TupleValidate();
                break;
            case "Entity": value = EntityValidate();
                break;
        }

        return value;
    }

    private boolean EntityValidate() {
        return false;
    }

    private boolean TupleValidate() {
        String table = "", column1 = "", column2 = "";
        for(String key: ruleValueBundle.getKeys()) {
            if(key.equals("tuple.table")) {
                table = (String) ruleValueBundle.getValue(key);
            }

            if(key.equals("tuple.column1")) {
                column1 = (String) ruleValueBundle.getValue(key);
            }

            if(key.equals("tuple.column2")) {
                column2 = (String) ruleValueBundle.getValue(key);
            }
        }

        if(databaseService.hasTable(table.toUpperCase()) && databaseService.hasColumn(table.toUpperCase(), column1.toUpperCase()) && databaseService.hasColumn(table.toUpperCase(), column2.toUpperCase())) {
            return true;
        }

        return false;
    }

    private boolean AttributeValidate() {
        String table = "", column = "";

        for(String key: ruleValueBundle.getKeys()) {
            if(key.equals("attribute.column")) {

                column = (String) ruleValueBundle.getValue(key);
            }

            if(key.equals("attribute.table")){
                table = (String) ruleValueBundle.getValue(key);
            }
        }

        if(databaseService.hasTable(table.toUpperCase()) && databaseService.hasColumn(table.toUpperCase(), column.toUpperCase())) {
            return true;
        }

        return false;
    }

}

@@ -815,32 +815,118 @@
          break;%0A
+            case %22InterEntity%22: value = InterEntityValidate();%0A                break;%0A
         %7D%0A%0A     
@@ -969,16 +969,21 @@
 boolean 
+Inter
 EntityVa
@@ -985,32 +985,1724 @@
 ityValidate() %7B%0A
+        String column1 = %22%22, column2 = %22%22, table1 = %22%22, table2 = %22%22;%0A%0A        for(String key: ruleValueBundle.getKeys()) %7B%0A            if(key.equals(%22interentity.table.1%22)) %7B%0A                table1 = (String) ruleValueBundle.getValue(key);%0A            %7D%0A%0A            if(key.equals(%22interentity.table.2%22)) %7B%0A                table2 = (String) ruleValueBundle.getValue(key);%0A            %7D%0A%0A            if(key.equals(%22interentity.column.1%22)) %7B%0A                column1 = (String) ruleValueBundle.getValue(key);%0A            %7D%0A%0A            if(key.equals(%22interentity.column.2%22)) %7B%0A                column2 = (String) ruleValueBundle.getValue(key);%0A            %7D%0A        %7D%0A%0A        if(databaseService.hasTable(table1.toUpperCase()) && databaseService.hasTable(table2.toUpperCase()) && databaseService.hasColumn(table1.toUpperCase(), column1.toUpperCase()) && databaseService.hasColumn(table2.toUpperCase(), column2.toUpperCase())) %7B%0A            return true;%0A        %7D%0A%0A        return false;%0A    %7D%0A%0A    private boolean EntityValidate() %7B%0A        String table = %22%22, column = %22%22;%0A%0A        System.out.println(ruleValueBundle.getKeys());%0A        for(String key: ruleValueBundle.getKeys()) %7B%0A            if(key.equals(%22entity.table%22)) %7B%0A                table = (String) ruleValueBundle.getValue(key);%0A                System.out.println(table);%0A            %7D%0A%0A            if(key.equals(%22entity.column%22)) %7B%0A                column = (String) ruleValueBundle.getValue(key);%0A                System.out.println(column);%0A            %7D%0A        %7D%0A%0A        if(databaseService.hasTable(table.toUpperCase()) && databaseService.hasColumn(table.toUpperCase(), column.toUpperCase())) %7B%0A            return true;%0A        %7D%0A%0A
         return f
@@ -2807,16 +2807,17 @@
 2 = %22%22;%0A
+%0A
         

add inter entity validate