Search in sources :

Example 6 with TypeConverter

use of org.datanucleus.store.types.converters.TypeConverter in project datanucleus-rdbms by datanucleus.

the class MappingManagerImpl method getDefaultJavaTypeMapping.

/**
 * Method to return the default java type mapping class for a specified java type.
 * @param javaType java type
 * @param colmds Metadata for the column(s) (optional)
 * @return The mapping class to use (by default)
 */
protected MappingConverterDetails getDefaultJavaTypeMapping(Class javaType, ColumnMetaData[] colmds) {
    // Check for an explicit mapping
    Class cls = storeMgr.getMappingManager().getMappingType(javaType.getName());
    if (cls == null) {
        // No explicit mapping for this java type, so fall back to TypeConverter if available
        TypeManager typeMgr = storeMgr.getNucleusContext().getTypeManager();
        if (colmds != null && colmds.length > 0) {
            if (colmds.length > 1) {
                // Find TypeConverter with right number of columns
                Collection<TypeConverter> converters = typeMgr.getTypeConvertersForType(javaType);
                if (converters != null && !converters.isEmpty()) {
                    for (TypeConverter conv : converters) {
                        if (conv instanceof MultiColumnConverter) {
                            if (((MultiColumnConverter) conv).getDatastoreColumnTypes().length == colmds.length) {
                                return new MappingConverterDetails(TypeConverterMultiMapping.class, conv);
                            }
                        }
                    }
                }
            } else {
                JdbcType jdbcType = colmds[0].getJdbcType();
                if (jdbcType != null) {
                    // JDBC type specified so don't just take the default
                    TypeConverter conv = null;
                    if (MetaDataUtils.isJdbcTypeString(jdbcType)) {
                        conv = typeMgr.getTypeConverterForType(javaType, String.class);
                    } else if (MetaDataUtils.isJdbcTypeNumeric(jdbcType)) {
                        conv = typeMgr.getTypeConverterForType(javaType, Long.class);
                    } else if (jdbcType == JdbcType.TIMESTAMP) {
                        conv = typeMgr.getTypeConverterForType(javaType, Timestamp.class);
                    } else if (jdbcType == JdbcType.TIME) {
                        conv = typeMgr.getTypeConverterForType(javaType, Time.class);
                    } else if (jdbcType == JdbcType.DATE) {
                        conv = typeMgr.getTypeConverterForType(javaType, Date.class);
                    }
                    if (conv != null) {
                        return new MappingConverterDetails(TypeConverterMapping.class, conv);
                    }
                }
            }
        }
        TypeConverter conv = typeMgr.getDefaultTypeConverterForType(javaType);
        if (conv != null) {
            if (conv instanceof MultiColumnConverter) {
                return new MappingConverterDetails(TypeConverterMultiMapping.class, conv);
            }
            return new MappingConverterDetails(TypeConverterMapping.class, conv);
        }
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("041000", javaType.getName()));
        return null;
    }
    return new MappingConverterDetails(cls);
}
Also used : TypeConverter(org.datanucleus.store.types.converters.TypeConverter) TypeManager(org.datanucleus.store.types.TypeManager) JdbcType(org.datanucleus.metadata.JdbcType) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) MultiColumnConverter(org.datanucleus.store.types.converters.MultiColumnConverter) Timestamp(java.sql.Timestamp) Date(java.sql.Date)

Example 7 with TypeConverter

use of org.datanucleus.store.types.converters.TypeConverter 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)

Example 8 with TypeConverter

use of org.datanucleus.store.types.converters.TypeConverter in project datanucleus-core by datanucleus.

the class TypeManagerImpl method loadTypeConverters.

/**
 * Method to load the java type that are currently registered in the PluginManager.
 * @param mgr the PluginManager
 * @param clr the ClassLoaderResolver
 */
private void loadTypeConverters(PluginManager mgr) {
    if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("016007"));
    }
    // Load up built-in converters
    registerConverter("dn.boolean-yn", new org.datanucleus.store.types.converters.BooleanYNConverter(), Boolean.class, Character.class, false, null);
    registerConverter("dn.boolean-integer", new org.datanucleus.store.types.converters.BooleanIntegerConverter(), Boolean.class, Integer.class, false, null);
    registerConverter("dn.character-string", new org.datanucleus.store.types.converters.CharacterStringConverter(), Character.class, String.class, false, null);
    registerConverter("dn.bigdecimal-string", new org.datanucleus.store.types.converters.BigDecimalStringConverter(), BigDecimal.class, String.class, false, null);
    registerConverter("dn.bigdecimal-double", new org.datanucleus.store.types.converters.BigDecimalDoubleConverter(), BigDecimal.class, Double.class, false, null);
    registerConverter("dn.biginteger-string", new org.datanucleus.store.types.converters.BigIntegerStringConverter(), BigInteger.class, String.class, false, null);
    registerConverter("dn.biginteger-long", new org.datanucleus.store.types.converters.BigIntegerLongConverter(), BigInteger.class, Long.class, false, null);
    registerConverter("dn.bitset-string", new org.datanucleus.store.types.converters.BitSetStringConverter(), BitSet.class, String.class, false, null);
    if (// Not present in some JDKs
    ClassUtils.isClassPresent("java.awt.Color", clr)) {
        // java.awt
        registerConverter("dn.color-string", new org.datanucleus.store.types.converters.ColorStringConverter(), java.awt.Color.class, String.class, false, null);
        registerConverter("dn.color-components", new org.datanucleus.store.types.converters.ColorComponentsConverter(), java.awt.Color.class, int[].class, false, null);
        registerConverter("dn.bufferedimage-bytearray", new org.datanucleus.store.types.converters.BufferedImageByteArrayConverter(), java.awt.image.BufferedImage.class, byte[].class, false, null);
        registerConverter("dn.bufferedimage-bytebuffer", new org.datanucleus.store.types.converters.BufferedImageByteBufferConverter(), java.awt.image.BufferedImage.class, ByteBuffer.class, false, null);
    }
    registerConverter("dn.class-string", new org.datanucleus.store.types.converters.ClassStringConverter(), Class.class, String.class, false, null);
    registerConverter("dn.integer-string", new org.datanucleus.store.types.converters.IntegerStringConverter(), Integer.class, String.class, false, null);
    registerConverter("dn.long-string", new org.datanucleus.store.types.converters.LongStringConverter(), Long.class, String.class, false, null);
    registerConverter("dn.currency-string", new org.datanucleus.store.types.converters.CurrencyStringConverter(), Currency.class, String.class, false, null);
    registerConverter("dn.locale-string", new org.datanucleus.store.types.converters.LocaleStringConverter(), Locale.class, String.class, false, null);
    registerConverter("dn.stringbuffer-string", new org.datanucleus.store.types.converters.StringBufferStringConverter(), StringBuffer.class, String.class, false, null);
    registerConverter("dn.stringbuilder-string", new org.datanucleus.store.types.converters.StringBuilderStringConverter(), StringBuilder.class, String.class, false, null);
    registerConverter("dn.timezone-string", new org.datanucleus.store.types.converters.TimeZoneStringConverter(), TimeZone.class, String.class, false, null);
    registerConverter("dn.uri-string", new org.datanucleus.store.types.converters.URIStringConverter(), java.net.URI.class, String.class, false, null);
    registerConverter("dn.url-string", new org.datanucleus.store.types.converters.URLStringConverter(), java.net.URL.class, String.class, false, null);
    registerConverter("dn.uuid-string", new org.datanucleus.store.types.converters.UUIDStringConverter(), UUID.class, String.class, false, null);
    // Date/Time
    registerConverter("dn.date-long", new org.datanucleus.store.types.converters.DateLongConverter(), java.util.Date.class, Long.class, false, null);
    registerConverter("dn.date-string", new org.datanucleus.store.types.converters.DateStringConverter(), java.util.Date.class, String.class, false, null);
    registerConverter("dn.sqldate-long", new org.datanucleus.store.types.converters.SqlDateLongConverter(), java.sql.Date.class, Long.class, false, null);
    registerConverter("dn.sqldate-string", new org.datanucleus.store.types.converters.SqlDateStringConverter(), java.sql.Date.class, String.class, false, null);
    registerConverter("dn.sqldate-date", new org.datanucleus.store.types.converters.SqlDateStringConverter(), java.sql.Date.class, java.util.Date.class, false, null);
    registerConverter("dn.sqltime-long", new org.datanucleus.store.types.converters.SqlTimeStringConverter(), java.sql.Time.class, Long.class, false, null);
    registerConverter("dn.sqltime-string", new org.datanucleus.store.types.converters.SqlTimeStringConverter(), java.sql.Time.class, String.class, false, null);
    registerConverter("dn.sqltime-date", new org.datanucleus.store.types.converters.SqlTimeStringConverter(), java.sql.Time.class, java.util.Date.class, false, null);
    registerConverter("dn.sqltimestamp-long", new org.datanucleus.store.types.converters.SqlTimestampStringConverter(), java.sql.Timestamp.class, Long.class, false, null);
    registerConverter("dn.sqltimestamp-date", new org.datanucleus.store.types.converters.SqlTimestampStringConverter(), java.sql.Timestamp.class, java.util.Date.class, false, null);
    registerConverter("dn.sqltimestamp-string", new org.datanucleus.store.types.converters.SqlTimestampStringConverter(), java.sql.Timestamp.class, String.class, false, null);
    registerConverter("dn.calendar-string", new org.datanucleus.store.types.converters.CalendarStringConverter(), Calendar.class, String.class, false, null);
    registerConverter("dn.calendar-date", new org.datanucleus.store.types.converters.CalendarDateConverter(), Calendar.class, java.util.Date.class, false, null);
    registerConverter("dn.calendar-timestamp", new org.datanucleus.store.types.converters.CalendarTimestampConverter(), Calendar.class, java.sql.Timestamp.class, false, null);
    registerConverter("dn.calendar-components", new org.datanucleus.store.types.converters.CalendarComponentsConverter(), Calendar.class, Object[].class, false, null);
    // Serializable
    registerConverter("dn.serializable-string", new org.datanucleus.store.types.converters.SerializableStringConverter(), java.io.Serializable.class, String.class, false, null);
    registerConverter("dn.serializable-bytearray", new org.datanucleus.store.types.converters.SerializableByteArrayConverter(), java.io.Serializable.class, byte[].class, false, null);
    registerConverter("dn.serializable-bytebuffer", new org.datanucleus.store.types.converters.SerializableByteBufferConverter(), java.io.Serializable.class, ByteBuffer.class, false, null);
    // Arrays
    registerConverter("dn.bytearray-bytebuffer", new org.datanucleus.store.types.converters.ByteArrayByteBufferConverter(), byte[].class, ByteBuffer.class, false, null);
    registerConverter("dn.booleanarray-bytebuffer", new org.datanucleus.store.types.converters.BooleanArrayByteBufferConverter(), boolean[].class, ByteBuffer.class, false, null);
    registerConverter("dn.chararray-bytebuffer", new org.datanucleus.store.types.converters.CharArrayByteBufferConverter(), char[].class, ByteBuffer.class, false, null);
    registerConverter("dn.doublearray-bytebuffer", new org.datanucleus.store.types.converters.DoubleArrayByteBufferConverter(), double[].class, ByteBuffer.class, false, null);
    registerConverter("dn.floatarray-bytebuffer", new org.datanucleus.store.types.converters.FloatArrayByteBufferConverter(), float[].class, ByteBuffer.class, false, null);
    registerConverter("dn.intarray-bytebuffer", new org.datanucleus.store.types.converters.IntArrayByteBufferConverter(), int[].class, ByteBuffer.class, false, null);
    registerConverter("dn.longarray-bytebuffer", new org.datanucleus.store.types.converters.LongArrayByteBufferConverter(), long[].class, ByteBuffer.class, false, null);
    registerConverter("dn.shortarray-bytebuffer", new org.datanucleus.store.types.converters.ShortArrayByteBufferConverter(), short[].class, ByteBuffer.class, false, null);
    registerConverter("dn.bigintegerarray-bytebuffer", new org.datanucleus.store.types.converters.BigIntegerArrayByteBufferConverter(), BigInteger[].class, ByteBuffer.class, false, null);
    registerConverter("dn.bigdecimalarray-bytebuffer", new org.datanucleus.store.types.converters.BigDecimalArrayByteBufferConverter(), BigDecimal[].class, ByteBuffer.class, false, null);
    // java.time
    registerConverter("dn.localdate-string", new org.datanucleus.store.types.converters.LocalDateStringConverter(), LocalDate.class, String.class, false, null);
    registerConverter("dn.localdate-sqldate", new org.datanucleus.store.types.converters.LocalDateSqlDateConverter(), LocalDate.class, java.sql.Date.class, false, null);
    registerConverter("dn.localdate-date", new org.datanucleus.store.types.converters.LocalDateDateConverter(), LocalDate.class, java.util.Date.class, false, null);
    registerConverter("dn.localtime-string", new org.datanucleus.store.types.converters.LocalTimeStringConverter(), LocalTime.class, String.class, false, null);
    registerConverter("dn.localtime-sqltime", new org.datanucleus.store.types.converters.LocalTimeSqlTimeConverter(), LocalTime.class, java.sql.Time.class, false, null);
    registerConverter("dn.localtime-date", new org.datanucleus.store.types.converters.LocalTimeDateConverter(), LocalTime.class, java.util.Date.class, false, null);
    registerConverter("dn.localtime-long", new org.datanucleus.store.types.converters.LocalTimeLongConverter(), LocalTime.class, Long.class, false, null);
    registerConverter("dn.localdatetime-string", new org.datanucleus.store.types.converters.LocalDateTimeStringConverter(), LocalDateTime.class, String.class, false, null);
    registerConverter("dn.localdatetime-timestamp", new org.datanucleus.store.types.converters.LocalDateTimeTimestampConverter(), LocalDateTime.class, java.sql.Timestamp.class, false, null);
    registerConverter("dn.localdatetime-date", new org.datanucleus.store.types.converters.LocalDateTimeDateConverter(), LocalDateTime.class, java.util.Date.class, false, null);
    registerConverter("dn.offsettime-string", new org.datanucleus.store.types.converters.OffsetTimeStringConverter(), OffsetTime.class, String.class, false, null);
    registerConverter("dn.offsettime-long", new org.datanucleus.store.types.converters.OffsetTimeLongConverter(), OffsetTime.class, Long.class, false, null);
    registerConverter("dn.offsettime-sqltime", new org.datanucleus.store.types.converters.OffsetTimeSqlTimeConverter(), OffsetTime.class, java.sql.Time.class, false, null);
    registerConverter("dn.offsetdatetime-string", new org.datanucleus.store.types.converters.OffsetDateTimeStringConverter(), OffsetDateTime.class, String.class, false, null);
    registerConverter("dn.offsetdatetime-timestamp", new org.datanucleus.store.types.converters.OffsetDateTimeTimestampConverter(), OffsetDateTime.class, java.sql.Timestamp.class, false, null);
    registerConverter("dn.offsetdatetime-date", new org.datanucleus.store.types.converters.OffsetDateTimeDateConverter(), OffsetDateTime.class, java.util.Date.class, false, null);
    registerConverter("dn.duration-string", new org.datanucleus.store.types.converters.DurationStringConverter(), Duration.class, String.class, false, null);
    registerConverter("dn.duration-long", new org.datanucleus.store.types.converters.DurationLongConverter(), Duration.class, Long.class, false, null);
    registerConverter("dn.duration-double", new org.datanucleus.store.types.converters.DurationDoubleConverter(), Duration.class, Double.class, false, null);
    registerConverter("dn.period-string", new org.datanucleus.store.types.converters.PeriodStringConverter(), Period.class, String.class, false, null);
    registerConverter("dn.period-components", new org.datanucleus.store.types.converters.PeriodComponentsConverter(), Period.class, int[].class, false, null);
    registerConverter("dn.instant-timestamp", new org.datanucleus.store.types.converters.InstantTimestampConverter(), Instant.class, java.sql.Timestamp.class, false, null);
    registerConverter("dn.instant-date", new org.datanucleus.store.types.converters.InstantDateConverter(), Instant.class, java.util.Date.class, false, null);
    registerConverter("dn.instant-string", new org.datanucleus.store.types.converters.InstantStringConverter(), Instant.class, String.class, false, null);
    registerConverter("dn.instant-long", new org.datanucleus.store.types.converters.InstantLongConverter(), Instant.class, Long.class, false, null);
    registerConverter("dn.year-string", new org.datanucleus.store.types.converters.YearStringConverter(), Year.class, String.class, false, null);
    registerConverter("dn.year-integer", new org.datanucleus.store.types.converters.YearIntegerConverter(), Year.class, Integer.class, false, null);
    registerConverter("dn.yearmonth-string", new org.datanucleus.store.types.converters.YearMonthStringConverter(), YearMonth.class, String.class, false, null);
    registerConverter("dn.yearmonth-components", new org.datanucleus.store.types.converters.YearMonthComponentsConverter(), YearMonth.class, int[].class, false, null);
    registerConverter("dn.yearmonth-sqldate", new org.datanucleus.store.types.converters.YearMonthSqlDateConverter(), YearMonth.class, java.sql.Date.class, false, null);
    registerConverter("dn.yearmonth-date", new org.datanucleus.store.types.converters.YearMonthDateConverter(), YearMonth.class, java.util.Date.class, false, null);
    registerConverter("dn.monthday-string", new org.datanucleus.store.types.converters.MonthDayStringConverter(), MonthDay.class, String.class, false, null);
    registerConverter("dn.monthday-components", new org.datanucleus.store.types.converters.MonthDayComponentsConverter(), MonthDay.class, int[].class, false, null);
    registerConverter("dn.monthday-sqldate", new org.datanucleus.store.types.converters.MonthDaySqlDateConverter(), MonthDay.class, java.sql.Date.class, false, null);
    registerConverter("dn.monthday-date", new org.datanucleus.store.types.converters.MonthDayDateConverter(), MonthDay.class, java.util.Date.class, false, null);
    registerConverter("dn.zoneid-string", new org.datanucleus.store.types.converters.ZoneIdStringConverter(), ZoneId.class, String.class, false, null);
    registerConverter("dn.zoneoffset-string", new org.datanucleus.store.types.converters.ZoneOffsetStringConverter(), ZoneOffset.class, String.class, false, null);
    registerConverter("dn.zoneddatetime-string", new org.datanucleus.store.types.converters.ZonedDateTimeStringConverter(), ZonedDateTime.class, String.class, false, null);
    registerConverter("dn.zoneddatetime-timestamp", new org.datanucleus.store.types.converters.ZonedDateTimeTimestampConverter(), ZonedDateTime.class, java.sql.Timestamp.class, false, null);
    // Add on any plugin mechanism types
    ConfigurationElement[] elems = mgr.getConfigurationElementsForExtension("org.datanucleus.type_converter", null, null);
    if (elems != null) {
        for (int i = 0; i < elems.length; i++) {
            String name = elems[i].getAttribute("name").trim();
            String memberTypeName = elems[i].getAttribute("member-type").trim();
            String datastoreTypeName = elems[i].getAttribute("datastore-type").trim();
            String converterClsName = elems[i].getAttribute("converter-class").trim();
            Class memberType = null;
            try {
                // Use plugin manager to instantiate the converter in case its in separate plugin
                TypeConverter conv = (TypeConverter) mgr.createExecutableExtension("org.datanucleus.type_converter", "name", name, "converter-class", null, null);
                memberType = clr.classForName(memberTypeName);
                Class datastoreType = clr.classForName(datastoreTypeName);
                registerConverter(name, conv, memberType, datastoreType, false, null);
            } catch (Exception e) {
                if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
                    if (memberType != null) {
                        NucleusLogger.PERSISTENCE.debug("TypeConverter for " + memberTypeName + "<->" + datastoreTypeName + " using " + converterClsName + " not instantiable (missing dependencies?) so ignoring");
                    } else {
                        NucleusLogger.PERSISTENCE.debug("TypeConverter for " + memberTypeName + "<->" + datastoreTypeName + " ignored since java type not present in CLASSPATH");
                    }
                }
            }
        }
    }
    if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("016008"));
        if (typeConverterMap != null) {
            List<Class> typesList = new ArrayList<Class>(typeConverterMap.keySet());
            Collections.sort(typesList, ALPHABETICAL_ORDER);
            for (Class javaType : typesList) {
                Set<Class> datastoreTypes = typeConverterMap.get(javaType).keySet();
                StringBuilder str = new StringBuilder();
                for (Class datastoreCls : datastoreTypes) {
                    if (str.length() > 0) {
                        str.append(',');
                    }
                    str.append(StringUtils.getNameOfClass(datastoreCls));
                }
                NucleusLogger.PERSISTENCE.debug("TypeConverter(s) available for " + StringUtils.getNameOfClass(javaType) + " to : " + str.toString());
            }
        }
    }
}
Also used : ClassStringConverter(org.datanucleus.store.types.converters.ClassStringConverter) ArrayList(java.util.ArrayList) ConfigurationElement(org.datanucleus.plugin.ConfigurationElement) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) NucleusException(org.datanucleus.exceptions.NucleusException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) TypeConverter(org.datanucleus.store.types.converters.TypeConverter)

Example 9 with TypeConverter

use of org.datanucleus.store.types.converters.TypeConverter in project datanucleus-core by datanucleus.

the class CompleteClassTable method processEmbeddedMember.

protected void processEmbeddedMember(List<AbstractMemberMetaData> mmds, ClassLoaderResolver clr, EmbeddedMetaData embmd, boolean ownerNested) {
    TypeManager typeMgr = storeMgr.getNucleusContext().getTypeManager();
    MetaDataManager mmgr = storeMgr.getMetaDataManager();
    NamingFactory namingFactory = storeMgr.getNamingFactory();
    AbstractMemberMetaData lastMmd = mmds.get(mmds.size() - 1);
    AbstractClassMetaData embCmd = null;
    if (lastMmd.hasCollection()) {
        // Embedded collection element
        embCmd = mmgr.getMetaDataForClass(lastMmd.getCollection().getElementType(), clr);
    } else if (lastMmd.hasArray()) {
        // Embedded array element
        embCmd = mmgr.getMetaDataForClass(lastMmd.getArray().getElementType(), clr);
    } else {
        // Embedded 1-1
        embCmd = mmgr.getMetaDataForClass(lastMmd.getType(), clr);
    }
    // Go through all members of the embedded class
    int[] memberPositions = embCmd.getAllMemberPositions();
    for (int i = 0; i < memberPositions.length; i++) {
        AbstractMemberMetaData mmd = embCmd.getMetaDataForManagedMemberAtAbsolutePosition(memberPositions[i]);
        if (mmd.getPersistenceModifier() != FieldPersistenceModifier.PERSISTENT) {
            // Don't need column if not persistent
            continue;
        }
        if (mmds.size() == 1 && embmd != null && embmd.getOwnerMember() != null && embmd.getOwnerMember().equals(mmd.getName())) {
            // Special case of this being a link back to the owner. TODO Repeat this for nested and their owners
            continue;
        }
        AbstractMemberMetaData embmdMmd = null;
        if (embmd != null) {
            AbstractMemberMetaData[] embmdMmds = embmd.getMemberMetaData();
            if (embmdMmds != null) {
                for (AbstractMemberMetaData thisMmd : embmdMmds) {
                    if (thisMmd.getName().equals(mmd.getName())) {
                        embmdMmd = thisMmd;
                        break;
                    }
                }
            }
        }
        RelationType relationType = mmd.getRelationType(clr);
        if (relationType != RelationType.NONE && MetaDataUtils.getInstance().isMemberEmbedded(mmgr, clr, mmd, relationType, lastMmd)) {
            if (RelationType.isRelationSingleValued(relationType)) {
                // Nested embedded PC, so recurse
                boolean nested = false;
                if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_PC_NESTED)) {
                    nested = !storeMgr.getNucleusContext().getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_METADATA_EMBEDDED_PC_FLAT);
                    String nestedStr = mmd.getValueForExtension("nested");
                    if (nestedStr != null && nestedStr.equalsIgnoreCase("" + !nested)) {
                        nested = !nested;
                    }
                }
                List<AbstractMemberMetaData> embMmds = new ArrayList<AbstractMemberMetaData>(mmds);
                embMmds.add(mmd);
                if (nested) {
                    // Embedded object stored as nested under this in the owner table (where the datastore supports that)
                    // Add column for the owner of the embedded object, typically for the column name only
                    ColumnMetaData[] colmds = mmd.getColumnMetaData();
                    String colName = namingFactory.getColumnName(embMmds, 0);
                    ColumnImpl col = addEmbeddedColumn(colName, null);
                    col.setNested(true);
                    if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
                        col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
                    } else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
                        col.setPosition(colmds[0].getPosition());
                    }
                    if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
                        col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
                    } else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
                        col.setJdbcType(colmds[0].getJdbcType());
                    }
                    MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
                    col.setMemberColumnMapping(mapping);
                    if (schemaVerifier != null) {
                        schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                    }
                    mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
                    // TODO Create mapping for the related info under the above column
                    processEmbeddedMember(embMmds, clr, embmdMmd != null ? embmdMmd.getEmbeddedMetaData() : null, true);
                } else {
                    // Embedded object stored flat into this table, with columns at same level as owner columns
                    processEmbeddedMember(embMmds, clr, embmdMmd != null ? embmdMmd.getEmbeddedMetaData() : null, false);
                }
            } else {
                if (mmd.hasCollection()) {
                    if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_COLLECTION_NESTED)) {
                    // TODO Support nested embedded collection element
                    }
                    NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " is an embedded collection. Not yet supported. Ignoring");
                    continue;
                } else if (mmd.hasMap()) {
                    if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_MAP_NESTED)) {
                    // TODO Support nested embedded map key/value
                    }
                    NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " is an embedded collection. Not yet supported. Ignoring");
                    continue;
                } else if (mmd.hasArray()) {
                    if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_ARRAY_NESTED)) {
                    // TODO Support nested embedded array element
                    }
                    NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " is an embedded array. Not yet supported. Ignoring");
                    continue;
                }
            }
        } else {
            List<AbstractMemberMetaData> embMmds = new ArrayList<AbstractMemberMetaData>(mmds);
            embMmds.add(mmd);
            ColumnMetaData[] colmds = mmd.getColumnMetaData();
            if (relationType != RelationType.NONE) {
                // 1-1/N-1 stored as single column with persistable-id
                // 1-N/M-N stored as single column with collection<persistable-id>
                // Create column for basic type
                String colName = namingFactory.getColumnName(embMmds, 0);
                ColumnImpl col = addEmbeddedColumn(colName, null);
                col.setNested(ownerNested);
                if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
                    col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
                } else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
                    col.setPosition(colmds[0].getPosition());
                }
                if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
                    col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
                } else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
                    col.setJdbcType(colmds[0].getJdbcType());
                }
                MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
                col.setMemberColumnMapping(mapping);
                if (schemaVerifier != null) {
                    schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                }
                mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
            } else {
                // TODO Pass in embedded colmds if they have jdbcType info?
                TypeConverter typeConv = getTypeConverterForMember(mmd, colmds, typeMgr);
                if (typeConv != null) {
                    // Create column(s) for this TypeConverter
                    if (typeConv instanceof MultiColumnConverter) {
                        Class[] colJavaTypes = ((MultiColumnConverter) typeConv).getDatastoreColumnTypes();
                        Column[] cols = new Column[colJavaTypes.length];
                        for (int j = 0; j < colJavaTypes.length; j++) {
                            String colName = namingFactory.getColumnName(embMmds, j);
                            ColumnImpl col = addEmbeddedColumn(colName, typeConv);
                            col.setNested(ownerNested);
                            if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == colJavaTypes.length && embmdMmd.getColumnMetaData()[j].getPosition() != null) {
                                col.setPosition(embmdMmd.getColumnMetaData()[j].getPosition());
                            } else if (colmds != null && colmds.length == colJavaTypes.length && colmds[j].getPosition() != null) {
                                col.setPosition(colmds[j].getPosition());
                            }
                            if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == colJavaTypes.length && embmdMmd.getColumnMetaData()[j].getJdbcType() != null) {
                                col.setJdbcType(embmdMmd.getColumnMetaData()[j].getJdbcType());
                            } else if (colmds != null && colmds.length == colJavaTypes.length && colmds[j].getJdbcType() != null) {
                                col.setJdbcType(colmds[j].getJdbcType());
                            }
                            cols[j] = col;
                        }
                        MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, cols, typeConv);
                        for (int j = 0; j < colJavaTypes.length; j++) {
                            ((ColumnImpl) cols[j]).setMemberColumnMapping(mapping);
                        }
                        if (schemaVerifier != null) {
                            schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                        }
                        mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
                    } else {
                        String colName = namingFactory.getColumnName(embMmds, 0);
                        ColumnImpl col = addEmbeddedColumn(colName, typeConv);
                        col.setNested(ownerNested);
                        if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
                            col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
                        } else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
                            col.setPosition(colmds[0].getPosition());
                        }
                        if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
                            col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
                        } else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
                            col.setJdbcType(colmds[0].getJdbcType());
                        }
                        MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
                        col.setMemberColumnMapping(mapping);
                        mapping.setTypeConverter(typeConv);
                        if (schemaVerifier != null) {
                            schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                        }
                        mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
                    }
                } else {
                    // Create column for basic type
                    String colName = namingFactory.getColumnName(embMmds, 0);
                    ColumnImpl col = addEmbeddedColumn(colName, null);
                    col.setNested(ownerNested);
                    AbstractMemberMetaData theMmd = embMmds.get(0);
                    if (theMmd.isPrimaryKey()) {
                        col.setPrimaryKey();
                    }
                    if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
                        col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
                    } else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
                        col.setPosition(colmds[0].getPosition());
                    }
                    if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
                        col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
                    } else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
                        col.setJdbcType(colmds[0].getJdbcType());
                    }
                    MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
                    col.setMemberColumnMapping(mapping);
                    if (schemaVerifier != null) {
                        schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                    }
                    mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MetaDataManager(org.datanucleus.metadata.MetaDataManager) MultiColumnConverter(org.datanucleus.store.types.converters.MultiColumnConverter) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) TypeConverter(org.datanucleus.store.types.converters.TypeConverter) RelationType(org.datanucleus.metadata.RelationType) TypeManager(org.datanucleus.store.types.TypeManager) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) NamingFactory(org.datanucleus.store.schema.naming.NamingFactory) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 10 with TypeConverter

use of org.datanucleus.store.types.converters.TypeConverter in project datanucleus-rdbms by datanucleus.

the class MappingManagerImpl method getMappingType.

/**
 * Accessor for the JavaTypeMapping class for the supplied java type.
 * @param javaTypeName The java type name
 * @return The Java mapping type
 */
public Class getMappingType(String javaTypeName) {
    if (javaTypeName == null) {
        return null;
    }
    MappedType type = mappedTypes.get(javaTypeName);
    if (type == null) {
        // No explicit <java_mapping> so check for a default TypeConverter against the basic <java-type>
        TypeManager typeMgr = storeMgr.getNucleusContext().getTypeManager();
        TypeConverter defaultTypeConv = typeMgr.getDefaultTypeConverterForType(clr.classForName(javaTypeName));
        if (defaultTypeConv != null) {
            // We have no explicit mapping and there is a defined default TypeConverter so return and that will be picked
            return null;
        }
        // Check if this is a SCO wrapper
        Class cls = typeMgr.getTypeForSecondClassWrapper(javaTypeName);
        if (cls != null) {
            // Supplied class is a SCO wrapper, so return the java type mapping for the underlying java type
            type = mappedTypes.get(cls.getName());
            if (type != null) {
                return type.javaMappingType;
            }
        }
        // Not SCO wrapper so find a type
        try {
            cls = clr.classForName(javaTypeName);
            type = findMappedTypeForClass(cls);
            return type.javaMappingType;
        } catch (Exception e) {
            return null;
        }
    }
    return type.javaMappingType;
}
Also used : TypeConverter(org.datanucleus.store.types.converters.TypeConverter) TypeManager(org.datanucleus.store.types.TypeManager) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NucleusException(org.datanucleus.exceptions.NucleusException) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Aggregations

TypeConverter (org.datanucleus.store.types.converters.TypeConverter)13 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)8 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)7 NucleusException (org.datanucleus.exceptions.NucleusException)6 NoTableManagedException (org.datanucleus.store.rdbms.exceptions.NoTableManagedException)5 TypeConverterMapping (org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping)5 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)4 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)4 TypeConverterMultiMapping (org.datanucleus.store.rdbms.mapping.java.TypeConverterMultiMapping)4 TypeManager (org.datanucleus.store.types.TypeManager)4 MultiColumnConverter (org.datanucleus.store.types.converters.MultiColumnConverter)4 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)3 BigDecimal (java.math.BigDecimal)2 Date (java.sql.Date)2 Timestamp (java.sql.Timestamp)2 ArrayList (java.util.ArrayList)2 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)2 JdbcType (org.datanucleus.metadata.JdbcType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1