Example: IDoc Client 1 for tRFC
Definition
This example shows how to create an IDoc document from scratch:
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.naming.InitialContext;
import javax.resource.cci.*;
import com.sap.conn.idoc.IDocDocument;
import com.sap.conn.idoc.IDocException;
import com.sap.conn.idoc.IDocRepository;
import com.sap.conn.idoc.IDocSegment;
import com.sap.mw.jco.jra.InteractionSpecFactory;
import com.sap.mw.jco.jra.idoc.JRAIDoc;
import com.sap.mw.jco.jra.idoc.JRAIDocFactory;
/**
* Servlet implementation for IDoc client example
*
* A client scenario for creating and sending an IDoc document to ABAP backend
*
*/
public class IDocClient1 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
{
private static final String CONTENT_TYPE = "text/html";
public void init() throws ServletException
{
}
/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
Connection connection = null;
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("Ühtml>");
out.println("Ühead>Ütitle>ExampleServletÜ/title>Ü/head>");
out.println("Übody>");
try
{
InitialContext initialcontext = new InitialContext();
// look up the ConnectionFactory
ConnectionFactory connectionFactory =
(ConnectionFactory)initialcontext.lookup("java:comp/env/H9B");
out.println("Got factory version:
"+connectionFactory.getMetaData().getAdapterVersion());
out.println("Übr>");
// get the IDocFactory instance
JRAIDocFactory idocFactory = JRAIDoc.getIDocFactory();
// get the IDoc repository via the specified connection factory
IDocRepository idocRepository = idocFactory.getIDocRepository(connectionFactory);
// create a new and empty RSINFO document from scratch according to its definition from IDoc rep.
out.println("Creating IDoc...Übr>");
IDocDocument doc = idocFactory.createIDocDocument(idocRepository, "RSINFO");
fillIDoc(doc);
out.println(doc.toString());
out.println("Übr>");
out.println("...done !");
out.println("Übr>");
// check the whole IDoc document's syntax
try
{
out.println("Checking IDoc syntax...");
doc.checkSyntax();
out.println(" done.");
out.println("Übr>");
}
catch (IDocException ex)
{
out.println(" Syntax error: " + ex);
return;
}
// create the IDoc MappedRecord object to be sent
out.println("Üp>sending IDoc... Ü/p>");
MappedRecord idocMR = idocFactory.createMappedRecord(doc);
// get a connection
connection = connectionFactory.getConnection();
out.println("Got connection meta data: Übr> "+connection.getMetaData());
// create an interaction
Interaction interaction = connection.createInteraction();
// create a new transaction ID (TID will be created automatically by the connector layer)
InteractionSpec interactionSpec =
((InteractionSpecFactory) connectionFactory).createInteractionSpec();
// send the IDoc document to the SAP system asynchronously
interaction.execute(interactionSpec, idocMR);
out.println("Üp>...IDoc sentÜ/p>");
// release the interaction
interaction.close();
}
catch ( java.lang.Exception ex )
{
out.println("Üp>Application error: Übr>Ücode>" + ex + "Ü/code>Ü/p>");
ex.printStackTrace(out);
}
finally
{
if (connection != null)
{
try
{
connection.close();
}
catch(Exception e)
{
out.println("Üp>Error during close operation:
Übr>Ücode>" + e + "Ü/code>Ü/p>");
}
}
out.println("Ü/body>");
}
}
/**Clean up resources*/
public void destroy()
{
}
private void fillIDoc(IDocDocument doc) throws Exception
{
// Get the root segment from the document
// The root segment does not contain any fields or data. It is only
// used as the standard parent segment and won't be transmitted when
// the document is sent to an SAP system.
IDocSegment segment = doc.getRootSegment();
//create and add a new and empty child segment (from Root) of type E1RSHIN and fill the segment data
segment = segment.addChild("E1RSHIN");
segment.setValue("REQUEST", "1");
segment.setValue("INFOIDOCNR", "0815");
segment.setValue("SELDATE", "20101020");
segment.setValue("SELTIME", "174500");
segment.setValue("RQSTATE", "1");
segment.setValue("RQRECORD", "10");
// create and add a new and empty child segment (child from child E1RSHIN) of type E1RSPIN and fill the segment
// data
segment = segment.addChild("E1RSPIN");
segment.setValue("DATAPAKID", "111");
segment.setValue("RQDRECORD", "10");
// copy from type E1RSPIN an empty sibling segment (on same level) and fill the segment data
segment =segment.addSibling();
segment.setValue("DATAPAKID", "222");
segment.setValue("RQDRECORD", "10");
// create and add a new and empty sibling segment (on same level) of type E1RSPIN and fill the segment data
segment =segment.addSibling("E1RSEIN");
segment.setValue("MSGID", "1");
segment.setValue("MSGTY", "1");
segment.setValue("MSGNO", "001");
segment.setValue("MSGV1", "msg variable I");
segment.setValue("MSGV2", "msg variable II" );
segment.setValue("MSGV3", "msg variable III");
segment.setValue("MSGV4", "msg variable IV");
// prepare document for sending and set the appropriate control data
doc.setMessageType("RSINFO");
doc.setRecipientPartnerType("LS");
doc.setRecipientPartnerNumber("TSTCLNT000");
doc.setSenderPort("SAPJCOIDOC");
doc.setSenderPartnerType("LS");
doc.setSenderPartnerNumber("JCOCLNT000");
}
}