throw new NucleusUserException(LOCALISER.msg("056052", valueClass.getName(), key_field_name));
}
}
if (vkfmd == null)
{
throw new ClassDefinitionException(LOCALISER.msg("056050", fmd.getFullFieldName()));
}
// Check that the key type is correct for the declared type
if (!ClassUtils.typesAreCompatible(vkfmd.getType(), keyType, clr))
{
throw new NucleusUserException(LOCALISER.msg("056051",
fmd.getFullFieldName(), keyType, vkfmd.getType().getName()));
}
// Set up key field
String keyFieldName = vkfmd.getName();
keyFieldNumber = vmd.getAbsolutePositionOfMember(keyFieldName);
keyMapping = valueTable.getMemberMapping(vmd.getMetaDataForManagedMemberAtAbsolutePosition(keyFieldNumber));
if (keyMapping == null)
{
throw new NucleusUserException(LOCALISER.msg("056053",
fmd.getAbstractClassMetaData().getFullClassName(), fmd.getName(), valueType, keyFieldName));
}
if (!keyMapping.hasSimpleDatastoreRepresentation())
{
// Check the type of the mapping
throw new NucleusUserException("Invalid field type for map key field: " + fmd.getClassName() + "." + fmd.getName());
}
keysAreEmbedded = isEmbeddedMapping(keyMapping);
keysAreSerialised = isEmbeddedMapping(keyMapping);
mapTable = valueTable;
if (fmd.getMappedBy() != null && ownerMapping.getDatastoreContainer() != mapTable)
{
// Value and owner don't have consistent tables so use the one with the mapping
// e.g map value is subclass, yet superclass has the link back to the owner
mapTable = ownerMapping.getDatastoreContainer();
}
}
else
{
// Key = PC, Value = field in key
kmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(keyClass, clr);
if (kmd == null)
{
// Key has no MetaData!
throw new NucleusUserException(LOCALISER.msg("056069", keyType, fmd.getFullFieldName()));
}
// TODO This should be called keyvalueTable or something and not valueTable
valueTable = storeMgr.getDatastoreClass(keyType, clr);
keyMapping = storeMgr.getDatastoreClass(keyType, clr).getIdMapping();
keysAreEmbedded = false;
keysAreSerialised = false;
if (fmd.getMappedBy() != null)
{
// 1-N bidirectional : The key class has a field for the owner.
AbstractMemberMetaData kofmd = kmd.getMetaDataForMember(ownerFieldName);
if (kofmd == null)
{
throw new NucleusUserException(LOCALISER.msg("056067", fmd.getFullFieldName(),
ownerFieldName, keyClass.getName()));
}
// Check that the type of the key "mapped-by" field is consistent with the owner type
if (!ClassUtils.typesAreCompatible(kofmd.getType(), fmd.getAbstractClassMetaData().getFullClassName(), clr))
{
throw new NucleusUserException(LOCALISER.msg("056068", fmd.getFullFieldName(),
kofmd.getFullFieldName(), kofmd.getTypeName(), fmd.getAbstractClassMetaData().getFullClassName()));
}
ownerFieldNumber = kmd.getAbsolutePositionOfMember(ownerFieldName);
ownerMapping = valueTable.getMemberMapping(kofmd);
if (ownerMapping == null)
{
throw new NucleusUserException(LOCALISER.msg("RDBMS.SCO.Map.InverseOwnerMappedByFieldNotPresent",
fmd.getAbstractClassMetaData().getFullClassName(), fmd.getName(), keyType, ownerFieldName));
}
if (isEmbeddedMapping(ownerMapping))
{
throw new NucleusUserException(LOCALISER.msg("056055",
ownerFieldName, keyType, kofmd.getTypeName(), fmd.getClassName()));
}
}
else
{
// 1-N Unidirectional : The key class knows nothing about the owner
ownerFieldNumber = -1;
ownerMapping = valueTable.getExternalMapping(fmd, MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
if (ownerMapping == null)
{
throw new NucleusUserException(LOCALISER.msg("056056",
fmd.getAbstractClassMetaData().getFullClassName(), fmd.getName(), keyType));
}
}
AbstractMemberMetaData vkfmd = null;
if (fmd.getValueMetaData() == null || fmd.getValueMetaData().getMappedBy() == null)
{
if (vkfmd == null)
{
throw new NucleusUserException(LOCALISER.msg("056057", keyClass.getName()));
}
}
String value_field_name = fmd.getValueMetaData().getMappedBy();
if (value_field_name != null)
{
// check if value field exists in the ClassMetaData for the element-value type
vkfmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForMember(keyClass, clr, value_field_name);
if (vkfmd == null)
{
throw new NucleusUserException(LOCALISER.msg("056059", keyClass.getName(), value_field_name));
}
}
if (vkfmd == null)
{
throw new ClassDefinitionException(LOCALISER.msg("056057", fmd.getFullFieldName()));
}
// Check that the value type is consistent with the declared type
if (!ClassUtils.typesAreCompatible(vkfmd.getType(), valueType, clr))
{