gr.abiss.calipso.jpasearch.specifications.BooleanPredicateFactory.java Source code

Java tutorial

Introduction

Here is the source code for gr.abiss.calipso.jpasearch.specifications.BooleanPredicateFactory.java

Source

/**
 *
 *
 * Copyright (c) 2007 - 2013 www.Abiss.gr
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package gr.abiss.calipso.jpasearch.specifications;

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

import org.apache.commons.lang3.BooleanUtils;
import org.springframework.data.domain.Persistable;

public class BooleanPredicateFactory implements IPredicateFactory<Boolean> {

    public BooleanPredicateFactory() {
    }

    /**
     * @see gr.abiss.calipso.jpasearch.jpa.search.specifications.IPredicateFactory#addPredicate(javax.persistence.criteria.Root,
     *      javax.persistence.criteria.CriteriaBuilder, java.lang.String,
     *      java.lang.Class, java.lang.String[])
     */
    @Override
    public Predicate getPredicate(Root<Persistable> root, CriteriaBuilder cb, String propertyName, Class fieldType,
            String[] propertyValues) {
        Predicate predicate = null;
        if (!Boolean.class.isAssignableFrom(fieldType)) {
            throw new IllegalArgumentException(
                    fieldType + " is not a subclass of Boolean for field: " + propertyName);
        }

        Boolean b = BooleanUtils.toBooleanObject(propertyValues[0]);
        if (b == null) {
            b = Boolean.FALSE;
        }

        predicate = cb.equal(root.<Boolean>get(propertyName), b);
        return predicate;
    }
}