Advertisement
rplantiko

Accessing ABAP data efficiently with Groovy

Aug 30th, 2011
1,285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 4.32 KB | None | 0 0
  1. // Perform an RFC and display the result in a JTable
  2. // Using some groovy tricks, the notational overhead is minimized
  3. import com.sap.mw.jco.*
  4. import org.junit.*
  5.  
  6. // For visual representation of the result
  7. import java.awt.BorderLayout;
  8. import javax.swing.*;
  9. import javax.swing.table.*;
  10. import groovy.swing.SwingBuilder;
  11.  
  12. class TestJcoDataHandling {
  13.  
  14.   Repository repo;
  15.  
  16.   @Test
  17.   void test() {      
  18.  
  19. // Provide handle to a remotely callable function  
  20.     def userList = repo.getFunction "BAPI_USER_GETLIST"
  21.    
  22. // Parametrize it      
  23.     userList.importParameter.set(
  24.       MAX_ROWS : 50,
  25.       WITH_USERNAME : "X"
  26.       )
  27.      
  28. // Do the call      
  29.     userList.execute( )
  30.    
  31. // Get and display result
  32.     display userList.tableParameter.USERLIST    
  33.    
  34.     }
  35.    
  36.   @Before
  37.   void setup() {
  38.  
  39. // Replace with your login data instead
  40. // see http://help.sap.com/javadocs/NW04/current/jc/com/sap/mw/jco/JCO.html    
  41.     def sys = new Properties()
  42.     sys.load( new FileInputStream( "h:\\Eigene Dateien\\groovy\\jco\\d12.properties" ) )
  43.  
  44.     def conn = JCO.createClient( sys )
  45.     conn.connect()
  46.  
  47.     repo = new Repository( new JCO.Repository( "repo", conn ), conn )    
  48.  
  49.     }
  50.  
  51.   @After
  52.   void teardown() {
  53.     repo.disconnect()
  54.     }
  55.  
  56.  
  57. // Show a JTable with the result    
  58.   void display( userlist ) {
  59.     def data = []
  60.     for (record in userlist) {
  61.       data.add( [ record.USERNAME, record.FULLNAME ] )
  62.       }
  63.     new GUI().showTable( data, ["Benutzername","Bürgerlicher Name"] )  
  64.     }  
  65.  
  66.  
  67. }
  68.  
  69.  
  70. // In this wrapper, we trap the member operator "." and subscript operator "[]"
  71. // mixing in the informations from JCo.MetaData calls
  72. class Data implements Iterator {
  73.   private JCO.Record record
  74.  
  75.   Data( JCO.Record record) {
  76.     this.record = record;
  77.     }
  78.  
  79.   def toString = {
  80.     record.getValue()
  81.     }
  82.    
  83.   def getProperty(String name) {
  84.     if (name == "length") return record.getNumRows()
  85.     if (name == "record") return record
  86.     def f = record.getValue( name )
  87.     if (record.isTable( name ) || record.isStructure( name ))
  88.       return new Data( f )
  89.     else {
  90.       return f
  91.       }
  92.     }
  93.    
  94.   void setProperty(String name, Object value) {
  95.     record.getField( name ).setValue( value );
  96.     }  
  97.    
  98.   def set = { pairs ->
  99.     for (p in pairs) {
  100.       setProperty p.key, p.value
  101.       }
  102.     }    
  103.  
  104.   def getAt(i) {
  105.     record.setRow( i );
  106. // Danach erfolgt ein Komponetenzugriff
  107.     this  
  108.     }
  109.        
  110.  
  111. // Iterator interface  
  112.   void remove() {}
  113.  
  114.   boolean hasNext() {
  115.     return !record.isLastRow()
  116.     }
  117.  
  118.   def next() {
  119.     record.nextRow()
  120.     this
  121.     }
  122.  
  123.   }
  124.  
  125. // Adapter for JCo Repository
  126. class Repository {
  127.  
  128.   @Delegate JCO.Repository repo
  129.  
  130.   JCO.Connection conn
  131.  
  132.   Repository( JCO.Repository repo, JCO.Connection conn) {
  133.     this.repo = repo
  134.     this.conn = conn
  135.     }
  136.    
  137.   def getFunction = { name ->
  138.     new Function( repo.getFunctionTemplate( name ).getFunction(), this )
  139.     }
  140.    
  141.   def disconnect = {
  142.     conn.disconnect( )
  143.     }  
  144.   }
  145.  
  146. // Adapter for JCO.Function
  147. class Function {
  148.  
  149.   @Delegate JCO.Function function
  150.   Repository repo
  151.  
  152.   private Data importParameter, exportParameter, tableParameter
  153.  
  154.   Function( JCO.Function function, Repository repo ) {
  155.     this.function = function
  156.     this.repo     = repo
  157.     this.importParameter = new Data( function.importParameterList )
  158.     this.exportParameter = new Data( function.exportParameterList )
  159.     this.tableParameter  = new Data( function.tableParameterList )
  160.     }
  161.  
  162.   def execute = {
  163.     repo.conn.execute(function)
  164.     }
  165.    
  166.   }  
  167.  
  168. // A small GUI for presentation purposes
  169. class GUI {
  170.   SwingBuilder builder
  171.   GUI() {
  172.     builder = new SwingBuilder()
  173.     }
  174.    
  175.   def showTable = { data, columns ->
  176.  
  177.     JTable table = new JTable((String[][]) data.toArray(),
  178.                               (String[]) columns.toArray() )
  179.  
  180.     JScrollPane scrollableTable = new JScrollPane(table)
  181.      
  182.     def gui =
  183.       builder.frame(
  184.         title:'Benutzerübersicht',
  185.         size:[520,500] ) {
  186.           panel(
  187.             layout: new BorderLayout(),
  188.             constraints: BorderLayout.NORTH ) {
  189.               widget(scrollableTable)  
  190.               }
  191.             }
  192.      gui.show();          
  193.      }
  194.  
  195.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement