Example usage for javax.persistence EnumType ORDINAL

List of usage examples for javax.persistence EnumType ORDINAL

Introduction

In this page you can find the example usage for javax.persistence EnumType ORDINAL.

Prototype

EnumType ORDINAL

To view the source code for javax.persistence EnumType ORDINAL.

Click Source Link

Document

Persist enumerated type property or field as an integer.

Usage

From source file:org.batoo.jpa.jdbc.AbstractColumn.java

/**
 * {@inheritDoc}//w ww  .j  a  v  a2 s.c  o  m
 * 
 */
@Override
public Object convertValue(Connection connection, final Object value) {
    if (value == null) {
        return null;
    }

    if (this.temporalType != null) {
        switch (this.temporalType) {
        case DATE:
            if (value instanceof java.sql.Date) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Date(((Date) value).getTime());
            }

            return new java.sql.Date(((Calendar) value).getTimeInMillis());
        case TIME:
            if (value instanceof java.sql.Time) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Time(((Date) value).getTime());
            }

            return new java.sql.Time(((Calendar) value).getTimeInMillis());
        case TIMESTAMP:
            if (value instanceof java.sql.Timestamp) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Timestamp(((Date) value).getTime());
            }

            return new java.sql.Timestamp(((Calendar) value).getTimeInMillis());
        }
    }

    if (this.numberType != null) {
        return ReflectHelper.convertNumber((Number) value, this.numberType);
    }

    if (this.enumType != null) {
        final Enum<?> enumValue = (Enum<?>) value;
        if (this.enumType == EnumType.ORDINAL) {
            return enumValue.ordinal();
        }

        return enumValue.name();
    }

    if (this.lob) {
        try {
            if (this.javaType == String.class) {
                return new SerialClob(((String) value).toCharArray());
            } else if (this.javaType == char[].class) {
                return new SerialClob((char[]) value);
            } else if (this.javaType == byte[].class) {
                return new SerialBlob((byte[]) value);
            } else {
                final ByteArrayOutputStream os = new ByteArrayOutputStream();
                final ObjectOutputStream oos = new ObjectOutputStream(os);
                try {
                    oos.writeObject(value);
                } finally {
                    oos.close();
                }

                return new SerialBlob(os.toByteArray());
            }
        } catch (final Exception e) {
            throw new PersistenceException("Cannot set parameter", e);
        }
    } else {
        return value;
    }
}

From source file:org.batoo.jpa.jdbc.AbstractColumn.java

/**
 * Converts the value corresponding to enum, temporal type, number or [cb]lob.
 * //  ww  w.j  a v  a  2s .c o m
 * @param value
 *            the raw value
 * @return the converted value
 * 
 * @since 2.0.0
 */
protected Object convertValueForSet(Object value) {
    if (value == null) {
        return null;
    }

    if (this.enumType != null) {
        if (this.enumType == EnumType.ORDINAL) {
            value = this.values[((Number) value).shortValue()];
        } else {
            try {
                value = this.method.invoke(null, value);
            } catch (final Exception e) {
            }
        }
    }

    if (this.lob) {
        value = this.readLob(value);
    }

    return value;
}

From source file:org.batoo.jpa.jdbc.ValueConverter.java

/**
 * convert jdbc data to entity basic values
 * //  ww  w . jav  a 2 s  .  co  m
 * @param value
 *            jdbc raw value
 * @param javaType
 *            type of value
 * @param temporalType
 *            temporal type
 * @param enumType
 *            enum type
 * @param values
 *            EnumType values array for faster operation, leave it to be null but it will be slow
 * @param method
 *            Method for enum valueOf for faster operation, leave it to be null but it will be slow
 * @param lob
 *            is Lob
 * @return java value
 * @since 2.0.1
 */
@SuppressWarnings("unchecked")
public static Object fromJdbc(Object value, Class<?> javaType, TemporalType temporalType, EnumType enumType,
        Enum<?>[] values, Method method, boolean lob) {
    if (value == null) {
        return null;
    }

    if (temporalType != null) {
        if (javaType == Calendar.class) {
            final Calendar calendarValue = Calendar.getInstance();
            switch (temporalType) {
            case DATE:
                if (value instanceof java.sql.Date) {
                    calendarValue.setTime((java.sql.Date) value);
                    return calendarValue;
                }
            case TIME:
                if (value instanceof java.sql.Time) {
                    calendarValue.setTime((java.sql.Time) value);
                    return calendarValue;
                }
            case TIMESTAMP:
                if (value instanceof java.sql.Timestamp) {
                    calendarValue.setTime((java.sql.Timestamp) value);
                    return calendarValue;
                }
            }
        }
    }

    if (enumType != null) {
        try {
            final Class<Enum<?>> enumJavaType = (Class<Enum<?>>) javaType;
            if (enumType == EnumType.ORDINAL && values == null) {
                values = (Enum<?>[]) enumJavaType.getMethod("values").invoke(null);
            } else if (enumType == EnumType.STRING && method == null) {
                method = enumJavaType.getMethod("valueOf", String.class);
            }

            if (enumType == EnumType.ORDINAL) {
                value = values[((Number) value).shortValue()];
            } else {
                value = method.invoke(null, value);
            }
        } catch (final Exception e) {
        }
    }

    if (lob) {
        value = readLob(value, javaType);
    }

    return value;
}

From source file:org.batoo.jpa.jdbc.ValueConverter.java

/**
 * convert java objects to jdbc friendly
 * //from   w w w.  j  a  va2  s .  c  o m
 * @param value
 *            jdbc raw value
 * @param javaType
 *            type of value
 * @param temporalType
 *            temporal type
 * @param enumType
 *            enum type
 * @param lob
 *            is Lob
 * @return jdbc friendly value
 * @since 2.0.1
 */
public static Object toJdbc(Object value, Class<?> javaType, TemporalType temporalType, EnumType enumType,
        boolean lob) {
    if (value == null) {
        return null;
    }

    if (temporalType != null) {
        switch (temporalType) {
        case DATE:
            if (value instanceof java.sql.Date) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Date(((Date) value).getTime());
            }

            return new java.sql.Date(((Calendar) value).getTimeInMillis());
        case TIME:
            if (value instanceof java.sql.Time) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Time(((Date) value).getTime());
            }

            return new java.sql.Time(((Calendar) value).getTimeInMillis());
        case TIMESTAMP:
            if (value instanceof java.sql.Timestamp) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Timestamp(((Date) value).getTime());
            }

            return new java.sql.Timestamp(((Calendar) value).getTimeInMillis());
        }
    }

    if (Number.class.isAssignableFrom(javaType)) {
        return ReflectHelper.convertNumber((Number) value, javaType);
    }

    if (enumType != null) {
        final Enum<?> enumValue = (Enum<?>) value;
        if (enumType == EnumType.ORDINAL) {
            return enumValue.ordinal();
        }

        return enumValue.name();
    }

    if (lob) {
        try {
            if (javaType == String.class) {
                return new SerialClob(((String) value).toCharArray());
            } else if (javaType == char[].class) {
                return new SerialClob((char[]) value);
            } else if (javaType == byte[].class) {
                return new SerialBlob((byte[]) value);
            } else {
                final ByteArrayOutputStream os = new ByteArrayOutputStream();
                final ObjectOutputStream oos = new ObjectOutputStream(os);
                try {
                    oos.writeObject(value);
                } finally {
                    oos.close();
                }

                return new SerialBlob(os.toByteArray());
            }
        } catch (final Exception e) {
            throw new PersistenceException("Cannot set parameter", e);
        }
    } else {
        return value;
    }

}

From source file:org.guzz.builder.JPA2AnnotationsBuilder.java

protected static void addPropertyMapping(GuzzContextImpl gf, POJOBasedObjectMapping map, SimpleTable st,
        String name, AnnotatedElement element, Class dataType) {
    javax.persistence.Column pc = element.getAnnotation(javax.persistence.Column.class);
    javax.persistence.Basic pb = element.getAnnotation(javax.persistence.Basic.class);
    javax.persistence.Enumerated pe = element.getAnnotation(javax.persistence.Enumerated.class);
    org.guzz.annotations.Column gc = element.getAnnotation(org.guzz.annotations.Column.class);

    String type = gc == null ? null : gc.type();
    String nullValue = gc == null ? null : gc.nullValue();
    String column = pc == null ? null : pc.name();
    boolean lazy = pb == null ? false : pb.fetch() == FetchType.LAZY;
    Class loader = gc == null ? null : gc.loader();

    if (dataType.isEnum()) {
        EnumType etype = EnumType.ORDINAL;

        if (pe != null) {
            etype = pe.value();//w w  w  . j  a v a  2  s  .  c  om
        }

        if (etype == EnumType.ORDINAL) {
            type = "enum.ordinal|" + dataType.getName();
        } else {
            type = "enum.string|" + dataType.getName();
        }
    }

    boolean insertIt = pc == null ? true : pc.insertable();
    boolean updateIt = pc == null ? true : pc.updatable();

    if (StringUtil.isEmpty(column)) {
        column = name;
    }

    TableColumn col = st.getColumnByPropName(name);
    if (col != null) {
        log.warn("field/property [" + name + "] already exsits in the parent class of [" + st.getBusinessName()
                + "]. Ignore it.");

        return;
    }

    if (loader == null || NullValue.class.isAssignableFrom(loader)) {
        loader = null;
    }

    try {
        col = ObjectMappingUtil.createTableColumn(gf, map, name, column, type,
                loader == null ? null : loader.getName());
        col.setNullValue(nullValue);
        col.setAllowInsert(insertIt);
        col.setAllowUpdate(updateIt);
        col.setLazy(lazy);

        st.addColumn(col);
    } catch (DataTypeException dte) {
        //???JPA??Map, Set
        if (log.isDebugEnabled()) {
            log.debug("Unsupported data type is found in annotation, property is:[" + name + "], business is:["
                    + st.getBusinessName() + "]. Ignore this property.", dte);
        } else {
            log.warn("Ignore unsupported data type in annotation, property is:[" + name + "], business is:["
                    + st.getBusinessName() + "], msg is:" + dte.getMessage());
        }
    }
}