How to deal with BLOBs
From WebRatio WebML Wiki
|
Developing a custom unit or writing a Groovy script for the Script Unit may imply facing the problem of handling BLOB files.
BLOBs are implemented in the WebRatio Runtime Framework using the RTXBLOBData interface, which permits to handle files. There are particular implementations of this interface that has to be used in different situations. See the following table for detailed information.
| Java Class | Usage |
|---|---|
| com.webratio.rtx.blob.BLOBData |
|
| com.webratio.rtx.blob.ExternalBLOBData |
|
| com.webratio.rtx.blob.ByteArrayBLOBData |
|
Lets' see the code of the Script Unit.
- The unit must have two inputs: the product to modify (OID) and the thumbnail to convert (thumbnail).
- The unit must have two outputs: the product to modify (OID) and the new thumbnail to associate (newThumbnail).
- Instantiate the RTXBLOBData corresponding to the thumbnail. Note that the current thumbnail is passed to the Script Unit as a "String" variable storing the file path. This path is relative to the Web application directory since it is stored using the "File" Storage Type and the "Managed" policy. So, in order to get the RTXBLOBData of the thumbnail just use the following line of code where "thumbnail" is the file path and "rtx" is the runtime manager:
import com.webratio.rtx.RTXBLOBData;
import com.webratio.rtx.blob.BLOBData;
import com.webratio.rtx.blob.ExternalBLOBData;
import com.webratio.rtx.blob.ByteArrayBLOBData;
import .....
RTXBLOBData blobFile = new BLOBData(thumbnail, rtx);
- Write the code to make the thumbnail conversion from RGB to Gray Scale (this is pure Java code). This is a sample
InputStream input = blobFile.openFileInputStream();
BufferedImage image = ImageIO.read(input);
String ext = StringUtils.substringAfterLast(imageName, ".");
def width = image.getWidth(null);
def height = image.getHeight(null);
BufferedImage grayScaleImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = grayScaleImage.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(grayScaleImage, ext.toUpperCase(), os);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
- Instantiate a new RTXBLOBData corresponding to the new thumbnail. Note that since the new thumbnail is contained into an OutputStream object, you must use the ByteArrayBLOBData class in order to perform this operation. This is the resulting code
File originalFile = new File(thumbnail);
def imageName = originalFile.getName();
RTXBLOBData newThumbnail = new ByteArrayBLOBData(imageName, os.toByteArray());
- Write the "return" statement
return ["newThumbnail" : newThumbnail, "OID": OID ]
- Wrap all the code with a try/catch/finally statement in order to catch the exception thrown by some of the used methods
try{
... all the Groovy code ...
} catch (IOException e) {
throw new RTXException(e);
} finally {
IOUtils.closeQuietly(input);
}
Suppose that you want to modify this code in order to be applied when the user is creating a new product. Suppose that you want the user choose an image from the Web and use it as the product thumbnail. This means that the Entry Unit must have an URL field and that the Script Unit must use another implementation of the BLOB file in order to correctly instantiate the RTXBLOBData, which is the ExternalBLOBData class. This is the code
//retrieve the URL written by the user
URL url = new URL(thumbnail)
//create a temp file to store the image
def ext = StringUtils.substringAfterLast(thumbnail, ".")
File tempFile = File.createTempFile("image", "." + ext)
tempFile.deleteOnExit()
//write the image from the URL content
InputStream inputStream = null
OutputStream out = null
try{
inputStream = url.openConnection().getInputStream()
out = new FileOutputStream(tempFile)
byte[] buf = new byte[1024]
int length = 0
while((length = inputStream.read(buf)) > 0 ){
out.write(buf, 0, length)
}
}finally{
IOUtils.closeQuietly(out)
IOUtils.closeQuietly(inputStream)
}
//instantiate the RTXBLOBData
RTXBLOBData blobFile = new ExternalBLOBData("RemoteContent." + ext, tempFile, rtx);
|


