*/
private void addForeignField(
SourceElement foreignKey,
ControllerState controllerState)
{
SourceElement localTable = foreignKey.getParent();
SourceElement foreignTable = foreignKey.getChild(
TorqueSchemaElementName.TABLE);
String referencedBySuffix = getForeignReferencedBySuffix(
foreignKey, controllerState);
{
// the field name for the variable used
String foreignFieldName = (String) controllerState.getOption(
TemplateOptionName.OM_FOREIGN_FIELD_NAME_PREFIX)
+ localTable.getAttribute(
TableAttributeName.DB_OBJECT_CLASS_NAME)
+ controllerState.getOption(
TemplateOptionName.OM_FOREIGN_FIELD_NAME_SUFFIX)
+ referencedBySuffix;
// the field name to create the name of the getter and setter
String getterSetterFieldName
= (String) localTable.getAttribute(
TableAttributeName.DB_OBJECT_CLASS_NAME)
+ referencedBySuffix;
SourceElement foreignFieldElement
= new SourceElement(
ForeignKeyChildElementName.FOREIGN_FIELD);
foreignFieldElement.setAttribute(
JavaFieldAttributeName.FIELD_NAME,
foreignFieldName);
foreignFieldElement.setAttribute(
JavaFieldAttributeName.FIELD_ACCESS_MODIFIER,
"protected");
{
// the field name to cache the used Criteria
String criteriaFieldName = "last"
+ StringUtils.capitalize(getterSetterFieldName)
+ "Criteria";
foreignFieldElement.setAttribute(
ForeignKeyChildAttributeName
.FOREIGN_COLUMN_CRITERIA_CACHE_FIELD,
criteriaFieldName);
}
String fieldContainedType = (String) localTable.getAttribute(
TorqueSchemaAttributeName.JAVA_NAME);
foreignFieldElement.setAttribute(
JavaFieldAttributeName.FIELD_CONTAINED_TYPE,
fieldContainedType);
String fieldType = (String) controllerState.getOption(
TemplateOptionName.OM_FOREIGN_FIELD_TYPE)
+ "<" + fieldContainedType + ">";
foreignFieldElement.setAttribute(
JavaFieldAttributeName.FIELD_TYPE,
fieldType);
foreignFieldElement.setAttribute(
JavaFieldAttributeName.DEFAULT_VALUE,
"null");
{
String getterName = FieldHelper.getGetterName(
getterSetterFieldName,
fieldType,
controllerState)
+ "s";
foreignFieldElement.setAttribute(
JavaFieldAttributeName.GETTER_NAME,
getterName);
}
{
String setterName = FieldHelper.getSetterName(
getterSetterFieldName)
+ "s";
foreignFieldElement.setAttribute(
JavaFieldAttributeName.SETTER_NAME,
setterName);
}
{
String adderName = FieldHelper.getAdderName(
getterSetterFieldName,
controllerState);
foreignFieldElement.setAttribute(
JavaFieldAttributeName.ADDER_NAME,
adderName);
}
{
String initializerName = FieldHelper.getInitializerName(
getterSetterFieldName,
controllerState);
foreignFieldElement.setAttribute(
JavaFieldAttributeName.INITIALIZER_NAME,
initializerName);
}
{
String isInitializedName = FieldHelper.getIsInitializedName(
getterSetterFieldName,
controllerState);
foreignFieldElement.setAttribute(
JavaFieldAttributeName.IS_INITIALIZED_NAME,
isInitializedName);
}
{
String initType = (String) controllerState.getOption(
TemplateOptionName.OM_FOREIGN_FIELD_INIT_TYPE)
+ "<" + fieldContainedType + ">";
foreignFieldElement.setAttribute(
JavaFieldAttributeName.INITIALIZER_TYPE,
initType);
}
{
String fillerName = FieldHelper.getFillerName(
getterSetterFieldName,
"",
controllerState);
foreignFieldElement.setAttribute(
JavaFieldAttributeName.FILLER_NAME,
fillerName);
}
{
String setAndSaveMethodName
= FieldHelper.getSetAndSaveMethodName(
getterSetterFieldName,
"",
controllerState);
foreignFieldElement.setAttribute(
"setAndSaveMethodName",
setAndSaveMethodName);
}
{
// Name for a getter in the foreign table to
// retrieve entries in the foreign table plus the joined
// entries in the local table.
String joinGetterFieldName
= StringUtils.capitalize(getterSetterFieldName)
+ "sJoin"
+ foreignTable.getAttribute(
TorqueSchemaAttributeName.JAVA_NAME);
String joinGetterName = FieldHelper.getGetterName(
joinGetterFieldName,
fieldType,
controllerState);
foreignFieldElement.setAttribute(
ForeignKeyChildAttributeName
.FOREIGN_FIELD_JOIN_GETTER,
joinGetterName);
}
{
// Name for the doSelectJoinXXX method in the Peer Class
// of the foreign table.
String peerJoinSelectMethodName
= "doSelectJoin"
+ foreignTable.getAttribute(
TorqueSchemaAttributeName.JAVA_NAME)
+ referencedBySuffix;
foreignFieldElement.setAttribute(
ForeignKeyChildAttributeName
.PEER_JOIN_SELECT_METHOD,
peerJoinSelectMethodName);
}
{
// Name for the doSelectJoinAllExceptXXX method
// in the Peer Class of the foreign table.
String peerJoinAllExceptSelectMethodName
= "doSelectJoinAllExcept"
+ foreignTable.getAttribute(
TorqueSchemaAttributeName.JAVA_NAME)
+ referencedBySuffix;
foreignFieldElement.setAttribute(
ForeignKeyChildAttributeName
.PEER_JOIN_ALL_EXCEPT_SELECT_METHOD,
peerJoinAllExceptSelectMethodName);
}
foreignKey.getChildren().add(foreignFieldElement);