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

net.n2oapp.security.admin.impl.service.specification.AccountSpecifications Maven / Gradle / Ivy

There is a newer version: 8.0.1
Show newest version
package net.n2oapp.security.admin.impl.service.specification;

import net.n2oapp.security.admin.api.criteria.AccountCriteria;
import net.n2oapp.security.admin.impl.entity.AccountEntity;
import net.n2oapp.security.admin.impl.entity.AccountEntity_;
import net.n2oapp.security.admin.impl.entity.UserEntity_;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import static java.util.Objects.nonNull;

public class AccountSpecifications implements Specification {

    private final AccountCriteria criteria;

    public AccountSpecifications(AccountCriteria criteria) {
        this.criteria = criteria;
    }

    @Override
    public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder builder) {
        Predicate predicate = builder.and();
        if (nonNull(criteria.getUserId()))
            predicate = builder.equal(root.get(AccountEntity_.user).get(UserEntity_.id), criteria.getUserId());
        if (nonNull(criteria.getUsername()))
            predicate = builder.equal(root.get(AccountEntity_.user).get(UserEntity_.username), criteria.getUsername());


        return predicate;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy