ArrayList remotePrimFields = elementClassDef.getPrimaryKeys();
String localKeyList = collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY);
String remoteKeyList = collDef.getProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY);
String ownerTable = getTargetTable(ownerClassDef, tableName, localKeyList);
String elementTable = getTargetTable(elementClassDef, tableName, remoteKeyList);
CommaListIterator localKeys = new CommaListIterator(localKeyList);
CommaListIterator localKeyDocs = new CommaListIterator(collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY_DOCUMENTATION));
CommaListIterator remoteKeys = new CommaListIterator(remoteKeyList);
CommaListIterator remoteKeyDocs = new CommaListIterator(collDef.getProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY_DOCUMENTATION));
ArrayList localColumns = new ArrayList();
ArrayList remoteColumns = new ArrayList();
boolean asPrimarykeys = collDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE_PRIMARYKEYS, false);
FieldDescriptorDef fieldDef;
ColumnDef columnDef;
String relationName;
String name;
int idx;
for (idx = 0; localKeys.hasNext(); idx++)
{
fieldDef = (FieldDescriptorDef)localPrimFields.get(idx);
name = localKeys.getNext();
columnDef = tableDef.getColumn(name);
if (columnDef == null)
{
columnDef = new ColumnDef(name);
tableDef.addColumn(columnDef);
}
columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_TYPE, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_JDBC_TYPE));
columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_SIZE, fieldDef.getSizeConstraint());
if (asPrimarykeys)
{
columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_PRIMARYKEY, "true");
}
if (localKeyDocs.hasNext())
{
columnDef.setProperty(PropertyHelper.OJB_PROPERTY_COLUMN_DOCUMENTATION, localKeyDocs.getNext());
}
localColumns.add(name);
remoteColumns.add(fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN));
}
if (collDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_DATABASE_FOREIGNKEY, true))
{
relationName = collDef.getProperty(PropertyHelper.TORQUE_PROPERTY_RELATION_NAME);
if ((relationName != null) && (ownerTable != null))
{
tableDef.addForeignkey(relationName, ownerTable, localColumns, remoteColumns);
}
}
localColumns.clear();
remoteColumns.clear();
for (idx = 0; remoteKeys.hasNext(); idx++)
{
fieldDef = (FieldDescriptorDef)remotePrimFields.get(idx);
name = remoteKeys.getNext();
columnDef = tableDef.getColumn(name);
if (columnDef == null)
{
columnDef = new ColumnDef(name);
tableDef.addColumn(columnDef);
}
columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_TYPE, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_JDBC_TYPE));
columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_SIZE, fieldDef.getSizeConstraint());
if (asPrimarykeys)
{
columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_PRIMARYKEY, "true");
}
if (remoteKeyDocs.hasNext())
{
columnDef.setProperty(PropertyHelper.OJB_PROPERTY_COLUMN_DOCUMENTATION, remoteKeyDocs.getNext());
}
localColumns.add(name);
remoteColumns.add(fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN));
}