RdbTable table = rcm.getRdbTable();
//Map the name def of the reference over new local columns
if (nd.isFieldName()) {
PrimitiveElement pe = (PrimitiveElement) jclass
.getTypedElement(nd.getFieldName());
RdbPrimitiveElementMapping pem = (RdbPrimitiveElementMapping) table
.getPrimitiveElementMappingByCol(sf.columns[0].name);
if (pem == null) {
// create it
table.createPrimitiveElementMapping(pe,
sf.columns[0].name, sf.columns[0].sqlType,
false);
} else {
//it already exists
//remove the old hidden field
((Class) pe.getParent()).removeTypedElement(pe
.getName());
//Use the existing one in the name def
pe = (PrimitiveElement) pem.getLinkedMO();
nd.setFieldName(pe.getName());
if (pem.getType() == null) {
pem.setType(sf.columns[0].sqlType);
}
}
} else {
Map tndproj = tnd.getNameRef().getProjection();
Iterator it = nd.getNameRef().getProjection().entrySet()
.iterator();
while (it.hasNext()) {
Map.Entry me = (Map.Entry) it.next();
//find the field in the class used in by this name def
PrimitiveElement pe = (PrimitiveElement) jclass
.getTypedElement((String) me.getValue());
//find the pk column
RdbPrimitiveElementMapping tpem = getPEMOfField(tclass,
(Mapping) rcm.getParent(),
(String) tndproj.get(me.getKey()));
//find the fk column name
SpeedoColumn fkCol = sf.getFKColumn(tpem.getName());
//check if the fk column name is already used
RdbPrimitiveElementMapping pem = (RdbPrimitiveElementMapping) table
.getPrimitiveElementMappingByCol(fkCol.name);
if (pem == null) {
// create it
table.createPrimitiveElementMapping(pe, fkCol.name,
fkCol.sqlType, false);
} else {
//it already exists
//remove the old hidden field
((Class) pe.getParent()).removeTypedElement(pe.getName());
//Use the existing one in the name def
pe = (PrimitiveElement) pem.getLinkedMO();
me.setValue(pe.getName());
if (pem.getType() == null) {
pem.setType(sf.columns[0].sqlType);
}
}
}
}
}