天天看點

ADF常見的代碼陷阱(一)

對于ADF沒了解原理的人在很多常用的功能上寫的代碼都是有陷阱的。昨晚寫了幾個提醒一下大家,原因嘛其實都在我前面的文章提到了。

/**
     * 重寫Table定位行
     * @param selectionEvent
     */
    public void overCurrentRow(SelectionEvent selectionEvent) {
        // Add event code here...
        RichTable rt = (RichTable)selectionEvent.getSource();
        CollectionModel cm = (CollectionModel)rt.getValue();
        JUCtrlHierBinding jub = (JUCtrlHierBinding)cm.getWrappedData();
        JUCtrlHierNodeBinding junodebing =
            (JUCtrlHierNodeBinding)rt.getSelectedRowData();
        Key key = junodebing.getRowKey();
        jub.getIteratorBinding().setCurrentRowWithKey(key.toStringFormat(true));
    }
    /**
     * 建立Row(通過Binding層)
     * @return
     */
    public String createRowByBinding() {
        // Add event code here...
        /**
         * 錯誤的做法:
         * 
         */
        
        ServiceModuleImpl serviceModuleImpl = (ServiceModuleImpl)ADFUtils.getApplicationModuleForDataControl("DataControl名稱");
        ViewObjectImpl vo=serviceModuleImpl.getJobsView1();
        vo.insertRow(vo.createRow());
        /**
         * 正确的做法
         */
        DCIteratorBinding dciteratorbind =
            ADFUtils.findIterator("JobsView1Iterator");
        RowSetIterator rsiterator = dciteratorbind.getRowSetIterator();
        Row newRow = rsiterator.createRow();
        newRow.setNewRowState(Row.STATUS_INITIALIZED);
        rsiterator.insertRow(newRow);
        dciteratorbind.setCurrentRowWithKey(newRow.getKey().toStringFormat(true));
        return null;
    }
    
    /**
     * 建立Row(通過RichTable,比通過Binding層更好)
     * @return
     */
    public String createRowByRichTable() {
        // Add event code here...
        CollectionModel cm = (CollectionModel)this.getRt1().getValue();
        JUCtrlHierBinding jubind = (JUCtrlHierBinding)cm.getWrappedData();
        
        DCIteratorBinding dciteratorbind =jubind.getDCIteratorBinding();
        RowSetIterator rsiterator = dciteratorbind.getRowSetIterator();
        Row newRow = rsiterator.createRow();
        newRow.setNewRowState(Row.STATUS_INITIALIZED);
        rsiterator.insertRow(newRow);
        dciteratorbind.setCurrentRowWithKey(newRow.getKey().toStringFormat(true));
        return null;
    }
    
    /**
     * 重新查詢VO
     * @return
     */
    public String queryTableVO(){
        /**
         * 錯誤的做法:
         */
        ServiceModuleImpl serviceModuleImpl = (ServiceModuleImpl)ADFUtils.getApplicationModuleForDataControl("DataControl名稱");
        ViewObjectImpl vo=serviceModuleImpl.getJobsView1();
        vo.executeQuery();
        
        /**
         * 正确的做法
         */
        DCIteratorBinding dciteratorbind =
            ADFUtils.findIterator("JobsView1Iterator");
        dciteratorbind.executeQuery();
        
        
        return null;
    }