Java Code Examples for javax.persistence.criteria.CriteriaBuilder.like()

The following are Jave code examples for showing how to use like() of the javax.persistence.criteria.CriteriaBuilder class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: plumdo-work   File: SimpleExpression.java   Source Code and License Vote up 17 votes
@SuppressWarnings({ "rawtypes", "unchecked" })  
public Predicate toPredicate(Root<?> root, CriteriaQuery<?> query,  
        CriteriaBuilder builder) {  
    Path expression = null;  
    if(fieldName.contains(".")){  
        String[] names = StringUtils.split(fieldName, ".");  
        expression = root.get(names[0]);  
        for (int i = 1; i < names.length; i++) {  
            expression = expression.get(names[i]);  
        }  
    }else{  
        expression = root.get(fieldName);  
    }  
    
    switch (operator) {  
    case EQ:  
        return builder.equal(expression, value);  
    case NE:  
        return builder.notEqual(expression, value);  
    case LIKE:  
        return builder.like((Expression<String>) expression, "%" + value + "%");  
    case LT:  
        return builder.lessThan(expression, (Comparable) value);  
    case GT:  
        return builder.greaterThan(expression, (Comparable) value);  
    case LTE:  
        return builder.lessThanOrEqualTo(expression, (Comparable) value);  
    case GTE:  
        return builder.greaterThanOrEqualTo(expression, (Comparable) value);  
    default:  
        return null;  
    }  
}
 
Example 2
Project: owl   File: TestSuiteFilterSpecification.java   Source Code and License Vote up 16 votes
@Override
public Predicate toPredicate(Root<TestSuite> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
	Predicate result = null;

	if (StringUtils.isNotEmpty(filter.getSuite())) {
		result = criteriaBuilder.like(criteriaBuilder.lower(root.get(SUITE_PROPERTY)),
				buildLikeValue(filter.getSuite()));
	}

	return result;
}
 
Example 3
Project: OperatieBRP   File: LeveringsautorisatieFilterFactory.java   Source Code and License Vote up 6 votes
@Override
public Predicate toPredicate(final Root<?> leveringsautorisatieRoot, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
    final Root<Dienstbundel> dienstbundelRoot = query.from(Dienstbundel.class);

    // Join dienst op leveringsautorisatie
    final Predicate joinPredicateLeveringsautorisatie = leveringsautorisatieRoot.get(ID).in(dienstbundelRoot.get(LEVERINGSAUTORISATIE).get(ID));

    // Dienstbundel
    final Expression<String> attributeExpression = cb.lower(cb.function(UNACCENT, String.class, dienstbundelRoot.get("naam")));
    final Expression<String> valueExpression =
            cb.lower(
                    cb.function(
                            UNACCENT,
                            String.class,
                            cb.concat(cb.concat(cb.literal(WILDCARD), cb.literal(value.toString())), cb.literal(WILDCARD))));
    final Predicate dienstBundelPredicate = cb.like(attributeExpression, valueExpression);

    return cb.and(joinPredicateLeveringsautorisatie, dienstBundelPredicate);
}
 
Example 4
Project: OperatieBRP   File: PersoonFilterFactory.java   Source Code and License Vote up 6 votes
@Override
public Predicate toPredicate(final Root<?> persoonRoot, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
    final Root<PersoonAdres> persoonAdressenRoot = query.from(PersoonAdres.class);
    final Predicate joinPredicateAdressen = persoonRoot.get(ID).in(persoonAdressenRoot.get(PERSOON).get(ID));
    final Predicate adresPredicate;
    if (likePredicate) {
        final Expression<String> attributeExpression = cb.lower(cb.function(UNACCENT, String.class, persoonAdressenRoot.get(field)));
        final Expression<String> valueExpression =
                cb.lower(
                        cb.function(
                                UNACCENT,
                                String.class,
                                cb.concat(cb.concat(cb.literal(WILDCARD), cb.literal(value.toString())), cb.literal(WILDCARD))));
        adresPredicate = cb.like(attributeExpression, valueExpression);
    } else {
        adresPredicate = cb.equal(persoonAdressenRoot.get(field), value);
    }
    return cb.and(joinPredicateAdressen, adresPredicate);
}
 
Example 5
Project: sctalk   File: SimpleExpression.java   Source Code and License Vote up 6 votes
@SuppressWarnings({ "rawtypes", "unchecked" })
public Predicate toPredicate(Root<?> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
    Path expression = null;
    if (fieldName.contains(".")) {
        
        System.out.println(root);
        String[] names = StringUtils.split(fieldName, ".");
        expression = root.get(names[0]);
        for (int i = 1; i < names.length; i++) {
            expression = expression.get(names[i]);
        }
    } else {
        expression = root.get(fieldName);
    }

    switch (operator) {
    case EQ:
        return builder.equal(expression, value);
    case NE:
        return builder.notEqual(expression, value);
    case LIKE:
        return builder.like((Expression<String>) expression, "%" + value + "%");
    case RLIKE:
        return builder.like((Expression<String>) expression, value + "%");
    case LLIKE:
        return builder.like((Expression<String>) expression, value + "%");
    case LT:
        return builder.lessThan(expression, (Comparable) value);
    case GT:
        return builder.greaterThan(expression, (Comparable) value);
    case LTE:
        return builder.lessThanOrEqualTo(expression, (Comparable) value);
    case GTE:
        return builder.greaterThanOrEqualTo(expression, (Comparable) value);
    case ISNULL:
        return builder.isNull(expression);
    case NOTNULL:
        return builder.isNotNull(expression);
    default:
        return null;
    }
}
 
Example 6
Project: plumdo-stock   File: SimpleExpression.java   Source Code and License Vote up 5 votes
@SuppressWarnings({ "rawtypes", "unchecked" })  
public Predicate toPredicate(Root<?> root, CriteriaQuery<?> query,  
        CriteriaBuilder builder) {  
    Path expression = null;  
    if(fieldName.contains(".")){  
        String[] names = StringUtils.split(fieldName, ".");  
        expression = root.get(names[0]);  
        for (int i = 1; i < names.length; i++) {  
            expression = expression.get(names[i]);  
        }  
    }else{  
        expression = root.get(fieldName);  
    }  
    
    switch (operator) {  
    case EQ:  
        return builder.equal(expression, value);  
    case NE:  
        return builder.notEqual(expression, value);  
    case LIKE:  
        return builder.like((Expression<String>) expression, "%" + value + "%");  
    case LT:  
        return builder.lessThan(expression, (Comparable) value);  
    case GT:  
        return builder.greaterThan(expression, (Comparable) value);  
    case LTE:  
        return builder.lessThanOrEqualTo(expression, (Comparable) value);  
    case GTE:  
        return builder.greaterThanOrEqualTo(expression, (Comparable) value);  
    default:  
        return null;  
    }  
}
 
Example 7
Project: CriteriaBuilder   File: CriteriaServiceImpl.java   Source Code and License Vote up 5 votes
@SuppressWarnings ({ "unchecked", "rawtypes" })
protected Predicate buildPredicate(Path<T> root, SearchField field)
{
	Path<T> tt = (!field.getField().contains(".")) ? root.get(field.getField()) : fetchNestedPath(root, field.getField());
	CriteriaBuilder criteriaBuilder = this.entitymanager.getCriteriaBuilder();
	
	Class<?> javaType = tt.getJavaType();
	
	if (!classCompatibleWithOperator(javaType, field.getOperator()))
	{
		throw new RuntimeException("operator incompatible with field");
	}
	
	Object valueObject = convertStringValueToObject(field.getValue(), javaType);
	switch (field.getOperator())
	{
		case GE:
			return criteriaBuilder.greaterThan((Expression) tt, (Comparable) valueObject);
		case GTE:
			return criteriaBuilder.greaterThanOrEqualTo((Expression) tt, (Comparable) valueObject);
		case LE:
			return criteriaBuilder.lessThan((Expression) tt, (Comparable) valueObject);
		case LTE:
			return criteriaBuilder.lessThanOrEqualTo((Expression) tt, (Comparable) valueObject);
		case NE: 
               return criteriaBuilder.notEqual(tt, valueObject); 
		case EX:
			return criteriaBuilder.like((Expression) tt, "%"+field.getValue()+"%");
		default:
		{
			//EQ
			return criteriaBuilder.equal(tt, valueObject);
		}
	}
}
 
Example 8
Project: OperatieBRP   File: LikePredicateBuilder.java   Source Code and License Vote up 5 votes
@Override
public Predicate toPredicate(final Root<?> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
    // Like en case insensitive
    final Path<String> path = PredicateBuilderUtil.<String>getPath(root, naam);
    final Expression<String> attributeExpression = cb.lower(cb.function(UNACCENT, String.class, path));
    final Expression<String> valueExpression =
            cb.lower(cb.function(UNACCENT, String.class, cb.concat(cb.concat(cb.literal(WILDCARD), cb.literal(value.toString())), cb.literal(WILDCARD))));

    return cb.like(attributeExpression, valueExpression);
}
 
Example 9
Project: OperatieBRP   File: PersoonFilterFactory.java   Source Code and License Vote up 5 votes
@Override
public Predicate toPredicate(final Root<?> persoonRoot, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
    final Root<PersoonAdres> persoonAdressenRoot = query.from(PersoonAdres.class);
    final Predicate joinPredicateAdressen = persoonRoot.get(ID).in(persoonAdressenRoot.get(PERSOON).get(ID));

    final Expression<String> attributeExpression1 =
            cb.lower(cb.function(UNACCENT, String.class, persoonAdressenRoot.get("buitenlandsAdresRegel1")));
    final Expression<String> attributeExpression2 =
            cb.lower(cb.function(UNACCENT, String.class, persoonAdressenRoot.get("buitenlandsAdresRegel2")));
    final Expression<String> attributeExpression3 =
            cb.lower(cb.function(UNACCENT, String.class, persoonAdressenRoot.get("buitenlandsAdresRegel3")));
    final Expression<String> attributeExpression4 =
            cb.lower(cb.function(UNACCENT, String.class, persoonAdressenRoot.get("buitenlandsAdresRegel4")));
    final Expression<String> attributeExpression5 =
            cb.lower(cb.function(UNACCENT, String.class, persoonAdressenRoot.get("buitenlandsAdresRegel5")));
    final Expression<String> attributeExpression6 =
            cb.lower(cb.function(UNACCENT, String.class, persoonAdressenRoot.get("buitenlandsAdresRegel6")));
    final Expression<String> valueExpression =
            cb.lower(
                    cb.function(
                            UNACCENT,
                            String.class,
                            cb.concat(cb.concat(cb.literal(WILDCARD), cb.literal(value.toString())), cb.literal(WILDCARD))));
    final Predicate adresPredicate1 = cb.like(attributeExpression1, valueExpression);
    final Predicate adresPredicate2 = cb.like(attributeExpression2, valueExpression);
    final Predicate adresPredicate3 = cb.like(attributeExpression3, valueExpression);
    final Predicate adresPredicate4 = cb.like(attributeExpression4, valueExpression);
    final Predicate adresPredicate5 = cb.like(attributeExpression5, valueExpression);
    final Predicate adresPredicate6 = cb.like(attributeExpression6, valueExpression);

    final Predicate buitenlandsAdres = cb.or(adresPredicate1, adresPredicate2, adresPredicate3, adresPredicate4, adresPredicate5, adresPredicate6);
    return cb.and(joinPredicateAdressen, buitenlandsAdres);
}
 
Example 10
Project: sucok-framework   File: Contains.java   Source Code and License Vote up 4 votes
@Override
public Predicate buildJpaPredicate(CriteriaBuilder builder, Root<?> root) {
	Expression<String> path = QueryFormHelper.getPath(root, field);
	return builder.like(path, "%" + value + "%");
}
 
Example 11
Project: sucok-framework   File: EndWith.java   Source Code and License Vote up 4 votes
@Override
public Predicate buildJpaPredicate(CriteriaBuilder builder, Root<?> root) {
	Expression<String> path = QueryFormHelper.getPath(root, field);
	return builder.like(path, "%" + value);
}
 
Example 12
Project: sucok-framework   File: StartWith.java   Source Code and License Vote up 4 votes
@Override
public Predicate buildJpaPredicate(CriteriaBuilder builder, Root<?> root) {
	Expression<String> path = QueryFormHelper.getPath(root, field);
	return builder.like(path, value + "%");
}
 
Example 13
Project: amanda   File: Criterion.java   Source Code and License Vote up 4 votes
@Override
public Predicate toPredicate(Criterion c, Root<?> r, CriteriaBuilder b) {
    return b.like(
            r.get(c.getPropertyName()), getString(c));
}
 
Example 14
Project: careconnect-reference-implementation   File: RIValueSetRepository.java   Source Code and License Vote up 4 votes
public List<ValueSet> searchValueset (
        @OptionalParam(name = ValueSet.SP_NAME) StringParam name
)
{
    List<ValueSetEntity> qryResults = null;

    CriteriaBuilder builder = em.getCriteriaBuilder();

    CriteriaQuery<ValueSetEntity> criteria = builder.createQuery(ValueSetEntity.class);
    Root<ValueSetEntity> root = criteria.from(ValueSetEntity.class);
   

    List<Predicate> predList = new LinkedList<Predicate>();
    List<ValueSet> results = new ArrayList<ValueSet>();

    if (name !=null)
    {

        Predicate p =
                builder.like(
                        builder.upper(root.get("name").as(String.class)),
                        builder.upper(builder.literal("%"+name.getValue()+"%"))
                );

        predList.add(p);
    }

   

    Predicate[] predArray = new Predicate[predList.size()];
    predList.toArray(predArray);
    if (predList.size()>0)
    {
        criteria.select(root).where(predArray);
    }
    else
    {
        criteria.select(root);
    }

    qryResults = em.createQuery(criteria).getResultList();

    for (ValueSetEntity valuesetEntity : qryResults)
    {
        ValueSet valueset = valuesetEntityToFHIRValuesetTransformer.transform(valuesetEntity);
        results.add(valueset);
    }

    return results;
}
 
Example 15
Project: careconnect-reference-implementation   File: ValueSetDao.java   Source Code and License Vote up 4 votes
public List<ValueSet> searchValueset (
        @OptionalParam(name = ValueSet.SP_NAME) StringParam name
)
{
    List<ValueSetEntity> qryResults = null;

    CriteriaBuilder builder = em.getCriteriaBuilder();

    CriteriaQuery<ValueSetEntity> criteria = builder.createQuery(ValueSetEntity.class);
    Root<ValueSetEntity> root = criteria.from(ValueSetEntity.class);
   

    List<Predicate> predList = new LinkedList<Predicate>();
    List<ValueSet> results = new ArrayList<ValueSet>();

    if (name !=null)
    {

        Predicate p =
                builder.like(
                        builder.upper(root.get("name").as(String.class)),
                        builder.upper(builder.literal(name.getValue()+"%"))
                );

        predList.add(p);
    }

   

    Predicate[] predArray = new Predicate[predList.size()];
    predList.toArray(predArray);
    if (predList.size()>0)
    {
        criteria.select(root).where(predArray);
    }
    else
    {
        criteria.select(root);
    }

    qryResults = em.createQuery(criteria).getResultList();

    for (ValueSetEntity valuesetEntity : qryResults)
    {
        ValueSet valueset = valuesetEntityToFHIRValuesetTransformer.transform(valuesetEntity);
        results.add(valueset);
    }

    return results;
}