Sometimes the Task Flow Return component from ADF Task Flow, doesn’t meet our use case requirements and we have to commit or rollback a transaction programmatically. Two cases that you will probably use this approach are:
- Commit or rollback a transaction and stay in the same Task Flow.
- Add some complex code for these operations (Commit and Rollback).
In this post we will commit a transaction programmatically without quit the current page. We will also rollback a transaction programmatically.
Download the sample application: CommitRollbackProgrammaticApp.zip
Create an application according to image below.
Configure the Task Flow.
Double-click on ViewDepartment and create the page.
Drag the DepartmentView1 from Data Controls pane and drop inside ViewDepartment page. Select Table | ADF Read-only Table.
Select Single Row and click on OK.
Include a button after the af:table tag, and configure it.
Go to DepartmentTaskFlow, double-click on EditDepartment and create the page.
Drag the DepartmentView1 from Data Controls pane and drop inside EditDepartment page.
Select Form | ADF Read-only Form, and click on OK.
Include two buttons after the af:panelFormLayout tag, and configure them.
Go to DepartmentTaskFlow, double-click on Commit component and create the DepartmentBean.
Create the commit method too.
Go to DepartmentTaskFlow, double-click on Rollback component and select the DepartmentBean.
Create the rollback method.
Go to DepartmentBean and implement the methods with these code snippets.
public void commit() { BindingContext bc = BindingContext.getCurrent(); String dcfName = bc.getCurrentDataControlFrame(); DataControlFrame dcf = bc.findDataControlFrame(dcfName); dcf.commit(); } public void rollback() { BindingContext bc = BindingContext.getCurrent(); String dcfName = bc.getCurrentDataControlFrame(); DataControlFrame dcf = bc.findDataControlFrame(dcfName); if (dcf.isTransactionDirty()) { dcf.rollback(); } }
Now we have to define the Fixed Outcome attribute from Commit and Rollback components.
Go to DepartmentTaskFlow, select Commit component and change the Fixed Outcome attribute value to commit. After that, select Rollback component and change the Fixed Outcome attribute value to goViewDepartment.
Save all and run the Task Flow.
To test commit operation, select a department and click on Edit.
Make some changes and click on Save.
Click on Back to go to list of departments.
Observe the department that you have edited.
To test rollback operation, select a department and make some changes.
If you just click on Back, the changes are rolled back.