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

com.kenshoo.pl.data.ImpersonatorTable Maven / Gradle / Ivy

Go to download

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

The newest version!
package com.kenshoo.pl.data;

import com.google.common.base.*;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
import com.kenshoo.jooq.DataTable;
import com.kenshoo.jooq.FieldAndValue;
import org.apache.commons.lang3.ArrayUtils;
import org.jooq.*;
import org.jooq.impl.TableImpl;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Objects;

import static org.jooq.impl.Internal.createForeignKey;
import static org.jooq.impl.Internal.createUniqueKey;

public class ImpersonatorTable extends TableImpl implements DataTable {

    public static final String TEMP_TABLE_PREFIX = "tmp_";

    private final DataTable realTable;

    private final Supplier> primaryKeySupplier = Suppliers.memoize(new PrimaryKeySupplier());
    private final Supplier>> foreignKeysSupplier = Suppliers.memoize(new ForeignKeysSupplier());

    public ImpersonatorTable(DataTable realTable) {
        super(TEMP_TABLE_PREFIX + realTable.getName());
        this.realTable = realTable;
    }

    public  TableField createField(Field realTableField) {
        return createField(realTableField.getName(), realTableField.getDataType());
    }

    @Override
    public Collection> getVirtualPartition() {
        return Collections2.transform(realTable.getVirtualPartition(), new Function, FieldAndValue>() {
            @Override
            public FieldAndValue apply(FieldAndValue input) {
                return transform(input);
            }
            private  FieldAndValue transform(FieldAndValue input) {
                return new FieldAndValue<>(getField(input.getField()), input.getValue());
            }
        });
    }

    @Override
    public UniqueKey getPrimaryKey() {
        return primaryKeySupplier.get();
    }

    @Override
    public List> getReferences() {
        return foreignKeysSupplier.get();
    }

    public  TableField getField(final TableField realTableField) {
        //noinspection unchecked
        return (TableField) Iterables.tryFind(Arrays.asList(fields()), new Predicate>() {
            @Override
            public boolean apply(Field field) {
                return field.getName().equals(realTableField.getName());
            }
        }).orNull();
    }

    private class PrimaryKeySupplier implements Supplier> {
        @Override
        public UniqueKey get() {
            return createPK();
        }
    }

    private class ForeignKeysSupplier implements Supplier>> {

        @Override
        public List> get() {
            return realTable.getReferences().stream()
                    .map(ImpersonatorTable.this::createFK)
                    .filter(Objects::nonNull)
                    .collect(Collectors.toUnmodifiableList());
        }
    }

    private UniqueKey createPK() {
        UniqueKey primaryKey = realTable.getPrimaryKey();
        if (primaryKey == null) {
            return null;
        }
        TableField[] transformedFields = transformFields(primaryKey.getFieldsArray());
        if (ArrayUtils.contains(transformedFields, null)) {
            // We don't have some of the fields of realTable PK
            return null;
        }
        //noinspection unchecked
        return createUniqueKey(ImpersonatorTable.this, transformedFields);
    }

    private ForeignKey createFK(ForeignKey foreignKey) {
        TableField[] fields = transformFields(foreignKey.getFieldsArray());
        if (ArrayUtils.contains(fields, null)) {
            return null;
        }
        UniqueKey uniqueKey = cloneUniqueKey(foreignKey.getKey());
        return createForeignKey(uniqueKey, uniqueKey.getTable(), fields);
    }

    @SuppressWarnings("unchecked")
    private static UniqueKey cloneUniqueKey(UniqueKey uniqueKey) {
        return createUniqueKey((Table) uniqueKey.getTable(), (TableField[]) uniqueKey.getFieldsArray());
    }

    private TableField[] transformFields(TableField[] originalFields) {
        //noinspection unchecked
        return Arrays.stream(originalFields)
                .map(this::getField)
                .toArray(TableField[]::new);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy