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

com.infusers.core.search.SpecificationUtility Maven / Gradle / Ivy

There is a newer version: 2024.12.0008
Show newest version
package com.infusers.core.search;

import java.util.ArrayList;
import java.util.Date;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;

public class SpecificationUtility {
	
    public static String getSearchLikeString(final String searchString) {
    	final String likeSearchString;
        	
        if (!searchString.contains("%")) {
        	likeSearchString = "%"+searchString.toLowerCase()+"%";
        }
        else {
        	likeSearchString = searchString.toLowerCase();
        }
        return likeSearchString;
    }
    
	static Predicate createPredicate(Root root, CriteriaBuilder criteriaBuilder, 
			SpecificationConfigElement element, final String searchString, final String likeSearchString) 
	{
		if(element==null) {
			return null;
		}
		if(element.getSourceDataType() ==  String.class) {
			return createPredicateString(root, criteriaBuilder, element.getFieldName(), searchString, likeSearchString);
		}
		else if(element.getSourceDataType() ==  Boolean.class) {
			return createPredicateBoolean(root, criteriaBuilder, element.getFieldName(), searchString, likeSearchString);
		}
		else if(element.getSourceDataType() ==  Date.class) {
			return createPredicateDate(root, criteriaBuilder, element.getFieldName(), searchString, likeSearchString);
		}
		else if(element.getSourceDataType() ==  Long.class) {
			return createPredicateLong(root, criteriaBuilder, element.getFieldName(), searchString, likeSearchString);
		}
		return null;
	}

	static Predicate createPredicateBoolean(Root root, CriteriaBuilder criteriaBuilder, 
			final String fieldName, final String searchString, final String likeSearchString) 
	{    	
		if(searchString!=null && !searchString.isEmpty()) {
	    	try {
	    		boolean searchBoolean = Boolean.getBoolean(searchString);
	        	return criteriaBuilder.equal(root.get(fieldName), searchBoolean);
	    	}
	    	catch(Exception e) {
	    		//
	    	}
		}
		return null;
	}

	static Predicate createPredicateDate(Root root, CriteriaBuilder criteriaBuilder, 
			final String fieldName, final String searchString, final String likeSearchString) 
	{
		if(searchString!=null && !searchString.isEmpty()) {
			Expression dateStringExpr = criteriaBuilder.function("TO_CHAR", String.class, root.get("createdAt"), criteriaBuilder.literal("YYYY-MM-DD HH:MI:SS"));
			return criteriaBuilder.like(dateStringExpr, likeSearchString);
		}
		return null;
	}
	

	static Predicate createPredicateString(Root root, CriteriaBuilder criteriaBuilder, 
			final String fieldName, final String searchString, final String likeSearchString) 
	{
	    if (searchString != null && !searchString.isEmpty()) {          
	    	return criteriaBuilder.like(criteriaBuilder.lower(root.get(fieldName)), likeSearchString);
	    }
		return null;
	}

	static Predicate createPredicateLong(Root root, CriteriaBuilder criteriaBuilder, 
			final String fieldName, final String searchString, final String likeSearchString) 
	{
		Long searchLong = 0L;
		
		try {
			searchLong = Long.parseLong(searchString);
		}
		catch(Exception e) {
			//
		}
		        	
		if(searchLong!=null && searchLong.intValue()!=0) {
			return criteriaBuilder.like(root.get(fieldName).as(String.class), likeSearchString);
		}
		return null;
	}

	static Predicate joinPredicateMultipleOr(CriteriaBuilder criteriaBuilder, ArrayList predicateListOr) {
		
		Predicate predicateSearch = null;
		
		int count = predicateListOr!=null ? predicateListOr.size() : 0;
		for(int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy