lucia
lucia

Reputation: 21

Error iterating table to get all row and processing some of it in adf

I'm getting error in my project. I am developing with Oracle ADF. Here is the brief :

I have a taskflow, which contains 2 view, browseBusiness (devault view) and chooseBusiness. Then i have a jspx page that contain region from the task flow.

In the browseBusiness, I have 2 buttons, Add and delete. If i press Add, then it'll show chooseBusiness, with a checkbox in one of the column. I'll check some of it, and when i click save, it should iterate to know which row i choose and then save it to the db.

My problem is it failed to iterate while saving Business. Here is my code to save :

       final RichTable table = this.getBisnisTabel();
       final AppModuleImpl appModul =
           (AppModuleImpl)ADFUtil.getApplicationModule("AppModuleDataControl");

       FacesContext facesContext = FacesContext.getCurrentInstance();
       VisitContext visitContext =
           RequestContext.getCurrentInstance().createVisitContext(facesContext, null, EnumSet.of(VisitHint.SKIP_TRANSIENT,
                                                                                                 VisitHint.SKIP_UNRENDERED),
                                                                  null);
       //ERROR IN HERE
       UIXComponent.visitTree(visitContext, facesContext.getViewRoot(), new VisitCallback() {
               public VisitResult visit(VisitContext context, UIComponent target) {
                   if (table != target) {
                       return VisitResult.ACCEPT;
                   } else if (table == target) {
                       //Here goes the Actual Logic
                       //for adding new Business
                       selectAllRowsInTable(table);
                       Iterator selection = table.getSelectedRowKeys().iterator();
                       while (selection.hasNext()) {
                           Object key = selection.next();
                           //store the original key
                           Object origKey = table.getRowKey();
                           try {
                               table.setRowKey(key);
                               Object o = table.getRowData();
                               JUCtrlHierNodeBinding rowData = (JUCtrlHierNodeBinding)o;
                               Row row = rowData.getRow();
                               if (row.getAttribute
                                   ("Selected") != null) {
                                   if ((Boolean)row.getAttribute("Selected"))
                                   {
                                       appModul.saveMTypeOfPolicyGrpBizCode(row.getAttribute("BizCode").toString());
                                       row.setAttribute("Selected", false);
                                   }
                               }
                           } catch (Exception ex) {
                               ex.printStackTrace();
                           } finally {
                               //restore original key
                               table.setRowKey(origKey);
                           }
                       }
                   }
                   return VisitResult.COMPLETE;
               }
           });

This is the code for select All Row in table :

public void selectAllRowsInTable(RichTable rt) {
    RowKeySet rks = new RowKeySetImpl();
    CollectionModel model = (CollectionModel)rt.getValue();
    int rowcount = model.getRowCount();
    for (int i = 0; i < rowcount; i++) {
        model.setRowIndex(i);
        Object key = model.getRowKey();
        rks.add(key);
    }
    rt.setSelectedRowKeys(rks);
}

I am confused, because when i used similar code to deleteBusiness in browseBusiness view, it run very smooth. Here is the code :

            final RichTable table = this.getBizPolicyTable();
        final AppModuleImpl appModul =
            (AppModuleImpl)ADFUtil.getApplicationModule("AppModuleDataControl");

        FacesContext facesContext = FacesContext.getCurrentInstance();
        VisitContext visitContext =
            RequestContext.getCurrentInstance().createVisitContext(facesContext,
                                                                   null,
                                                                   EnumSet.of(VisitHint.SKIP_TRANSIENT,
                                                                              VisitHint.SKIP_UNRENDERED),
                                                                   null);
        //Annonymous call
        UIXComponent.visitTree(visitContext, facesContext.getViewRoot(),
                               new VisitCallback() {
                public VisitResult visit(VisitContext context,
                                         UIComponent target) {
                    if (table != target) {
                        return VisitResult.ACCEPT;
                    } else if (table == target) {
                        //Here goes the Actual Logic
                        //for deleting multiple Business of Policy

                        CollectionModel cm =
                            (CollectionModel)getBizPolicyTable().getValue();
                        RowKeySet rowKeySet =
                            (RowKeySet)getBizPolicyTable().getSelectedRowKeys();
                        Object[] rowKeySetArray = rowKeySet.toArray();
                        for (Object key : rowKeySetArray) {
                            cm.setRowKey(key);
                            //store the original key
                            JUCtrlHierNodeBinding rowData =
                                (JUCtrlHierNodeBinding)cm.getRowData();
                            try {
                                Row row = rowData.getRow();
                                appModul.deleteMTypeOfPolicyGrpBizCode(row);
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            } finally {
                                //restore original key
                            }
                        }
                        appModul.getTypeOfPolicyBizCodeView3().executeQuery();
                        appModul.getTypeOfPolicyBizCodeView1().executeQuery();
                    }
                    return VisitResult.COMPLETE;
                }
            });

Is there anything wrong in my code? Thanks for any feedback :)

UPDATE : try to debug my project. In this line :

Object key = selection.next();

The value is null. I dont know why..

And i'm getting this error : Constraint "TYPE_OF_POLICY_BIZ_CODE_FK1" is violated during post operation "Delete" using SQL statement "DELETE FROM M_BUSSINESS MBussiness WHERE BIZ_CODE=:1".

Here i confused again, because i try to add, not deleting values. Am i doing wrong?

Upvotes: 2

Views: 2995

Answers (2)

Joe
Joe

Reputation: 3347

It appears you are obtaining a reference to the AM directly:

final AppModuleImpl appModul =
           (AppModuleImpl)ADFUtil.getApplicationModule("AppModuleDataControl");

This practice is not recommended. You are violating your Model and View layers and introducing brittleness into your design. A much better and recommended practice is to: 1. Expose the AM method as a service method on the AM 2. Bind this method to your page 3. Invoke the method from your backing bean.

https://blogs.oracle.com/jdevotnharvest/entry/best_practice_invoking_business_services

As for the problem you are having, it would help to know: The version of the product.

Also, it does appear you are deleting here:

 appModul.deleteMTypeOfPolicyGrpBizCode(row);

Yes?

Upvotes: 1

Billy Bob Bain
Billy Bob Bain

Reputation: 2895

Instead of creating a new RowKeySetImpl, try getting the existing one using getSelectedRowKeys(). Or change your logic to loop through all rows and drop the selected row logic altogether. It seems like extra work that isn't necessary.

Upvotes: 0

Related Questions