Differences between revisions 1 and 2
Revision 1 as of 2014-03-16 11:42:42
Size: 2499
Editor: chandanva
Comment:
Revision 2 as of 2014-04-02 12:06:51
Size: 3533
Editor: chandanva
Comment:
Deletions are marked like this. Additions are marked like this.
Line 18: Line 18:
Following is the pseudo code for handling the '''java.sql.Blob''' during run time Following is the pseudo code for handling the '''java.sql.Blob''' and '''java.sql.Clob''' during run time
Line 20: Line 20:
Before discussing the pseudo code, it is mandatory to add an additional access modifier to the JPA entity that takes in a byte array as parameter. Before discussing the pseudo code, it is mandatory for developers to implement the callback interface org.apache.olingo.odata2.jpa.processor.api.OnJPAWriteContent. The implemented interface needs to be registered with the service via the method '''setOnJPAWriteContent''' method part of '''ODataJPAServiceFactory'''.
Line 24: Line 24:
public void setBlob(Blob blob) {
  this.blob = blob;
/* Callback Implementation */

public class OnDBWriteContent implements OnJPAWriteContent {

  @Override
  public Blob getJPABlob(byte[] binaryData) throws ODataJPARuntimeException {
    try {
      return new JDBCBlob(binaryData);
    } catch (SerialException e) {
      ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
    } catch (SQLException e) {
      ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
    }
    return null;
  }

  @Override
  public Clob getJPAClob(char[] characterData) throws ODataJPARuntimeException {
    try {
      return new JDBCClob(new String(characterData));
    } catch (SQLException e) {
      ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
    }
    return null;
  }
Line 28: Line 51:
public void setBlob(byte[] b) {
  this.blob = new <Provider Specific Blob implementation>(b);

/* Call Back registration in Service Factory */
public class JPAReferenceServiceFactory extends ODataJPAServiceFactory {

  public static final OnJPAWriteContent onDBWriteContent = new OnDBWriteContent();

  @Override
  public ODataJPAContext initializeODataJPAContext()
      throws ODataJPARuntimeException {

    ....
    ........
    ..............

    setOnWriteJPAContent(onDBWriteContent); //Register Call Back

    return oDataJPAContext;
  }
Line 34: Line 73:
OData JPA processor internally does the following
Line 35: Line 75:
  '''Step 1''' - Check if the JPA entity property is of type java.sql.Blob.   '''Step 1''' - Check if the JPA entity property is of type java.sql.Blob or java.sql.Clob.
Line 37: Line 77:
  '''Step 2''' - If Step 1 is true then invoke the method setBlob(byte[].class).   '''Step 2''' - If Step 1 is true then invoke the registered callback method getJPABlob or getJPAClob respectively
Line 39: Line 79:
  '''Step 3''' - If method setBlob(byte[].class) is not defined throw exception.   '''Step 3''' - If callback method is not defined throw exception.
Line 41: Line 81:
Step 3 is needed because the OData library is not bound to any specific implementation of Blob interface.

In case of '''java.sql.Clob''' it is mandatory that an additional access modifier with character array as parameter should be defined.

Example:
{{{
public void setClob(Clob clob) {
  this.clob = clob;
}

public void setClob(char[] c) {
  this.clob = new <Provider Specific Clob implementation>(c);
}

}}}

The pseudo code for setting value for type java.sql.Clob is similar to that of java.sql.Blob.
Step 3 is needed because the OData library is not bound to any specific implementation of Blob or Clob interface.

Handling Blob and Clob data types

Overview

JPA Entities can have properties that are typed as java.sql.Blob or java.sql.Clob. Internally JPA entities can instantiate a JPA provider (Eclipse Link or Hibernate or ...) specific implementation of the above two interfaces and bind them to the properties. Now to enable write on such properties using OData JPA processor an additional access modifier is required to be added to the JPA entities. Following is the proposal on how OData JPA processor handles java.sql.Blob and java.sql.Clob during meta data generation and run time processing.

EDM generation

Following is the pseudo code for generating the EDM when the JPA entity property is typed as java.sql.Blob

  • Step 1 - Check if JPA entity property is of type byte[] or ( of type java.sql.Blob and annotated with @Lob annotation ).

    Step 2 - If Step 1 is true then generate an EDM property with type as Edm.Binary

In case of java.sql.Clob

  • Step 1 - Check if JPA entity property is of type java.sql.Clob and annotated with @Lob annotation.

    Step 2 - If Step 1 is true then generate an EDM property with type as Edm.String with no max length unless a max length is specified.

Run time Processing

Following is the pseudo code for handling the java.sql.Blob and java.sql.Clob during run time

Before discussing the pseudo code, it is mandatory for developers to implement the callback interface org.apache.olingo.odata2.jpa.processor.api.OnJPAWriteContent. The implemented interface needs to be registered with the service via the method setOnJPAWriteContent method part of ODataJPAServiceFactory.

Example

/* Callback Implementation */

public class OnDBWriteContent implements OnJPAWriteContent {

  @Override
  public Blob getJPABlob(byte[] binaryData) throws ODataJPARuntimeException {
    try {
      return new JDBCBlob(binaryData);
    } catch (SerialException e) {
      ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
    } catch (SQLException e) {
      ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
    }
    return null;
  }

  @Override
  public Clob getJPAClob(char[] characterData) throws ODataJPARuntimeException {
    try {
      return new JDBCClob(new String(characterData));
    } catch (SQLException e) {
      ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
    }
    return null;
  }
}


/* Call Back registration in Service Factory */
public class JPAReferenceServiceFactory extends ODataJPAServiceFactory {

  public static final OnJPAWriteContent onDBWriteContent = new OnDBWriteContent();

  @Override
  public ODataJPAContext initializeODataJPAContext()
      throws ODataJPARuntimeException {

    ....
    ........
    ..............

    setOnWriteJPAContent(onDBWriteContent); //Register Call Back

    return oDataJPAContext;
  }
}

OData JPA processor internally does the following

  • Step 1 - Check if the JPA entity property is of type java.sql.Blob or java.sql.Clob.

    Step 2 - If Step 1 is true then invoke the registered callback method getJPABlob or getJPAClob respectively

    Step 3 - If callback method is not defined throw exception.

Step 3 is needed because the OData library is not bound to any specific implementation of Blob or Clob interface.

ojp-handling-blob-proposal (last edited 2014-04-02 12:06:51 by chandanva)