// these will be initailzed every time a new target entity
// is found in the result set (which should be ordered by table name
// among
// other things)
DbRelationship forwardRelationship = null;
DbRelationshipDetected reverseRelationship = null;
DbEntity fkEntity = null;
ExportedKey key = null;
do {
// extract data from resultset
key = ExportedKey.extractData(rs);
short keySeq = rs.getShort("KEY_SEQ");
if (keySeq == 1) {
if (forwardRelationship != null) {
postprocessMasterDbRelationship(forwardRelationship,
key);
forwardRelationship = null;
}
// start new entity
String fkEntityName = key.getFKTableName();
String fkName = key.getFKName();
if (!includeTableName(fkEntityName)) {
continue;
}
fkEntity = map.getDbEntity(fkEntityName);
if (fkEntity == null) {
logger.info("FK warning: no entity found for name '"
+ fkEntityName + "'");
} else if (skippedEntities.contains(pkEntity)
&& skippedEntities.contains(fkEntity)) {
// cay-479 - don't load relationships between two
// skipped entities.
continue;
} else {
// init relationship
String forwardPreferredName = namingStrategy
.createDbRelationshipName(key, true);
forwardRelationship = new DbRelationship(uniqueRelName(
pkEntity, forwardPreferredName));
forwardRelationship.setSourceEntity(pkEntity);
forwardRelationship.setTargetEntity(fkEntity);
pkEntity.addRelationship(forwardRelationship);
String reversePreferredName = namingStrategy
.createDbRelationshipName(key, false);
reverseRelationship = new DbRelationshipDetected(
uniqueRelName(fkEntity, reversePreferredName));
reverseRelationship.setFkName(fkName);
reverseRelationship.setToMany(false);
reverseRelationship.setSourceEntity(fkEntity);
reverseRelationship.setTargetEntity(pkEntity);
fkEntity.addRelationship(reverseRelationship);
}
}
if (fkEntity != null) {
// Create and append joins
String pkName = key.getPKColumnName();
String fkName = key.getFKColumnName();
// skip invalid joins...
DbAttribute pkAtt = (DbAttribute) pkEntity
.getAttribute(pkName);
if (pkAtt == null) {
logger.info("no attribute for declared primary key: "
+ pkName);
continue;
}
DbAttribute fkAtt = (DbAttribute) fkEntity
.getAttribute(fkName);
if (fkAtt == null) {
logger.info("no attribute for declared foreign key: "
+ fkName);
continue;
}
if (forwardRelationship != null) {
forwardRelationship.addJoin(new DbJoin(
forwardRelationship, pkName, fkName));
}
if (reverseRelationship != null) {
reverseRelationship.addJoin(new DbJoin(
reverseRelationship, fkName, pkName));
}
}
} while (rs.next());