Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.idoc.actif;
- import com.sap.conn.idoc.IDocDocumentList;
- import com.sap.conn.idoc.IDocXMLProcessor;
- import com.sap.conn.idoc.jco.*;
- import com.sap.conn.jco.server.*;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- public class IDocServerExample
- {
- public static void main(String[] a)
- {
- try
- {
- // see provided examples of configuration files xxxx.jcoServer and xxxx.jcoDestination
- JCoIDocServer server = JCoIDoc.getServer("xxxx");
- server.setIDocHandlerFactory(new MyIDocHandlerFactory());
- server.setTIDHandler(new MyTidHandler());
- MyThrowableListener listener = new MyThrowableListener();
- server.addServerErrorListener(listener);
- server.addServerExceptionListener(listener);
- //server.setConnectionCount(1);
- server.start();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- static class MyIDocHandler implements JCoIDocHandler
- {
- public void handleRequest(JCoServerContext serverCtx, IDocDocumentList idocList)
- {
- FileOutputStream fos=null;
- OutputStreamWriter osw=null;
- try
- {
- //receiving XML IDoc, how to get the IDoc as a Flat IDoc (SAP is sending the IDoc as Flat)???
- IDocXMLProcessor xmlProcessor = JCoIDoc.getIDocFactory().getIDocXMLProcessor();
- fos=new FileOutputStream(serverCtx.getTID());
- osw=new OutputStreamWriter(fos, "UTF8");
- xmlProcessor.render(idocList, osw, IDocXMLProcessor.RENDER_WITH_TABS_AND_CRLF);
- osw.flush();
- }
- catch (Throwable thr)
- {
- thr.printStackTrace();
- }
- finally
- {
- try
- {
- if (osw!=null)
- osw.close();
- if (fos!=null)
- fos.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- static class MyIDocHandlerFactory implements JCoIDocHandlerFactory
- {
- private JCoIDocHandler handler = new MyIDocHandler();
- public JCoIDocHandler getIDocHandler(JCoIDocServerContext serverCtx)
- {
- return handler;
- }
- }
- static class MyThrowableListener implements JCoServerErrorListener, JCoServerExceptionListener
- {
- public void serverErrorOccurred(JCoServer server, String connectionId, JCoServerContextInfo ctx, Error error)
- {
- System.out.println(">>> Error occured on " + server.getProgramID() + " connection " + connectionId);
- error.printStackTrace();
- }
- public void serverExceptionOccurred(JCoServer server, String connectionId, JCoServerContextInfo ctx, Exception error)
- {
- System.out.println(">>> Error occured on " + server.getProgramID() + " connection " + connectionId);
- error.printStackTrace();
- }
- }
- static class MyTidHandler implements JCoServerTIDHandler
- {
- public boolean checkTID(JCoServerContext serverCtx, String tid)
- {
- System.out.println("checkTID called for TID="+tid);
- return true;
- }
- public void confirmTID(JCoServerContext serverCtx, String tid)
- {
- System.out.println("confirmTID called for TID="+tid);
- }
- public void commit(JCoServerContext serverCtx, String tid)
- {
- System.out.println("commit called for TID="+tid);
- }
- public void rollback(JCoServerContext serverCtx, String tid)
- {
- System.out.print("rollback called for TID="+tid);
- }
- }
- }
- package com.idoc.actif;
- import com.sap.conn.idoc.*;
- import com.sap.conn.idoc.jco.JCoIDoc;
- import com.sap.conn.jco.JCoDestination;
- import com.sap.conn.jco.JCoDestinationManager;
- import java.io.BufferedReader;
- import java.io.FileReader;
- public class IDocClientExample {
- public static void main(String[] args) {
- try {
- String iDocXML = null;
- FileReader fileReader;
- try {
- fileReader = new FileReader("TestSalesOrder.xml");
- BufferedReader br = new BufferedReader(fileReader);
- StringBuffer sb = new StringBuffer();
- String line;
- while ((line = br.readLine()) != null) {
- sb.append(line);
- }
- iDocXML = sb.toString();
- br.close();
- fileReader.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- // see provided configuration file xxxx.jcoDestination
- JCoDestination destination = JCoDestinationManager.getDestination("xxxx");
- IDocFactory iDocFactory = JCoIDoc.getIDocFactory();
- IDocRepository iDocRepository = JCoIDoc.getIDocRepository(destination);
- String tid = destination.createTID();
- // a) create sample new idoc
- IDocDocument doc = iDocFactory.createIDocDocument(iDocRepository, "RSINFO");
- fillIDoc(doc);
- doc.checkSyntax();
- JCoIDoc.send(doc, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);
- // b) use existent xml file
- /* IDocXMLProcessor processor = iDocFactory.getIDocXMLProcessor();
- IDocDocumentList iDocList = processor.parse(iDocRepository, iDocXML);
- JCoIDoc.send(iDocList, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);*/
- destination.confirmTID(tid);
- System.out.print("Sent success :)");
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.print("End!!");
- }
- private static 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");
- }
- }
Add Comment
Please, Sign In to add comment