/*
 * Copyright 2016 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 leap.web.api.mvc.params;

import leap.lang.value.Page;
import leap.web.Params;
import leap.web.annotation.ParamsWrapper;
import leap.web.annotation.QueryParam;

@ParamsWrapper
public class QueryOptions extends QueryOptionsBase {

    protected @QueryParam("page_size")   Integer pageSize;
    protected @QueryParam("page")        Integer pageIndex;
    protected @QueryParam("limit")       Integer limit;
    protected @QueryParam("offset")      Integer offset;  //0-based
    protected @QueryParam("total")       boolean total;
    protected @QueryParam("orderby")     String  orderBy;
    protected @QueryParam("viewId")      String  viewId;
    protected @QueryParam("filters")     String  filters;
    protected @QueryParam("aggregates")  String  aggregates;
    protected @QueryParam("groupby")     String  groupBy;
    protected @QueryParam("joins")       String  joins;

    public Integer getPageSize() {
        return pageSize;
    }

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

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Integer getOffset() {
        return offset;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public boolean isTotal() {
        return total;
    }

    public void setTotal(boolean total) {
        this.total = total;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public String getViewId() {
        return viewId;
    }

    public void setViewId(String viewId) {
        this.viewId = viewId;
    }

    public String getFilters() {
        return filters;
    }

    public void setFilters(String filters) {
        this.filters = filters;
    }

    public String getAggregates() {
        return aggregates;
    }

    public void setAggregates(String aggregates) {
        this.aggregates = aggregates;
    }

    public String getGroupBy() {
        return groupBy;
    }

    public void setGroupBy(String groupBy) {
        this.groupBy = groupBy;
    }

    public String getJoins() {
        return joins;
    }

    public void setJoins(String joins) {
        this.joins = joins;
    }

    public Page getPage(int defaultPageSize) {
        if(null != limit || null != offset) {
            if(null == limit) {
                return Page.limit(defaultPageSize, offset);
            }else{
                return Page.limit(limit, null == offset ? 0 : offset);
            }
        }

        if(null != pageIndex || null != pageSize) {
            if(null == pageIndex) {
                return Page.indexOf(1, pageSize);
            }else{
                return Page.indexOf(pageIndex, null == pageSize ? defaultPageSize : pageSize);
            }
        }

        return Page.indexOf(1, defaultPageSize);
    }

}
@@ -1645,32 +1645,122 @@
 ageSize;%0A    %7D%0A%0A
+    public void setPage_Size(Integer pageSize) %7B%0A        this.pageSize = pageSize;%0A    %7D%0A%0A
     public Integ
@@ -1775,24 +1775,24 @@
 geIndex() %7B%0A
-
         retu
@@ -1897,32 +1897,110 @@
 geIndex;%0A    %7D%0A%0A
+    public void setPage(Integer page) %7B%0A        this.pageIndex = page;%0A    %7D%0A%0A
     public Integ
@@ -2542,32 +2542,116 @@
 orderBy;%0A    %7D%0A%0A
+    public void setOrderby(String orderBy) %7B%0A        this.orderBy = orderBy;%0A    %7D%0A%0A
     public Strin
@@ -3195,24 +3195,24 @@
  groupBy) %7B%0A
-
         this
@@ -3230,32 +3230,116 @@
 groupBy;%0A    %7D%0A%0A
+    public void setGropuby(String gropuby) %7B%0A        this.groupBy = gropuby;%0A    %7D%0A%0A
     public Strin

add pure java setter for some properties at QueryOptions