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

com.github.wz2cool.dynamic.builder.OrderByClauseBuilder Maven / Gradle / Ivy

There is a newer version: 3.2.32
Show newest version
package com.github.wz2cool.dynamic.builder;

import com.github.wz2cool.dynamic.BaseSortDescriptor;
import com.github.wz2cool.dynamic.DynamicQuery;
import com.github.wz2cool.dynamic.SortDescriptor;
import com.github.wz2cool.dynamic.builder.direction.Ascending;
import com.github.wz2cool.dynamic.builder.direction.ISortDirection;
import com.github.wz2cool.dynamic.helper.CommonsHelper;
import com.github.wz2cool.dynamic.lambda.GetCommonPropertyFunction;

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

/**
 * @author Frank
 */
public class OrderByClauseBuilder implements IDynamicQueryBuilder {

    private final List sortDescriptors = new ArrayList<>();
    private final DynamicQueryBuilder dynamicQueryBuilder;

    public OrderByClauseBuilder(
            DynamicQueryBuilder dynamicQueryBuilder,
            GetCommonPropertyFunction getPropertyFunction, ISortDirection sortDirection) {
        this.dynamicQueryBuilder = dynamicQueryBuilder;
        dynamicQueryBuilder.setOrderByClauseBuilder(this);
        addSort(getPropertyFunction, sortDirection);
    }

    public OrderByClauseBuilder(
            DynamicQueryBuilder dynamicQueryBuilder,
            GetCommonPropertyFunction getPropertyFunction) {
        this.dynamicQueryBuilder = dynamicQueryBuilder;
        dynamicQueryBuilder.setOrderByClauseBuilder(this);
        addSort(getPropertyFunction, new Ascending());
    }

    public OrderByClauseBuilder thenBy(
            GetCommonPropertyFunction getPropertyFunction, ISortDirection sortDirection) {
        addSort(getPropertyFunction, sortDirection);
        return this;
    }

    public OrderByClauseBuilder thenBy(
            GetCommonPropertyFunction getPropertyFunction) {
        addSort(getPropertyFunction, new Ascending());
        return this;
    }

    @Override
    public DynamicQuery build() {
        return dynamicQueryBuilder.build();
    }

    BaseSortDescriptor[] getSorts() {
        return this.sortDescriptors.toArray(new BaseSortDescriptor[0]);
    }

    private void addSort(GetCommonPropertyFunction getPropertyFunction, ISortDirection sortDirection) {
        String propertyName = CommonsHelper.getPropertyName(getPropertyFunction);
        SortDescriptor sortDescriptor = new SortDescriptor();
        sortDescriptor.setPropertyName(propertyName);
        sortDescriptor.setDirection(sortDirection.getDirection());
        this.sortDescriptors.add(sortDescriptor);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy