Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Perform an RFC and display the result in a JTable
- // Using some groovy tricks, the notational overhead is minimized
- import com.sap.mw.jco.*
- import org.junit.*
- // For visual representation of the result
- import java.awt.BorderLayout;
- import javax.swing.*;
- import javax.swing.table.*;
- import groovy.swing.SwingBuilder;
- class TestJcoDataHandling {
- Repository repo;
- @Test
- void test() {
- // Provide handle to a remotely callable function
- def userList = repo.getFunction "BAPI_USER_GETLIST"
- // Parametrize it
- userList.importParameter.set(
- MAX_ROWS : 50,
- WITH_USERNAME : "X"
- )
- // Do the call
- userList.execute( )
- // Get and display result
- display userList.tableParameter.USERLIST
- }
- @Before
- void setup() {
- // Replace with your login data instead
- // see http://help.sap.com/javadocs/NW04/current/jc/com/sap/mw/jco/JCO.html
- def sys = new Properties()
- sys.load( new FileInputStream( "h:\\Eigene Dateien\\groovy\\jco\\d12.properties" ) )
- def conn = JCO.createClient( sys )
- conn.connect()
- repo = new Repository( new JCO.Repository( "repo", conn ), conn )
- }
- @After
- void teardown() {
- repo.disconnect()
- }
- // Show a JTable with the result
- void display( userlist ) {
- def data = []
- for (record in userlist) {
- data.add( [ record.USERNAME, record.FULLNAME ] )
- }
- new GUI().showTable( data, ["Benutzername","Bürgerlicher Name"] )
- }
- }
- // In this wrapper, we trap the member operator "." and subscript operator "[]"
- // mixing in the informations from JCo.MetaData calls
- class Data implements Iterator {
- private JCO.Record record
- Data( JCO.Record record) {
- this.record = record;
- }
- def toString = {
- record.getValue()
- }
- def getProperty(String name) {
- if (name == "length") return record.getNumRows()
- if (name == "record") return record
- def f = record.getValue( name )
- if (record.isTable( name ) || record.isStructure( name ))
- return new Data( f )
- else {
- return f
- }
- }
- void setProperty(String name, Object value) {
- record.getField( name ).setValue( value );
- }
- def set = { pairs ->
- for (p in pairs) {
- setProperty p.key, p.value
- }
- }
- def getAt(i) {
- record.setRow( i );
- // Danach erfolgt ein Komponetenzugriff
- this
- }
- // Iterator interface
- void remove() {}
- boolean hasNext() {
- return !record.isLastRow()
- }
- def next() {
- record.nextRow()
- this
- }
- }
- // Adapter for JCo Repository
- class Repository {
- @Delegate JCO.Repository repo
- JCO.Connection conn
- Repository( JCO.Repository repo, JCO.Connection conn) {
- this.repo = repo
- this.conn = conn
- }
- def getFunction = { name ->
- new Function( repo.getFunctionTemplate( name ).getFunction(), this )
- }
- def disconnect = {
- conn.disconnect( )
- }
- }
- // Adapter for JCO.Function
- class Function {
- @Delegate JCO.Function function
- Repository repo
- private Data importParameter, exportParameter, tableParameter
- Function( JCO.Function function, Repository repo ) {
- this.function = function
- this.repo = repo
- this.importParameter = new Data( function.importParameterList )
- this.exportParameter = new Data( function.exportParameterList )
- this.tableParameter = new Data( function.tableParameterList )
- }
- def execute = {
- repo.conn.execute(function)
- }
- }
- // A small GUI for presentation purposes
- class GUI {
- SwingBuilder builder
- GUI() {
- builder = new SwingBuilder()
- }
- def showTable = { data, columns ->
- JTable table = new JTable((String[][]) data.toArray(),
- (String[]) columns.toArray() )
- JScrollPane scrollableTable = new JScrollPane(table)
- def gui =
- builder.frame(
- title:'Benutzerübersicht',
- size:[520,500] ) {
- panel(
- layout: new BorderLayout(),
- constraints: BorderLayout.NORTH ) {
- widget(scrollableTable)
- }
- }
- gui.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement