Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

For someone coming from the Java world, the way JCO 2.1 Tables are processed may seem a bit clumsy. Instead of

if (!table.isEmpty()) {

    do {

          process(table);
          // this may possibly fill the screen (if it does one might think about refactoring)

    } while (table.nextRow());

}

it would be more intuitive to have something like for SQL ResultSets. You should not have to check for an empty table and you should be able to use a while-loop instead of do-while:

while (table.next()) {

    process(table);

}

Another convenient way to loop over a Table, Collection, etc. would be using a for-each-loop:

for (Record record : table) {

    process(record)

}

(of course, one needs to be aware that the record is actually the same object as the table but with some "cursor" moved to the next position).

Until someone at SAP decides that this would be a good thing to add to JCO tables, one way to be able to use the for-each-loop style is to create a JCO Table Iterator from a given table (or a given function & table name) and loop over that iterator, as follows:

for (Record record : new JCOTableIterator(table)) {

    process(record)

}

or

for (Record record : new JCOTableIterator(function, tableName)) {

    process(record)

}

Below, you find a simple version of such a JCOTableIterator (it does not care about NullPointerExceptions and so on).

Any comments, ideas, improvements on this code would be highly appreciated.

package com.ascom.sap;

import java.util.Iterator;

import com.sap.mw.jco.JCO;

/**

* An iterator class to iterate over a JCO Table with a for-each-loop.

*/

public class JCOTableIterator implements Iterator<JCO.Table>, Iterable<JCO.Table> {

    private final JCO.Table table;

    private boolean firstRow;

    /**

     * Creates a table iterator for the given function and table name.

     *

     * @param tableName table name

     * @param function function

     */

    public JCOTableIterator(JCO.Function function, String tableName) {

        this(function.getTableParameterList(), tableName);

    }

    /**

     * Creates a table iterator for the given parameter list and table name.

     *

     * @param paramList parameter list

     * @param tableName table name

     */

    public JCOTableIterator(JCO.ParameterList paramList, String tableName) {

        this(paramList.getTable(tableName));

    }

    /**

     * Creates a table iterator for the table.

     *

     * @param table table

     */

    public JCOTableIterator(JCO.Table table) {

        this.table = table;

        table.firstRow();

        // for the first row, next() may not call nextRow()

        firstRow = true;

    }

    @Override

    public void remove() {

        table.deleteRow();

    }

    @Override

    public JCO.Table next() {

        if (firstRow) {

            // do not call nextRow() but remember to do so next time next() is called

            firstRow = false;

        } else {

            table.nextRow();

        }

        return table;

    }

    @Override

    public boolean hasNext() {

        // if there is no row at all, isEmpty() has to be checked

        // if there is exactly one row, isLastRow() is true for the first row; therefore hasNext() should be true if firstRow is true

        // otherwise, isLastRow() works as expected

        return !table.isEmpty() && (!table.isLastRow() || firstRow);

    }

    @Override

    public Iterator<JCO.Table> iterator() {

        return this;

    }

}

5 Comments
Labels in this area