setValue(sqlP, modelField, entity.getEntityName(), fieldValue, modelFieldTypeReader);
}
public static <T> void setValue(SQLProcessor sqlP, ModelField modelField, String entityName, Object fieldValue, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException {
ModelFieldType mft = modelFieldTypeReader.getModelFieldType(modelField.getType());
if (mft == null) {
throw new GenericModelException("GenericDAO.getValue: definition fieldType " + modelField.getType() + " not found, cannot setValue for field " +
entityName + "." + modelField.getName() + ".");
}
// if the value is the GenericEntity.NullField, treat as null
if (fieldValue == GenericEntity.NULL_FIELD) {
fieldValue = null;
}
// ----- Try out the new handler code -----
JdbcValueHandler<T> handler = UtilGenerics.cast(mft.getJdbcValueHandler());
if (handler != null) {
try {
sqlP.setValue(handler, handler.getJavaClass().cast(fieldValue));
return;
} catch (SQLException e) {
throw new GenericDataSourceException("SQL Exception while setting value on field [" + modelField.getName() + "] of entity " + entityName + ": ", e);
}
} else {
Debug.logWarning("JdbcValueHandler not found for java-type " + mft.getJavaType() +
", falling back on switch statement. Entity = " +
modelField.getModelEntity().getEntityName() +
", field = " + modelField.getName() + ".", module);
}
// ------------------------------------------
String fieldType = mft.getJavaType();
if (fieldValue != null) {
if (!ObjectType.instanceOf(fieldValue, fieldType)) {
// this is only an info level message because under normal operation for most JDBC
// drivers this will be okay, but if not then the JDBC driver will throw an exception
// and when lower debug levels are on this should help give more info on what happened
String fieldClassName = fieldValue.getClass().getName();
if (fieldValue instanceof byte[]) {
fieldClassName = "byte[]";
}
if (Debug.verboseOn()) Debug.logVerbose("type of field " + entityName + "." + modelField.getName() +
" is " + fieldClassName + ", was expecting " + mft.getJavaType() + "; this may " +
"indicate an error in the configuration or in the class, and may result " +
"in an SQL-Java data conversion error. Will use the real field type: " +
fieldClassName + ", not the definition.", module);
fieldType = fieldClassName;
}