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;

import io.delphiplatform.api.util.JpaUtils;

public class SingleValuePropertySpecification<T> extends OperationOnPropertySpecification<T> {

    private final List<String> propertyPath;
    private final Object searchValue;

    public SingleValuePropertySpecification(List<String> propertyPath, Object searchValue, List<String> joins) {
        super(joins);
        this.propertyPath = propertyPath;
        this.searchValue = searchValue;
    }

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

    @Override
    protected Predicate doOperation(CriteriaBuilder cb, Path<T> path) {
        return cb.equal(JpaUtils.getPath(path, propertyPath), searchValue);
    }

}
