Simple Material Data Retrieval

Team-Fly

In this example we are actually going to query for a material number within SAP and report whether or not the number exists. If it does exist, we will print out the description. We've kept this example as simple as possible; it has no graphics and skips some of the detail in the steps that were performed in the first example, such as getting your SAP login and setting up the Orbix Daemon. Please refer to the first example if you require more details on these steps.

Step 1: Building up the JavaGetMat Class

Start this example by creating a new console application in the Java Compiler and adding one class to it, JavaGetMat, as shown in Figure 5.10.

The code for this example is similar to the first example but has some additional calls to get the material information through a few BAPI calls.

click to expand

Figure 5.10: Project JavaGetMat in the Java Compiler

Step 2: Entering and Compiling the Code

The complete listing for the JavaGetMat program follows. Enter the code and compile it through the build operation (see the JavaPing example earlier in this chapter).

// JavaGetMat, a very simple Java program to log in to a SAP system // and check for the existence of a material. If the material exists, // then return the material number and description.  For this we will // use two BAPIs:  BAPI_MATERIAL_EXISTENCE_CHECK and  // BAPI_MATERIAL_GETDETAIL import com.sap.rfc.*; import com.sap.rfc.exception.*; import java.io.*; public class JavaGetMat {    public static void main (String[] args)     {         // Simple declaration of variables         String   rfcHost  = "testsystem",        // Java RFC Host                 r3Host    = "test.system.com",   // SAP System Name                 r3SysNo   = "00",                // System Number                 client    = "001",               // SAP Client Number                 user      = "kenkroes",          // Your User Name                 password  = "scoobydoo"          // Your Password                 language  = "EN";                // Your Login Language         FactoryManager facMan = null;         IRfcConnection connection = null;         // Establish the connection to the Orbix Middleware         MiddlewareInfo mdInfo = new MiddlewareInfo();         mdInfo.setMiddlewareType(MiddlewareInfo.middlewareTypeOrbix);         mdInfo.setOrbServerName(rfcHost);         // let the global factory manager use the middlewareInfo          // to create all object factories by binding to the server         facMan = FactoryManager.getSingleInstance();         facMan.setMiddlewareInfo(mdInfo);         // Define The User information         UserInfo        userInfo    = new UserInfo();         ConnectInfo     connectInfo = new ConnectInfo();         userInfo.setClient(client);         userInfo.setUserName(user);         userInfo.setPassword(password);         userInfo.setLanguage(language);         // Set up the Connection          connectInfo.setRfcMode(ConnectInfo.RFC_MODE_VERSION_3);         connectInfo.setDestination("xxx");         connectInfo.setHostName(r3Host);         connectInfo.setSystemNo((short)Integer.parseInt(r3SysNo));        connectInfo.setLoadBalancing(false);         connectInfo.setCheckAuthorization(true);         // Try to establish the connection         try         {             connection = facMan.getRfcConnectionFactory()                             .createRfcConnection(connectInfo, userInfo);             connection.open();         }         catch (JRfcRfcConnectionException re)         {             System.out.println("Unexpected RfcError while opening connection.\n"                             + re.toString());             return;         }         catch (JRfcBaseRuntimeException je)         {             System.out.println("Unexpected JRFC runtime exception:\n"                             + " while opening connection.\n"                             + je.toString());             return;         } //      Simple routine to get data from user for the material number //      if the user types in ‘bye' the routine stops.         System.out.println("Congratulations, Connection Established !");         StringBuffer buf = new StringBuffer(50);         int c;         Character inChar = null;         while(true)         {             buf.setLength(0);             try             {                 while((c = System.in.read()) != ‘\n')                 {                     if (inChar.isLetterOrDigit((char)c) ||                        c == ‘ ‘)                         buf.append((char)c);                 }                 if (buf.toString().equals("bye")) break;             }             catch(IOException io)             {                 System.out.println("Problem reading keyboard");                 return;             } //      Now setup the parameters for the BAPI call.  This is a very simple //        (yet inefficient) way to do this. More information on setting up //        RFC parameters through "Factories" is discussed in Chapter 11 //        Build up object for the RFC Module             IRfcModuleFactory moduleFac = facMan.getRfcModuleFactory();             IRfcModule theModule = null;             try             {                 theModule    = moduleFac.                     autoCreateRfcModule(connection,                     "BAPI_MATERIAL_EXISTENCECHECK");                 theModule.getSimpleImportParam("MATERIAL").setString(buf.toString()); //            Make the Call !!                 theModule.callReceive();             }             catch(JRfcRemoteServerException je)             {                 System.out.println("Unexpected JRFC runtime exception:\n"                                   + " while Calling RFC.\n"                                   + je.toString());                 return;             }             catch(JRfcRfcConnectionException je)             {                 System.out.println("Unexpected JRFC runtime exception:\n"                                   + " while Calling RFC.\n"                                   + je.toString());                 return;            }             catch(JRfcRfcAbapException je)             {                 System.out.println("Unexpected JRFC runtime exception:\n"                                   + " while Calling RFC.\n"                                   + je.toString());                 return;             }             catch(JRfcRemoteAutoCreateException je)             {                 System.out.println("Unexpected JRFC runtime exception:\n"                                   + " while Calling RFC.\n"                                   + je.toString());                 return;             }         //        Display the results             char    bapiReturn;             bapiReturn = theModule.getStructExportParam("RETURN").                             getSimpleField("TYPE").getChar();                 if ( String.valueOf(bapiReturn) != "S" )                 System.out.println("Return =" + bapiReturn);             //        Material Exists get description             try             {                 theModule = moduleFac.                     autoCreateRfcModule(connection,                     "BAPI_MATERIAL_GET_DETAIL");                 theModule.getSimpleImportParam("MATERIAL").setString(buf.toString());                 theModule.callReceive();             }             catch(JRfcRemoteServerException je)             {                 System.out.println("Unexpected JRFC runtime exception:\n"                                   + " while Calling RFC.\n"                                  + je.toString());                 return;             }             catch(JRfcRfcConnectionException je)             {                 System.out.println("Unexpected JRFC runtime exception:\n"                                   + " while Calling RFC.\n"                                   + je.toString());                 return;             }             catch(JRfcRfcAbapException je)             {                 System.out.println("Unexpected JRFC runtime exception:\n"                                   + " while Calling RFC.\n"                                   + je.toString());                 return;             }             catch(JRfcRemoteAutoCreateException je)             {                 System.out.println("Unexpected JRFC runtime exception:\n"                                   + " while Calling RFC.\n"                                   + je.toString());                 return;             }             // Print the material number and description             String matDesc;             matDesc = theModule.getStructExportParam("MATERIAL_GENERAL_DATA").                       getSimpleField("MATL_DESC").getString();             System.out.println(buf.toString() + " " + matDesc );         }     } }

Step 3: Running the Program

Start the Orbix Daemon as described in the JavaPing example. Open a DOS window on your system and change directories to the one that contains your JavaGetMat application. Type JavaGetMat at the prompt; your screen should look like that in Figure 5.11.

click to expand

Figure 5.11: Output from the JavaGetMat example


Team-Fly


Java & BAPI Technology for SAP
Java & BAPI Technology for SAP
ISBN: 761523057
EAN: N/A
Year: 1998
Pages: 199

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net