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

com.liberologico.cloudesire.cmw.model.dto.PageRequestDTO Maven / Gradle / Ivy

The newest version!
package com.liberologico.cloudesire.cmw.model.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.liberologico.cloudesire.cmw.model.enums.SortDirection;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

public class PageRequestDTO
{
    @NotNull
    @Min ( value = 1, message = "must be > 1" )
    private Integer pageNumber = 1;
    @NotNull
    @Max ( value = 50, message = "must be < 50" )
    private Integer pageSize = 20;
    private SortDirection sortDirection;
    private String sortField;

    public PageRequestDTO( int pageNumber, int pageSize )
    {
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
    }

    public PageRequestDTO( int pageNumber, int pageSize, String sortField )
    {
        this( pageNumber, pageSize );
        this.sortField = sortField;
    }

    public PageRequestDTO( int pageNumber, int pageSize, String sortField, SortDirection sortDirection )
    {
        this( pageNumber, pageSize, sortField );
        this.sortDirection = sortDirection;
    }

    public Map toMap()
    {
        Map map = new HashMap<>();
        map.put( "pageNumber", Integer.toString( pageNumber ) );
        map.put( "pageSize", Integer.toString( pageSize ) );
        if ( sortDirection != null ) map.put( "sortDirection", sortDirection.toString() );
        if ( sortField != null ) map.put( "sortField", sortField );
        return map;
    }

    public static Map defaultPager()
    {
        return new PageRequestDTO().toMap();
    }

    // region Auto-generated code
    public PageRequestDTO()
    {
    }

    @JsonIgnore
    public boolean isAscending()
    {
        return sortDirection == SortDirection.ASC;
    }

    @JsonIgnore
    public boolean isSet()
    {
        return pageNumber != null && pageSize != null;
    }

    public PageRequestDTO setSort( String field, SortDirection direction )
    {
        this.sortField = field;
        this.sortDirection = direction;
        return this;
    }

    public Integer getPageNumber()
    {
        return pageNumber;
    }

    public void setPageNumber( Integer pageNumber )
    {
        this.pageNumber = pageNumber;
    }

    public Integer getPageSize()
    {
        return pageSize;
    }

    public void setPageSize( Integer pageSize )
    {
        this.pageSize = pageSize;
    }

    public SortDirection getSortDirection()
    {
        return sortDirection;
    }

    public void setSortDirection( SortDirection sortDirection )
    {
        this.sortDirection = sortDirection;
    }

    public String getSortField()
    {
        return sortField;
    }

    public void setSortField( String sortField )
    {
        this.sortField = sortField;
    }
    // endregion
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy