How to deal with BLOBs

From WebRatio WebML Wiki

Jump to: navigation, search


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
  • The file to manage is contained in the "upload" directory or in a folder inside the Web application directory as an accessible resource.
  • The file is stored directly in the Database.
  • The Script Unit or the Custom Unit provides it as output suitable for other WebRatio units using normal links. This is necessary because this class is able to generate a string identifying the BLOB which can be appended to an URL.
com.webratio.rtx.blob.ExternalBLOBData
  • The file to manage is contained in a generic folder that can be outside of the Web application directory.
  • The file is identified by the full path.
com.webratio.rtx.blob.ByteArrayBLOBData
  • The file is available as byte array.


Let's make an example using the Acme Sample Project. Suppose you want to model a page in which the user can select a product image and convert it into the Gray Scale version of the image itself and use that version as the image thumbnail. The model of the page is shown in the following figure. The Script Unit contains the code to make the conversion. In this code you have to handle the BLOB file representing the image in order to create a Gray Scale image and provide it as output suitable for standard WebRatio units (e.g Modify Unit).

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);


Related articles:
Category Difficulty Refers
Custom Unit Guide Developing Custom Units Advanced Custom Unit
Handling BLOBs Presentation Intermediate Style Project
How to deal with BLOBs Developing Custom Units Advanced Custom Unit
How to debug the code of a custom unit inside the Web application Developing Custom Units Advanced Custom Unit
Web Application
How to implement a custom database Metadata provider Data Model Advanced WebRatio
… further results












Did you find this article useful? Please rate it!

Rating: 0.0/5 (0 votes cast)

Personal tools