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

com.kenshoo.pl.entity.internal.ChildrenIdFetcher 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.internal;

import com.kenshoo.jooq.DataTable;
import com.kenshoo.jooq.QueryExtension;
import com.kenshoo.pl.entity.UniqueKey;
import com.kenshoo.pl.entity.*;
import com.kenshoo.pl.entity.internal.fetch.QueryBuilder;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.jooq.lambda.Seq;

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

import static java.util.stream.Collectors.toList;


public class ChildrenIdFetcher, CHILD extends EntityType> {

    private final DSLContext jooq;

    public ChildrenIdFetcher(DSLContext jooq) {
        this.jooq = jooq;
    }

    public Stream>
    fetch(Collection> parentIds, UniqueKey childKey) {

        if (parentIds.isEmpty()) {
            return Stream.empty();
        }

        final PARENT parentType = first(parentIds).getUniqueKey().getEntityType();
        final CHILD childType = childKey.getEntityType();
        final EntityType.ForeignKey keyToParent = childType.getKeyTo(parentType);
        final UniqueKey parentKey = first(parentIds).getUniqueKey();
        final UniqueKey childFK = new UniqueKey<>(keyToParent.from());
        final DataTable childTable = childType.getPrimaryTable();

        final SelectFinalStep query = jooq.select(getTableFields(Seq.concat(
                Seq.of(parentKey.getFields()),
                Seq.of(childKey.getFields()),
                Seq.of(childFK.getFields())))
        )
                .from(childTable)
                .join(parentType.getPrimaryTable())
                .on(everyFieldOf(keyToParent));

        final QueryExtension> queryExtender = new QueryBuilder(jooq).addIdsCondition(query, childTable, parentKey, parentIds);
        final ResultQuery finalQuery = queryExtender.getQuery();
        return finalQuery.stream()
                .map(record -> readIdentifiers(record, parentKey, childKey, childFK))
                .onClose(() -> {
                    queryExtender.close();
                    query.close();
                });
    }

    private Condition everyFieldOf(EntityType.ForeignKey keyToParent) {
        // TODO: Test keyToParent with more than one field
        List conditions = keyToParent.references.stream().map(ref -> dbFieldOf(ref.v1).eq(dbFieldOf(ref.v2))).collect(toList());
        return DSL.and(conditions);
    }

    private Field dbFieldOf(EntityField field) {
        return field.getDbAdapter().getTableFields().findFirst().get();
    }

    private FullIdentifier
    readIdentifiers(Record record, UniqueKey parentKey, UniqueKey childKey, UniqueKey childFK) {
        return new FullIdentifier<>(
                parse(parentKey, record),
                parse(childKey, record),
                parse(childFK, record)
        );
    }

    private > Identifier parse(UniqueKey key, Record values) {
        final Object[] ids = Seq.of(key.getFields())
                .map(field -> {
                    Stream dbValues = field.getDbAdapter().getTableFields().map(values::get);
                    return field.getDbAdapter().getFromRecord(dbValues.iterator());
                })
                .toArray();
        return new UniqueKeyValue<>(key, ids);
    }

    private Collection>
    getTableFields(Stream> entityFields) {
        return entityFields.flatMap(f -> f.getDbAdapter().getTableFields()).collect(toList());
    }

    private  T first(Collection collection) {
        return collection.iterator().next();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy