class FunctionJdbcRepository extends JdbcRepository {

	public Pagination<Function> search(FunctionSearch domain) {
		FunctionSearchEntity functionSearchEntity = new FunctionSearchEntity();
		functionSearchEntity.toEntity(domain);

		String searchQuery = "select :selectfields from :tablename :condition  :orderby   ";

		Map<String, Object> paramValues = new HashMap<>();
		StringBuffer params = new StringBuffer();

		if (functionSearchEntity.getSortBy() != null && !functionSearchEntity.getSortBy().isEmpty()) {
			validateSortByOrder(functionSearchEntity.getSortBy());
			validateEntityFieldName(functionSearchEntity.getSortBy(), FunctionEntity.class);
		}

		String orderBy = "order by name";
		if (functionSearchEntity.getSortBy() != null && !functionSearchEntity.getSortBy().isEmpty()) {
			orderBy = "order by " + functionSearchEntity.getSortBy();
		}

		searchQuery = searchQuery.replace(":tablename", FunctionEntity.TABLE_NAME);

		searchQuery = searchQuery.replace(":selectfields", " * ");

		// implement jdbc specfic search
		if (functionSearchEntity.getTenantId() != null) {
			if (params.length() > 0) {
				params.append(" and ");
			}
			params.append("tenantId =:tenantId");
			paramValues.put("tenantId", functionSearchEntity.getTenantId());
		}
		if (functionSearchEntity.getId() != null) {
			if (params.length() > 0) {
				params.append(" and ");
			}
			params.append("id =:id");
			paramValues.put("id", functionSearchEntity.getId());
		}
		if (functionSearchEntity.getIds() != null) {
			if (params.length() > 0) {
				params.append(" and ");
			}
			params.append("id in(:ids) ");
			paramValues.put("ids", new ArrayList<String>(Arrays.asList(functionSearchEntity.getIds().split(","))));
		}
		if (functionSearchEntity.getName() != null) {
			if (params.length() > 0) {
				params.append(" and ");
			}
			params.append("name =:name");
			paramValues.put("name", functionSearchEntity.getName());
		}
		if (functionSearchEntity.getCode() != null) {
			if (params.length() > 0) {
				params.append(" and ");
			}
			params.append("code =:code");
			paramValues.put("code", functionSearchEntity.getCode());
		}
		if (functionSearchEntity.getLevel() != null) {
			if (params.length() > 0) {
				params.append(" and ");
			}
			params.append("level =:level");
			paramValues.put("level", functionSearchEntity.getLevel());
		}
		if (functionSearchEntity.getActive() != null) {
			if (params.length() > 0) {
				params.append(" and ");
			}
			params.append("active =:active");
			paramValues.put("active", functionSearchEntity.getActive());
		}
		if (functionSearchEntity.getParentId() != null) {
			if (params.length() > 0) {
				params.append(" and ");
			}
			params.append("parentId =:parentId");
			paramValues.put("parentId", functionSearchEntity.getParentId());
		}

		Pagination<Function> page = new Pagination<>();
		if (functionSearchEntity.getOffset() != null) {
			page.setOffset(functionSearchEntity.getOffset());
		}
		if (functionSearchEntity.getPageSize() != null) {
			page.setPageSize(functionSearchEntity.getPageSize());
		}

		if (params.length() > 0) {

			searchQuery = searchQuery.replace(":condition", " where " + params.toString());

		} else

			searchQuery = searchQuery.replace(":condition", "");

		searchQuery = searchQuery.replace(":orderby", orderBy);

		page = (Pagination<Function>) getPagination(searchQuery, page, paramValues);
		searchQuery = searchQuery + " :pagination";

		searchQuery = searchQuery.replace(":pagination",
				"limit " + page.getPageSize() + " offset " + page.getOffset() * page.getPageSize());

		BeanPropertyRowMapper row = new BeanPropertyRowMapper(FunctionEntity.class);

		List<FunctionEntity> functionEntities = namedParameterJdbcTemplate.query(searchQuery.toString(), paramValues,
				row);

		page.setTotalResults(functionEntities.size());

		List<Function> functions = new ArrayList<>();
		for (FunctionEntity functionEntity : functionEntities) {

			functions.add(functionEntity.toDomain());
		}
		page.setPagedData(functions);

		return page;
	}

	public  FunctionJdbcRepository(NamedParameterJdbcTemplate namedParameterJdbcTemplate);

}