All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.saro.sap.jco.SapUtils Maven / Gradle / Ivy

The newest version!
package me.saro.sap.jco;

import com.sap.conn.jco.JCoField;
import com.sap.conn.jco.JCoTable;
import me.saro.kit.Streams;
import me.saro.kit.functions.ThrowableBiConsumer;
import me.saro.kit.functions.ThrowableConsumer;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Supplier;

/**
 * Sap Util
 * @author      PARK Yong Seo
 * @since       0.1
 */
public class SapUtils {
    
    // this class have only static method
    private SapUtils() {
    }
    
    /**
     * JCoTable to List[Map[String, Object]]
     * @param table
     * jCo table
     * @return JCoTable to List>
     */
    public static List> toMapList(JCoTable table) {
        List> rv = new ArrayList<>();

        if (!table.isEmpty()) {
            table.firstRow();
            do {
                Map map = new LinkedHashMap<>();
                Streams.toStream(table, false).forEach(field -> map.put(field.getName(), field.getValue()));
                rv.add(map);
            } while (table.nextRow());
        }
        
        return rv;
    }
    
    /**
     * JCoTable to custom class
     * @param table
     * jCo table
     * @param createRow
     * create custom class row
     * @param bindField
     * bind field in custom class row
     * @return JCoTable to Custom Class
     */
    public static  List toClass(JCoTable table, Supplier createRow, ThrowableBiConsumer bindField) {
        List rv = new ArrayList<>();

        if (!table.isEmpty()) {
            table.firstRow();
            do {
                R r = createRow.get();
                Streams.toStream(table, false).forEach(ThrowableConsumer.wrap(field -> bindField.accept(r, field)));
                rv.add(r);
            } while (table.nextRow());
        }
        
        return rv;
    }
    
    /**
     * date filter
     * "value" is "java.util.Date" then return "format String" 
* "value" is not "java.util.Date" then return "value" * @param value data * @param format Date format you want * @return result */ public static Object filterDate(Object value, String format) { if (value != null && "java.util.Date".equals(value.getClass().getName())) { return new SimpleDateFormat(format).format((Date)value); } return value; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy