Search in sources :

Example 1 with TypeConverterMapping

use of org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping 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");
}
Also used : SerialisedElementPCMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedElementPCMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NucleusException(org.datanucleus.exceptions.NucleusException) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) TypeConverter(org.datanucleus.store.types.converters.TypeConverter) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) TypeConverterMapping(org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) TypeConverterMultiMapping(org.datanucleus.store.rdbms.mapping.java.TypeConverterMultiMapping)

Example 2 with TypeConverterMapping

use of org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping 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");
}
Also used : TypeConverter(org.datanucleus.store.types.converters.TypeConverter) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) TypeConverterMapping(org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SerialisedKeyPCMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedKeyPCMapping) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NucleusException(org.datanucleus.exceptions.NucleusException) NucleusException(org.datanucleus.exceptions.NucleusException) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) TypeConverterMultiMapping(org.datanucleus.store.rdbms.mapping.java.TypeConverterMultiMapping)

Example 3 with TypeConverterMapping

use of org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping 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");
}
Also used : TypeConverter(org.datanucleus.store.types.converters.TypeConverter) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) TypeConverterMapping(org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping) SerialisedValuePCMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedValuePCMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NucleusException(org.datanucleus.exceptions.NucleusException) NucleusException(org.datanucleus.exceptions.NucleusException) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) TypeConverterMultiMapping(org.datanucleus.store.rdbms.mapping.java.TypeConverterMultiMapping)

Example 4 with TypeConverterMapping

use of org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping 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");
}
Also used : Serializable(java.io.Serializable) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) EmbeddedMetaData(org.datanucleus.metadata.EmbeddedMetaData) MultiColumnConverter(org.datanucleus.store.types.converters.MultiColumnConverter) NucleusException(org.datanucleus.exceptions.NucleusException) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) TypeConverter(org.datanucleus.store.types.converters.TypeConverter) TypeConverterMapping(org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping) TypeManager(org.datanucleus.store.types.TypeManager) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) TypeConverterMultiMapping(org.datanucleus.store.rdbms.mapping.java.TypeConverterMultiMapping)

Example 5 with TypeConverterMapping

use of org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping in project datanucleus-rdbms by datanucleus.

the class AbstractLargeBinaryRDBMSMapping method getObjectForBytes.

protected Object getObjectForBytes(byte[] bytes, int param) {
    String typeName = getJavaTypeMapping().getType();
    if (getJavaTypeMapping() instanceof TypeConverterMapping) {
        // Using TypeConverterMapping so use the datastore type for the converter
        TypeConverter conv = ((TypeConverterMapping) getJavaTypeMapping()).getTypeConverter();
        Class datastoreType = storeMgr.getNucleusContext().getTypeManager().getDatastoreTypeForTypeConverter(conv, getJavaTypeMapping().getJavaType());
        typeName = datastoreType.getName();
    }
    // Use Java serialisation, else byte-streaming, and if not determined then Java serialisation
    if (getJavaTypeMapping().isSerialised()) {
        // Serialised field so just perform basic Java deserialisation for retrieval
        try {
            BlobImpl blob = new BlobImpl(bytes);
            return blob.getObject();
        } catch (SQLException sqle) {
            // Impossible (JDK 1.6 use of free())
            return null;
        }
    } else if (typeName.equals(ClassNameConstants.BOOLEAN_ARRAY)) {
        return TypeConversionHelper.getBooleanArrayFromByteArray(bytes);
    } else if (typeName.equals(ClassNameConstants.BYTE_ARRAY)) {
        return bytes;
    } else if (typeName.equals(ClassNameConstants.CHAR_ARRAY)) {
        return TypeConversionHelper.getCharArrayFromByteArray(bytes);
    } else if (typeName.equals(ClassNameConstants.DOUBLE_ARRAY)) {
        return TypeConversionHelper.getDoubleArrayFromByteArray(bytes);
    } else if (typeName.equals(ClassNameConstants.FLOAT_ARRAY)) {
        return TypeConversionHelper.getFloatArrayFromByteArray(bytes);
    } else if (typeName.equals(ClassNameConstants.INT_ARRAY)) {
        return TypeConversionHelper.getIntArrayFromByteArray(bytes);
    } else if (typeName.equals(ClassNameConstants.LONG_ARRAY)) {
        return TypeConversionHelper.getLongArrayFromByteArray(bytes);
    } else if (typeName.equals(ClassNameConstants.SHORT_ARRAY)) {
        return TypeConversionHelper.getShortArrayFromByteArray(bytes);
    } else if (typeName.equals(ClassNameConstants.JAVA_LANG_BOOLEAN_ARRAY)) {
        return TypeConversionHelper.getBooleanObjectArrayFromByteArray(bytes);
    } else if (typeName.equals(ClassNameConstants.JAVA_LANG_BYTE_ARRAY)) {
        return TypeConversionHelper.getByteObjectArrayFromByteArray(bytes);
    } else if (typeName.equals(ClassNameConstants.JAVA_LANG_CHARACTER_ARRAY)) {
        return TypeConversionHelper.getCharObjectArrayFromByteArray(bytes);
    } else if (typeName.equals(ClassNameConstants.JAVA_LANG_DOUBLE_ARRAY)) {
        return TypeConversionHelper.getDoubleObjectArrayFromByteArray(bytes);
    } else if (typeName.equals(ClassNameConstants.JAVA_LANG_FLOAT_ARRAY)) {
        return TypeConversionHelper.getFloatObjectArrayFromByteArray(bytes);
    } else if (typeName.equals(ClassNameConstants.JAVA_LANG_INTEGER_ARRAY)) {
        return TypeConversionHelper.getIntObjectArrayFromByteArray(bytes);
    } else if (typeName.equals(ClassNameConstants.JAVA_LANG_LONG_ARRAY)) {
        return TypeConversionHelper.getLongObjectArrayFromByteArray(bytes);
    } else if (typeName.equals(ClassNameConstants.JAVA_LANG_SHORT_ARRAY)) {
        return TypeConversionHelper.getShortObjectArrayFromByteArray(bytes);
    } else if (typeName.equals(BigDecimal[].class.getName())) {
        return TypeConversionHelper.getBigDecimalArrayFromByteArray(bytes);
    } else if (typeName.equals(BigInteger[].class.getName())) {
        return TypeConversionHelper.getBigIntegerArrayFromByteArray(bytes);
    } else if (getJavaTypeMapping().getJavaType() != null && getJavaTypeMapping().getJavaType().getName().equals("java.util.BitSet")) {
        return TypeConversionHelper.getBitSetFromBooleanArray(TypeConversionHelper.getBooleanArrayFromByteArray(bytes));
    } else if (getJavaTypeMapping().getJavaType() != null && getJavaTypeMapping().getJavaType().getName().equals("java.awt.image.BufferedImage")) {
        try {
            return ImageIO.read(new ByteArrayInputStream(bytes));
        } catch (IOException e) {
            throw new NucleusDataStoreException(Localiser.msg("055002", "Object", "" + param, column, e.getMessage()), e);
        }
    } else {
        // Fallback to just perform basic Java deserialisation for retrieval
        try {
            BlobImpl blob = new BlobImpl(bytes);
            return blob.getObject();
        } catch (SQLException sqle) {
            // Impossible (JDK 1.6 use of free())
            return null;
        }
    }
}
Also used : TypeConverter(org.datanucleus.store.types.converters.TypeConverter) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) TypeConverterMapping(org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal)

Aggregations

TypeConverterMapping (org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping)8 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)7 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)7 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)5 TypeConverter (org.datanucleus.store.types.converters.TypeConverter)5 NucleusException (org.datanucleus.exceptions.NucleusException)4 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)4 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)4 NoTableManagedException (org.datanucleus.store.rdbms.exceptions.NoTableManagedException)4 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)4 TypeConverterMultiMapping (org.datanucleus.store.rdbms.mapping.java.TypeConverterMultiMapping)4 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 PersistenceNucleusContext (org.datanucleus.PersistenceNucleusContext)2 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)2 PersonWithConverters (org.datanucleus.samples.converters.PersonWithConverters)2 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 BigDecimal (java.math.BigDecimal)1