package io.delphiplatform.api.v3.rdb.service.specification;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;

public class LessThanOrEqualToPropertySpecification<T, V extends Comparable<V>> extends
    OperationOnPropertySpecification<T> {

    private final String propertyName;
    private final V compareValue;

    public LessThanOrEqualToPropertySpecification(String propertyName, V compareValue, List<String> joins) {
        super(joins);
        this.propertyName = propertyName;
        this.compareValue = compareValue;
    }

    @Override
    protected boolean canProducePredicate() {
        return compareValue != null;
    }

    @Override
    protected Predicate doOperation(CriteriaBuilder cb, Path<T> path) {
        return cb.lessThanOrEqualTo(path.get(propertyName), compareValue);
    }

}
