Skip to content

Commit b20199b

Browse files
authored
Document Management for SAP ERP
Java Implementation of Document Management Functions for SAP ERP. (#503) Issue 90257
1 parent f46ddb5 commit b20199b

File tree

3 files changed

+431
-36
lines changed

3 files changed

+431
-36
lines changed

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

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import com.genexus.diagnostics.Log;
77
import com.sap.conn.jco.ext.DestinationDataEventListener;
88
import com.sap.conn.jco.ext.DestinationDataProvider;
9+
import com.sap.conn.jco.ext.ServerDataEventListener;
10+
import com.sap.conn.jco.ext.ServerDataProvider;
911
import com.sap.conn.jco.ext.Environment;
1012

11-
public class DestinationProvider implements DestinationDataProvider
13+
public class DestinationProvider implements DestinationDataProvider, ServerDataProvider
1214
{
1315

1416
private static DestinationProvider _instance;
@@ -19,10 +21,10 @@ public static DestinationProvider Instance()
1921
_instance = new DestinationProvider();
2022
}
2123
return _instance;
22-
2324
}
2425

2526
private String SAP_SERVER = "SAP_SERVER";
27+
private String SAP_DOC_SERVER = "DMS_SERVER";
2628
private Properties connectionProperties;
2729
private Hashtable<String, Properties> connectionList = new Hashtable<String, Properties>();
2830
private DestinationDataEventListener eventListener;
@@ -52,7 +54,6 @@ public Properties getDestinationProperties(String sessionName)
5254
public boolean supportsEvents()
5355
{
5456
return true;
55-
5657
}
5758

5859
/*@Override*/
@@ -61,6 +62,73 @@ public void setDestinationDataEventListener(DestinationDataEventListener eventLi
6162
this.eventListener = eventListener;
6263
}
6364

65+
@Override
66+
public Properties getServerProperties(String serverName) {
67+
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+
}
78+
}
79+
else
80+
{
81+
return null;
82+
}
83+
}
84+
85+
@Override
86+
public void setServerDataEventListener(ServerDataEventListener arg0) {
87+
// Our logon parameters don't change dynamically, so we don't need to fire events. See above comment on DestinationDataEventListener.
88+
}
89+
90+
public void removeServerProperties(String serverName)
91+
{
92+
if (eventListener != null)
93+
{
94+
eventListener.deleted(serverName);
95+
}
96+
connectionProperties = null;
97+
connectionList.remove(serverName);
98+
}
99+
100+
public void setServerProperties( String serverName, Properties properties)
101+
{
102+
if ( serverName == null || serverName.equals(""))
103+
{
104+
serverName = SAP_DOC_SERVER;
105+
}
106+
if (!Environment.isServerDataProviderRegistered())
107+
{
108+
Environment.registerServerDataProvider(this);
109+
}
110+
if (properties == null)
111+
{
112+
if (eventListener != null)
113+
{
114+
eventListener.deleted( serverName);
115+
}
116+
connectionProperties = null;
117+
connectionList.remove( serverName);
118+
}
119+
else
120+
{
121+
Log.info("GX SAP Doc Server - Setting Properties : " + serverName + " total : " + Integer.toString(connectionList.size()));
122+
connectionProperties = properties;
123+
connectionList.put( serverName, connectionProperties);
124+
if (eventListener != null)
125+
{
126+
eventListener.updated(serverName);
127+
}
128+
}
129+
}
130+
131+
64132
public void removeConnectionProperties(String sessionName)
65133
{
66134
if (eventListener != null)
@@ -76,7 +144,6 @@ public void setConnectionProperties( String sessionName, Properties properties)
76144
if ( sessionName == null || sessionName.equals(""))
77145
{
78146
sessionName = SAP_SERVER;
79-
80147
}
81148
if (!Environment.isDestinationDataProviderRegistered())
82149
{
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.genexus.sap;
2+
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+
12+
import com.sap.conn.jco.JCoFunction;
13+
import com.sap.conn.jco.JCoParameterList;
14+
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;
20+
21+
import com.sap.conn.jco.server.JCoServerFunctionHandler;
22+
23+
public class DocumentClient {
24+
private static final int BLOB_LENGTH = 1022;
25+
26+
static class ErrorHandler implements JCoServerErrorListener, JCoServerExceptionListener {
27+
28+
@Override
29+
public void serverExceptionOccurred(JCoServer server, String connectionID, JCoServerContextInfo serverCtx, Exception error) {
30+
// Technical problem in server connection (network, logon data, etc.)
31+
error.printStackTrace();
32+
}
33+
34+
@Override
35+
public void serverErrorOccurred(JCoServer server, String connectionID, JCoServerContextInfo serverCtx, Error error) {
36+
// Technical problem in server connection (out-of-memory, etc.)
37+
error.printStackTrace();
38+
}
39+
40+
}
41+
42+
// BAPI_DOCUMENT_CHECKOUTVIEW2 will send the file data via this function module.
43+
static class FTP_R3_TO_CLIENTHandler implements JCoServerFunctionHandler {
44+
45+
@Override
46+
public void handleRequest(JCoServerContext serverCtx, JCoFunction function) throws AbapException, AbapClassException {
47+
String fname;
48+
int length;
49+
JCoTable blob;
50+
51+
// In the case of BAPI_DOCUMENT_CHECKOUTVIEW2, MODE is always binary, so the MODE and TEXT parameters of FTP_R3_TO_CLIENT can be ignored.
52+
JCoParameterList imports = function.getImportParameterList();
53+
fname = imports.getString("FNAME");
54+
length = imports.getInt("LENGTH");
55+
blob = function.getTableParameterList().getTable("BLOB");
56+
FileOutputStream out = null;
57+
try {
58+
out = new FileOutputStream(fname);
59+
boolean hasNextRow = false;
60+
if (!blob.isEmpty()){
61+
hasNextRow = true;
62+
blob.firstRow();
63+
}
64+
while (length > BLOB_LENGTH){
65+
if (hasNextRow){
66+
out.write(blob.getByteArray(0), 0, BLOB_LENGTH);
67+
length -= BLOB_LENGTH;
68+
hasNextRow = blob.nextRow();
69+
}
70+
else throw new IOException("Not enough data in table BLOB ("+String.valueOf(BLOB_LENGTH * blob.getNumRows())+") for requested file size (" + String.valueOf(length) + ")");
71+
}
72+
if (length > 0){
73+
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) + ")");
75+
}
76+
}
77+
catch (IOException e) {
78+
e.printStackTrace();
79+
function.getExportParameterList().setValue("ERROR", 3);
80+
}
81+
finally{
82+
if (out != null){
83+
try{
84+
out.close();
85+
}
86+
catch (IOException ioe){}
87+
}
88+
}
89+
}
90+
91+
}
92+
93+
static class FTP_CLIENT_TO_R3Handler implements JCoServerFunctionHandler {
94+
@Override
95+
public void handleRequest(JCoServerContext serverCtx, JCoFunction function) throws AbapException, AbapClassException {
96+
String fname;
97+
98+
fname = function.getImportParameterList().getString("FNAME");
99+
fname = fname.replace("#","");
100+
101+
try (InputStream source = Files.newInputStream(Paths.get(fname));) {
102+
103+
byte[] file2 = new byte[BLOB_LENGTH];
104+
int bytesread;
105+
int totallenght =0;
106+
107+
InputStream source2 = Files.newInputStream(Paths.get(fname));
108+
109+
JCoTable blobtable = function.getTableParameterList().getTable("BLOB");
110+
while((bytesread = source2.read(file2,0,file2.length)) >0 )
111+
{
112+
113+
blobtable.appendRow();
114+
blobtable.setValue("LINE", file2);
115+
totallenght += bytesread;
116+
}
117+
118+
JCoParameterList exports = function.getExportParameterList();
119+
exports.setValue("LENGTH", totallenght);
120+
}
121+
catch (IOException e) {
122+
e.printStackTrace();
123+
function.getExportParameterList().setValue("ERROR", 3);
124+
}
125+
}
126+
}
127+
128+
129+
130+
}

0 commit comments

Comments
 (0)