Friday, June 1, 2012

Getting DB sequence number programmatically in ADF

Using ADF Business Components, you can declaratively base an entity object attribute on a database Sequence. However, you can do the same from Java, using the ADF BC SequenceImpl class, which is a wrapper for a database sequence.
First, open the Application Module editor and create an implementation file.
    public oracle.jbo.domain.Number getDBSequencenumber(String sequenceName){
        try{
        oracle.jbo.server.SequenceImpl theSeq = new oracle.jbo.server.SequenceImpl(sequenceName, getDBTransaction());
        oracle.jbo.domain.Number seqNextNumber = theSeq.getSequenceNumber();
        return seqNextNumber;
        }
        catch (Exception e){
        //handle exceptions
        }
        return null;       
    }
To access the sequence number from Java in the view layer, for example a managed bean, you expose this method on the Application Module client interface. You then create a method binding in the PageDef file used by the view.

Once the binding is created, add the following method to a managed bean to access the sequence next value exposed on the Application Module.
    public oracle.jbo.domain.Number getNextEmployeeSequenceNumber() {
        BindingContext bindingContext = BindingContext.getCurrent();
        BindingContainer bindings = bindingContext.getCurrentBindingsEntry();
        OperationBinding getNextSequence = bindings.getOperationBinding("getDBSequencenumber");
        getNextSequence.getParamsMap().put("sequenceName", "
sequence_Name");
        oracle.jbo.domain.Number seqVal = (oracle.jbo.domain.Number)getNextSequence.execute();
        return seqVal;
    }