private JavaTypeMapping addOrderColumn(AbstractMemberMetaData fmd) {
Class indexType = Integer.class;
JavaTypeMapping indexMapping = new IndexMapping();
indexMapping.initialize(storeMgr, indexType.getName());
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
DatastoreIdentifier indexColumnName = null;
ColumnMetaData colmd = null;
// Allow for any user definition in OrderMetaData
OrderMetaData omd = fmd.getOrderMetaData();
if (omd != null) {
colmd =
(omd.getColumnMetaData() != null && omd.getColumnMetaData().length > 0 ? omd.getColumnMetaData()[0] : null);
if (omd.getMappedBy() != null) {
// User has defined ordering using the column(s) of an existing field.
JavaTypeMapping orderMapping = getMemberMapping(omd.getMappedBy());
if (orderMapping == null) {
throw new NucleusUserException(String.format(
"Field \"{0}\" has an <order> defined to be persisted into the columns in the element table for element field \"{1}\". This field is not found in the element class.",
fmd.getFullFieldName(), omd.getMappedBy()));
}
if (!(orderMapping instanceof IntegerMapping) && !(orderMapping instanceof LongMapping)) {
throw new NucleusUserException(
String.format(
"Field \"{0}\" has an <order> defined to be persisted into the column of field \"{1}\". This field is of an invalid type. Must be an int/Integer.",
fmd.getFullFieldName(), omd.getMappedBy()));
}
return orderMapping;
}
String colName;
if (omd.getColumnMetaData() != null && omd.getColumnMetaData().length > 0
&& omd.getColumnMetaData()[0].getName() != null) {
// User-defined name so create an identifier using it
colName = omd.getColumnMetaData()[0].getName();
indexColumnName = idFactory.newDatastoreFieldIdentifier(colName);
}
}
if (indexColumnName == null) {
// No index column name defined so generate one
indexColumnName = idFactory.newForeignKeyFieldIdentifier(fmd, null, null, true, FieldRole.ROLE_INDEX);
}
// if the relationship is in a base class with multiple subclasses, each
// subclass will try to add the index column. We need to avoid adding
// the same column twice.
DatastoreField column = datastorePropertiesByName.get(indexColumnName.getIdentifierName());
if (column == null) {
column = addDatastoreField(indexType.getName(), indexColumnName, indexMapping, colmd);
}
if (colmd == null || (colmd.getAllowsNull() == null) ||
(colmd.getAllowsNull() != null && colmd.isAllowsNull())) {
// User either wants it nullable, or havent specified anything, so make it nullable
column.setNullable();
}
DatastoreFKMapping fkMapping =
(DatastoreFKMapping) storeMgr.getMappingManager().createDatastoreMapping(
indexMapping, column, indexType.getName());
DatastoreProperty field = fkMapping.getDatastoreField();
DatastoreTable elementTable = field.getDatastoreContainerObject();
PropertyMetaData pmd = new PropertyMetaData(elementTable.getClassMetaData(), indexColumnName.getIdentifierName());
field.setMemberMetaData(pmd);
return indexMapping;
}