saro-lab / sap-jco-manager

sap jco manager - maven (using the sapjco3.jar)
Apache License 2.0
38 stars 12 forks source link

SAP JCo (Java Connector) Manager

Maven Central GitHub license

QUICK START

Dependency injection

maven

<dependency>
  <groupId>me.saro</groupId>
  <artifactId>sap-jco-manager</artifactId>
  <version>3.0.14.7</version>
</dependency>

gradle

compile 'me.saro:sap-jco-manager:3.0.14.7'

Download and include sapjco3

Official download link (required SAP account)

example

basic

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoTable;

import me.saro.sap.jco.SapFunction;
import me.saro.sap.jco.SapFunctionResult;
import me.saro.sap.jco.SapManager;
import me.saro.sap.jco.SapManagerBuilderOption;

public class SapManagerNormalTest {

    // example connect
    public SapManager getSapManager() throws JCoException, IOException {
        return SapManager
                .builder()
                .set(SapManagerBuilderOption.ASHOST, "host") // AS host
                .set(SapManagerBuilderOption.MSSERV, "9999") // MS port [AS, MS is MSSERV, GW is JCO_GWSERV]
                .set(SapManagerBuilderOption.SYSNR, "01") // system number
                .set(SapManagerBuilderOption.GROUP, "Group Name") // group
                .set(SapManagerBuilderOption.LANG, "KO") // language code
                .set(SapManagerBuilderOption.CLIENT, "100") // client number
                .set(SapManagerBuilderOption.USER, "user") // user
                .set(SapManagerBuilderOption.PASSWD, "password") // password
                .build();
    }

    // example basic
    public void basic() throws JCoException, IOException {
        // connect
        SapManager sap = getSapManager();

        // load sap function
        SapFunction function = sap.getFunction("SAP_RFC_FUNC_NAME");

        // set parameters
        function.getImportParameterList().setValue("param1", "text");
        function.getImportParameterList().setValue("param2", 1);
        function.getImportParameterList().setValue("param3", true);

        // set table parameters [example table parameter name is param4]
        JCoTable requestTableParameter = function.getImportTableParameter("param4");
        List.of("value1", "value2", "value3").forEach(e -> {
            requestTableParameter.appendRow();
            requestTableParameter.setValue("field1", "text");
            requestTableParameter.setValue("field2", e);
            requestTableParameter.setValue("field3", false);
        });

        // execute
        SapFunctionResult result = function.execute();

        // get result parameters
        result.getExportParameterList().getString("param1");
        result.getExportParameterList().getInt("param2");
        result.getExportParameterList().getDate("param3");

        // get result tables
        List<Map<String, Object>> resultTable = result.getTable("SAP_RESULT_TABLE_NAME");

        // print result table
        System.out.println("print SAP_RESULT_TABLE_NAME");
        resultTable.forEach(row -> {
            System.out.println("=============================================");
            row.forEach( (key, value) -> System.out.println(key + " : " + value) );
        });
    }
}

multiple thread

import java.io.IOException;
import java.util.List;

import com.sap.conn.jco.JCoException;

import me.saro.sap.jco.SapFunctionResult;
import me.saro.sap.jco.SapManager;
import me.saro.sap.jco.SapManagerBuilderOption;

public class SapManagerMultipleThreadTest {

    // example connect
    public SapManager getSapManager() throws JCoException, IOException {
        return SapManager
                .builder()
                .set(SapManagerBuilderOption.ASHOST, "host") // AS host
                .set(SapManagerBuilderOption.MSSERV, "9999") // MS port [AS, MS is MSSERV, GW is JCO_GWSERV]
                .set(SapManagerBuilderOption.SYSNR, "01") // system number
                .set(SapManagerBuilderOption.GROUP, "Group Name") // group
                .set(SapManagerBuilderOption.LANG, "KO") // language code
                .set(SapManagerBuilderOption.CLIENT, "100") // client number
                .set(SapManagerBuilderOption.USER, "user") // user
                .set(SapManagerBuilderOption.PASSWD, "password") // password
                .build();
    }

    // example multiple thread
    public void multipleThread() throws JCoException, IOException {
        // example user no -> name
        List<Integer> userNoList = List.of(1, 2, 3, 4);

        // connect
        SapManager sap = getSapManager();

        // use 10 thread
        // executeAllThreads method is blocking until complete all a tasks
        List<String> userNameList = sap.getFunctionTemplate("USER_TABLE").executeAllThreads(10, userNoList, (function, userNo) -> {

            function.getImportParameterList().setValue("user_no", userNo);

            SapFunctionResult result = function.execute();

            String name = result.getExportParameterList().getString("USER_NAME");

            return name;
        });

        // print
        System.out.println("user names");
        System.out.println(userNameList);
    }
}

custom class

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.sap.conn.jco.JCoException;

import me.saro.sap.jco.SapFunction;
import me.saro.sap.jco.SapFunctionResult;
import me.saro.sap.jco.SapManager;
import me.saro.sap.jco.SapManagerBuilderOption;

public class SapManagerCustomClassTest {

    // example connect
    public SapManager getSapManager() throws JCoException, IOException {
        return SapManager
                .builder()
                .set(SapManagerBuilderOption.ASHOST, "host") // AS host
                .set(SapManagerBuilderOption.MSSERV, "9999") // MS port [AS, MS is MSSERV, GW is JCO_GWSERV]
                .set(SapManagerBuilderOption.SYSNR, "01") // system number
                .set(SapManagerBuilderOption.GROUP, "Group Name") // group
                .set(SapManagerBuilderOption.LANG, "KO") // language code
                .set(SapManagerBuilderOption.CLIENT, "100") // client number
                .set(SapManagerBuilderOption.USER, "user") // user
                .set(SapManagerBuilderOption.PASSWD, "password") // password
                .build();
    }

    // example recv table to custom class
    public void recvTableToCustomClass() throws JCoException, IOException {
        // connect
        SapManager sap = getSapManager();

        SapFunction function = sap.getFunction("CALL_ALL_USER_LIST");
        SapFunctionResult result = function.execute();

        // table to custom class list
        List<User> userList = result.getTable("USER_TABLE", User::new, (user, field) -> {
            switch (field.getName()) {
                case "first_name" :
                    user.setFirstName(field.getString());
                break;
                case "last_name" :
                    user.setLastName(field.getString());
                break;
                case "birth" :
                    user.setBirth(new SimpleDateFormat("yyyyMMdd").format(field.getDate()));
                break;
                case "join_date" :
                    user.setJoinDate(field.getDate());
                break;
            }
        });

        // print
        System.out.println("print user list");
        userList.forEach(System.out::println);
    }

    public static class User {
        String firstName;
        String lastName;
        String birth;
        Date joinDate;
        public String getFirstName() {
            return firstName;
        }
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
        public String getLastName() {
            return lastName;
        }
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
        public String getBirth() {
            return birth;
        }
        public void setBirth(String birth) {
            this.birth = birth;
        }
        public Date getJoinDate() {
            return joinDate;
        }
        public void setJoinDate(Date joinDate) {
            this.joinDate = joinDate;
        }
        public String toString() {
            return firstName + " " + lastName + " " + birth + " " + joinDate;
        }
    }
}

Star History

Star History Chart