24 May, 2012

ADF : Dynamic View Object


Today I want to write about dynamic view object which allow me to change its data source (SQL query) and attributes at run time.

I will use oracle.jbo.ApplicationModule::createViewObjectFromQueryStmt method to do this issue.

I will present how to do this step by step


Create View Object and Application Module
1-  Right click on Model project and choose New


2- Choose from left pane "ADF Business Component" , then from list choose  "View Object" and click "OK" button


3-  Enter "DynamicVO" in "Name" and choose "Sql Query" radio button and click "Next" button.


4-  Write in Select field "select * from dual" and click "Next" button until reach Window "Step 8 of 9"


5- Check "Add to Application Module" check box and click "Finish" button.


Implement Changes in Application Module
1- Open application module "AppModule", then open Java tab and check "Generate Application Module Class AppModuleImpl" check box


2- Open AppModuleImpl.java Class and Add the below method for dynamic view object

   public void changeDynamicVoQuery(String sqlStatement) {  
     ViewObject dynamicVO = this.findViewObject("DynamicVO1");  
     dynamicVO.remove();  
     dynamicVO = this.createViewObjectFromQueryStmt("DynamicVO1", sqlStatement);  
     dynamicVO.executeQuery();  
   }  

3- Open "AppModule" then open Java tab and Add changeDynamicVoQuery method to Client Interface



Test Business Component
1- Right click on AppModue in Application navigator and choose Run from drop down list.


2- Right click on AppModule in left pane and choose Show from drop down lsit
     Write "Select * from Emp" in sqlStatement parameter
      Click on Execute button, The result will be Success .


3- Click double click on DynamicVO1 in left pane, it will display the data of DynamicVO and display data which I entered "Select * from Emp" before not "Select * from dual" that was used in design time of view object.


To use dynamic view objects in ADF Faces, you should use ADF Dynamic Table or ADF Dynamic Form.

You can download sample application from here

Thanks
Mahmoud A. El-Sayed

7 comments:

  1. Nice post. It's worth warning programmers of the potential of SQL Injection attacks with this method.

    CM.

    ReplyDelete
    Replies
    1. Really it is big hole of SQL injection and also good solution of generic programming.
      So programmers must take care when using like this methods

      Delete
  2. Nice article. Thanks for writing - Javapins.com

    ReplyDelete
  3. Thanks, Its helpful.
    the main challenge of business is to make applications as dynamic but not tooo much

    ReplyDelete
  4. Hi Mahmoud, Can you please provide me the steps for creating Dynamic table in ADF faces?

    ReplyDelete
  5. I have a problem when a drag dynamic view to page as dynamic table, after I trigger
    changeDynamicVoQuery the table is always empty with dynamic form works no problem

    ReplyDelete

ADF : Scope Variables

Oracle ADF uses many variables and each variable has a scope. There are five scopes in ADF (Application, Request, Session, View and PageFl...