// Mostly copied from RDBMSMappingManager.createDatastoreField
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;
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();
}
DatastoreProperty prop;
ColumnMetaData[] colmds;
if (columnContainer != null
&& columnContainer.getColumnMetaData() != 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());
if (columnContainer != null) {
columnContainer.addColumn(colmd);
colmds = columnContainer.getColumnMetaData();
} else {
colmds = new ColumnMetaData[1];
colmds[0] = colmd;
}
}
// Generate the column identifier
MappedStoreManager storeMgr = datastoreContainer.getStoreManager();
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_FIELD) {
identifier = idFactory.newIdentifier(IdentifierType.COLUMN, fmd.getName());
int i = 0;
while (datastoreContainer.hasDatastoreField(identifier)) {
identifier = idFactory.newIdentifier(IdentifierType.COLUMN, fmd.getName() + "_" + i);
i++;
}
} else 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);
}
colmd.setName(identifier.getIdentifierName());
} else {
// User has specified a name, so try to keep this unmodified
identifier = idFactory.newDatastoreFieldIdentifier(colmds[datastoreFieldIndex].getName(),
storeMgr.getOMFContext().getTypeManager().isDefaultEmbeddedType(fmd.getType()),
FieldRole.ROLE_CUSTOM);
}
// Create the column
prop = (DatastoreProperty) datastoreContainer.addDatastoreField(javaType, identifier, mapping, colmd);
if (fmd.isPrimaryKey()) {
prop.setAsPrimaryKey();
}