Search in sources :

Example 1 with ModelFieldTypeReader

use of org.apache.ofbiz.entity.model.ModelFieldTypeReader in project ofbiz-framework by apache.

the class GenericDelegator method getModelFieldTypeReader.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#getModelFieldTypeReader(org.apache.ofbiz.entity.model.ModelEntity)
     */
@Override
public ModelFieldTypeReader getModelFieldTypeReader(ModelEntity entity) {
    String helperName = getEntityHelperName(entity);
    if (UtilValidate.isEmpty(helperName)) {
        return null;
    }
    ModelFieldTypeReader modelFieldTypeReader = ModelFieldTypeReader.getModelFieldTypeReader(helperName);
    if (modelFieldTypeReader == null) {
        throw new IllegalArgumentException("ModelFieldTypeReader not found for entity " + entity.getEntityName() + " with helper name " + helperName);
    }
    return modelFieldTypeReader;
}
Also used : ModelFieldTypeReader(org.apache.ofbiz.entity.model.ModelFieldTypeReader)

Example 2 with ModelFieldTypeReader

use of org.apache.ofbiz.entity.model.ModelFieldTypeReader in project ofbiz-framework by apache.

the class SqlJdbcUtil method getValue.

public static void getValue(ResultSet rs, int ind, ModelField curField, GenericEntity entity, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException {
    ModelFieldType mft = modelFieldTypeReader.getModelFieldType(curField.getType());
    if (mft == null) {
        throw new GenericModelException("definition fieldType " + curField.getType() + " not found, cannot getValue for field " + entity.getEntityName() + "." + curField.getName() + ".");
    }
    ModelEntity model = entity.getModelEntity();
    String encryptionKeyName = entity.getEntityName();
    if (curField.getEncryptMethod().isEncrypted() && model instanceof ModelViewEntity) {
        ModelViewEntity modelView = (ModelViewEntity) model;
        encryptionKeyName = modelView.getAliasedEntity(modelView.getAlias(curField.getName()).getEntityAlias(), entity.getDelegator().getModelReader()).getEntityName();
    }
    // ----- Try out the new handler code -----
    JdbcValueHandler<?> handler = mft.getJdbcValueHandler();
    if (handler != null) {
        try {
            Object jdbcValue = handler.getValue(rs, ind);
            if (jdbcValue instanceof String && curField.getEncryptMethod().isEncrypted()) {
                jdbcValue = entity.getDelegator().decryptFieldValue(encryptionKeyName, curField.getEncryptMethod(), (String) jdbcValue);
            }
            entity.dangerousSetNoCheckButFast(curField, jdbcValue);
            return;
        } catch (Exception e) {
            Debug.logError(e, module);
        }
    } else {
        Debug.logWarning("JdbcValueHandler not found for java-type " + mft.getJavaType() + ", falling back on switch statement. Entity = " + curField.getModelEntity().getEntityName() + ", field = " + curField.getName() + ".", module);
    }
    // ------------------------------------------
    String fieldType = mft.getJavaType();
    try {
        // checking to see if the object is null is really only necessary for the numbers
        int typeValue = getType(fieldType);
        ResultSetMetaData rsmd = rs.getMetaData();
        int colType = rsmd.getColumnType(ind);
        if (typeValue <= 4 || typeValue >= 11) {
            switch(typeValue) {
                case 1:
                    if (java.sql.Types.CLOB == colType) {
                        // Debug.logInfo("For field " + curField.getName() + " of entity " + entity.getEntityName() + " getString is a CLOB, trying getCharacterStream", module);
                        // if the String is empty, try to get a text input stream, this is required for some databases for larger fields, like CLOBs
                        Clob valueClob = rs.getClob(ind);
                        Reader valueReader = null;
                        if (valueClob != null) {
                            valueReader = valueClob.getCharacterStream();
                        }
                        // Reader valueReader = rs.getCharacterStream(ind);
                        if (valueReader != null) {
                            char[] inCharBuffer = new char[CHAR_BUFFER_SIZE];
                            StringBuilder strBuf = new StringBuilder();
                            int charsRead = 0;
                            try {
                                while ((charsRead = valueReader.read(inCharBuffer, 0, CHAR_BUFFER_SIZE)) > 0) {
                                    strBuf.append(inCharBuffer, 0, charsRead);
                                }
                                valueReader.close();
                            } catch (IOException e) {
                                throw new GenericEntityException("Error reading long character stream for field " + curField.getName() + " of entity " + entity.getEntityName(), e);
                            }
                            entity.dangerousSetNoCheckButFast(curField, strBuf.toString());
                        } else {
                            entity.dangerousSetNoCheckButFast(curField, null);
                        }
                    } else {
                        String value = rs.getString(ind);
                        if (curField.getEncryptMethod().isEncrypted()) {
                            value = (String) entity.getDelegator().decryptFieldValue(encryptionKeyName, curField.getEncryptMethod(), value);
                        }
                        entity.dangerousSetNoCheckButFast(curField, value);
                    }
                    break;
                case 2:
                    entity.dangerousSetNoCheckButFast(curField, rs.getTimestamp(ind));
                    break;
                case 3:
                    entity.dangerousSetNoCheckButFast(curField, rs.getTime(ind));
                    break;
                case 4:
                    entity.dangerousSetNoCheckButFast(curField, rs.getDate(ind));
                    break;
                case 11:
                    Object obj = null;
                    byte[] originalBytes = rs.getBytes(ind);
                    obj = deserializeField(originalBytes, ind, curField);
                    if (obj != null) {
                        entity.dangerousSetNoCheckButFast(curField, obj);
                    } else {
                        entity.dangerousSetNoCheckButFast(curField, originalBytes);
                    }
                    break;
                case 12:
                    Object originalObject;
                    byte[] fieldBytes;
                    try {
                        Blob theBlob = rs.getBlob(ind);
                        fieldBytes = theBlob != null ? theBlob.getBytes(1, (int) theBlob.length()) : null;
                        originalObject = theBlob;
                    } catch (SQLException e) {
                        // for backward compatibility if getBlob didn't work try getBytes
                        fieldBytes = rs.getBytes(ind);
                        originalObject = fieldBytes;
                    }
                    if (originalObject != null) {
                        // for backward compatibility, check to see if there is a serialized object and if so return that
                        Object blobObject = deserializeField(fieldBytes, ind, curField);
                        if (blobObject != null) {
                            entity.dangerousSetNoCheckButFast(curField, blobObject);
                        } else {
                            if (originalObject instanceof Blob) {
                                // NOTE using SerialBlob here instead of the Blob from the database to make sure we can pass it around, serialize it, etc
                                entity.dangerousSetNoCheckButFast(curField, new SerialBlob((Blob) originalObject));
                            } else {
                                entity.dangerousSetNoCheckButFast(curField, originalObject);
                            }
                        }
                    }
                    break;
                case 13:
                    entity.dangerousSetNoCheckButFast(curField, new SerialClob(rs.getClob(ind)));
                    break;
                case 14:
                case 15:
                    entity.dangerousSetNoCheckButFast(curField, rs.getObject(ind));
                    break;
            }
        } else {
            switch(typeValue) {
                case 5:
                    int intValue = rs.getInt(ind);
                    if (rs.wasNull()) {
                        entity.dangerousSetNoCheckButFast(curField, null);
                    } else {
                        entity.dangerousSetNoCheckButFast(curField, Integer.valueOf(intValue));
                    }
                    break;
                case 6:
                    long longValue = rs.getLong(ind);
                    if (rs.wasNull()) {
                        entity.dangerousSetNoCheckButFast(curField, null);
                    } else {
                        entity.dangerousSetNoCheckButFast(curField, Long.valueOf(longValue));
                    }
                    break;
                case 7:
                    float floatValue = rs.getFloat(ind);
                    if (rs.wasNull()) {
                        entity.dangerousSetNoCheckButFast(curField, null);
                    } else {
                        entity.dangerousSetNoCheckButFast(curField, Float.valueOf(floatValue));
                    }
                    break;
                case 8:
                    double doubleValue = rs.getDouble(ind);
                    if (rs.wasNull()) {
                        entity.dangerousSetNoCheckButFast(curField, null);
                    } else {
                        entity.dangerousSetNoCheckButFast(curField, Double.valueOf(doubleValue));
                    }
                    break;
                case 9:
                    BigDecimal bigDecimalValue = rs.getBigDecimal(ind);
                    if (rs.wasNull()) {
                        entity.dangerousSetNoCheckButFast(curField, null);
                    } else {
                        entity.dangerousSetNoCheckButFast(curField, bigDecimalValue);
                    }
                    break;
                case 10:
                    boolean booleanValue = rs.getBoolean(ind);
                    if (rs.wasNull()) {
                        entity.dangerousSetNoCheckButFast(curField, null);
                    } else {
                        entity.dangerousSetNoCheckButFast(curField, Boolean.valueOf(booleanValue));
                    }
                    break;
            }
        }
    } catch (SQLException sqle) {
        throw new GenericDataSourceException("SQL Exception while getting value : " + curField.getName() + " [" + curField.getColName() + "] (" + ind + ")", sqle);
    }
}
Also used : GenericModelException(org.apache.ofbiz.entity.GenericModelException) SQLException(java.sql.SQLException) ModelFieldTypeReader(org.apache.ofbiz.entity.model.ModelFieldTypeReader) Reader(java.io.Reader) SerialBlob(javax.sql.rowset.serial.SerialBlob) ResultSetMetaData(java.sql.ResultSetMetaData) ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) GenericDataSourceException(org.apache.ofbiz.entity.GenericDataSourceException) SerialBlob(javax.sql.rowset.serial.SerialBlob) Blob(java.sql.Blob) IOException(java.io.IOException) SerialClob(javax.sql.rowset.serial.SerialClob) SQLException(java.sql.SQLException) GenericDataSourceException(org.apache.ofbiz.entity.GenericDataSourceException) GenericNotImplementedException(org.apache.ofbiz.entity.GenericNotImplementedException) GenericModelException(org.apache.ofbiz.entity.GenericModelException) IOException(java.io.IOException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) BigDecimal(java.math.BigDecimal) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelFieldType(org.apache.ofbiz.entity.model.ModelFieldType) Clob(java.sql.Clob) SerialClob(javax.sql.rowset.serial.SerialClob)

Example 3 with ModelFieldTypeReader

use of org.apache.ofbiz.entity.model.ModelFieldTypeReader in project ofbiz-framework by apache.

the class GenericDelegator method getEntityFieldTypeNames.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#getEntityFieldTypeNames(org.apache.ofbiz.entity.model.ModelEntity)
     */
@Override
public Collection<String> getEntityFieldTypeNames(ModelEntity entity) throws GenericEntityException {
    String helperName = getEntityHelperName(entity);
    if (UtilValidate.isEmpty(helperName)) {
        return null;
    }
    ModelFieldTypeReader modelFieldTypeReader = ModelFieldTypeReader.getModelFieldTypeReader(helperName);
    if (modelFieldTypeReader == null) {
        throw new GenericEntityException("ModelFieldTypeReader not found for entity " + entity.getEntityName() + " with helper name " + helperName);
    }
    return modelFieldTypeReader.getFieldTypeNames();
}
Also used : ModelFieldTypeReader(org.apache.ofbiz.entity.model.ModelFieldTypeReader)

Aggregations

ModelFieldTypeReader (org.apache.ofbiz.entity.model.ModelFieldTypeReader)3 IOException (java.io.IOException)1 Reader (java.io.Reader)1 BigDecimal (java.math.BigDecimal)1 Blob (java.sql.Blob)1 Clob (java.sql.Clob)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 SerialBlob (javax.sql.rowset.serial.SerialBlob)1 SerialClob (javax.sql.rowset.serial.SerialClob)1 GenericDataSourceException (org.apache.ofbiz.entity.GenericDataSourceException)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1 GenericModelException (org.apache.ofbiz.entity.GenericModelException)1 GenericNotImplementedException (org.apache.ofbiz.entity.GenericNotImplementedException)1 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)1 ModelFieldType (org.apache.ofbiz.entity.model.ModelFieldType)1 ModelViewEntity (org.apache.ofbiz.entity.model.ModelViewEntity)1