Guest User

Untitled

a guest
Nov 24th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. package com.idoc.actif;
  2.  
  3. import com.sap.conn.idoc.IDocDocumentList;
  4. import com.sap.conn.idoc.IDocXMLProcessor;
  5. import com.sap.conn.idoc.jco.*;
  6. import com.sap.conn.jco.server.*;
  7.  
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.io.OutputStreamWriter;
  11.  
  12.  
  13. public class IDocServerExample
  14. {
  15. public static void main(String[] a)
  16. {
  17. try
  18. {
  19. // see provided examples of configuration files xxxx.jcoServer and xxxx.jcoDestination
  20. JCoIDocServer server = JCoIDoc.getServer("xxxx");
  21. server.setIDocHandlerFactory(new MyIDocHandlerFactory());
  22.  
  23. server.setTIDHandler(new MyTidHandler());
  24.  
  25. MyThrowableListener listener = new MyThrowableListener();
  26. server.addServerErrorListener(listener);
  27. server.addServerExceptionListener(listener);
  28. //server.setConnectionCount(1);
  29. server.start();
  30.  
  31. }
  32. catch (Exception e)
  33. {
  34. e.printStackTrace();
  35. }
  36. }
  37.  
  38. static class MyIDocHandler implements JCoIDocHandler
  39. {
  40. public void handleRequest(JCoServerContext serverCtx, IDocDocumentList idocList)
  41. {
  42.  
  43. FileOutputStream fos=null;
  44. OutputStreamWriter osw=null;
  45. try
  46. {
  47. //receiving XML IDoc, how to get the IDoc as a Flat IDoc (SAP is sending the IDoc as Flat)???
  48. IDocXMLProcessor xmlProcessor = JCoIDoc.getIDocFactory().getIDocXMLProcessor();
  49. fos=new FileOutputStream(serverCtx.getTID());
  50. osw=new OutputStreamWriter(fos, "UTF8");
  51. xmlProcessor.render(idocList, osw, IDocXMLProcessor.RENDER_WITH_TABS_AND_CRLF);
  52. osw.flush();
  53. }
  54. catch (Throwable thr)
  55. {
  56. thr.printStackTrace();
  57. }
  58. finally
  59. {
  60. try
  61. {
  62. if (osw!=null)
  63. osw.close();
  64. if (fos!=null)
  65. fos.close();
  66. }
  67. catch (IOException e)
  68. {
  69. e.printStackTrace();
  70. }
  71. }
  72. }
  73. }
  74.  
  75. static class MyIDocHandlerFactory implements JCoIDocHandlerFactory
  76. {
  77. private JCoIDocHandler handler = new MyIDocHandler();
  78. public JCoIDocHandler getIDocHandler(JCoIDocServerContext serverCtx)
  79. {
  80. return handler;
  81. }
  82. }
  83.  
  84. static class MyThrowableListener implements JCoServerErrorListener, JCoServerExceptionListener
  85. {
  86.  
  87. public void serverErrorOccurred(JCoServer server, String connectionId, JCoServerContextInfo ctx, Error error)
  88. {
  89. System.out.println(">>> Error occured on " + server.getProgramID() + " connection " + connectionId);
  90. error.printStackTrace();
  91. }
  92. public void serverExceptionOccurred(JCoServer server, String connectionId, JCoServerContextInfo ctx, Exception error)
  93. {
  94. System.out.println(">>> Error occured on " + server.getProgramID() + " connection " + connectionId);
  95. error.printStackTrace();
  96. }
  97. }
  98.  
  99. static class MyTidHandler implements JCoServerTIDHandler
  100. {
  101. public boolean checkTID(JCoServerContext serverCtx, String tid)
  102. {
  103. System.out.println("checkTID called for TID="+tid);
  104. return true;
  105. }
  106.  
  107.  
  108. public void confirmTID(JCoServerContext serverCtx, String tid)
  109. {
  110. System.out.println("confirmTID called for TID="+tid);
  111. }
  112.  
  113.  
  114. public void commit(JCoServerContext serverCtx, String tid)
  115. {
  116. System.out.println("commit called for TID="+tid);
  117. }
  118.  
  119.  
  120. public void rollback(JCoServerContext serverCtx, String tid)
  121. {
  122. System.out.print("rollback called for TID="+tid);
  123. }
  124. }
  125. }
  126.  
  127. package com.idoc.actif;
  128.  
  129.  
  130. import com.sap.conn.idoc.*;
  131. import com.sap.conn.idoc.jco.JCoIDoc;
  132. import com.sap.conn.jco.JCoDestination;
  133. import com.sap.conn.jco.JCoDestinationManager;
  134.  
  135. import java.io.BufferedReader;
  136. import java.io.FileReader;
  137.  
  138.  
  139. public class IDocClientExample {
  140.  
  141. public static void main(String[] args) {
  142.  
  143. try {
  144.  
  145. String iDocXML = null;
  146. FileReader fileReader;
  147.  
  148. try {
  149. fileReader = new FileReader("TestSalesOrder.xml");
  150. BufferedReader br = new BufferedReader(fileReader);
  151. StringBuffer sb = new StringBuffer();
  152. String line;
  153. while ((line = br.readLine()) != null) {
  154. sb.append(line);
  155. }
  156.  
  157. iDocXML = sb.toString();
  158.  
  159. br.close();
  160. fileReader.close();
  161. } catch (Exception ex) {
  162. ex.printStackTrace();
  163. }
  164.  
  165. // see provided configuration file xxxx.jcoDestination
  166. JCoDestination destination = JCoDestinationManager.getDestination("xxxx");
  167. IDocFactory iDocFactory = JCoIDoc.getIDocFactory();
  168. IDocRepository iDocRepository = JCoIDoc.getIDocRepository(destination);
  169.  
  170. String tid = destination.createTID();
  171.  
  172.  
  173. // a) create sample new idoc
  174. IDocDocument doc = iDocFactory.createIDocDocument(iDocRepository, "RSINFO");
  175. fillIDoc(doc);
  176. doc.checkSyntax();
  177. JCoIDoc.send(doc, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);
  178.  
  179.  
  180. // b) use existent xml file
  181. /* IDocXMLProcessor processor = iDocFactory.getIDocXMLProcessor();
  182. IDocDocumentList iDocList = processor.parse(iDocRepository, iDocXML);
  183. JCoIDoc.send(iDocList, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);*/
  184.  
  185. destination.confirmTID(tid);
  186. System.out.print("Sent success :)");
  187.  
  188. } catch (Exception e) {
  189. e.printStackTrace();
  190. }
  191. System.out.print("End!!");
  192. }
  193.  
  194. private static void fillIDoc(IDocDocument doc) throws Exception
  195. {
  196.  
  197. // Get the root segment from the document
  198. // The root segment does not contain any fields or data. It is only
  199. // used as the standard parent segment and won't be transmitted when
  200. // the document is sent to an SAP system.
  201. IDocSegment segment = doc.getRootSegment();
  202.  
  203. //create and add a new and empty child segment (from Root) of type E1RSHIN and fill the segment data
  204. segment = segment.addChild("E1RSHIN");
  205. segment.setValue("REQUEST", "1");
  206. segment.setValue("INFOIDOCNR", "0815");
  207. segment.setValue("SELDATE", "20101020");
  208. segment.setValue("SELTIME", "174500");
  209. segment.setValue("RQSTATE", "1");
  210. segment.setValue("RQRECORD", "10");
  211.  
  212. // create and add a new and empty child segment (child from child E1RSHIN) of type E1RSPIN and fill the segment
  213. // data
  214. segment = segment.addChild("E1RSPIN");
  215. segment.setValue("DATAPAKID", "111");
  216. segment.setValue("RQDRECORD", "10");
  217.  
  218. // copy from type E1RSPIN an empty sibling segment (on same level) and fill the segment data
  219. segment =segment.addSibling();
  220. segment.setValue("DATAPAKID", "222");
  221. segment.setValue("RQDRECORD", "10");
  222.  
  223. // create and add a new and empty sibling segment (on same level) of type E1RSPIN and fill the segment data
  224. segment =segment.addSibling("E1RSEIN");
  225. segment.setValue("MSGID", "1");
  226. segment.setValue("MSGTY", "1");
  227. segment.setValue("MSGNO", "001");
  228. segment.setValue("MSGV1", "msg variable I");
  229. segment.setValue("MSGV2", "msg variable II" );
  230. segment.setValue("MSGV3", "msg variable III");
  231. segment.setValue("MSGV4", "msg variable IV");
  232.  
  233. // prepare document for sending and set the appropriate control data
  234. doc.setMessageType("RSINFO");
  235. doc.setRecipientPartnerType("LS");
  236. doc.setRecipientPartnerNumber("TSTCLNT000");
  237. doc.setSenderPort("SAPJCOIDOC");
  238. doc.setSenderPartnerType("LS");
  239. doc.setSenderPartnerNumber("JCOCLNT000");
  240. }
  241.  
  242. }
Add Comment
Please, Sign In to add comment