Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 71 additions & 4 deletions java/src/main/java/com/genexus/sap/DestinationProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import com.genexus.diagnostics.Log;
import com.sap.conn.jco.ext.DestinationDataEventListener;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.ext.ServerDataEventListener;
import com.sap.conn.jco.ext.ServerDataProvider;
import com.sap.conn.jco.ext.Environment;

public class DestinationProvider implements DestinationDataProvider
public class DestinationProvider implements DestinationDataProvider, ServerDataProvider
{

private static DestinationProvider _instance;
Expand All @@ -19,10 +21,10 @@ public static DestinationProvider Instance()
_instance = new DestinationProvider();
}
return _instance;

}

private String SAP_SERVER = "SAP_SERVER";
private String SAP_DOC_SERVER = "DMS_SERVER";
private Properties connectionProperties;
private Hashtable<String, Properties> connectionList = new Hashtable<String, Properties>();
private DestinationDataEventListener eventListener;
Expand Down Expand Up @@ -52,7 +54,6 @@ public Properties getDestinationProperties(String sessionName)
public boolean supportsEvents()
{
return true;

}

/*@Override*/
Expand All @@ -61,6 +62,73 @@ public void setDestinationDataEventListener(DestinationDataEventListener eventLi
this.eventListener = eventListener;
}

@Override
public Properties getServerProperties(String serverName) {
if (serverName != null)
{
Properties val = connectionList.get(serverName);
if (val != null)
{
return val;
}
else
{
return null;
}
}
else
{
return null;
}
}

@Override
public void setServerDataEventListener(ServerDataEventListener arg0) {
// Our logon parameters don't change dynamically, so we don't need to fire events. See above comment on DestinationDataEventListener.
}

public void removeServerProperties(String serverName)
{
if (eventListener != null)
{
eventListener.deleted(serverName);
}
connectionProperties = null;
connectionList.remove(serverName);
}

public void setServerProperties( String serverName, Properties properties)
{
if ( serverName == null || serverName.equals(""))
{
serverName = SAP_DOC_SERVER;
}
if (!Environment.isServerDataProviderRegistered())
{
Environment.registerServerDataProvider(this);
}
if (properties == null)
{
if (eventListener != null)
{
eventListener.deleted( serverName);
}
connectionProperties = null;
connectionList.remove( serverName);
}
else
{
Log.info("GX SAP Doc Server - Setting Properties : " + serverName + " total : " + Integer.toString(connectionList.size()));
connectionProperties = properties;
connectionList.put( serverName, connectionProperties);
if (eventListener != null)
{
eventListener.updated(serverName);
}
}
}


public void removeConnectionProperties(String sessionName)
{
if (eventListener != null)
Expand All @@ -76,7 +144,6 @@ public void setConnectionProperties( String sessionName, Properties properties)
if ( sessionName == null || sessionName.equals(""))
{
sessionName = SAP_SERVER;

}
if (!Environment.isDestinationDataProviderRegistered())
{
Expand Down
130 changes: 130 additions & 0 deletions java/src/main/java/com/genexus/sap/DocumentClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.genexus.sap;

import java.io.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import com.sap.conn.jco.AbapClassException;
import com.sap.conn.jco.AbapException;

import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;
import com.sap.conn.jco.server.JCoServer;
import com.sap.conn.jco.server.JCoServerContext;
import com.sap.conn.jco.server.JCoServerContextInfo;
import com.sap.conn.jco.server.JCoServerErrorListener;
import com.sap.conn.jco.server.JCoServerExceptionListener;

import com.sap.conn.jco.server.JCoServerFunctionHandler;

public class DocumentClient {
private static final int BLOB_LENGTH = 1022;

static class ErrorHandler implements JCoServerErrorListener, JCoServerExceptionListener {

@Override
public void serverExceptionOccurred(JCoServer server, String connectionID, JCoServerContextInfo serverCtx, Exception error) {
// Technical problem in server connection (network, logon data, etc.)
error.printStackTrace();
}

@Override
public void serverErrorOccurred(JCoServer server, String connectionID, JCoServerContextInfo serverCtx, Error error) {
// Technical problem in server connection (out-of-memory, etc.)
error.printStackTrace();
}

}

// BAPI_DOCUMENT_CHECKOUTVIEW2 will send the file data via this function module.
static class FTP_R3_TO_CLIENTHandler implements JCoServerFunctionHandler {

@Override
public void handleRequest(JCoServerContext serverCtx, JCoFunction function) throws AbapException, AbapClassException {
String fname;
int length;
JCoTable blob;

// 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.
JCoParameterList imports = function.getImportParameterList();
fname = imports.getString("FNAME");
length = imports.getInt("LENGTH");
blob = function.getTableParameterList().getTable("BLOB");
FileOutputStream out = null;
try {
out = new FileOutputStream(fname);
boolean hasNextRow = false;
if (!blob.isEmpty()){
hasNextRow = true;
blob.firstRow();
}
while (length > BLOB_LENGTH){
if (hasNextRow){
out.write(blob.getByteArray(0), 0, BLOB_LENGTH);
length -= BLOB_LENGTH;
hasNextRow = blob.nextRow();
}
else throw new IOException("Not enough data in table BLOB ("+String.valueOf(BLOB_LENGTH * blob.getNumRows())+") for requested file size (" + String.valueOf(length) + ")");
}
if (length > 0){
if (hasNextRow) out.write(blob.getByteArray(0), 0, length);
else throw new IOException("Not enough data in table BLOB ("+String.valueOf(BLOB_LENGTH * blob.getNumRows())+") for requested file size (" + String.valueOf(length) + ")");
}
}
catch (IOException e) {
e.printStackTrace();
function.getExportParameterList().setValue("ERROR", 3);
}
finally{
if (out != null){
try{
out.close();
}
catch (IOException ioe){}
}
}
}

}

static class FTP_CLIENT_TO_R3Handler implements JCoServerFunctionHandler {
@Override
public void handleRequest(JCoServerContext serverCtx, JCoFunction function) throws AbapException, AbapClassException {
String fname;

fname = function.getImportParameterList().getString("FNAME");
fname = fname.replace("#","");

try (InputStream source = Files.newInputStream(Paths.get(fname));) {

byte[] file2 = new byte[BLOB_LENGTH];
int bytesread;
int totallenght =0;

InputStream source2 = Files.newInputStream(Paths.get(fname));

JCoTable blobtable = function.getTableParameterList().getTable("BLOB");
while((bytesread = source2.read(file2,0,file2.length)) >0 )
{

blobtable.appendRow();
blobtable.setValue("LINE", file2);
totallenght += bytesread;
}

JCoParameterList exports = function.getExportParameterList();
exports.setValue("LENGTH", totallenght);
}
catch (IOException e) {
e.printStackTrace();
function.getExportParameterList().setValue("ERROR", 3);
}
}
}



}
Loading