use of javax.sql.rowset.serial.SerialBlob 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);
}
}
use of javax.sql.rowset.serial.SerialBlob in project nutz by nutzam.
the class UpdateTest method test_update_clob.
@Test
public void test_update_clob() throws SerialException, SQLException {
dao.create(UpdateClobBlobBean.class, true);
UpdateClobBlobBean bean = new UpdateClobBlobBean();
bean.setManytext(new SerialClob(Strings.dup('8', 4097).toCharArray()));
bean.setManybinary(new SerialBlob(Strings.dup('9', 4097).getBytes()));
dao.insert(bean);
bean.setManytext(new SerialClob(Strings.dup('7', 4097).toCharArray()));
bean.setManybinary(new SerialBlob(Strings.dup('6', 4097).getBytes()));
dao.update(bean);
}
use of javax.sql.rowset.serial.SerialBlob in project open-kilda by telstra.
the class SamlConversionUtil method toUpdateSamlConfigEntity.
/**
* To update saml config entity.
*
* @param samlConfigEntity the saml config entity
* @param roleEntities the role entities
* @param file the metadata file
* @param name the provider name
* @param url the metadata url
* @param entityId the entityId
* @param status the provider status
* @param userCreation the userCreation
* @param attribute the attribute
* @return the requireManagerUpdateStatus
*/
public static boolean toUpdateSamlConfigEntity(SamlConfigEntity samlConfigEntity, Set<RoleEntity> roleEntities, MultipartFile file, String name, String url, String entityId, boolean status, boolean userCreation, String attribute) {
Blob blob = null;
boolean requireManagerUpdate = false;
try {
if (file != null) {
byte[] bytes = file.getBytes();
try {
blob = new SerialBlob(bytes);
} catch (SerialException e) {
LOGGER.error("Error occurred while updating saml provider" + e);
} catch (SQLException e) {
LOGGER.error("Error occurred while updating saml provider" + e);
}
samlConfigEntity.setMetadata(blob);
samlConfigEntity.setType(IConstants.ProviderType.FILE);
requireManagerUpdate = true;
samlConfigEntity.setUrl(null);
} else if (url != null) {
samlConfigEntity.setUrl(url);
samlConfigEntity.setType(IConstants.ProviderType.URL);
requireManagerUpdate = true;
samlConfigEntity.setMetadata(null);
}
samlConfigEntity.setEntityId(entityId);
if (userCreation) {
samlConfigEntity.setUserCreation(true);
if (!samlConfigEntity.getRoles().isEmpty()) {
samlConfigEntity.getRoles().clear();
}
samlConfigEntity.getRoles().addAll(roleEntities);
} else {
samlConfigEntity.setRoles(null);
}
samlConfigEntity.setName(name);
samlConfigEntity.setUserCreation(userCreation);
samlConfigEntity.setAttribute(attribute);
samlConfigEntity.setStatus(status);
} catch (RequestValidationException e) {
throw new RequestValidationException(e.getMessage());
} catch (FileNotFoundException e) {
LOGGER.error("Error occurred while updating saml provider" + e);
} catch (IOException e) {
LOGGER.error("Error occurred while updating saml provider" + e);
}
return requireManagerUpdate;
}
use of javax.sql.rowset.serial.SerialBlob in project jdk8u_jdk by JetBrains.
the class SQLOutputImplTests method test05.
/*
* Validate a Blob can be written and returned
*/
@Test(enabled = true)
public void test05() throws Exception {
Blob b = new StubBlob();
outImpl.writeBlob(b);
SerialBlob sb = (SerialBlob) results.get(0);
assertTrue(Arrays.equals(b.getBytes(1, (int) b.length()), sb.getBytes(1, (int) sb.length())));
}
use of javax.sql.rowset.serial.SerialBlob in project jdk8u_jdk by JetBrains.
the class SerialBlobTests method test11.
/*
* Validate getBinaryStream returns the correct bytes
*/
@Test
public void test11() throws Exception {
byte[] expected = new byte[] { 1, 2, 3 };
SerialBlob sb = new SerialBlob(bytes);
InputStream is = sb.getBinaryStream(1, 3);
for (byte b : expected) {
byte val = (byte) is.read();
assertTrue(b == val, val + " does not match " + b);
}
}
Aggregations