Skip to content

Instantly share code, notes, and snippets.

@pavankjadda
Last active January 12, 2021 06:22
Show Gist options
  • Save pavankjadda/84afc3c02eba02ea50ad9f7fbe3eb853 to your computer and use it in GitHub Desktop.
Save pavankjadda/84afc3c02eba02ea50ad9f7fbe3eb853 to your computer and use it in GitHub Desktop.
Get Specification
/**
 * Builds and return specification object that filters data based on search string
 *
 * @param employeeRequestDTO Employee Projects Request DTO object
 *
 * @return Specification with Employee Id and Filter Text
 */
private Specification<EmployeeProjectView> getSpecification(EmployeeRequestDTO employeeRequestDTO)
{
	//Build Specification with Employee Id and Filter Text
	return (root, criteriaQuery, criteriaBuilder) ->
	{
		criteriaQuery.distinct(true);
		//Predicate for Employee Id
		Predicate predicateForEmployee = criteriaBuilder.equal(root.get("employeeId"), employeeRequestDTO.getEmployeeId());

		if (isNotNullOrEmpty(employeeRequestDTO.getFilterText()))
		{
			//Predicate for Employee Projects data
			Predicate predicateForData = criteriaBuilder.or(
					criteriaBuilder.like(root.get("firstName"), "%" + employeeRequestDTO.getFilterText() + "%"),
					criteriaBuilder.like(root.get("lastName"), "%" + employeeRequestDTO.getFilterText() + "%"),
					criteriaBuilder.like(root.get("projectId").as(String.class), "%" + employeeRequestDTO.getFilterText() + "%"),
					criteriaBuilder.like(root.get("projectName"), "%" + employeeRequestDTO.getFilterText() + "%"),
					criteriaBuilder.like(root.get("projectBudget").as(String.class), "%" + employeeRequestDTO.getFilterText() + "%"),
					criteriaBuilder.like(root.get("projectLocation"), "%" + employeeRequestDTO.getFilterText() + "%"));

			//Combine both predicates
			return criteriaBuilder.and(predicateForEmployee, predicateForData);
		}
		return criteriaBuilder.and(predicateForEmployee);
	};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment