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

com.kenshoo.pl.entity.spi.helpers.EntitiesTempTableCreator Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
package com.kenshoo.pl.entity.spi.helpers;

import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import com.kenshoo.jooq.DataTable;
import com.kenshoo.jooq.TempTableHelper;
import com.kenshoo.jooq.TempTableResource;
import com.kenshoo.pl.data.ImpersonatorTable;
import com.kenshoo.pl.entity.EntityField;
import com.kenshoo.pl.entity.EntityType;
import com.kenshoo.pl.entity.FieldsValueMap;
import org.jooq.DSLContext;

import java.util.Collection;
import java.util.stream.Stream;

public class EntitiesTempTableCreator {

    private final DSLContext dslContext;

    public EntitiesTempTableCreator(DSLContext dslContext) {
        this.dslContext = dslContext;
    }

    public > TempTableResource createTempTable(final Collection> fields, final Collection> fieldsValueMaps) {
        Preconditions.checkArgument(!fields.isEmpty(), "fields is empty");
        //noinspection ConstantConditions
        DataTable primaryTable = Iterables.getFirst(fields, null).getDbAdapter().getTable();
        ImpersonatorTable impersonatorTable = new ImpersonatorTable(primaryTable);
        fields.stream().flatMap(field -> field.getDbAdapter().getTableFields()).forEach(impersonatorTable::createField);
        return TempTableHelper.tempInMemoryTable(dslContext, impersonatorTable, batchBindStep -> {
            fieldsValueMaps.forEach(entityChange -> batchBindStep.bind(fields.stream().flatMap(field -> getDbValues(entityChange, field)).toArray()));
        });
    }

    private , T> Stream getDbValues(FieldsValueMap fieldsValueMap, EntityField field) {
        return field.getDbAdapter().getDbValues(fieldsValueMap.get(field));
    }
}