package ie.corballis.treeway.generate.overrides;

import org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.TableIdentifier;
import org.hibernate.mapping.Column;

import java.util.List;

import static java.beans.Introspector.decapitalize;
import static org.hibernate.cfg.reveng.ReverseEngineeringStrategyUtil.simplePluralize;
import static org.hibernate.cfg.reveng.ReverseEngineeringStrategyUtil.toUpperCamelCase;

public class TreewayReverseEngineeringStrategy extends DelegatingReverseEngineeringStrategy {

    public TreewayReverseEngineeringStrategy(ReverseEngineeringStrategy delegate) {
        super(delegate);
    }

    @Override
    public String foreignKeyToEntityName(String keyname,
                                         TableIdentifier fromTable,
                                         List fromColumnNames,
                                         TableIdentifier referencedTable,
                                         List referencedColumnNames,
                                         boolean uniqueReference) {

        // for now we assume there will only be simple foreign keys with only one column
        String name = ((Column) fromColumnNames.get(0)).getName();

        if (name.endsWith("_id")) {
            name = name.substring(0, name.length() - 3);
        }

        return columnToPropertyName(null, name);
    }

    @Override
    public String foreignKeyToCollectionName(String keyname,
                                             TableIdentifier fromTable,
                                             List fromColumns,
                                             TableIdentifier referencedTable,
                                             List referencedColumns,
                                             boolean uniqueReference) {
        if (uniqueReference) {
            return super.foreignKeyToCollectionName(keyname,
                                                    fromTable,
                                                    fromColumns,
                                                    referencedTable,
                                                    referencedColumns,
                                                    uniqueReference);
        } else {
            // for now we assume there will only be simple foreign keys with only one column
            String foreignKeyName = ((Column) fromColumns.get(0)).getName();
            if (foreignKeyName.endsWith("_id")) {
                foreignKeyName = foreignKeyName.substring(0, foreignKeyName.length() - 3);
            }

            String tableName = simplePluralize(decapitalize(toUpperCamelCase(fromTable.getName())));
            String columnName = toUpperCamelCase(columnToPropertyName(null, foreignKeyName));

            return tableName + "For" + columnName;
        }
    }
}

@@ -105,32 +105,95 @@
 eeringStrategy;%0A
+import org.hibernate.cfg.reveng.ReverseEngineeringRuntimeInfo;%0A
 import org.hiber
@@ -228,32 +228,32 @@
 eeringStrategy;%0A
-
 import org.hiber
@@ -321,16 +321,52 @@
 .Column;
+%0Aimport org.hibernate.mapping.Table;
 %0A%0Aimport
@@ -702,24 +702,80 @@
 Strategy %7B%0A%0A
+    private ReverseEngineeringRuntimeInfo runtimeInfo;%0A%0A
     public T
@@ -842,24 +842,24 @@
 delegate) %7B%0A
-
         supe
@@ -870,32 +870,134 @@
 legate);%0A    %7D%0A%0A
+    public void configure(ReverseEngineeringRuntimeInfo rti) %7B%0A        this.runtimeInfo = rti;%0A    %7D%0A%0A
     @Override%0A  
@@ -3175,11 +3175,913 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    public boolean isForeignKeyCollectionInverse(String name,%0A                                                 TableIdentifier foreignKeyTableIdentifier,%0A                                                 List columns,%0A                                                 TableIdentifier foreignKeyReferencedTable,%0A                                                 List referencedColumns) %7B%0A%0A        Table foreignKeyTable = runtimeInfo.getTable(foreignKeyTableIdentifier);%0A        if (foreignKeyTable == null) %7B%0A            return true; // we don't know better%0A        %7D%0A%0A        if (isManyToManyTable(foreignKeyTable)) %7B%0A            // if the reference column is the first one then we are inverse.%0A            Column column = foreignKeyTable.getColumn(0);%0A            Column fkColumn = (Column) columns.get(0);%0A            return fkColumn.equals(column);%0A        %7D%0A%0A        return true;%0A    %7D
 %0A%7D%0A

Fix many-to-many generation