package org.batgen.generators;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.batgen.Column;
import org.batgen.ForeignNode;
import org.batgen.Table;

public class ForeignKeyGenerator {
    private ArrayList<ForeignNode> foreignNodeList;
    private HashMap<String, Table> tableMap;

    public ForeignKeyGenerator( ArrayList<ForeignNode> foreignNodeList, HashMap<String, Table> tableMap ) {
        this.foreignNodeList = foreignNodeList;
        this.tableMap = tableMap;
    }

    public void createForeignKeys() {
        StringBuilder sb = new StringBuilder();
        sb.append( writeAddForeignKeys() );
        GenUtil.writeToFile( "sql/_AlterTables.sql", sb.toString() );
        sb = new StringBuilder();
        sb.append( writeDropForeignKeys() );
        GenUtil.appendToFile( "sql/_DropTables.sql", sb.toString() );
    }

    private String writeAddForeignKeys() {
        StringBuilder sb = new StringBuilder();
        boolean fromFieldExist = false;
        boolean toFieldExist = false;
        Table fromTable;
        Table toTable;
        String fromField = "";
        String toField = "";

        ForeignNode node;
        for ( int keyNum = 0; keyNum < foreignNodeList.size(); keyNum++ ) {
            node = foreignNodeList.get( keyNum );
            fromTable = tableMap.get( node.getFromTable() );
            toTable = tableMap.get( node.getToTable() );
            fromFieldExist = false;
            toFieldExist = false;

            if ( fromTable != null && toTable != null ) {
                for ( Column col : fromTable.getColumns() ) {
                    if ( col.getFldName().equals( node.getFromField() ) ) {
                        fromFieldExist = true;
                        fromField = col.getColName();
                        break;
                    }
                }
                for ( Column col : toTable.getColumns() ) {
                    if ( col.getFldName().equals( node.getToField() ) ) {
                        toFieldExist = true;
                        toField = col.getColName();
                        break;
                    }
                }

                if ( fromFieldExist && toFieldExist ) {
                    sb.append( "ALTER TABLE " + fromTable.getTableName() );
                    sb.append( " ADD CONSTRAINT FK_" + fromTable.getTableName() + "_" + keyNum );
                    sb.append( " FOREIGN KEY (" + fromField + ")" );
                    sb.append( " REFERENCES " + toTable.getTableName() + "(" + toField + ");\n" );
                }
                else
                    throw new IllegalArgumentException( "In Table" + node.getFromTable() + " and/or "
                            + node.getToTable() + ", either the field names are wrong or don't exist for foreign keys." );
            }
            else
                throw new IllegalArgumentException( "Either the tables names ( " + node.getFromTable() + " and/or "
                        + node.getToTable() + " ) are wrong or don't exist for foreign keys." );
        }

        sb.append( "\n-- PROTECTED CODE -->" );
        List<String> lines = GenUtil.getProtectedLines( "sql/_AlterTables.sql" );
        if ( lines.isEmpty() ) {
            sb.append( "\n" );
        }
        for ( String line : lines ) {
            sb.append( line );
        }
        return sb.toString();
    }

    private String writeDropForeignKeys() {
        StringBuilder sb = new StringBuilder();
        boolean fromFieldExist = false;
        Table fromTable;
        String fromField = "";

        for ( ForeignNode node : foreignNodeList ) {
            fromTable = tableMap.get( node.getFromTable() );
            fromFieldExist = false;
            for ( Column col : fromTable.getColumns() ) {
                if ( col.getFldName().equals( node.getFromField() ) ) {
                    fromFieldExist = true;
                    fromField = col.getColName();
                    break;
                }
            }

            if ( fromFieldExist ) {
                sb.append( "ALTER TABLE " + fromTable.getTableName() );
                sb.append( " DROP CONSTRAINT FK_" + fromTable.getTableName() + "_" + fromField + ";\n" );
            }
        }
        return sb.toString();
    }
}

@@ -3495,48 +3495,8 @@
 ();%0A
-        boolean fromFieldExist = false;%0A
     
@@ -3512,32 +3512,33 @@
  fromTable;%0A
+%0A
         
 String fromF
@@ -3529,504 +3529,213 @@
     
-String fromField = %22%22;%0A%0A        for ( ForeignNode node : foreignNodeList ) %7B%0A            fromTable = tableMap.get( node.getFromTable() );%0A            fromFieldExist = false;%0A            for ( Column col : fromTable.getColumns() ) %7B%0A                if ( col.getFldName().equals( node.getFromField() ) ) %7B%0A                    fromFieldExist = true;%0A                    fromField = col.getColName();%0A                    break;%0A                %7D%0A            %7D%0A%0A            if ( fromFieldExist ) %7B%0A   
+ForeignNode node;%0A        for ( int keyNum = 0; keyNum %3C foreignNodeList.size(); keyNum++ ) %7B%0A            node = foreignNodeList.get( keyNum );%0A            fromTable = tableMap.get( node.getFromTable() );%0A
     
@@ -3734,33 +3734,32 @@
  );%0A            
- 
 sb.append( %22ALTE
@@ -3790,36 +3790,32 @@
 tTableName() );%0A
-    
             sb.a
@@ -3875,33 +3875,30 @@
 e() + %22_%22 + 
-fromField
+keyNum
  + %22;%5Cn%22 );%0A
@@ -3893,38 +3893,24 @@
  + %22;%5Cn%22 );%0A
-            %7D%0A
         %7D%0A  

Update ForeignKeyGenerator.java