Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.

Sunday, March 8, 2009

Firing direct SQL using Application Module in ADF

There are times when you may need to fire a sql query like for a table clean up in ADF. For that you may not like to create a VO and doing all that standard stuff for your too simple requirement.

Other scenario is you may not have the access to the AM directly to which an entity belongs to. So, in that case you will not be able to use the VOs belonging to the AM for your requirements.

In such cases you can use the following code:
        try{
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExpressionFactory exp = facesContext.getApplication().getExpressionFactory();
        DCBindingContainer bindingContainer = (DCBindingContainer)exp.createValueExpression(facesContext.getELContext(),"#{bindings}",DCBindingContainer.class).getValue(facesContext.getELContext());    
        DCIteratorBinding itr = bindingContainer.findIteratorBinding("Dept1Iterator");   
        DemoAppAMImpl svc = (DemoAppAMImpl)itr.getDataControl().getApplicationModule();
        PreparedStatement stmt = svc.getDBTransaction().createPreparedStatement("delete from dept_table",DBTransaction.DEFAULT);
        stmt.execute();
            svc.getDBTransaction().commit();
        }catch(Exception e){}

This method in this case is written in a managed bean which is bound to a button on the ui. So, first thing that is done is getting the handle to ApplicationModule using iteratorBindings created for this purpose in the pageDef file. 

Then getting the transaction and firing a sql using the transaction and finally commiting it.
  

No comments: