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

com.alilitech.integration.web.SortArgumentResolver Maven / Gradle / Ivy

/**
 *    Copyright 2017-2020 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.alilitech.integration.web;

import com.alilitech.integration.jpa.domain.Direction;
import com.alilitech.integration.jpa.domain.Sort;
import com.alilitech.integration.jpa.domain.Order;
import org.springframework.core.MethodParameter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Zhou Xiaoxiang
 * @since 1.0
 */
public class SortArgumentResolver implements HandlerMethodArgumentResolver {

    private static final String DEFAULT_PARAMETER = "sort";
    private static final String DEFAULT_PROPERTY_DELIMITER = ",";
    private static final Sort DEFAULT_SORT = null;

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return Sort.class.equals(parameter.getParameterType());
    }

    @Override
    public Sort resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        String[] directionParameter = webRequest.getParameterValues(DEFAULT_PARAMETER);

        // No parameter
        if (directionParameter == null) {
            return DEFAULT_SORT;
        }

        // Single empty parameter, e.g "sort="
        if (directionParameter.length == 1 && !StringUtils.hasText(directionParameter[0])) {
            return DEFAULT_SORT;
        }

        return parseParameterIntoSort(directionParameter, DEFAULT_PROPERTY_DELIMITER);
    }

    Sort parseParameterIntoSort(String[] source, String delimiter) {

        List allOrders = new ArrayList<>();

        for (String part : source) {

            if (part == null) {
                continue;
            }

            String[] elements = part.split(delimiter);
            Direction direction = elements.length == 0 ? null : Direction.fromStringOrNull(elements[elements.length - 1]);

            for (int i = 0; i < elements.length; i++) {

                if (i == elements.length - 1 && direction != null) {
                    continue;
                }

                String property = elements[i];

                if (!StringUtils.hasText(property)) {
                    continue;
                }

                allOrders.add(new Order(direction, property));
            }
        }

        return allOrders.isEmpty() ? null : new Sort(allOrders);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy