Skip to content

Commit e9eb08b

Browse files
authored
Document Management support for SAP ERP/Java (#411)
* Issue: 90257 Document Management support for SAP ERP/Java (Download Only) * - Soporte para SAP Cloud Destination - Separo Document receiver y DocumentSender de la connection SessionManager. Issue 100181 * - Duplicate method * - Code Cleaning
1 parent 319eaea commit e9eb08b

File tree

7 files changed

+538
-370
lines changed

7 files changed

+538
-370
lines changed

java/src/main/java/com/genexus/sap/DestinationProvider.java

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public static DestinationProvider Instance()
2323
return _instance;
2424
}
2525

26-
private String SAP_SERVER = "SAP_SERVER";
27-
private String SAP_DOC_SERVER = "DMS_SERVER";
26+
private static final String SAP_SERVER = "SAP_SERVER";
27+
private static final String SAP_DOC_SERVER = "DMS_SERVER";
2828
private Properties connectionProperties;
2929
private Hashtable<String, Properties> connectionList = new Hashtable<String, Properties>();
3030
private DestinationDataEventListener eventListener;
@@ -33,16 +33,8 @@ public static DestinationProvider Instance()
3333
public Properties getDestinationProperties(String sessionName)
3434
{
3535
if (sessionName != null)
36-
{
37-
Properties val = connectionList.get(sessionName);
38-
if (val != null)
39-
{
40-
return val;
41-
}
42-
else
43-
{
44-
return null;
45-
}
36+
{
37+
return connectionList.get(sessionName);
4638
}
4739
else
4840
{
@@ -65,16 +57,8 @@ public void setDestinationDataEventListener(DestinationDataEventListener eventLi
6557
@Override
6658
public Properties getServerProperties(String serverName) {
6759
if (serverName != null)
68-
{
69-
Properties val = connectionList.get(serverName);
70-
if (val != null)
71-
{
72-
return val;
73-
}
74-
else
75-
{
76-
return null;
77-
}
60+
{
61+
return connectionList.get(serverName);
7862
}
7963
else
8064
{
@@ -118,7 +102,7 @@ public void setServerProperties( String serverName, Properties properties)
118102
}
119103
else
120104
{
121-
Log.info("GX SAP Doc Server - Setting Properties : " + serverName + " total : " + Integer.toString(connectionList.size()));
105+
Log.info("GX SAP Doc Server - Setting Properties : " + serverName + " total : " + connectionList.size());
122106
connectionProperties = properties;
123107
connectionList.put( serverName, connectionProperties);
124108
if (eventListener != null)
@@ -160,7 +144,7 @@ public void setConnectionProperties( String sessionName, Properties properties)
160144
}
161145
else
162146
{
163-
Log.info("GX SAP - Setting Properties : " + sessionName + " total : " + Integer.toString(connectionList.size()));
147+
Log.info("GX SAP - Setting Properties : " + sessionName + " total : " + connectionList.size());
164148
connectionProperties = properties;
165149
connectionList.put( sessionName, connectionProperties);
166150
if (eventListener != null)

java/src/main/java/com/genexus/sap/DocumentClient.java

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
package com.genexus.sap;
22

3-
import java.io.*;
4-
import java.io.FileOutputStream;
5-
import java.io.IOException;
6-
import java.nio.file.Files;
7-
import java.nio.file.Paths;
8-
9-
import com.sap.conn.jco.AbapClassException;
10-
import com.sap.conn.jco.AbapException;
11-
123
import com.sap.conn.jco.JCoFunction;
134
import com.sap.conn.jco.JCoParameterList;
145
import com.sap.conn.jco.JCoTable;
15-
import com.sap.conn.jco.server.JCoServer;
16-
import com.sap.conn.jco.server.JCoServerContext;
17-
import com.sap.conn.jco.server.JCoServerContextInfo;
18-
import com.sap.conn.jco.server.JCoServerErrorListener;
19-
import com.sap.conn.jco.server.JCoServerExceptionListener;
6+
import com.sap.conn.jco.server.*;
207

21-
import com.sap.conn.jco.server.JCoServerFunctionHandler;
8+
import java.io.FileOutputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.nio.file.Files;
12+
import java.nio.file.Paths;
13+
import java.nio.file.Path;
2214

2315
public class DocumentClient {
2416
private static final int BLOB_LENGTH = 1022;
@@ -43,7 +35,7 @@ public void serverErrorOccurred(JCoServer server, String connectionID, JCoServer
4335
static class FTP_R3_TO_CLIENTHandler implements JCoServerFunctionHandler {
4436

4537
@Override
46-
public void handleRequest(JCoServerContext serverCtx, JCoFunction function) throws AbapException, AbapClassException {
38+
public void handleRequest(JCoServerContext serverCtx, JCoFunction function) {
4739
String fname;
4840
int length;
4941
JCoTable blob;
@@ -67,14 +59,18 @@ public void handleRequest(JCoServerContext serverCtx, JCoFunction function) thro
6759
length -= BLOB_LENGTH;
6860
hasNextRow = blob.nextRow();
6961
}
70-
else throw new IOException("Not enough data in table BLOB ("+String.valueOf(BLOB_LENGTH * blob.getNumRows())+") for requested file size (" + String.valueOf(length) + ")");
62+
else throw new IOException("Not enough data in table BLOB ("+ BLOB_LENGTH * blob.getNumRows() +") for requested file size (" + length + ")");
7163
}
7264
if (length > 0){
7365
if (hasNextRow) out.write(blob.getByteArray(0), 0, length);
74-
else throw new IOException("Not enough data in table BLOB ("+String.valueOf(BLOB_LENGTH * blob.getNumRows())+") for requested file size (" + String.valueOf(length) + ")");
66+
else throw new IOException("Not enough data in table BLOB ("+ BLOB_LENGTH * blob.getNumRows() +") for requested file size (" + length + ")");
7567
}
7668
}
7769
catch (IOException e) {
70+
71+
// Unfortunately there is no way of transmitting error details back to SAP, so we better log it here,
72+
// if we want to keep the chance of troubleshooting later, what exactly went wrong...
73+
7874
e.printStackTrace();
7975
function.getExportParameterList().setValue("ERROR", 3);
8076
}
@@ -92,19 +88,19 @@ public void handleRequest(JCoServerContext serverCtx, JCoFunction function) thro
9288

9389
static class FTP_CLIENT_TO_R3Handler implements JCoServerFunctionHandler {
9490
@Override
95-
public void handleRequest(JCoServerContext serverCtx, JCoFunction function) throws AbapException, AbapClassException {
91+
public void handleRequest(JCoServerContext serverCtx, JCoFunction function) {
9692
String fname;
9793

9894
fname = function.getImportParameterList().getString("FNAME");
9995
fname = fname.replace("#","");
100-
101-
try (InputStream source = Files.newInputStream(Paths.get(fname));) {
96+
Path inputFile = Paths.get(fname);
97+
try (InputStream source = Files.newInputStream(inputFile)) {
10298

10399
byte[] file2 = new byte[BLOB_LENGTH];
104100
int bytesread;
105101
int totallenght =0;
106102

107-
InputStream source2 = Files.newInputStream(Paths.get(fname));
103+
InputStream source2 = Files.newInputStream(inputFile);
108104

109105
JCoTable blobtable = function.getTableParameterList().getTable("BLOB");
110106
while((bytesread = source2.read(file2,0,file2.length)) >0 )
@@ -124,7 +120,4 @@ public void handleRequest(JCoServerContext serverCtx, JCoFunction function) thro
124120
}
125121
}
126122
}
127-
128-
129-
130123
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.genexus.sap;
2+
3+
import com.genexus.ModelContext;
4+
import com.sap.conn.jco.JCoException;
5+
6+
import com.sap.conn.jco.JCoMetaData;
7+
import com.sap.conn.jco.JCoRecordMetaData;
8+
import com.sap.conn.jco.server.JCoServer;
9+
import com.sap.conn.jco.server.DefaultServerHandlerFactory;
10+
11+
import com.sap.conn.jco.server.JCoServerFactory;
12+
import com.sap.conn.jco.JCoCustomRepository;
13+
import com.sap.conn.jco.JCoFunctionTemplate;
14+
import com.sap.conn.jco.JCoListMetaData;
15+
import com.sap.conn.jco.JCo;
16+
import com.genexus.diagnostics.Log;
17+
18+
public class DocumentReceiver
19+
{
20+
private String serverName;
21+
private String repositoryName;
22+
private ModelContext _context;
23+
JCoServer receiver = null;
24+
25+
26+
public DocumentReceiver(String server, String repository, ModelContext context)
27+
{
28+
this.serverName = server;
29+
this.repositoryName = repository;
30+
this._context = context;
31+
}
32+
33+
public void start()
34+
{
35+
Log.info("GX SAP - DMS Receiver Start " + serverName + " " + repositoryName);
36+
try {
37+
receiver = setupServerDownload(serverName, repositoryName);
38+
receiver.start();
39+
}
40+
catch (JCoException ex)
41+
{
42+
Log.error("GX SAP - Error Starting " + ex) ;
43+
}
44+
}
45+
46+
public void stop()
47+
{
48+
49+
String receiverName = "";
50+
if (serverName == null || serverName.equals(""))
51+
{
52+
Object receiverObj = _context.getContextProperty("SAPReceiverServerName");
53+
if (receiverObj != null)
54+
{
55+
receiverName = (String)receiverObj;
56+
}
57+
}
58+
else
59+
{
60+
receiverName = serverName;
61+
}
62+
63+
try {
64+
if (!receiverName.equals(""))
65+
{
66+
receiver = JCoServerFactory.getServer(receiverName);
67+
if (receiver != null) receiver.stop();
68+
Log.info("GX SAP - DMS Receiver stop " + receiverName );
69+
}
70+
else
71+
{
72+
Log.error("GX SAP - Error Stopping receiver." );
73+
}
74+
}
75+
catch (JCoException ex)
76+
{
77+
Log.error("GX SAP - Error Stopping " + ex) ;
78+
}
79+
80+
}
81+
82+
public JCoServer setupServerDownload(String serverName, String repositoryName) throws JCoException
83+
{
84+
JCoServer server = JCoServerFactory.getServer(serverName);
85+
JCoCustomRepository repo = JCo.createCustomRepository(repositoryName);
86+
JCoListMetaData imports, exports, tables;
87+
JCoFunctionTemplate FTP_R3_TO_CLIENT;
88+
89+
imports = JCo.createListMetaData("IMPORTING");
90+
imports.add("FNAME", JCoMetaData.TYPE_CHAR, 256, 512, JCoListMetaData.IMPORT_PARAMETER);
91+
imports.add("LENGTH", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.IMPORT_PARAMETER);
92+
imports.add("MODE", JCoMetaData.TYPE_CHAR, 1, 2, JCoListMetaData.IMPORT_PARAMETER | JCoListMetaData.OPTIONAL_PARAMETER);
93+
imports.lock();
94+
95+
exports = JCo.createListMetaData("EXPORTING");
96+
exports.add("ERROR", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.EXPORT_PARAMETER);
97+
exports.add("LENGTH", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.EXPORT_PARAMETER);
98+
exports.lock();
99+
100+
JCoRecordMetaData tabLine = JCo.createRecordMetaData("BLOB");
101+
tabLine.add("LINE", JCoMetaData.TYPE_BYTE, EnterpriseConnect.BLOB_LENGTH, 0, EnterpriseConnect.BLOB_LENGTH, 0);
102+
tabLine.lock();
103+
tables = JCo.createListMetaData("TABLES");
104+
tables.add("BLOB", JCoMetaData.TYPE_TABLE, tabLine, 0);
105+
tables.lock();
106+
107+
FTP_R3_TO_CLIENT = JCo.createFunctionTemplate("FTP_R3_TO_CLIENT", imports, exports, null, tables, null);
108+
repo.addFunctionTemplateToCache(FTP_R3_TO_CLIENT);
109+
server.setRepository(repo);
110+
111+
DefaultServerHandlerFactory.FunctionHandlerFactory handlerFactory = new DefaultServerHandlerFactory.FunctionHandlerFactory();
112+
handlerFactory.registerHandler("FTP_R3_TO_CLIENT", new DocumentClient.FTP_R3_TO_CLIENTHandler());
113+
server.setCallHandlerFactory(handlerFactory);
114+
115+
DocumentClient.ErrorHandler hdl = new DocumentClient.ErrorHandler();
116+
server.addServerErrorListener(hdl);
117+
server.addServerExceptionListener(hdl);
118+
119+
return server;
120+
}
121+
122+
}

0 commit comments

Comments
 (0)