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

cz.encircled.joiner.query.JoinerQueryBase Maven / Gradle / Ivy

package cz.encircled.joiner.query;

import com.querydsl.core.types.EntityPath;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.Predicate;
import cz.encircled.joiner.query.join.J;
import cz.encircled.joiner.query.join.JoinDescription;
import cz.encircled.joiner.util.Assert;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Implementation of joiner query with {@link com.querydsl.core.Tuple non-tuple} result
 *
 * @author Kisel on 13.9.2016.
 */
public class JoinerQueryBase implements JoinerQuery, JoinRoot {

    private final EntityPath from;
    private Expression returnProjection;

    private Predicate where;

    /**
     * Alias to join
     */
    private Map joins = new LinkedHashMap<>(8);

    private Set joinGraphs = new LinkedHashSet<>();

    private boolean distinct = true;

    private Path groupBy;

    private Predicate having;

    private LinkedHashMap> hints = new LinkedHashMap<>(2);

    private List features = new ArrayList<>(2);

    private Long offset;

    private Long limit;

    private List orders = new ArrayList<>(2);

    private boolean isCount;

    public JoinerQueryBase(EntityPath from) {
        this.from = from;
    }

    JoinerQueryBase(EntityPath from, boolean isCount) {
        this.from = from;
        this.isCount = isCount;
    }

    public JoinerQueryBase(EntityPath from, Expression returnProjection) {
        this.from = from;
        this.returnProjection = returnProjection;
    }

    @Override
    public Predicate getWhere() {
        return where;
    }

    @Override
    public JoinerQueryBase distinct(boolean isDistinct) {
        distinct = isDistinct;
        return this;
    }

    @Override
    public JoinerQueryBase groupBy(Path groupBy) {
        this.groupBy = groupBy;
        return this;
    }

    @Override
    public Path getGroupBy() {
        return groupBy;
    }

    @Override
    public boolean isDistinct() {
        return distinct;
    }

    @Override
    public JoinerQueryBase where(Predicate where) {
        this.where = where;
        return this;
    }

    @Override
    public JoinerQueryBase having(Predicate having) {
        this.having = having;
        return this;
    }

    @Override
    public Predicate getHaving() {
        return having;
    }

    @Override
    public EntityPath getFrom() {
        return from;
    }

    @Override
    public Set getJoinGraphs() {
        return joinGraphs;
    }

    @Override
    public Map getAllJoins() {
        return joins;
    }

    @Override
    public Collection getJoins() {
        return joins.values();
    }

    @Override
    public JoinerQueryBase joinGraphs(final String... names) {
        Collections.addAll(joinGraphs, names);

        return this;
    }

    @Override
    public JoinerQueryBase joinGraphs(final Enum... names) {
        Collections.addAll(joinGraphs, names);

        return this;
    }

    @Override
    public JoinerQueryBase joinGraphs(Collection names) {
        Assert.notNull(names);

        joinGraphs.addAll(names);

        return this;
    }

    @Override
    public JoinerQueryBase joins(EntityPath... paths) {
        for (EntityPath path : paths) {
            Assert.notNull(path);

            addJoin(J.left(path));
        }

        return this;
    }

    @Override
    public JoinerQueryBase joins(JoinDescription... joins) {
        for (JoinDescription join : joins) {
            Assert.notNull(join);

            addJoin(join);
        }

        return this;
    }

    @Override
    public JoinerQueryBase joins(Collection joins) {
        Assert.notNull(joins);

        joins.forEach(this::addJoin);

        return this;
    }

    @Override
    public JoinerQueryBase addHint(String hint, Object value) {
        Assert.notNull(hint);

        hints.computeIfAbsent(hint, h -> new ArrayList<>(2));
        hints.get(hint).add(value);

        return this;
    }

    @Override
    public JoinerQueryBase addFeatures(QueryFeature... features) {
        Assert.notNull(features);

        Collections.addAll(this.features, features);
        return this;
    }

    @Override
    public JoinerQueryBase addFeatures(Collection features) {
        Assert.notNull(features);

        this.features.addAll(features);
        return this;
    }

    @Override
    public List getFeatures() {
        return features;
    }

    @Override
    public LinkedHashMap> getHints() {
        return hints;
    }

    @Override
    public Expression getReturnProjection() {
        return returnProjection;
    }

    @Override
    public JoinerQueryBase offset(Long offset) {
        this.offset = offset;
        return this;
    }

    @Override
    public Long getOffset() {
        return offset;
    }

    @Override
    public JoinerQueryBase limit(Long limit) {
        this.limit = limit;
        return this;
    }

    @Override
    public Long getLimit() {
        return limit;
    }

    @Override
    public JoinerQueryBase asc(Expression orderBy) {
        Assert.notNull(orderBy);

        orders.add(new QueryOrder<>(true, orderBy));
        return this;
    }

    @Override
    public JoinerQueryBase desc(Expression orderBy) {
        Assert.notNull(orderBy);

        orders.add(new QueryOrder<>(false, orderBy));
        return this;
    }

    @Override
    public List getOrder() {
        return orders;
    }

    @Override
    public JoinerQuery copy() {
        JoinerQueryBase copy = Q.select(returnProjection)
                .from(from)
                .joinGraphs(new LinkedHashSet<>(joinGraphs))
                .joins(getJoins().stream().map(JoinDescription::copy).collect(Collectors.toList()))
                .addFeatures(new ArrayList<>(getFeatures()))
                .where(where)
                .offset(offset)
                .limit(limit)
                .groupBy(groupBy)
                .having(this.having);

        copy.isCount = isCount;
        copy.orders = new ArrayList<>(orders);

        // TODO deep-deep copy?
        copy.hints = new LinkedHashMap<>(hints);

        return copy;
    }

    @Override
    public boolean isCount() {
        return isCount;
    }

    public void count() {
        isCount = true;
    }

    @Override
    public String toString() {
        return "JoinerQueryBase{" +
                "from=" + from +
                ", returnProjection=" + returnProjection +
                ", where=" + where +
                ", joins=" + joins +
                ", joinGraphs=" + joinGraphs +
                ", distinct=" + distinct +
                ", groupBy=" + groupBy +
                ", having=" + having +
                ", hints=" + hints +
                ", features=" + features +
                ", offset=" + offset +
                ", limit=" + limit +
                ", orders=" + orders +
                ", isCount=" + isCount +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof JoinerQueryBase)) return false;
        JoinerQueryBase that = (JoinerQueryBase) o;
        return distinct == that.distinct &&
                isCount == that.isCount &&
                Objects.equals(from, that.from) &&
                Objects.equals(returnProjection, that.returnProjection) &&
                Objects.equals(where, that.where) &&
                Objects.equals(joins, that.joins) &&
                Objects.equals(joinGraphs, that.joinGraphs) &&
                Objects.equals(groupBy, that.groupBy) &&
                Objects.equals(having, that.having) &&
                Objects.equals(hints, that.hints) &&
                Objects.equals(features, that.features) &&
                Objects.equals(offset, that.offset) &&
                Objects.equals(limit, that.limit) &&
                Objects.equals(orders, that.orders);
    }

    @Override
    public int hashCode() {

        return Objects.hash(from, returnProjection, where, joins, joinGraphs, distinct, groupBy, having, hints, features, offset, limit, orders, isCount);
    }
}