use of org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping in project datanucleus-rdbms by datanucleus.
the class MappingManagerImpl method getMapping.
/**
* Accessor for the mapping for the specified class.
* This simply creates a JavaTypeMapping for the java type and returns it.
* The mapping has no underlying datastore mapping(s) and no associated field/table.
* @param javaType Java type
* @param serialised Whether the type is serialised
* @param embedded Whether the type is embedded
* @param fieldName Name of the field (for logging)
* @return The mapping for the class.
*/
public JavaTypeMapping getMapping(Class javaType, boolean serialised, boolean embedded, String fieldName) {
// TODO Pass in 4th arg?
MappingConverterDetails mcd = getMappingClass(javaType, serialised, embedded, null, fieldName);
Class mc = mcd.mappingClass;
// Allow override by Oracle
mc = getOverrideMappingClass(mc, null, null);
try {
JavaTypeMapping m = (JavaTypeMapping) mc.newInstance();
m.initialize(storeMgr, javaType.getName());
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc.getName(), e), e).setFatal();
}
}
use of org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping in project datanucleus-rdbms by datanucleus.
the class MappingManagerImpl method getElementMapping.
/**
* Convenience accessor for the element mapping for the element of a collection/array of elements.
* @param table The table
* @param mmd MetaData for the collection member containing the collection/array of PCs
* @param fieldRole role of this mapping for this member
* @param clr ClassLoader resolver
* @return The mapping
*/
protected JavaTypeMapping getElementMapping(Table table, AbstractMemberMetaData mmd, FieldRole fieldRole, ClassLoaderResolver clr) {
if (!mmd.hasCollection() && !mmd.hasArray()) {
// TODO Localise this message
throw new NucleusException("Attempt to get element mapping for field " + mmd.getFullFieldName() + " that has no collection/array!").setFatal();
}
if (mmd.getJoinMetaData() == null) {
AbstractMemberMetaData[] refMmds = mmd.getRelatedMemberMetaData(clr);
if (refMmds == null || refMmds.length == 0) {
// TODO Localise this
throw new NucleusException("Attempt to get element mapping for field " + mmd.getFullFieldName() + " that has no join table defined for the collection/array").setFatal();
}
if (refMmds[0].getJoinMetaData() == null) {
// TODO Localise this
throw new NucleusException("Attempt to get element mapping for field " + mmd.getFullFieldName() + " that has no join table defined for the collection/array").setFatal();
}
}
MappingConverterDetails mcd = null;
Class mc = null;
String userMappingClassName = null;
String userTypeConverterName = null;
if (mmd.getElementMetaData() != null) {
userTypeConverterName = mmd.getElementMetaData().getValueForExtension(MetaData.EXTENSION_MEMBER_TYPE_CONVERTER_NAME);
userMappingClassName = mmd.getElementMetaData().getValueForExtension("mapping-class");
}
if (userTypeConverterName != null) {
TypeConverter conv = storeMgr.getNucleusContext().getTypeManager().getTypeConverterForName(userTypeConverterName);
if (conv == null) {
throw new NucleusUserException("Field " + mmd.getFullFieldName() + " ELEMENT has been specified to use type converter " + userTypeConverterName + " but not found!");
}
// TODO Could be TypeConverterMultiMapping?
mcd = new MappingConverterDetails(TypeConverterMapping.class, conv);
} else if (userMappingClassName != null) {
// User has defined their own mapping class for this element so use that
try {
mc = clr.classForName(userMappingClassName);
} catch (NucleusException jpe) {
throw new NucleusUserException(Localiser.msg("041014", userMappingClassName)).setFatal();
}
} else {
boolean serialised = ((mmd.hasCollection() && mmd.getCollection().isSerializedElement()) || (mmd.hasArray() && mmd.getArray().isSerializedElement()));
boolean embeddedPC = (mmd.hasCollection() && mmd.getCollection().elementIsPersistent() && mmd.getCollection().isEmbeddedElement()) || (mmd.getElementMetaData() != null && mmd.getElementMetaData().getEmbeddedMetaData() != null);
boolean elementPC = ((mmd.hasCollection() && mmd.getCollection().elementIsPersistent()) || (mmd.hasArray() && mmd.getArray().elementIsPersistent()));
boolean embedded = true;
if (mmd.hasCollection()) {
embedded = mmd.getCollection().isEmbeddedElement();
} else if (mmd.hasArray()) {
embedded = mmd.getArray().isEmbeddedElement();
}
Class elementCls = null;
if (mmd.hasCollection()) {
elementCls = clr.classForName(mmd.getCollection().getElementType());
} else if (mmd.hasArray()) {
// Use declared element type rather than any restricted type specified in metadata
elementCls = mmd.getType().getComponentType();
}
boolean elementReference = ClassUtils.isReferenceType(elementCls);
if (serialised) {
if (elementPC) {
// Serialised PC element
mc = SerialisedElementPCMapping.class;
} else if (elementReference) {
// Serialised Reference element
mc = SerialisedReferenceMapping.class;
} else {
// Serialised Non-PC element
mc = SerialisedMapping.class;
}
} else if (embedded) {
if (embeddedPC) {
// Embedded PC type
mc = EmbeddedElementPCMapping.class;
} else if (elementPC) {
// "Embedded" PC type but no <embedded> so dont embed for now. Is this correct?
mc = PersistableMapping.class;
} else {
// Embedded Non-PC type
mcd = getMappingClass(elementCls, serialised, embedded, mmd.getElementMetaData() != null ? mmd.getElementMetaData().getColumnMetaData() : null, mmd.getFullFieldName());
}
} else {
// Normal element mapping
mcd = getMappingClass(elementCls, serialised, embedded, mmd.getElementMetaData() != null ? mmd.getElementMetaData().getColumnMetaData() : null, mmd.getFullFieldName());
}
}
if (mcd != null && mcd.typeConverter == null) {
mc = mcd.mappingClass;
}
if (mc != null && (mcd == null || mcd.typeConverter == null)) {
// Create the mapping of the selected type
JavaTypeMapping m = null;
try {
m = (JavaTypeMapping) mc.newInstance();
m.setRoleForMember(fieldRole);
m.initialize(mmd, table, clr);
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc.getName(), e), e).setFatal();
}
} else if (mcd != null && mcd.typeConverter != null) {
try {
JavaTypeMapping m = (JavaTypeMapping) mcd.mappingClass.newInstance();
m.setRoleForMember(fieldRole);
if (m instanceof TypeConverterMapping) {
((TypeConverterMapping) m).initialize(mmd, table, clr, mcd.typeConverter);
} else if (m instanceof TypeConverterMultiMapping) {
((TypeConverterMultiMapping) m).initialize(mmd, table, clr, mcd.typeConverter);
}
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc != null ? mc.getName() : null, e), e).setFatal();
}
}
throw new NucleusException("Unable to create mapping for element of collection/array at " + mmd.getFullFieldName() + " - no available mapping");
}
use of org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping in project datanucleus-rdbms by datanucleus.
the class MappingManagerImpl method getKeyMapping.
/**
* Convenience accessor for the mapping of the key of a map.
* @param table The container
* @param mmd MetaData for the field containing the map that this key is for
* @param clr ClassLoader resolver
* @return The mapping
*/
protected JavaTypeMapping getKeyMapping(Table table, AbstractMemberMetaData mmd, ClassLoaderResolver clr) {
if (mmd.getMap() == null) {
// TODO Localise this
throw new NucleusException("Attempt to get key mapping for field " + mmd.getFullFieldName() + " that has no map!").setFatal();
}
MappingConverterDetails mcd = null;
Class mc = null;
String userTypeConverterName = null;
String userMappingClassName = null;
if (mmd.getKeyMetaData() != null) {
userTypeConverterName = mmd.getKeyMetaData().getValueForExtension(MetaData.EXTENSION_MEMBER_TYPE_CONVERTER_NAME);
userMappingClassName = mmd.getKeyMetaData().getValueForExtension("mapping-class");
}
if (userTypeConverterName != null) {
TypeConverter conv = storeMgr.getNucleusContext().getTypeManager().getTypeConverterForName(userTypeConverterName);
if (conv == null) {
throw new NucleusUserException("Field " + mmd.getFullFieldName() + " KEY has been specified to use type converter " + userTypeConverterName + " but not found!");
}
// TODO Could be TypeConverterMultiMapping?
mcd = new MappingConverterDetails(TypeConverterMapping.class, conv);
} else if (userMappingClassName != null) {
// User has defined their own mapping class for this key so use that
try {
mc = clr.classForName(userMappingClassName);
} catch (NucleusException jpe) {
throw new NucleusUserException(Localiser.msg("041014", userMappingClassName)).setFatal();
}
} else {
boolean serialised = (mmd.hasMap() && mmd.getMap().isSerializedKey());
boolean embedded = (mmd.hasMap() && mmd.getMap().isEmbeddedKey());
boolean embeddedPC = (mmd.hasMap() && mmd.getMap().keyIsPersistent() && mmd.getMap().isEmbeddedKey()) || (mmd.getKeyMetaData() != null && mmd.getKeyMetaData().getEmbeddedMetaData() != null);
boolean keyPC = (mmd.hasMap() && mmd.getMap().keyIsPersistent());
Class keyCls = clr.classForName(mmd.getMap().getKeyType());
boolean keyReference = ClassUtils.isReferenceType(keyCls);
if (serialised) {
if (keyPC) {
// Serialised PC key
mc = SerialisedKeyPCMapping.class;
} else if (keyReference) {
// Serialised Reference key
mc = SerialisedReferenceMapping.class;
} else {
// Serialised Non-PC element
mc = SerialisedMapping.class;
}
} else if (embedded) {
if (embeddedPC) {
// Embedded PC key
mc = EmbeddedKeyPCMapping.class;
} else if (keyPC) {
// "Embedded" PC type but no <embedded> so dont embed for now. Is this correct?
mc = PersistableMapping.class;
} else {
// Embedded Non-PC type
mcd = getMappingClass(keyCls, serialised, embedded, mmd.getKeyMetaData() != null ? mmd.getKeyMetaData().getColumnMetaData() : null, mmd.getFullFieldName());
}
} else {
// Normal key mapping
mcd = getMappingClass(keyCls, serialised, embedded, mmd.getKeyMetaData() != null ? mmd.getKeyMetaData().getColumnMetaData() : null, mmd.getFullFieldName());
}
}
if (mcd != null && mcd.typeConverter == null) {
mc = mcd.mappingClass;
}
if (mc != null && (mcd == null || mcd.typeConverter == null)) {
// Create the mapping of the selected type
JavaTypeMapping m = null;
try {
m = (JavaTypeMapping) mc.newInstance();
m.setRoleForMember(FieldRole.ROLE_MAP_KEY);
m.initialize(mmd, table, clr);
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc.getName(), e), e).setFatal();
}
} else if (mcd != null && mcd.typeConverter != null) {
try {
JavaTypeMapping m = (JavaTypeMapping) mcd.mappingClass.newInstance();
m.setRoleForMember(FieldRole.ROLE_MAP_KEY);
if (m instanceof TypeConverterMapping) {
((TypeConverterMapping) m).initialize(mmd, table, clr, mcd.typeConverter);
} else if (m instanceof TypeConverterMultiMapping) {
((TypeConverterMultiMapping) m).initialize(mmd, table, clr, mcd.typeConverter);
}
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc != null ? mc.getName() : null, e), e).setFatal();
}
}
throw new NucleusException("Unable to create mapping for key of map at " + mmd.getFullFieldName() + " - no available mapping");
}
use of org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping in project datanucleus-rdbms by datanucleus.
the class MappingManagerImpl method getValueMapping.
/**
* Convenience accessor for the mapping of the value for a map.
* @param table The container
* @param mmd MetaData for the field/property containing the map that this value is for
* @param clr ClassLoader resolver
* @return The mapping
*/
protected JavaTypeMapping getValueMapping(Table table, AbstractMemberMetaData mmd, ClassLoaderResolver clr) {
if (mmd.getMap() == null) {
// TODO Localise this
throw new NucleusException("Attempt to get value mapping for field " + mmd.getFullFieldName() + " that has no map!").setFatal();
}
MappingConverterDetails mcd = null;
Class mc = null;
String userTypeConverterName = null;
String userMappingClassName = null;
if (mmd.getValueMetaData() != null) {
userTypeConverterName = mmd.getValueMetaData().getValueForExtension(MetaData.EXTENSION_MEMBER_TYPE_CONVERTER_NAME);
userMappingClassName = mmd.getValueMetaData().getValueForExtension("mapping-class");
}
if (userTypeConverterName != null) {
TypeConverter conv = storeMgr.getNucleusContext().getTypeManager().getTypeConverterForName(userTypeConverterName);
if (conv == null) {
throw new NucleusUserException("Field " + mmd.getFullFieldName() + " VALUE has been specified to use type converter " + userTypeConverterName + " but not found!");
}
// TODO Could be TypeConverterMultiMapping?
mcd = new MappingConverterDetails(TypeConverterMapping.class, conv);
} else if (userMappingClassName != null) {
// User has defined their own mapping class for this value so use that
try {
mc = clr.classForName(userMappingClassName);
} catch (NucleusException jpe) {
throw new NucleusUserException(Localiser.msg("041014", userMappingClassName)).setFatal();
}
} else {
boolean serialised = (mmd.hasMap() && mmd.getMap().isSerializedValue());
boolean embedded = (mmd.hasMap() && mmd.getMap().isEmbeddedValue());
boolean embeddedPC = (mmd.hasMap() && mmd.getMap().valueIsPersistent() && mmd.getMap().isEmbeddedValue()) || (mmd.getValueMetaData() != null && mmd.getValueMetaData().getEmbeddedMetaData() != null);
boolean valuePC = (mmd.hasMap() && mmd.getMap().valueIsPersistent());
Class valueCls = clr.classForName(mmd.getMap().getValueType());
boolean valueReference = ClassUtils.isReferenceType(valueCls);
if (serialised) {
if (valuePC) {
// Serialised PC value
mc = SerialisedValuePCMapping.class;
} else if (valueReference) {
// Serialised Reference value
mc = SerialisedReferenceMapping.class;
} else {
// Serialised Non-PC element
mc = SerialisedMapping.class;
}
} else if (embedded) {
if (embeddedPC) {
// Embedded PC key
mc = EmbeddedValuePCMapping.class;
} else if (valuePC) {
// "Embedded" PC type but no <embedded> so dont embed for now. Is this correct?
mc = PersistableMapping.class;
} else {
// Embedded Non-PC type
mcd = getMappingClass(valueCls, serialised, embedded, mmd.getValueMetaData() != null ? mmd.getValueMetaData().getColumnMetaData() : null, mmd.getFullFieldName());
}
} else {
// Normal value mapping
mcd = getMappingClass(valueCls, serialised, embedded, mmd.getValueMetaData() != null ? mmd.getValueMetaData().getColumnMetaData() : null, mmd.getFullFieldName());
}
}
if (mcd != null && mcd.typeConverter == null) {
mc = mcd.mappingClass;
}
if (mc != null && (mcd == null || mcd.typeConverter == null)) {
// Create the mapping of the selected type
JavaTypeMapping m = null;
try {
m = (JavaTypeMapping) mc.newInstance();
m.setRoleForMember(FieldRole.ROLE_MAP_VALUE);
m.initialize(mmd, table, clr);
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc.getName(), e), e).setFatal();
}
} else if (mcd != null && mcd.typeConverter != null) {
try {
JavaTypeMapping m = (JavaTypeMapping) mcd.mappingClass.newInstance();
m.setRoleForMember(FieldRole.ROLE_MAP_VALUE);
if (m instanceof TypeConverterMapping) {
((TypeConverterMapping) m).initialize(mmd, table, clr, mcd.typeConverter);
} else if (m instanceof TypeConverterMultiMapping) {
((TypeConverterMultiMapping) m).initialize(mmd, table, clr, mcd.typeConverter);
}
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc != null ? mc.getName() : null, e), e).setFatal();
}
}
throw new NucleusException("Unable to create mapping for value of map at " + mmd.getFullFieldName() + " - no available mapping");
}
use of org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping in project datanucleus-rdbms by datanucleus.
the class MappingManagerImpl method getMapping.
/**
* Accessor for the mapping for the member of the specified table.
* Can be used for members of a class, element of a collection of a class, element of an array of a class,
* keys of a map of a class, values of a map of a class; this is controlled by the role argument.
* @param table Table to add the mapping to
* @param mmd MetaData for the member to map
* @param clr The ClassLoaderResolver
* @param fieldRole Role that this mapping plays for the field
* @return The mapping for the member.
*/
public JavaTypeMapping getMapping(Table table, AbstractMemberMetaData mmd, ClassLoaderResolver clr, FieldRole fieldRole) {
if (fieldRole == FieldRole.ROLE_COLLECTION_ELEMENT || fieldRole == FieldRole.ROLE_ARRAY_ELEMENT) {
// Mapping a collection/array element (in a join table)
return getElementMapping(table, mmd, fieldRole, clr);
} else if (fieldRole == FieldRole.ROLE_MAP_KEY) {
// Mapping a map key (in a join table)
return getKeyMapping(table, mmd, clr);
} else if (fieldRole == FieldRole.ROLE_MAP_VALUE) {
// Mapping a map value (in a join table)
return getValueMapping(table, mmd, clr);
}
// Check for use of TypeConverter (either specific, or auto-apply for this type)
TypeManager typeMgr = table.getStoreManager().getNucleusContext().getTypeManager();
TypeConverter conv = null;
if (!mmd.isTypeConversionDisabled()) {
// User-specified TypeConverter defined for the whole member, or an autoApply is present for this member type
if (mmd.getTypeConverterName() != null) {
conv = typeMgr.getTypeConverterForName(mmd.getTypeConverterName());
if (conv == null) {
throw new NucleusUserException(Localiser.msg("044062", mmd.getFullFieldName(), mmd.getTypeConverterName()));
}
} else {
TypeConverter autoApplyConv = typeMgr.getAutoApplyTypeConverterForType(mmd.getType());
if (autoApplyConv != null) {
conv = autoApplyConv;
}
}
if (conv != null) {
// Create the mapping of the selected type
JavaTypeMapping m = null;
if (conv instanceof MultiColumnConverter) {
Class mc = TypeConverterMultiMapping.class;
try {
m = (JavaTypeMapping) mc.newInstance();
m.setRoleForMember(FieldRole.ROLE_FIELD);
((TypeConverterMultiMapping) m).initialize(mmd, table, clr, conv);
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc.getName(), e), e).setFatal();
}
}
Class mc = TypeConverterMapping.class;
try {
m = (JavaTypeMapping) mc.newInstance();
m.setRoleForMember(FieldRole.ROLE_FIELD);
((TypeConverterMapping) m).initialize(mmd, table, clr, conv);
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc.getName(), e), e).setFatal();
}
}
}
AbstractMemberMetaData overrideMmd = null;
MappingConverterDetails mcd = null;
Class mc = null;
String userMappingClassName = mmd.getValueForExtension("mapping-class");
if (userMappingClassName != null) {
// User has defined their own mapping class for this field so use that
try {
mc = clr.classForName(userMappingClassName);
} catch (NucleusException ne) {
throw new NucleusUserException(Localiser.msg("041014", mmd.getFullFieldName(), userMappingClassName)).setFatal();
}
} else {
AbstractClassMetaData typeCmd = null;
if (mmd.getType().isInterface()) {
typeCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForInterface(mmd.getType(), clr);
} else {
typeCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getType(), clr);
}
if (mmd.hasExtension(SerialisedLocalFileMapping.EXTENSION_SERIALIZE_TO_FOLDER) && Serializable.class.isAssignableFrom(mmd.getType())) {
// Special case : use file serialization mapping
mc = SerialisedLocalFileMapping.class;
} else if (mmd.isSerialized()) {
// Field is marked as serialised then we have no other option - serialise it
mcd = getMappingClass(mmd.getType(), true, false, null, mmd.getFullFieldName());
} else if (mmd.getEmbeddedMetaData() != null) {
// Field has an <embedded> specification so use that
mcd = getMappingClass(mmd.getType(), false, true, null, mmd.getFullFieldName());
} else if (typeCmd != null && typeCmd.isEmbeddedOnly()) {
// Reference type is declared with embedded only
mcd = getMappingClass(mmd.getType(), false, true, null, mmd.getFullFieldName());
} else if (// TODO Check this since it will push all basic nonPC fields through here
mmd.isEmbedded()) {
// Otherwise, if the field is embedded then we request that it be serialised into the owner table
// This is particularly for java.lang.Object which should be "embedded" by default, and hence serialised
mcd = getMappingClass(mmd.getType(), true, false, mmd.getColumnMetaData(), mmd.getFullFieldName());
} else {
// Non-embedded/non-serialised - Just get the basic mapping for the type
Class memberType = mmd.getType();
mcd = getMappingClass(memberType, false, false, mmd.getColumnMetaData(), mmd.getFullFieldName());
if (mmd.getParent() instanceof EmbeddedMetaData && mmd.getRelationType(clr) != RelationType.NONE) {
// See NUCCORE-697 - always need to use the real member metadata for the mapping
// so that it can find sub-fields when persisting/querying etc
AbstractClassMetaData cmdForFmd = table.getStoreManager().getMetaDataManager().getMetaDataForClass(mmd.getClassName(), clr);
overrideMmd = cmdForFmd.getMetaDataForMember(mmd.getName());
}
}
}
// Create the mapping of the selected type
if (mcd != null) {
mc = mcd.mappingClass;
// Allow override by Oracle
mc = getOverrideMappingClass(mc, mmd, fieldRole);
}
if (mc != null && (mcd == null || mcd.typeConverter == null)) {
try {
JavaTypeMapping m = (JavaTypeMapping) mc.newInstance();
m.setRoleForMember(FieldRole.ROLE_FIELD);
m.initialize(mmd, table, clr);
if (overrideMmd != null) {
// Note cannot just use this overrideMmd in the initialize(...) call above, a test fails.
m.setMemberMetaData(overrideMmd);
}
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc.getName(), e), e).setFatal();
}
} else if (mcd != null && mcd.typeConverter != null) {
try {
JavaTypeMapping m = (JavaTypeMapping) mcd.mappingClass.newInstance();
m.setRoleForMember(FieldRole.ROLE_FIELD);
if (m instanceof TypeConverterMapping) {
((TypeConverterMapping) m).initialize(mmd, table, clr, mcd.typeConverter);
} else if (m instanceof TypeConverterMultiMapping) {
((TypeConverterMultiMapping) m).initialize(mmd, table, clr, mcd.typeConverter);
}
if (overrideMmd != null) {
// Note cannot just use this overrideMmd in the initialize(...) call above, a test fails.
m.setMemberMetaData(overrideMmd);
}
return m;
} catch (Exception e) {
throw new NucleusException(Localiser.msg("041009", mc.getName(), e), e).setFatal();
}
}
throw new NucleusException("Unable to create mapping for member at " + mmd.getFullFieldName() + " - no available mapping");
}
Aggregations