Java Code Examples for org.springframework.data.domain.Sort#and()

The following examples show how to use org.springframework.data.domain.Sort#and() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseController.java    From SpringBoot-Base-System with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 获取分页请求 带排序
 * 
 * @param sort
 *            排序条件
 * @return PageRequest
 */
protected PageRequest getPageRequest(Sort sort) {
	int page = 0;
	int size = 10;
	try {
		String sortName = request.getParameter("sortName");
		String sortOrder = request.getParameter("sortOrder");
		if (StringUtils.isNoneBlank(sortName) && StringUtils.isNoneBlank(sortOrder)) {
			if (sortOrder.equalsIgnoreCase("desc")) {
				sort.and(new Sort(Direction.DESC, sortName));
			} else {
				sort.and(new Sort(Direction.ASC, sortName));
			}
		}
		page = Integer.parseInt(request.getParameter("pageNumber")) - 1;
		size = Integer.parseInt(request.getParameter("pageSize"));
	} catch (Exception e) {
		e.printStackTrace();
	}
	PageRequest pageRequest = new PageRequest(page, size, sort);
	return pageRequest;
}
 
Example 2
Source File: SearchRequest.java    From es with Apache License 2.0 6 votes vote down vote up
private void merge(Sort sort, Pageable page) {
    if (sort == null) {
        sort = this.sort;
    }
    if (page == null) {
        page = this.page;
    }

    //合并排序
    if (sort == null) {
        this.sort = page != null ? page.getSort() : null;
    } else {
        this.sort = (page != null ? sort.and(page.getSort()) : sort);
    }
    //把排序合并到page中
    if (page != null) {
        this.page = new PageRequest(page.getPageNumber(), page.getPageSize(), this.sort);
    } else {
        this.page = null;
    }
}
 
Example 3
Source File: SorterQueryParser.java    From KOMORAN with Apache License 2.0 6 votes vote down vote up
public Sort getSorter(Map<String, String> inputParam) {
    ArrayList<HashMap<String, String>> sorters = this.parse(inputParam);
    Sort sortBy = null;

    if (sorters == null) {
        return null;
    } else if (sorters.isEmpty()) {
        sortBy = Sort.by("token").ascending();
    } else {
        for (HashMap<String, String> sorter : sorters) {
            Sort.Direction dir = sorter.get("dir").equalsIgnoreCase("asc") ? Sort.Direction.ASC : Sort.Direction.DESC;

            if (sortBy == null) {
                sortBy = Sort.by(dir, sorter.get("field"));
            } else {
                sortBy = sortBy.and(Sort.by(dir, sorter.get("field")));
            }
        }
    }

    return sortBy;
}
 
Example 4
Source File: RolloutBeanQuery.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parametric Constructor.
 *
 * @param definition
 *            as QueryDefinition
 * @param queryConfig
 *            as Config
 * @param sortIds
 *            as sort
 * @param sortStates
 *            as Sort status
 */
public RolloutBeanQuery(final QueryDefinition definition, final Map<String, Object> queryConfig,
        final Object[] sortIds, final boolean[] sortStates) {
    super(definition, queryConfig, sortIds, sortStates);

    searchText = getSearchText();

    if (sortStates != null && sortStates.length > 0) {

        sort = new Sort(sortStates[0] ? Direction.ASC : Direction.DESC, (String) sortIds[0]);

        for (int targetId = 1; targetId < sortIds.length; targetId++) {
            sort.and(new Sort(sortStates[targetId] ? Direction.ASC : Direction.DESC, (String) sortIds[targetId]));
        }
    }
}
 
Example 5
Source File: RolloutGroupBeanQuery.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parametric Constructor.
 *
 * @param definition
 *            as QueryDefinition
 * @param queryConfig
 *            as Config
 * @param sortPropertyIds
 *            as sort
 * @param sortStates
 *            as Sort status
 */
public RolloutGroupBeanQuery(final QueryDefinition definition, final Map<String, Object> queryConfig,
        final Object[] sortPropertyIds, final boolean[] sortStates) {

    super(definition, queryConfig, sortPropertyIds, sortStates);

    rolloutId = getRolloutId();

    if (sortStates != null && sortStates.length > 0) {

        sort = new Sort(sortStates[0] ? ASC : DESC, (String) sortPropertyIds[0]);

        for (int targetId = 1; targetId < sortPropertyIds.length; targetId++) {
            sort.and(new Sort(sortStates[targetId] ? ASC : DESC, (String) sortPropertyIds[targetId]));
        }
    }
}
 
Example 6
Source File: TargetFilterBeanQuery.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 *
 * @param definition
 * @param queryConfig
 * @param sortPropertyIds
 * @param sortStates
 */
public TargetFilterBeanQuery(final QueryDefinition definition, final Map<String, Object> queryConfig,
        final Object[] sortPropertyIds, final boolean[] sortStates) {
    super(definition, queryConfig, sortPropertyIds, sortStates);
    if (HawkbitCommonUtil.isNotNullOrEmpty(queryConfig)) {
        searchText = (String) queryConfig.get(SPUIDefinitions.FILTER_BY_TEXT);
        if (!StringUtils.isEmpty(searchText)) {
            searchText = String.format("%%%s%%", searchText);
        }
    }

    if (sortStates != null && sortStates.length > 0) {
        // Initalize sort
        sort = new Sort(sortStates[0] ? Direction.ASC : Direction.DESC, (String) sortPropertyIds[0]);
        // Add sort
        for (int tfId = 1; tfId < sortPropertyIds.length; tfId++) {
            sort.and(new Sort(sortStates[tfId] ? Direction.ASC : Direction.DESC, (String) sortPropertyIds[tfId]));
        }
    }
}
 
Example 7
Source File: CustomTargetBeanQuery.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parametric Constructor.
 *
 * @param definition
 *            as Def
 * @param queryConfig
 *            as Config
 * @param sortIds
 *            as sort
 * @param sortStates
 *            as Sort status
 */
public CustomTargetBeanQuery(final QueryDefinition definition, final Map<String, Object> queryConfig,
        final Object[] sortIds, final boolean[] sortStates) {
    super(definition, queryConfig, sortIds, sortStates);

    if (isNotNullOrEmpty(queryConfig)) {
        filterQuery = (String) queryConfig.get(FILTER_BY_QUERY);
    }

    if (sortStates != null && sortStates.length > 0) {

        sort = new Sort(sortStates[0] ? ASC : DESC, (String) sortIds[0]);

        for (int targetId = 1; targetId < sortIds.length; targetId++) {
            sort.and(new Sort(sortStates[targetId] ? ASC : DESC, (String) sortIds[targetId]));
        }
    }
}
 
Example 8
Source File: ArtifactBeanQuery.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parametric Constructor.
 *
 * @param definition
 *            as Def
 * @param queryConfig
 *            as Config
 * @param sortIds
 *            as sort
 * @param sortStates
 *            as Sort status
 */
public ArtifactBeanQuery(final QueryDefinition definition, final Map<String, Object> queryConfig,
        final Object[] sortIds, final boolean[] sortStates) {

    super(definition, queryConfig, sortIds, sortStates);

    if (HawkbitCommonUtil.isNotNullOrEmpty(queryConfig)) {
        baseSwModuleId = (Long) queryConfig.get(SPUIDefinitions.BY_BASE_SOFTWARE_MODULE);
    }

    if (!ArrayUtils.isEmpty(sortStates)) {
        sort = new Sort(sortStates[0] ? Direction.ASC : Direction.DESC, (String) sortIds[0]);

        for (int targetId = 1; targetId < sortIds.length; targetId++) {
            sort.and(new Sort(sortStates[targetId] ? Direction.ASC : Direction.DESC, (String) sortIds[targetId]));
        }
    }
}
 
Example 9
Source File: DistributionBeanQuery.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Bean query for retrieving beans/objects of type.
 *
 * @param definition
 *            query definition
 * @param queryConfig
 *            as queryConfig
 * @param sortPropertyIds
 *            property id's for sorting
 * @param sortStates
 *            sort states
 */
public DistributionBeanQuery(final QueryDefinition definition, final Map<String, Object> queryConfig,
        final Object[] sortPropertyIds, final boolean[] sortStates) {
    super(definition, queryConfig, sortPropertyIds, sortStates);

    if (HawkbitCommonUtil.isNotNullOrEmpty(queryConfig)) {
        distributionTags = (Collection<String>) queryConfig.get(SPUIDefinitions.FILTER_BY_TAG);
        searchText = (String) queryConfig.get(SPUIDefinitions.FILTER_BY_TEXT);
        noTagClicked = (Boolean) queryConfig.get(SPUIDefinitions.FILTER_BY_NO_TAG);
        pinnedTarget = (TargetIdName) queryConfig.get(SPUIDefinitions.ORDER_BY_PINNED_TARGET);
        if (!StringUtils.isEmpty(searchText)) {
            searchText = String.format("%%%s%%", searchText);
        }
    }

    if (sortStates != null && sortStates.length > 0) {
        // Initalize sort
        sort = new Sort(sortStates[0] ? Direction.ASC : Direction.DESC, (String) sortPropertyIds[0]);
        // Add sort
        for (int distId = 1; distId < sortPropertyIds.length; distId++) {
            sort.and(new Sort(sortStates[distId] ? Direction.ASC : Direction.DESC,
                    (String) sortPropertyIds[distId]));
        }
    }
}
 
Example 10
Source File: ActionStatusMsgBeanQuery.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parametric Constructor.
 *
 * @param definition
 *            QueryDefinition contains the query properties.
 * @param queryConfig
 *            Implementation specific configuration.
 * @param sortPropertyIds
 *            The properties participating in sort.
 * @param sortStates
 *            The ascending or descending state of sort properties.
 */
public ActionStatusMsgBeanQuery(final QueryDefinition definition, final Map<String, Object> queryConfig,
        final Object[] sortPropertyIds, final boolean[] sortStates) {
    super(definition, queryConfig, sortPropertyIds, sortStates);

    if (isNotNullOrEmpty(queryConfig)) {
        currentSelectedActionStatusId = (Long) queryConfig.get(SPUIDefinitions.MESSAGES_BY_ACTIONSTATUS);
        noMessageText = (String) queryConfig.get(SPUIDefinitions.NO_MSG_PROXY);
    }

    if (sortStates != null && sortStates.length > 0) {
        // Initialize sort
        sort = new Sort(sortStates[0] ? Direction.ASC : Direction.DESC, (String) sortPropertyIds[0]);
        // Add sort
        for (int distId = 1; distId < sortPropertyIds.length; distId++) {
            sort.and(new Sort(sortStates[distId] ? Direction.ASC : Direction.DESC,
                    (String) sortPropertyIds[distId]));
        }
    }
}
 
Example 11
Source File: ActionBeanQuery.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parametric Constructor.
 *
 * @param definition
 *            QueryDefinition contains the query properties.
 * @param queryConfig
 *            Implementation specific configuration.
 * @param sortPropertyIds
 *            The properties participating in sort.
 * @param sortStates
 *            The ascending or descending state of sort properties.
 */
public ActionBeanQuery(final QueryDefinition definition, final Map<String, Object> queryConfig,
        final Object[] sortPropertyIds, final boolean[] sortStates) {
    super(definition, queryConfig, sortPropertyIds, sortStates);

    if (isNotNullOrEmpty(queryConfig)) {
        currentSelectedConrollerId = (String) queryConfig.get(SPUIDefinitions.ACTIONS_BY_TARGET);
    }

    if (sortStates == null || sortStates.length <= 0) {
        return;
    }

    // Initialize sort
    sort = new Sort(sortStates[0] ? Direction.ASC : Direction.DESC, (String) sortPropertyIds[0]);
    // Add sort
    for (int distId = 1; distId < sortPropertyIds.length; distId++) {
        sort.and(new Sort(sortStates[distId] ? Direction.ASC : Direction.DESC, (String) sortPropertyIds[distId]));
    }
}
 
Example 12
Source File: ActionStatusBeanQuery.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parametric Constructor.
 *
 * @param definition
 *            QueryDefinition contains the query properties.
 * @param queryConfig
 *            Implementation specific configuration.
 * @param sortPropertyIds
 *            The properties participating in sort.
 * @param sortStates
 *            The ascending or descending state of sort properties.
 */
public ActionStatusBeanQuery(final QueryDefinition definition, final Map<String, Object> queryConfig,
        final Object[] sortPropertyIds, final boolean[] sortStates) {
    super(definition, queryConfig, sortPropertyIds, sortStates);

    if (isNotNullOrEmpty(queryConfig)) {
        currentSelectedActionId = (Long) queryConfig.get(SPUIDefinitions.ACTIONSTATES_BY_ACTION);
    }

    if (sortStates != null && sortStates.length > 0) {
        // Initialize sort
        sort = new Sort(sortStates[0] ? Direction.ASC : Direction.DESC, (String) sortPropertyIds[0]);
        // Add sort
        for (int distId = 1; distId < sortPropertyIds.length; distId++) {
            sort.and(new Sort(sortStates[distId] ? Direction.ASC : Direction.DESC,
                    (String) sortPropertyIds[distId]));
        }
    }
}
 
Example 13
Source File: PageableMethodArgumentResolver.java    From es with Apache License 2.0 5 votes vote down vote up
private Sort getSort(String sortNamePrefix, Map<String, String[]> sortMap, Pageable defaultPageRequest, NativeWebRequest webRequest) {
    Sort sort = null;
    List<OrderedSort> orderedSortList = Lists.newArrayList();
    for (String name : sortMap.keySet()) {

        //sort1.abc
        int propertyIndex = name.indexOf(".") + 1;

        int order = 0;
        String orderStr = name.substring(sortNamePrefix.length(), propertyIndex - 1);
        try {
            if (!StringUtils.isEmpty(orderStr)) {
                order = Integer.valueOf(orderStr);
            }
        } catch (Exception e) {
        }

        String property = name.substring(propertyIndex);
        assertSortProperty(property);
        Sort.Direction direction = Sort.Direction.fromString(sortMap.get(name)[0]);

        orderedSortList.add(new OrderedSort(property, direction, order));
    }

    Collections.sort(orderedSortList);
    for (OrderedSort orderedSort : orderedSortList) {
        Sort newSort = new Sort(orderedSort.direction, orderedSort.property);
        if (sort == null) {
            sort = newSort;
        } else {
            sort = sort.and(newSort);
        }
    }

    if (sort == null) {
        return defaultPageRequest.getSort();
    }

    return sort;
}
 
Example 14
Source File: TransactionServiceImpl.java    From hermes with Apache License 2.0 5 votes vote down vote up
@Override
public Page<Transaction> findRechargeByEmailAndDateBetweenAndRemark(final String email, final Date beginDate, final Date endDate, final String remark, Integer page, Integer size) {
	Sort sort = new Sort(Direction.DESC, "datetime");
	sort = sort.and(new Sort(Direction.DESC, "type"));
	sort = sort.and(new Sort(Direction.DESC, "sourceUserAccount.user.email"));
	Pageable pageable = new PageRequest(page, size == null ? Integer.valueOf(HermesConstants.DEFAULT_PAGE_SIZE) : size, sort);
	return transactionRepository.findAll(new Specification<Transaction>() {
		@Override
		public Predicate toPredicate(Root<Transaction> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			List<Predicate> list = new ArrayList<Predicate>();

			if (StringUtils.isNotEmpty(email)) {
				list.add(cb.like(root.<UserAccount> get("sourceUserAccount").<User> get("user").<String> get("email"), "%" + email.trim() + "%"));
			}

			if (StringUtils.isNotEmpty(remark)) {
				list.add(cb.like(root.<String> get("remark"), "%" + remark.trim() + "%"));
			}

			if (beginDate != null && endDate != null) {
				try {
					list.add(cb.greaterThanOrEqualTo(root.<Date> get("datetime"), beginDate));
					list.add(cb.lessThanOrEqualTo(root.<Date> get("datetime"), endDate));
				} catch (Exception e) {
					Logger.error("充值明细列表查询:格式化开始时间[" + beginDate + "],结束时间[" + endDate + "],异常,忽略时间查询条件");
				}
			}
			list.add(cb.notEqual(root.<UserAccount> get("sourceUserAccount").<User> get("user").<String> get("email"), HermesConstants.CROP_EMAIL));

			List<String> inList = new ArrayList<String>();
			inList.add(Transaction.Type.CHARGE);
			inList.add(Transaction.Type.REVERSE_CHARGE);
			Predicate in = ((root.<String> get("type")).in(inList));

			list.add(in);

			return cb.and(list.toArray(new Predicate[list.size()]));
		}
	}, pageable);
}
 
Example 15
Source File: ManageDistBeanQuery.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
private void setupSorting(final Object[] sortPropertyIds, final boolean[] sortStates) {
    if (sortStates != null && sortStates.length > 0) {
        // Initialize sort
        sort = new Sort(sortStates[0] ? Direction.ASC : Direction.DESC, (String) sortPropertyIds[0]);
        // Add sort
        for (int distId = 1; distId < sortPropertyIds.length; distId++) {
            sort = sort.and(new Sort(sortStates[distId] ? Direction.ASC : Direction.DESC,
                    (String) sortPropertyIds[distId]));
        }
    }
}
 
Example 16
Source File: TargetBeanQuery.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Parametric Constructor.
 *
 * @param definition
 *            as Def
 * @param queryConfig
 *            as Config
 * @param sortIds
 *            as sort
 * @param sortStates
 *            as Sort status
 */
public TargetBeanQuery(final QueryDefinition definition, final Map<String, Object> queryConfig,
        final Object[] sortIds, final boolean[] sortStates) {

    super(definition, queryConfig, sortIds, sortStates);

    if (HawkbitCommonUtil.isNotNullOrEmpty(queryConfig)) {
        status = (Collection<TargetUpdateStatus>) queryConfig.get(SPUIDefinitions.FILTER_BY_STATUS);
        overdueState = (Boolean) queryConfig.get(SPUIDefinitions.FILTER_BY_OVERDUE_STATE);
        targetTags = (String[]) queryConfig.get(SPUIDefinitions.FILTER_BY_TAG);
        noTagClicked = (Boolean) queryConfig.get(SPUIDefinitions.FILTER_BY_NO_TAG);
        distributionId = (Long) queryConfig.get(SPUIDefinitions.FILTER_BY_DISTRIBUTION);
        searchText = (String) queryConfig.get(SPUIDefinitions.FILTER_BY_TEXT);
        targetFilterQueryId = (Long) queryConfig.get(SPUIDefinitions.FILTER_BY_TARGET_FILTER_QUERY);
        if (!StringUtils.isEmpty(searchText)) {
            searchText = String.format("%%%s%%", searchText);
        }
        pinnedDistId = (Long) queryConfig.get(SPUIDefinitions.ORDER_BY_DISTRIBUTION);
    }

    if (sortStates != null && sortStates.length > 0) {

        sort = new Sort(sortStates[0] ? Direction.ASC : Direction.DESC, (String) sortIds[0]);

        for (int targetId = 1; targetId < sortIds.length; targetId++) {
            sort.and(new Sort(sortStates[targetId] ? Direction.ASC : Direction.DESC, (String) sortIds[targetId]));
        }
    }
}
 
Example 17
Source File: DistributionBeanQuery.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Parametric Constructor.
 * 
 * @param definition
 *            as QueryDefinition
 * @param queryConfig
 *            as Config
 * @param sortPropertyIds
 *            as sort
 * @param sortStates
 *            as Sort status
 */
public DistributionBeanQuery(final QueryDefinition definition, final Map<String, Object> queryConfig,
        final Object[] sortPropertyIds, final boolean[] sortStates) {
    super(definition, queryConfig, sortPropertyIds, sortStates);

    if (sortStates.length > 0) {
        // Initalize sort
        sort = new Sort(sortStates[0] ? Direction.ASC : Direction.DESC, (String) sortPropertyIds[0]);
        // Add sort
        for (int distId = 1; distId < sortPropertyIds.length; distId++) {
            sort.and(new Sort(sortStates[distId] ? Direction.ASC : Direction.DESC,
                    (String) sortPropertyIds[distId]));
        }
    }
}
 
Example 18
Source File: DocumentQuery.java    From spring-data-cosmosdb with MIT License 5 votes vote down vote up
public DocumentQuery with(@NonNull Sort sort) {
    if (sort.isSorted()) {
        this.sort = sort.and(this.sort);
    }

    return this;
}
 
Example 19
Source File: PartTreeDatastoreQuery.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
private StructuredQuery applyQueryBody(Object[] parameters,
		Builder builder, boolean total, boolean singularResult, Cursor cursor) {
	ParameterAccessor paramAccessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters);
	if (this.tree.hasPredicate()) {
		applySelectWithFilter(parameters, builder);
	}

	Pageable pageable = paramAccessor.getPageable();
	Integer limit = null;
	Integer offset = null;
	if (singularResult || this.tree.isExistsProjection()) {
		limit = 1;
	}
	else if (this.tree.isLimiting()) {
		limit = this.tree.getMaxResults();
	}

	if (!singularResult && !total && pageable.isPaged()) {
		limit = pageable.getPageSize();
	}

	Sort sort = this.tree.getSort();
	if (getQueryMethod().getParameters().hasPageableParameter()) {
		sort = sort.and(pageable.getSort());
	}
	if (getQueryMethod().getParameters().hasSortParameter()) {
		sort = sort.and(paramAccessor.getSort());
	}

	if (pageable.isPaged() && !total) {
		offset = (int) pageable.getOffset();
	}

	Cursor cursorToApply = null;
	if (cursor != null) {
		cursorToApply = cursor;
	}
	else if (pageable instanceof DatastorePageable) {
		cursorToApply = ((DatastorePageable) pageable).toCursor();
	}
	DatastoreTemplate.applyQueryOptions(
			builder, new DatastoreQueryOptions.Builder().setLimit(limit).setOffset(offset).setSort(sort)
					.setCursor(cursorToApply).build(),
			this.datastorePersistentEntity);
	return builder.build();
}