0

Am trying to implement some Java patterns (Facade, and Iterator) against JCo objects.

Here, I'm trying to create an Iterator over the JCoTable much like I would any collection in Java. Below is my class snippet. In MyClass I have an inner class that performs the iteration.

I'm using the table.getStructure(...) methods, but am missing something. I've seen some snippets where they're explicitly creating a structure using metadata, but honestly, the code is not present in those examples, and I struggle a little there, worrying that I'm trading one complexity for another.

My goal is to simplify SAP Bapi calls and table handling for other Java programmers. This is a refactoring exercise. My return variables are incompatible in the Iterator.

Thank you in advance.

My class:

class MyClass 
...
protected JCoTable table;
...

private class JCoTableIterator implements Iterator<JCoRecord> {

    public boolean hasNext() {
        return table.getRow() < table.getNumRows();
    }
    @Override
    public JCoRecord next() {
        return table.getStructure(table.getRow() + 1);
    }
    @Override
    public void remove() {
        table.deleteRow();
    }
}

My implementation:

Iterator<JCoRecord> it = table.iterator();
while(it.hasNext()) {
    JCoRecord record = it.next(); <<<<< *Exception
    logger.info(record.getString("WERKS"));
    ... other field procesing ... 
}  

But on the JCoRecord record = it.next() line I get the following error.

com.sap.conn.jco.ConversionException: (122) JCO_ERROR_CONVERSION: Cannot convert field VBELN of type CHAR to StructureRecord
    at com.sap.conn.jco.rt.AbstractRecord.createConversionException(AbstractRecord.java:415)
    at com.sap.conn.jco.rt.AbstractRecord.createConversionException(AbstractRecord.java:410)
    at com.sap.conn.jco.rt.AbstractRecord.getStructure(AbstractRecord.java:2472)    
Davidson
  • 1,032
  • 2
  • 17
  • 32
  • Could you please specify exactly which version of the JCo you're using? There are huge API differences between version 2 and 3, maybe that's why you're confused by various examples that don't work. – vwegert Apr 23 '13 at 06:46
  • @vwegert, I'm using JCo 3. I presume that because I use the sapjco3.dll and sapjco3.jar. – Davidson Apr 23 '13 at 14:01

1 Answers1

1

Assuming that you want to iterate over the rows of the table, I'd say you're using JCoTable.getStructure() out of place. As the name suggests, it takes the nth field of the record that is currently selected and attempts to return it as a structure. This is of course only possible if that individual column is a structured column - if it is a single (atomic) field, then I'd expect this conversion to fail with exactly the error message you're encountering.

vwegert
  • 18,093
  • 3
  • 33
  • 54
  • I realized after further looking at the API, that I really didn't need to write my own Iterator... The methods off JCoTable are sufficient with .nextRow(), .firstRow(), etc.... – Davidson Apr 23 '13 at 20:51
  • To be honest, I was wondering why you tried to reinvent the wheel in the first place... ;-) – vwegert Apr 24 '13 at 06:04
  • Sometimes things don't click with me right away--old age, I think. I'm the primary/sole developer here with a couple others who are new to Java but eager. So, I was trying to simplify the interfaces somewhat. We are a new SAP shop (1 year). I'm still learning the RFC/JCo stuff myself. I appreciate the suppressed laughter! :) – Davidson Apr 24 '13 at 15:27