use of com.servoy.j2db.dataprocessing.ValueFactory.NullValue in project servoy-client by Servoy.
the class Column method getAsRightType.
public static Object getAsRightType(BaseColumnType type, int flags, Object obj, String format, TimeZone timeZone, boolean throwOnFail, boolean truncate) {
if (obj == null)
return null;
if (obj instanceof DbIdentValue || obj instanceof NullValue)
return obj;
// can't do anything else
if (format == null)
return getAsRightType(type, flags, obj, throwOnFail, truncate);
try {
if (obj instanceof String) {
String str = ((String) obj).trim();
ParsePosition pos = new ParsePosition(0);
switch(mapToDefaultType(type)) {
case DATETIME:
SimpleDateFormat dformatter = new SimpleDateFormat(format);
if (timeZone != null) {
dformatter.setTimeZone(timeZone);
}
Date date = dformatter.parse(str, pos);
return getAsRightType(type, flags, date, throwOnFail, false);
case NUMBER:
DecimalFormat nformatter = new DecimalFormat(format);
{
String pos_prefix = nformatter.getPositivePrefix();
// $NON-NLS-1$
if (pos_prefix == null)
pos_prefix = "";
String neg_prefix = nformatter.getNegativePrefix();
// $NON-NLS-1$
if (neg_prefix == null)
neg_prefix = "-";
if (!str.startsWith(pos_prefix) && !str.startsWith(neg_prefix)) {
// $NON-NLS-1$
nformatter.setPositivePrefix("");
// $NON-NLS-1$
nformatter.setNegativePrefix("-");
}
}
{
String pos_suffix = nformatter.getPositiveSuffix();
// $NON-NLS-1$
if (pos_suffix == null)
pos_suffix = "";
String neg_suffix = nformatter.getNegativeSuffix();
// $NON-NLS-1$
if (neg_suffix == null)
neg_suffix = "";
if (!str.endsWith(pos_suffix) && !str.endsWith(neg_suffix)) {
// $NON-NLS-1$
nformatter.setPositiveSuffix("");
// $NON-NLS-1$
nformatter.setNegativeSuffix("");
}
}
return getAsRightType(type, flags, nformatter.parse(str, pos), throwOnFail, false);
case INTEGER:
DecimalFormat iformatter = new DecimalFormat(format);
{
String pos_prefix = iformatter.getPositivePrefix();
// $NON-NLS-1$
if (pos_prefix == null)
pos_prefix = "";
String neg_prefix = iformatter.getNegativePrefix();
// $NON-NLS-1$
if (neg_prefix == null)
neg_prefix = "-";
if (!str.startsWith(pos_prefix) && !str.startsWith(neg_prefix)) {
// $NON-NLS-1$
iformatter.setPositivePrefix("");
// $NON-NLS-1$
iformatter.setNegativePrefix("-");
}
}
{
String pos_suffix = iformatter.getPositiveSuffix();
// $NON-NLS-1$
if (pos_suffix == null)
pos_suffix = "";
String neg_suffix = iformatter.getNegativeSuffix();
// $NON-NLS-1$
if (neg_suffix == null)
neg_suffix = "";
if (!str.endsWith(pos_suffix) && !str.endsWith(neg_suffix)) {
// $NON-NLS-1$
iformatter.setPositiveSuffix("");
// $NON-NLS-1$
iformatter.setNegativeSuffix("");
}
}
return getAsRightType(type, flags, iformatter.parse(str, pos), throwOnFail, false);
case TEXT:
if (type.getLength() > 0 && str.length() >= type.getLength()) {
obj = str.substring(0, type.getLength());
}
return obj;
case MEDIA:
if (obj instanceof byte[]) {
return obj;
}
if (throwOnFail) {
// $NON-NLS-1$
throw new RuntimeException(Messages.getString("servoy.conversion.error.media", new Object[] { obj }));
}
return null;
default:
return obj.toString();
}
} else {
switch(mapToDefaultType(type)) {
case DATETIME:
if (obj instanceof Date) {
return getAsRightType(type, flags, obj, throwOnFail, false);
}
if (obj instanceof Number) {
return getAsRightType(type, flags, new Date(((Number) obj).longValue()), throwOnFail, false);
}
return getAsRightType(type, flags, obj.toString(), format, timeZone, throwOnFail, false);
case NUMBER:
if (obj instanceof Number) {
return obj;
}
return getAsRightType(type, flags, obj.toString(), format, timeZone, throwOnFail, false);
case INTEGER:
if (obj instanceof Number) {
if (obj instanceof Integer || obj instanceof Long) {
return obj;
}
return new Long(((Number) obj).longValue());
}
return getAsRightType(type, flags, obj.toString(), format, timeZone, throwOnFail, false);
case TEXT:
String str = obj.toString();
if (truncate && type.getLength() > 0 && str.length() >= type.getLength()) {
str = str.substring(0, type.getLength());
}
return str;
case MEDIA:
if (obj instanceof byte[]) {
return obj;
}
if (throwOnFail) {
// $NON-NLS-1$
throw new RuntimeException(Messages.getString("servoy.conversion.error.media", new Object[] { obj }));
}
return null;
default:
return obj.toString();
}
}
} catch (RuntimeException e) {
if (throwOnFail)
throw e;
Debug.log(e);
}
return null;
}
Aggregations