Friday, March 27, 2015

Copy and paste selected rows

Select number of rows in a view as a source (v1) then insert them into other view as target (v2) :
  • At source view xml (v1):
     Create transient attribute "MarkedForInsert", set type to boolean.
  • At application module Impl:
    You need to keep your current iterator not affected, so you need to make a secondary rowSet
Create a secondary row set to not impact the row set
   RowSet selecetdRowSet= vo1.createRowSet("selecetdRowSet");
Set rowset to first row to avoid "attempt to access dead row" exception
  selecetdRowSet.first();
Get all rows that have the transient attribute "MarkedForInsert" set to true
  Row[] selectedRows =
  selecetdRowSet.getFilteredRows("MarkedForInsert", true);
Close the rowSet and clear the selection back to false for further selection next time.
  selectedRowSet.closeRowSet();
  vo1.first().setAttribute("selected", false);
  while(vo1.hasNext()){
  vo1.next().setAttribute("selected", false);
Now you can insert the rows into v2
for(Row row : rows){
Row newRow = vo2.createRow();
vo2.insertRowAtRangeIndex(vo1.getRowCount,row);
The all code :
 public Row [] getSelectedRows(){
       ViewObject vo1 =   getFirstViewObject();
        Row [] selectedRows = null;
       if(vo1.getRowCount()>0){
       RowSet selectedRowSet = vo1.createRowSet("selectedRowSet");
       selectedRowSet.first();
       selectedRows = selectedRowSet.getFilteredRows("selected", true);
        selectedRowSet.closeRowSet();
        vo1.first().setAttribute("selected", false);
        while(vo1.hasNext()){
            vo1.next().setAttribute("selected", false);
        }
       }
       return selectedRows;
    }
    public void insertSelectedRows(){
        ViewObject vo2 = getSecondViewObject();
        Row [] rows = getSelectedRows();
        if(rows!=null){
        for(Row row : rows){
            Row newRow = vo2.createRow();
            vo2.insertRowAtRangeIndex(vo.getRowCount() , newRow);
        }
        }
    }

No comments:

Post a Comment

java - fill distinct objects in ArrayList

If you have a list that contains some objects that it is considered as duplication when two or three fields of these objects are equal. How ...