Saturday, March 12, 2016

ADF: Control rows which you want to commit

Problem Description: Let say you have a table in ADF and you want to make sure that even if user edits or create certain rows, they should not get committed but other rows should get committed. Generally when we do commit in adf all rows gets committed together but our requirement is to chose which records should get committed and which one should not.


In this blog I have a hypothetical usecase that all employees rows having firstname=Sanjeev should not get inserted. Its just frozen and no one should be able to modify them.
In ideal case we would like to throw error to end user and let him/her know why certain row can not be committed, but then real world is not ideal and there are chances that we don't want to show error but we silently want to make sure that certain rows (in our example where firstname=Sanjeev) are not getting inserted.

Solution: Its prettey simple. Use doDML method of EOImpl and do not call super.doDML for certain rows. This is doDML operation, which is going to fire actual insert/update/delete operation. If we don't call superclasses doDML operation that those operations will not happen for the row.

Step 1: Create EO/VO/AM based on employee table.

Step 2: Generate EOImpl and override doDML operation as shown below
    protected void doDML(int operation, TransactionEvent e) {
        if(operation == DML_INSERT && "Sanjeev".equals(this.getFirstName())){
            System.out.println("Do not commit employee rows having first name = sanjeev");
        }
        else{
            super.doDML(operation, e);
        }
       
    }

Step 3: Run AM and tester in bc4j tester and show VO in tabular format

a. Create two new rows one having FirstName=Sanjeev and other FirstName=Sanjeev1
 

b. Commit transaction

c. Requery vo

You see there is only one row has got committed.



3 comments:

Unknown said...

Very wonderful article.In this information is getting wonderful reviews from the students .

Muhammad Azwar said...

Nice Article, thanks for this information
Also check this out
Laptops, Mobiles, Games, Tv, Smartwatches etc Daily Tech News Updates.

Unknown said...

For me its not working:

I need to commit some records in an ADF table and not others
protected void doDML(int operation, TransactionEvent e) {
System.out.println("doDML start");
ArrayList selectedList = null;
BigDecimal recordId=null;
BigDecimal currentRecId=null;
//getBigDecimalValue()
int result =0;
try {
if (operation == DML_UPDATE){
System.out.println("recordIdlist:::"+ADFContext.getCurrent().getPageFlowScope().get("selectedRecordList"));
selectedList=(ArrayList)ADFContext.getCurrent().getPageFlowScope().get("selectedRecordList") ;
for(int i=0;i<selectedList.size();i++){
recordId=new BigDecimal((selectedList.get(i)).toString());
System.out.println("recordId:::"+recordId);
currentRecId=new BigDecimal((this.getRecordId()).toString());
System.out.println("currentRecId:::"+recordId);
result=recordId.compareTo(recordId);
if(result==0){
System.out.println("commit happen");
super.doDML(operation, e);
}
}

}else{
System.out.println("else loop");
// super.doDML(operation, e);
}
}catch(Exception ex){
ex.printStackTrace();

}
}