This is an automated email from the ASF dual-hosted git repository.
doebele pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/empire-db.git
The following commit(s) were added to refs/heads/master by this push:
new 9e87ea04 EMPIREDB-459 DBRecordData / DataListEntry: Allow both local
and global overrides for all data type conversions.
9e87ea04 is described below
commit 9e87ea046f53bd43668c516edc9fa85e302176fe
Author: Rainer Döbele <[email protected]>
AuthorDate: Tue Mar 25 18:44:39 2025 +0100
EMPIREDB-459
DBRecordData / DataListEntry: Allow both local and global overrides for all
data type conversions.
---
.../org/apache/empire/commons/ObjectUtils.java | 31 +++++
.../java/org/apache/empire/commons/ValueUtils.java | 155 ++++++++++++++++++++-
.../java/org/apache/empire/data/RecordData.java | 16 +++
.../apache/empire/data/bean/BeanRecordProxy.java | 45 +++++-
.../org/apache/empire/data/list/DataListEntry.java | 103 +++++++-------
.../org/apache/empire/data/list/DataListHead.java | 33 +----
.../java/org/apache/empire/db/DBRecordData.java | 127 +++++++----------
7 files changed, 341 insertions(+), 169 deletions(-)
diff --git a/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
b/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
index df47cb9b..52beb9f1 100644
--- a/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
@@ -31,6 +31,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
+import java.util.Locale;
import org.apache.commons.beanutils.MethodUtils;
import org.apache.empire.data.ColumnExpr;
@@ -451,6 +452,18 @@ public final class ObjectUtils
return valueUtils.toString(value);
}
+ /**
+ * Returns a formatted column value
+ * @param column the column
+ * @param value the value to convert
+ * @param local the locale (optional)
+ * @return the corresponding string value
+ */
+ public static String formatColumnValue(ColumnExpr column, Object value,
Locale locale)
+ {
+ return valueUtils.formatColumnValue(column, value, locale);
+ }
+
/**
* Converts an object value to a Date.
* <P>
@@ -543,6 +556,24 @@ public final class ObjectUtils
{
return valueUtils.convertToJava(c, v);
}
+
+ /**
+ * Converts a column value to a Java type
+ *
+ * @param <T> the type to convert to
+ * @param column the column expression
+ * @param value the object to convert
+ * @param returnType the class type to convert to
+ *
+ * @return the converted value of v or null
+ *
+ * @throws ClassCastException if the object is not null and is not
assignable to the type T.
+ */
+ public static <T> T convertColumnValue(ColumnExpr column, Object value,
Class<T> returnType)
+ throws ClassCastException
+ {
+ return valueUtils.convertColumnValue(column, value, returnType);
+ }
/**
* Converts a value to a specific DataType
diff --git a/empire-db/src/main/java/org/apache/empire/commons/ValueUtils.java
b/empire-db/src/main/java/org/apache/empire/commons/ValueUtils.java
index 4ea9cb67..0997dd9c 100644
--- a/empire-db/src/main/java/org/apache/empire/commons/ValueUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/commons/ValueUtils.java
@@ -25,18 +25,25 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
+import java.time.format.DateTimeParseException;
import java.time.temporal.Temporal;
import java.util.Collection;
import java.util.Date;
+import java.util.Locale;
import org.apache.commons.beanutils.MethodUtils;
+import org.apache.empire.data.Column;
+import org.apache.empire.data.ColumnExpr;
import org.apache.empire.data.DataType;
import org.apache.empire.db.DBDatabase;
import org.apache.empire.db.DBExpr;
+import org.apache.empire.db.exceptions.FieldIllegalValueException;
import org.apache.empire.db.expr.column.DBValueExpr;
import org.apache.empire.exceptions.InvalidValueException;
import org.apache.empire.exceptions.ItemNotFoundException;
import org.apache.empire.exceptions.ValueConversionException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This class allows to customize value type conversion as well as other value
related functions.
@@ -51,7 +58,7 @@ import org.apache.empire.exceptions.ValueConversionException;
public class ValueUtils
{
// Logger
- // private static final Logger log =
LoggerFactory.getLogger(ValueUtils.class);
+ private static final Logger log =
LoggerFactory.getLogger(ValueUtils.class);
protected static final String DATE_FORMAT = "yyyy-MM-dd";
protected static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
@@ -260,7 +267,7 @@ public class ValueUtils
* @param v the value to convert
* @return the integer value
*/
- public int toInteger(Object v)
+ public Integer toInteger(Object v)
{
if (isEmpty(v))
return 0;
@@ -278,10 +285,10 @@ public class ValueUtils
* @param v the value to convert
* @return the long value
*/
- public long toLong(Object v)
+ public Long toLong(Object v)
{
if (isEmpty(v))
- return 0;
+ return 0l;
if (v instanceof Number)
return ((Number)v).longValue();
if (v instanceof Enum)
@@ -296,7 +303,7 @@ public class ValueUtils
* @param v the value to convert
* @return the double value
*/
- public double toDouble(Object v)
+ public Double toDouble(Object v)
{
// Get Double value
if (isEmpty(v))
@@ -351,7 +358,7 @@ public class ValueUtils
* @param defValue the default value
* @return the boolean value or defValue if v is null or empty
*/
- public boolean toBoolean(Object v, boolean defValue)
+ public Boolean toBoolean(Object v, boolean defValue)
{
// Get Boolean value
if (isEmpty(v))
@@ -647,6 +654,60 @@ public class ValueUtils
return dateOnlyFormatter.get().format(date);
}
+ /**
+ * formats a column value into a string
+ * @param column the column
+ * @param value the value to convert
+ * @param local the locale (optional)
+ * @return the corresponding string value
+ */
+ public String formatColumnValue(ColumnExpr column, Object value, Locale
locale)
+ {
+ // check options first
+ Options options = column.getOptions();
+ if (options!=null && options.has(value))
+ { // lookup option
+ return options.get(value);
+ }
+ else if (value instanceof String)
+ { // we already have a string
+ return (String)value;
+ }
+ else if (ObjectUtils.isEmpty(value))
+ { // empty
+ value = column.getAttribute(Column.COLATTR_NULLTEXT);
+ return (value!=null ? value.toString() : StringUtils.EMPTY);
+ }
+ else if ((value instanceof Date) || (value instanceof LocalDate) ||
(value instanceof LocalDateTime))
+ { // format date
+ boolean dateOnly = column.getDataType().equals(DataType.DATE);
+ if (locale==null)
+ { // standard ISO format
+ try {
+ return formatDate(toDate(value), !dateOnly);
+ } catch (ParseException e) {
+ // just use the sting
+ return value.toString();
+ }
+ }
+ else
+ { // format according to Locale
+ if (dateOnly)
+ return DateUtils.formatDate(toLocalDate(value), locale);
+ else
+ return DateUtils.formatDate(toLocalDateTime(value),
locale);
+ }
+ }
+ else if (value instanceof Enum<?>)
+ { // from enum
+ return enumToString((Enum<?>)value);
+ }
+ else
+ { // all other types
+ return StringUtils.valueOf(value);
+ }
+ }
+
/**
* Generic conversion function that will convert a object to another value
type.
* This function is intended to be used for converting values coming from
the database
@@ -695,6 +756,88 @@ public class ValueUtils
return c.cast(v);
}
+ /*
+ * Primitive Wrapper defaults
+ */
+ public static final Integer INTEGER_ZERO = new Integer(0);
+ public static final Long LONG_ZERO = new Long(0l);
+ public static final Double DOUBLE_ZERO = new Double(0.0d);
+
+ /**
+ * Converts a column value to a Java type
+ * Allows custom conversions and handling of special cases.
+ *
+ * For compatibility reasons we must use the old converter functions such
as
+ * toString(), toInteger(), toDecimal(), toDate(), etc.
+ *
+ * @param <T> the type to convert to
+ * @param column the column expression
+ * @param value the object to convert
+ * @param returnType the class type to convert to
+ *
+ * @return the converted value
+ *
+ * @throws ClassCastException if the object is not null and is not
assignable to the type T.
+ */
+ @SuppressWarnings("unchecked")
+ public <T> T convertColumnValue(ColumnExpr column, Object value, Class<T>
rt)
+ throws ClassCastException
+ {
+ // check
+ if (value==ObjectUtils.NO_VALUE)
+ throw new InvalidValueException(value);
+ // use a is
+ if (rt==Object.class)
+ return ((T)value);
+ // convert
+ if (rt==String.class)
+ return (T)toString(value);
+ if (rt==Integer.class)
+ return (T)(value!=null ? toInteger(value) : INTEGER_ZERO);
+ if (rt==Long.class)
+ return (T)(value!=null ? toLong(value) : LONG_ZERO);
+ if (rt==Double.class)
+ return (T)(value!=null ? toDouble(value) : DOUBLE_ZERO);
+ if (rt==BigDecimal.class)
+ return (T)(value!=null ? toDecimal(value) : BigDecimal.ZERO); //
required for compatibility
+ if (rt==Boolean.class)
+ return (T)(value!=null ? toBoolean(value, false) : Boolean.FALSE);
+ // enum
+ if (rt.isEnum())
+ { // Null
+ if (value==null)
+ return null;
+ // convert
+ Class<? extends Enum<?>> enumType = (Class<? extends Enum<?>>)rt;
+ try {
+ // Convert to enum, depending on DataType
+ boolean numeric = column.getDataType().isNumeric();
+ return (T)toEnum(enumType, (numeric ? toInteger(value) :
value));
+
+ } catch (Exception e) {
+ // Illegal value
+ log.error("Unable to resolve enum value of '{}' for type {}",
value, enumType.getName());
+ throw new FieldIllegalValueException(column.getUpdateColumn(),
String.valueOf(value), e);
+ }
+ }
+ // Date conversions
+ try {
+ // DateTimeFormatter.ISO_LOCAL_DATE_TIME
+ if (rt==Date.class)
+ return (T)toDate(value);
+ if (rt==Timestamp.class)
+ return (T)toTimestamp(value);
+ if (rt==LocalDate.class)
+ return (T)toLocalDate(value);
+ if (rt==LocalDateTime.class)
+ return (T)toLocalDateTime(value);
+ } catch (ParseException | DateTimeParseException e) {
+ throw new ValueConversionException(Timestamp.class, value, e);
+ }
+ // something else?
+ return convertToJava(rt, value);
+ }
+
/**
* Converts a value to a specific DataType
* The returned value is used for generating SQL statements
diff --git a/empire-db/src/main/java/org/apache/empire/data/RecordData.java
b/empire-db/src/main/java/org/apache/empire/data/RecordData.java
index 7655d464..a1057791 100644
--- a/empire-db/src/main/java/org/apache/empire/data/RecordData.java
+++ b/empire-db/src/main/java/org/apache/empire/data/RecordData.java
@@ -72,6 +72,22 @@ public interface RecordData
*/
Object get(ColumnExpr column);
+ /**
+ * returns the record value for a particular column
+ * @param index the field index for which to return the value
+ * @param valueType the desired value type
+ * @return the record value for the given column
+ */
+ <V> V get(int index, Class<V> valueType);
+
+ /**
+ * returns the record value for a particular column
+ * @param column the column for which to return the value
+ * @param valueType the desired value type
+ * @return the record value for the given column
+ */
+ <V> V get(ColumnExpr column, Class<V> valueType);
+
/**
* checks if the field at the given index position contains no value
(null)
* Indexed operations provide better performance for bulk processing
compared to isNull(ColumnExpr)
diff --git
a/empire-db/src/main/java/org/apache/empire/data/bean/BeanRecordProxy.java
b/empire-db/src/main/java/org/apache/empire/data/bean/BeanRecordProxy.java
index 520e6ea2..959bc20e 100644
--- a/empire-db/src/main/java/org/apache/empire/data/bean/BeanRecordProxy.java
+++ b/empire-db/src/main/java/org/apache/empire/data/bean/BeanRecordProxy.java
@@ -221,18 +221,31 @@ public class BeanRecordProxy<T> implements Record
}
@Override
- public Object get(ColumnExpr column)
+ public final Object getValue(int index)
+ {
+ return get(getColumn(index));
+ }
+
+ @Override
+ public final <V> V get(int index, Class<V> valueType)
+ {
+ return get(getColumn(index), valueType);
+ }
+
+ @Override
+ public <V> V get(ColumnExpr column, Class<V> valueType)
{
if (!isValid())
throw new ObjectNotValidException(this);
// getBeanPropertyValue
- return getBeanProperty(data, column);
+ Object value = getBeanProperty(data, column);
+ return ObjectUtils.convertColumnValue(column, value, valueType);
}
@Override
- public Object getValue(int index)
+ public final Object get(ColumnExpr column)
{
- return get(getColumn(index));
+ return get(column, Object.class);
}
@Override
@@ -308,6 +321,19 @@ public class BeanRecordProxy<T> implements Record
modified = null;
}
+ /**
+ * Returns the value of a column as a formatted text
+ * This converts the value to a string if necessary and performs an
options lookup
+ * To customize conversion please override convertToString()
+ * @param column the column for which to get the formatted value
+ * @return the formatted value
+ */
+ public final String getText(ColumnExpr column)
+ {
+ Object value = get(column);
+ return formatValue(column, value);
+ }
+
// --------------- Bean support ------------------
public int setBeanProperties(Object bean)
@@ -396,5 +422,16 @@ public class BeanRecordProxy<T> implements Record
log.info("The bean property \"{}\" does not exist on {} and
will be ignored!", property, bean.getClass().getName());
}
}
+
+ /**
+ * Convert a non-string value to a string
+ * @param column the column expression
+ * @param value the value to format
+ * @return the formatted string
+ */
+ protected String formatValue(ColumnExpr column, Object value)
+ {
+ return ObjectUtils.formatColumnValue(column, value, null);
+ }
}
diff --git
a/empire-db/src/main/java/org/apache/empire/data/list/DataListEntry.java
b/empire-db/src/main/java/org/apache/empire/data/list/DataListEntry.java
index d1e4e27a..257c9647 100644
--- a/empire-db/src/main/java/org/apache/empire/data/list/DataListEntry.java
+++ b/empire-db/src/main/java/org/apache/empire/data/list/DataListEntry.java
@@ -28,13 +28,11 @@ import java.util.Date;
import org.apache.empire.commons.BeanPropertyUtils;
import org.apache.empire.commons.ObjectUtils;
-import org.apache.empire.commons.StringUtils;
import org.apache.empire.data.Column;
import org.apache.empire.data.ColumnExpr;
import org.apache.empire.data.EntityType;
import org.apache.empire.data.RecordData;
import org.apache.empire.db.DBRecordBase;
-import org.apache.empire.db.exceptions.FieldIllegalValueException;
import org.apache.empire.db.exceptions.NoPrimaryKeyException;
import org.apache.empire.exceptions.InvalidArgumentException;
import org.apache.empire.exceptions.ItemNotFoundException;
@@ -228,14 +226,27 @@ public class DataListEntry implements RecordData,
Serializable
}
/**
- * Deprecated Renamed to get(...)
- * @param column the column for which to obtain the value
- * @return the record value
+ * returns the record value for a particular column
+ * @param index the field index for which to return the value
+ * @param valueType the desired value type
+ * @return the record value for the given column
+ */
+ @Override
+ public <V> V get(int index, Class<V> valueType)
+ {
+ return ObjectUtils.convertColumnValue(getColumn(index),
getValue(index), valueType);
+ }
+
+ /**
+ * returns the record value for a particular column
+ * @param column the column for which to return the value
+ * @param valueType the desired value type
+ * @return the record value for the given column
*/
- @Deprecated
- public Object getValue(ColumnExpr column)
+ @Override
+ public final <V> V get(ColumnExpr column, Class<V> valueType)
{
- return get(column);
+ return get(getFieldIndex(column), valueType);
}
/**
@@ -247,19 +258,7 @@ public class DataListEntry implements RecordData,
Serializable
@Override
public final Object get(ColumnExpr column)
{
- return getValue(getFieldIndex(column));
- }
-
- /**
- * Returns the value of a field as an object of a given (wrapper)type
- * @param <T> the value type
- * @param column the column for which to retrieve the value
- * @param returnType the type of the returned value
- * @return the value
- */
- public final <T> T get(Column column, Class<T> returnType)
- {
- return ObjectUtils.convert(returnType, get(column));
+ return get(getFieldIndex(column), Object.class);
}
/**
@@ -314,7 +313,8 @@ public class DataListEntry implements RecordData,
Serializable
*/
public final String getString(int index)
{
- return ObjectUtils.getString(getValue(index));
+ // return ObjectUtils.getString(getValue(index));
+ return get(index, String.class);
}
/**
@@ -338,7 +338,9 @@ public class DataListEntry implements RecordData,
Serializable
*/
public final int getInt(int index)
{
- return ObjectUtils.getInteger(getValue(index));
+ // return ObjectUtils.getInteger(getValue(index));
+ Integer value = get(index, Integer.class);
+ return (value!=null ? value : 0) ;
}
/**
@@ -362,7 +364,9 @@ public class DataListEntry implements RecordData,
Serializable
*/
public final long getLong(int index)
{
- return ObjectUtils.getLong(getValue(index));
+ // return ObjectUtils.getLong(getValue(index));
+ Long value = get(index, Long.class);
+ return (value!=null ? value : 0l) ;
}
/**
@@ -386,7 +390,9 @@ public class DataListEntry implements RecordData,
Serializable
*/
public double getDouble(int index)
{
- return ObjectUtils.getDouble(getValue(index));
+ // return ObjectUtils.getDouble(getValue(index));
+ Double value = get(index, Double.class);
+ return (value!=null ? value : 0.0d) ;
}
/**
@@ -410,7 +416,8 @@ public class DataListEntry implements RecordData,
Serializable
*/
public final BigDecimal getDecimal(int index)
{
- return ObjectUtils.getDecimal(getValue(index));
+ // return ObjectUtils.getDecimal(getValue(index));
+ return get(index, BigDecimal.class);
}
/**
@@ -434,7 +441,9 @@ public class DataListEntry implements RecordData,
Serializable
*/
public final boolean getBoolean(int index)
{
- return ObjectUtils.getBoolean(getValue(index));
+ // return ObjectUtils.getBoolean(getValue(index));
+ Boolean value = get(index, Boolean.class);
+ return (value!=null ? value : false);
}
/**
@@ -458,7 +467,8 @@ public class DataListEntry implements RecordData,
Serializable
*/
public final Date getDate(int index)
{
- return ObjectUtils.getDate(getValue(index));
+ // return ObjectUtils.getDate(getValue(index));
+ return get(index, Date.class);
}
/**
@@ -479,7 +489,8 @@ public class DataListEntry implements RecordData,
Serializable
*/
public final Timestamp getTimestamp(int index)
{
- return ObjectUtils.getTimestamp(getValue(index));
+ // return ObjectUtils.getTimestamp(getValue(index));
+ return get(index, Timestamp.class);
}
/**
@@ -500,7 +511,8 @@ public class DataListEntry implements RecordData,
Serializable
*/
public final LocalDate getLocalDate(int index)
{
- return ObjectUtils.getLocalDate(getValue(index));
+ // return ObjectUtils.getLocalDate(getValue(index));
+ return get(index, LocalDate.class);
}
/**
@@ -524,7 +536,8 @@ public class DataListEntry implements RecordData,
Serializable
*/
public final LocalDateTime getLocalDateTime(int index)
{
- return ObjectUtils.getLocalDateTime(getValue(index));
+ // return ObjectUtils.getLocalDateTime(getValue(index));
+ return get(index, LocalDateTime.class);
}
/**
@@ -549,23 +562,9 @@ public class DataListEntry implements RecordData,
Serializable
* @param enumType the enum type class
* @return the enum value
*/
- public <T extends Enum<?>> T getEnum(int index, Class<T> enumType)
- { // check for null
- if (isNull(index))
- return null;
- // convert
- ColumnExpr col = getColumn(index);
- try {
- // Convert to enum, depending on DataType
- boolean numeric = col.getDataType().isNumeric();
- return ObjectUtils.getEnum(enumType, (numeric ? getInt(index) :
getValue(index)));
-
- } catch (Exception e) {
- // Illegal value
- String value = StringUtils.valueOf(getValue(index));
- log.error("Unable to resolve enum value of '{}' for type {}",
value, enumType.getName());
- throw new FieldIllegalValueException(col.getUpdateColumn(), value,
e);
- }
+ public final <T extends Enum<?>> T getEnum(int index, Class<T> enumType)
+ {
+ return get(index, enumType);
}
/**
@@ -621,7 +620,7 @@ public class DataListEntry implements RecordData,
Serializable
}
return values;
}
-
+
/**
* Returns the value of a column as a formatted text
* This converts the value to a string if necessary and performs an
options lookup
@@ -629,10 +628,10 @@ public class DataListEntry implements RecordData,
Serializable
* @param column the column for which to get the formatted value
* @return the formatted value
*/
- public String getText(ColumnExpr col)
+ public String getText(ColumnExpr column)
{
- int idx = getFieldIndex(col);
- return head.getText(idx, values[idx]);
+ int index = getFieldIndex(column);
+ return head.getText(index, values[index]);
}
/**
diff --git
a/empire-db/src/main/java/org/apache/empire/data/list/DataListHead.java
b/empire-db/src/main/java/org/apache/empire/data/list/DataListHead.java
index e4ca8bf4..c9ed6665 100644
--- a/empire-db/src/main/java/org/apache/empire/data/list/DataListHead.java
+++ b/empire-db/src/main/java/org/apache/empire/data/list/DataListHead.java
@@ -21,9 +21,6 @@ package org.apache.empire.data.list;
import java.io.Serializable;
import org.apache.empire.commons.ObjectUtils;
-import org.apache.empire.commons.Options;
-import org.apache.empire.commons.StringUtils;
-import org.apache.empire.data.Column;
import org.apache.empire.data.ColumnExpr;
import org.apache.empire.db.DBDatabase;
import org.apache.empire.db.DBObject;
@@ -100,44 +97,22 @@ public class DataListHead implements Serializable
* @param value the value to format
* @return the formatted value
*/
- public String getText(int index, Object value)
+ public final String getText(int index, Object value)
{ // find text
- String text;
ColumnExpr column = columns[index];
- // check options first
- Options options = column.getOptions();
- if (options!=null && options.has(value))
- { // lookup option
- text = options.get(value);
- }
- else if (value instanceof String)
- { // we already have a string
- text = (String)value;
- }
- else if (ObjectUtils.isEmpty(value))
- { // empty
- value = column.getAttribute(Column.COLATTR_NULLTEXT);
- text = (value!=null ? value.toString() : StringUtils.EMPTY);
- }
- else
- { // format value
- text = formatValue(column, value);
- if (text== null)
- text = StringUtils.EMPTY;
- }
- // Done
- return text;
+ return formatValue(column, value);
}
/**
* Convert a non-string value to a string
+ * Overwrite this function in order to provide a locale
* @param column the column expression
* @param value the value to format
* @return the formatted string
*/
protected String formatValue(ColumnExpr column, Object value)
{
- return ObjectUtils.getString(value);
+ return ObjectUtils.formatColumnValue(column, value, null);
}
}
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java
b/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java
index c5d4004b..944de1a9 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java
@@ -26,13 +26,10 @@ import java.util.Date;
import org.apache.empire.commons.BeanPropertyUtils;
import org.apache.empire.commons.ObjectUtils;
-import org.apache.empire.commons.Options;
-import org.apache.empire.commons.StringUtils;
import org.apache.empire.data.Column;
import org.apache.empire.data.ColumnExpr;
import org.apache.empire.data.RecordData;
import org.apache.empire.db.context.DBContextAware;
-import org.apache.empire.db.exceptions.FieldIllegalValueException;
import org.apache.empire.exceptions.InvalidArgumentException;
import org.apache.empire.exceptions.ItemNotFoundException;
import org.slf4j.Logger;
@@ -80,14 +77,27 @@ public abstract class DBRecordData extends DBObject
public abstract Object getValue(int index);
/**
- * Deprecated Renamed to get(...)
- * @param column the column for which to obtain the value
- * @return the record value
+ * returns the record value for a particular column
+ * @param index the field index for which to return the value
+ * @param valueType the desired value type
+ * @return the record value for the given column
*/
- @Deprecated
- public Object getValue(ColumnExpr column)
+ @Override
+ public <V> V get(int index, Class<V> valueType)
+ {
+ return ObjectUtils.convertColumnValue(getColumn(index),
getValue(index), valueType);
+ }
+
+ /**
+ * returns the record value for a particular column
+ * @param column the column for which to return the value
+ * @param valueType the desired value type
+ * @return the record value for the given column
+ */
+ @Override
+ public final <V> V get(ColumnExpr column, Class<V> valueType)
{
- return get(column);
+ return get(getFieldIndex(column), valueType);
}
/**
@@ -99,19 +109,7 @@ public abstract class DBRecordData extends DBObject
@Override
public final Object get(ColumnExpr column)
{
- return getValue(getFieldIndex(column));
- }
-
- /**
- * Returns the value of a field as an object of a given (wrapper)type
- * @param <T> the value type
- * @param column the column for which to retrieve the value
- * @param returnType the type of the returned value
- * @return the value
- */
- public final <T> T get(Column column, Class<T> returnType)
- {
- return ObjectUtils.convert(returnType, get(column));
+ return get(getFieldIndex(column), Object.class);
}
/**
@@ -166,7 +164,8 @@ public abstract class DBRecordData extends DBObject
*/
public final String getString(int index)
{
- return ObjectUtils.getString(getValue(index));
+ // return ObjectUtils.getString(getValue(index));
+ return get(index, String.class);
}
/**
@@ -190,7 +189,9 @@ public abstract class DBRecordData extends DBObject
*/
public final int getInt(int index)
{
- return ObjectUtils.getInteger(getValue(index));
+ // return ObjectUtils.getInteger(getValue(index));
+ Integer value = get(index, Integer.class);
+ return (value!=null ? value : 0) ;
}
/**
@@ -214,7 +215,9 @@ public abstract class DBRecordData extends DBObject
*/
public final long getLong(int index)
{
- return ObjectUtils.getLong(getValue(index));
+ // return ObjectUtils.getLong(getValue(index));
+ Long value = get(index, Long.class);
+ return (value!=null ? value : 0l) ;
}
/**
@@ -238,7 +241,9 @@ public abstract class DBRecordData extends DBObject
*/
public double getDouble(int index)
{
- return ObjectUtils.getDouble(getValue(index));
+ // return ObjectUtils.getDouble(getValue(index));
+ Double value = get(index, Double.class);
+ return (value!=null ? value : 0.0d) ;
}
/**
@@ -262,7 +267,8 @@ public abstract class DBRecordData extends DBObject
*/
public final BigDecimal getDecimal(int index)
{
- return ObjectUtils.getDecimal(getValue(index));
+ // return ObjectUtils.getDecimal(getValue(index));
+ return get(index, BigDecimal.class);
}
/**
@@ -286,7 +292,9 @@ public abstract class DBRecordData extends DBObject
*/
public final boolean getBoolean(int index)
{
- return ObjectUtils.getBoolean(getValue(index));
+ // return ObjectUtils.getBoolean(getValue(index));
+ Boolean value = get(index, Boolean.class);
+ return (value!=null ? value : false);
}
/**
@@ -310,7 +318,8 @@ public abstract class DBRecordData extends DBObject
*/
public final Date getDate(int index)
{
- return ObjectUtils.getDate(getValue(index));
+ // return ObjectUtils.getDate(getValue(index));
+ return get(index, Date.class);
}
/**
@@ -349,7 +358,8 @@ public abstract class DBRecordData extends DBObject
*/
public final Timestamp getTimestamp(int index)
{
- return ObjectUtils.getTimestamp(getValue(index));
+ // return ObjectUtils.getTimestamp(getValue(index));
+ return get(index, Timestamp.class);
}
/**
@@ -370,7 +380,8 @@ public abstract class DBRecordData extends DBObject
*/
public final LocalDate getLocalDate(int index)
{
- return ObjectUtils.getLocalDate(getValue(index));
+ // return ObjectUtils.getLocalDate(getValue(index));
+ return get(index, LocalDate.class);
}
/**
@@ -394,7 +405,8 @@ public abstract class DBRecordData extends DBObject
*/
public final LocalDateTime getLocalDateTime(int index)
{
- return ObjectUtils.getLocalDateTime(getValue(index));
+ // return ObjectUtils.getLocalDateTime(getValue(index));
+ return get(index, LocalDateTime.class);
}
/**
@@ -419,23 +431,9 @@ public abstract class DBRecordData extends DBObject
* @param enumType the enum type class
* @return the enum value
*/
- public <T extends Enum<?>> T getEnum(int index, Class<T> enumType)
- { // check for null
- if (isNull(index))
- return null;
- // convert
- ColumnExpr col = getColumn(index);
- try {
- // Convert to enum, depending on DataType
- boolean numeric = col.getDataType().isNumeric();
- return ObjectUtils.getEnum(enumType, (numeric ? getInt(index) :
getValue(index)));
-
- } catch (Exception e) {
- // Illegal value
- String value = StringUtils.valueOf(getValue(index));
- log.error("Unable to resolve enum value of '{}' for type {}",
value, enumType.getName());
- throw new FieldIllegalValueException(col.getUpdateColumn(), value,
e);
- }
+ public final <T extends Enum<?>> T getEnum(int index, Class<T> enumType)
+ {
+ return get(index, enumType);
}
/**
@@ -501,31 +499,8 @@ public abstract class DBRecordData extends DBObject
*/
public String getText(ColumnExpr column)
{
- String text;
Object value = get(column);
- // check options first
- Options options = column.getOptions();
- if (options!=null && options.has(value))
- { // lookup option
- text = options.get(value);
- }
- else if (value instanceof String)
- { // we already have a string
- text = (String)value;
- }
- else if (ObjectUtils.isEmpty(value))
- { // empty
- value = column.getAttribute(Column.COLATTR_NULLTEXT);
- text = (value!=null ? value.toString() : StringUtils.EMPTY);
- }
- else
- { // format value
- text = formatValue(column, value);
- if (text== null)
- text = StringUtils.EMPTY;
- }
- // done
- return text;
+ return formatValue(column, value);
}
/**
@@ -599,10 +574,6 @@ public abstract class DBRecordData extends DBObject
}
}
- /*
- * Miscellaneous functions
- */
-
/**
* Convert a non-string value to a string
* @param column the column expression
@@ -611,7 +582,7 @@ public abstract class DBRecordData extends DBObject
*/
protected String formatValue(ColumnExpr column, Object value)
{
- return ObjectUtils.getString(value);
+ return ObjectUtils.formatColumnValue(column, value, null);
}
}