Package org.datanucleus.store.types.converters

Examples of org.datanucleus.store.types.converters.TypeConverter


          mmd = getClassMetaData().getMetaDataForMember(mmd.getName());
        }

        if (mmd.getTypeConverterName() != null) {
          // User-defined TypeConverter
          TypeConverter conv = ec.getTypeManager().getTypeConverterForName(mmd.getTypeConverterName());
          value = conv.toMemberType(value);
        } else {
          // Perform any conversions from the stored-type to the field type
          TypeManager typeMgr = ec.getNucleusContext().getTypeManager();
          value = ((DatastoreManager) ec.getStoreManager()).getTypeConversionUtils()
              .datastoreValueToPojoValue(typeMgr, clr, value, mmd);
View Full Code Here


        } else {
          if (supportedClasses.contains(ammd.getType()) ||
              ammd.getTypeName().startsWith("com.google.appengine.api")) {
            value = getDatastoreToPojoTypeFunc(Utils.identity(), ammd.getType()).apply(value);
          } else if (value instanceof String) {
            TypeConverter conv = typeMgr.getTypeConverterForType(ammd.getType(), String.class);
            if (conv != null) {
              // Persisted as String, so convert back
              value = conv.toMemberType((String)value);
            }
          } else if (value instanceof Long) {
            TypeConverter conv = typeMgr.getTypeConverterForType(ammd.getType(), Long.class);
            if (conv != null) {
              // Persisted as Long, so convert back
              value = conv.toMemberType((Long)value);
            }
          } else {
            // Unsupported type on GAE/J ?
            value = getDatastoreToPojoTypeFunc(Utils.identity(), ammd.getType()).apply(value);
          }
View Full Code Here

      return null;
    }

    if (ammd.getTypeConverterName() != null) {
      // User-defined type-converter
      TypeConverter conv = typeMgr.getTypeConverterForName(ammd.getTypeConverterName());
      value = conv.toDatastoreType(value);
    } else {
      // Perform conversion
      if (ammd.hasArray()) {
        value = convertPojoArrayToDatastoreValue(ammd, value);
      } else if (ammd.hasCollection()) {
        value = convertPojoCollectionToDatastoreValue(clr, ammd, (Collection<?>) value);
      } else if (ammd.hasMap()) {
        value = convertPojoMapToDatastoreValue(clr, ammd, (Map)value);
      } else {
        if (value != null) {
          if (java.sql.Time.class.isAssignableFrom(ammd.getType())) {
            // java.sql.Time -> Long
            value = ((java.sql.Time)value).getTime(); // Pass a long through since this is not supported
          } else if (java.sql.Date.class.isAssignableFrom(ammd.getType())) {
            // java.sql.Date -> Long
            value = ((java.sql.Date)value).getTime(); // Pass a long through since this is not supported
          } else if (java.sql.Timestamp.class.isAssignableFrom(ammd.getType())) {
            // java.sql.Timestamp -> Long
            value = ((java.sql.Timestamp)value).getTime(); // Pass a long through since this is not supported
          } else if (Date.class.isAssignableFrom(ammd.getType())) {
            // java.util.Date -> Date (date, time, datetime)
            ColumnMetaData[] colmds = ammd.getColumnMetaData();
            if (colmds != null && colmds.length > 0 && colmds[0].getJdbcType() != null) {
              String jdbcType = colmds[0].getJdbcType();
              if (jdbcType.equalsIgnoreCase("time")) { // Dump the date part
                Calendar cal = Calendar.getInstance();
                cal.setTime((Date) value);
                java.sql.Time time = new java.sql.Time(0);
                time.setHours(cal.get(Calendar.HOUR_OF_DAY));
                time.setMinutes(cal.get(Calendar.MINUTE));
                time.setSeconds(cal.get(Calendar.SECOND));
                value = new Date(time.getTime());
              } else if (jdbcType.equalsIgnoreCase("date")) { // Dump the time part
                Calendar cal = Calendar.getInstance();
                cal.setTime((Date) value);
                java.sql.Date date = new java.sql.Date(0);
                date.setDate(cal.get(Calendar.DAY_OF_MONTH));
                date.setMonth(cal.get(Calendar.MONTH));
                date.setYear(cal.get(Calendar.YEAR)-1900);
                value = new Date(date.getTime());
              }
            }
          } else if (Enum.class.isAssignableFrom(ammd.getType())) {
            // Enum -> String(name) / Long(ordinal)
            boolean asOrdinal = false;
            if (ammd.getColumnMetaData() != null && ammd.getColumnMetaData().length > 0) {
              String jdbcType = ammd.getColumnMetaData()[0].getJdbcType();
              if (jdbcType != null && jdbcType.equalsIgnoreCase("integer")) {
                // Persisted as ordinal
                asOrdinal = true;
              }
            }
            if (asOrdinal) {
              if (value instanceof Enum) {
                value = ((Enum)value).ordinal();
              }
            } else {
              if (value instanceof Enum) {
                value = ((Enum) value).name();
              }
            }
          } else {
            if (supportedClasses.contains(ammd.getType()) ||
                ammd.getTypeName().startsWith("com.google.appengine.api")) {
              value = getPojoToDatastoreTypeFunc(Utils.identity(), ammd.getType()).apply(value);
            } else {
              // Use TypeConverter where appropriate
              // Note : We also come through here when converting parameter values for queries, which may not be the
              // same type as the value of the field (e.g pass in the String form when using a UUID)
              if (ammd.getType().isAssignableFrom(value.getClass())) {
                TypeConverter strConv = typeMgr.getTypeConverterForType(ammd.getType(), String.class);
                if (strConv != null) {
                  // Persist as String
                  value = strConv.toDatastoreType(value);
                } else {
                  TypeConverter longConv = typeMgr.getTypeConverterForType(ammd.getType(), Long.class);
                  if (longConv != null) {
                    // Persist as Long
                    value = longConv.toDatastoreType(value);
                  } else {
                    // Unsupported type on GAE/J ?
                    value = getPojoToDatastoreTypeFunc(Utils.identity(), ammd.getType()).apply(value);
                  }
                }
View Full Code Here

TOP

Related Classes of org.datanucleus.store.types.converters.TypeConverter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.