*/
public DatastoreField createDatastoreField(JavaTypeMapping mapping, String javaType, int datastoreFieldIndex)
{
AbstractMemberMetaData fmd = mapping.getMemberMetaData();
int roleForField = mapping.getRoleForMember();
DatastoreContainerObject datastoreContainer = mapping.getDatastoreContainer();
// Take the column MetaData from the component that this mappings role relates to
ColumnMetaData colmd = null;
ColumnMetaDataContainer columnContainer = fmd;
if (roleForField == FieldRole.ROLE_COLLECTION_ELEMENT ||
roleForField == FieldRole.ROLE_ARRAY_ELEMENT)
{
columnContainer = fmd.getElementMetaData();
}
else if (roleForField == FieldRole.ROLE_MAP_KEY)
{
columnContainer = fmd.getKeyMetaData();
}
else if (roleForField == FieldRole.ROLE_MAP_VALUE)
{
columnContainer= fmd.getValueMetaData();
}
Column col;
ColumnMetaData[] colmds;
if (columnContainer != null && columnContainer.getColumnMetaData().length > datastoreFieldIndex)
{
colmd = columnContainer.getColumnMetaData()[datastoreFieldIndex];
colmds = columnContainer.getColumnMetaData();
}
else
{
// If column specified add one (use any column name specified on field element)
colmd = new ColumnMetaData();
colmd.setName(fmd.getColumn()); // TODO Avoid use of getColumn() - try getColumnMetaData() but test with spatial too
if (columnContainer != null)
{
columnContainer.addColumn(colmd);
colmds = columnContainer.getColumnMetaData();
}
else
{
colmds = new ColumnMetaData[1];
colmds[0] = colmd;
}
}
// Generate the column identifier
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
DatastoreIdentifier identifier = null;
if (colmd.getName() == null)
{
// No name specified, so generate the identifier from the field name
if (roleForField == FieldRole.ROLE_COLLECTION_ELEMENT)
{
// Join table collection element
identifier = idFactory.newJoinTableFieldIdentifier(fmd, null, null,
true, FieldRole.ROLE_COLLECTION_ELEMENT);
}
else if (roleForField == FieldRole.ROLE_ARRAY_ELEMENT)
{
// Join table array element
identifier = idFactory.newJoinTableFieldIdentifier(fmd, null, null,
true, FieldRole.ROLE_ARRAY_ELEMENT);
}
else if (roleForField == FieldRole.ROLE_MAP_KEY)
{
// Join table map key
identifier = idFactory.newJoinTableFieldIdentifier(fmd, null, null,
true, FieldRole.ROLE_MAP_KEY);
}
else if (roleForField == FieldRole.ROLE_MAP_VALUE)
{
// Join table map value
identifier = idFactory.newJoinTableFieldIdentifier(fmd, null, null,
true, FieldRole.ROLE_MAP_VALUE);
}
else
{
identifier = idFactory.newIdentifier(IdentifierType.COLUMN, fmd.getName());
int i=0;
while (datastoreContainer.hasDatastoreField(identifier))
{
identifier = idFactory.newIdentifier(IdentifierType.COLUMN, fmd.getName() + "_" + i);
i++;
}
}
colmd.setName(identifier.getIdentifierName());
}
else
{
// User has specified a name, so try to keep this unmodified
identifier = idFactory.newDatastoreFieldIdentifier(colmds[datastoreFieldIndex].getName(),
storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(fmd.getType()),
FieldRole.ROLE_CUSTOM);
}
// Create the column
col = (Column) datastoreContainer.addDatastoreField(javaType, identifier, mapping, colmd);
if (fmd.isPrimaryKey())
{
col.setAsPrimaryKey();
}