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);
        }
        }
    }

Thursday, March 26, 2015

Fast build where clause

To fast your code production it will be a good idea to write the common code to be general one then you can use it when you need without write and test it from beginning.
We want to build dynamic where clause at run-time in a general way.
Building where clause is included in cases like printing  jasper report or making a custom search form.

The basic structure of where clause is :
COLUMN_NAME Operator  Value1

Eg.
EMPLOYEES.EMP_NO = 5
EMPLOYEES.EMP_NAME likes '%Mahmoud%'
EMPLOYEES.HireDate between 1/1/2011 and 1/1/2015

"concatenateWhereClause" is a method that perform previous structure:
 public void concatenateWhereClause(StringBuilder where, String attr, String operator, String... value) {

        if (where.length() > 0) {

            where.append(" and ");

        }

        if (operator.trim().equals("like")) {

            where.append(attr + " like '%" + value[0] + "%'" + " ");

        } else if (operator.trim().equals("between")) {

            where.append(" " + attr + " between " + value[0] + " and " + value[1] + " ");

        } else {

            where.append(" " + attr + " " + operator + " " + value[0] + " ");

        }

    }

and you can call previous method in your search method:
 StringBuilder whereClause;
 public String startSearch() {

        ViewObject vo = ADFUtils.findIterator("VIEW_ITERATOR").getViewObject();

        whereClause = new StringBuilder("");

        if (SEARCH_COMPONENT.getValue() != null && !SEARCH_COMPONENT.getValue().toString().trim().equals("") ) {

            concatenateWhereClause(whereClause, "EMP_NO", "=", SEARCH_COMPONENT.getValue().toString());

        }

Sonar tip for code quality

Collection.isEmpty() should be used to test for emptiness

Using Collection.size() to test for emptiness works, but using Collection.isEmpty() makes the code more readable.
The following code:
if (myCollection.size() == 0) {  // Non-Compliant
  /* ... */
}
should be refactored into:
if (myCollection.isEmpty()) {    // Compliant
  /* ... */
}

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 ...