/** * Transfers the contents of a {@link JCoTable} into a typed list. * * @param source the {@link JCoTable} to read the data from * @return a new {@link List} instance containing the data from the table */ public static List<TransportShortText> fromTable(JCoTable source) { List<TransportShortText> list = new ArrayList<TransportShortText>(); if (!source.isEmpty()) { source.firstRow(); do { list.add(new TransportShortText(source)); } while (source.nextRow()); } return list; }
/** * A slightly more complex example than before. Query the companies list returned in a table and * then obtain more details for each company. * * @throws com.sap.conn.jco.JCoException */ public static void step4WorkWithTable() throws JCoException { JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED); JCoFunction function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETLIST"); if (function == null) throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP."); try { function.execute(destination); } catch (AbapException e) { System.out.println(e.toString()); return; } JCoStructure returnStructure = function.getExportParameterList().getStructure("RETURN"); if (!(returnStructure.getString("TYPE").equals("") || returnStructure.getString("TYPE").equals("S"))) { throw new RuntimeException(returnStructure.getString("MESSAGE")); } JCoTable codes = function.getTableParameterList().getTable("COMPANYCODE_LIST"); for (int i = 0; i < codes.getNumRows(); i++) { codes.setRow(i); System.out.println(codes.getString("COMP_CODE") + '\t' + codes.getString("COMP_NAME")); } // move the table cursor to first row codes.firstRow(); for (int i = 0; i < codes.getNumRows(); i++, codes.nextRow()) { function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETDETAIL"); if (function == null) throw new RuntimeException("BAPI_COMPANYCODE_GETDETAIL not found in SAP."); function.getImportParameterList().setValue("COMPANYCODEID", codes.getString("COMP_CODE")); // We do not need the addresses, so set the corresponding parameter to inactive. // Inactive parameters will be either not generated or at least converted. function.getExportParameterList().setActive("COMPANYCODE_ADDRESS", false); try { function.execute(destination); } catch (AbapException e) { System.out.println(e.toString()); return; } returnStructure = function.getExportParameterList().getStructure("RETURN"); if (!(returnStructure.getString("TYPE").equals("") || returnStructure.getString("TYPE").equals("S") || returnStructure.getString("TYPE").equals("W"))) { throw new RuntimeException(returnStructure.getString("MESSAGE")); } JCoStructure detail = function.getExportParameterList().getStructure("COMPANYCODE_DETAIL"); System.out.println( detail.getString("COMP_CODE") + '\t' + detail.getString("COUNTRY") + '\t' + detail.getString("CITY")); } // for }