com.wiiyaya.framework.consumer.support.easyui.EasyuiSortHandlerMethodArgumentResolver.java Source code

Java tutorial

Introduction

Here is the source code for com.wiiyaya.framework.consumer.support.easyui.EasyuiSortHandlerMethodArgumentResolver.java

Source

/*
 * Copyright 2016-2017 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.wiiyaya.framework.consumer.support.easyui;

import com.wiiyaya.framework.common.utils.StringUtils;
import org.springframework.core.MethodParameter;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.web.SortHandlerMethodArgumentResolver;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;

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

/**
 *
 * <p>web?easyui???spring data??</p>
 *
 * <p>??</p>
 *
 * <p>easyui??sort=foo,bar;order=asc,desc</p>
 *
 * @author wiiyaya
 *
 */
public class EasyuiSortHandlerMethodArgumentResolver extends SortHandlerMethodArgumentResolver {

    private static final String EASYUI_SORT_PARAMETER = "sort";
    private static final String EASYUI_ORDER_PARAMETER = "order";

    @Override
    public Sort resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
            NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
        String[] sortParameter = webRequest.getParameterValues(EASYUI_SORT_PARAMETER);
        String[] orderParameter = webRequest.getParameterValues(EASYUI_ORDER_PARAMETER);

        if (sortParameter != null && sortParameter.length != 0) {
            return prepareSort(sortParameter[0], orderParameter);
        } else {
            return null;
        }
    }

    private Direction[] prepareDirection(int length, String[] orderParameter) {
        Direction[] directions = new Direction[length];
        if (orderParameter != null && orderParameter.length != 0) {
            String[] orders = orderParameter[0].split(StringUtils.COMMA);
            for (int i = 0; i < length; i = i + 1) {
                if (i < orders.length) {
                    directions[i] = Direction.fromStringOrNull(orders[i]);
                } else {
                    directions[i] = null;
                }
            }
        }
        return directions;
    }

    private Sort prepareSort(String sortParameter, String[] orderParameter) {
        if (sortParameter == null) {
            return null;
        }
        List<Order> allOrders = new ArrayList<Order>();

        String[] elements = sortParameter.split(StringUtils.COMMA);
        Direction[] directions = prepareDirection(elements.length, orderParameter);
        for (int i = 0; i < elements.length; i = i + 1) {
            String property = elements[i];
            if (!org.springframework.util.StringUtils.hasText(property)) {
                continue;
            }

            allOrders.add(new Order(directions[i], property));
        }

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