/*
* DbForeignKey.java
*
*/
package com.m16e.tools.db;
import com.m16e.tools.xml.XmlInterpreter;
import com.m16e.tools.xml.XmlTreeNode;
import com.m16e.tools.xml.XmlTreeNodeable;
import org.apache.log4j.Logger;
////////////////////////////////////////////////////////////
/** A wrapper to a foreign key constraint
*/
public class DbForeignKey
extends DbConstraint
{
public static final String TAG_V_FK = "v-fk";
public static final String TAG_FK_TABLE = "fk-table";
public static final String TAG_FK_FIELD = "fk-field";
public static final String ATTR_DEFERRABLE = "deferrable";
public static final String ATTR_INITIALLY_DEFERRABLE = "initially-deferrable";
private boolean deferrable = false;
private boolean initiallyDeferrable = false;
Logger logger = Logger.getLogger( DbForeignKey.class );
////////////////////////////////////////////////////////////
public DbForeignKey(
DbTable fromTable, DbTable toTable, String constraintName )
{
if( fromTable == null )
throw new IllegalArgumentException(
"Table-from name can NOT be null!" );
if( toTable == null )
throw new IllegalArgumentException(
"Table-to name can NOT be null!" );
this.constraintName = constraintName;
this.fromTable = fromTable;
this.toTable = toTable;
}
////////////////////////////////////////////////////////////
public void fromXmlBeforeV0r7( String elFkTable )
{
constraintName =
fromTable.getSugestedConstraintName( toTable.getTableName() );
String fk = XmlInterpreter.getElementValue( elFkTable );
String[] elFields = XmlInterpreter.getElementArray( fk, TAG_FK_FIELD );
if( elFields == null )
return;
clear();
int f = 0;
for( f = 0; f < elFields.length; f++ )
{
String aux =
XmlInterpreter.getElementValue( elFields[f], ATTR_FROM );
int from = fromTable.getColumnIndex( aux );
aux =
XmlInterpreter.getElementValue( elFields[f], ATTR_TO );
int to = toTable.getColumnIndex( aux );
add( from, to );
}
}
////////////////////////////////////////////////////////////
// interface XmlExportable: BEGIN
////////////////////////////////////////////////////////////
public void fromXmlTreeNode( XmlTreeNodeable xtnFK )
{
constraintName = xtnFK.getAttribute( ATTR_NAME );
if( constraintName.length() == 0 )
{
constraintName =
fromTable.getSugestedConstraintName( toTable.getTableName() );
}
deferrable = Boolean.valueOf( xtnFK.getAttribute( ATTR_DEFERRABLE ) );
initiallyDeferrable =
Boolean.valueOf( xtnFK.getAttribute( ATTR_INITIALLY_DEFERRABLE ) );
java.util.List<XmlTreeNodeable> lstFlds =
xtnFK.getChildrenByTagName( TAG_FK_FIELD );
clear();
for( XmlTreeNodeable x : lstFlds )
{
String aux = x.getChildByTagName( false, ATTR_FROM ).getElementValue();
int from = fromTable.getColumnIndex( aux );
aux = x.getChildByTagName( false, ATTR_TO ).getElementValue();
int to = toTable.getColumnIndex( aux );
add( from, to );
}
}
///////////////////////////////////////////////////////////
public XmlTreeNodeable toXmlTreeNode()
{
XmlTreeNode xtnFK = new XmlTreeNode( TAG_V_FK );
xtnFK.setAttribute( DbConstraint.ATTR_NAME, constraintName );
xtnFK.setAttribute( ATTR_DEFERRABLE, "" + deferrable );
xtnFK.setAttribute( ATTR_INITIALLY_DEFERRABLE, "" + initiallyDeferrable );
int i = 0;
for( Integer fromField : fromFields )
{
if( fromField < 0 )
continue;
XmlTreeNode xtnField = new XmlTreeNode( xtnFK, TAG_FK_FIELD );
try
{
XmlTreeNode xtnFrom =
new XmlTreeNode(
xtnField, TAG_FROM, fromTable.getField( fromField ).getFieldName() );
}
catch( ArrayIndexOutOfBoundsException e )
{
logger.error(
"Bad from field! Table: " + fromTable.getTableName() +
" - field #" + fromField, e );
throw e;
}
try
{
XmlTreeNode xtnTo =
new XmlTreeNode(
xtnField, TAG_TO, toTable.getField( i ).getFieldName() );
}
catch( ArrayIndexOutOfBoundsException e )
{
logger.error(
"Bad to field! Table: " + toTable.getTableName() +
" - field #" + toFields.get( i ), e );
throw e;
}
i++;
}
return xtnFK;
}
public
boolean isDeferrable()
{
return deferrable;
}
public void setDeferrable( boolean deferrable )
{
this.deferrable = deferrable;
}
public boolean isInitiallyDeferrable()
{
return initiallyDeferrable;
}
public void setInitiallyDeferrable( boolean initiallyDeferrable )
{
this.initiallyDeferrable = initiallyDeferrable;
}
////////////////////////////////////////////////////////////
// interface XmlExportable: END
////////////////////////////////////////////////////////////
}
|