- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
package util;
import play.db.jpa.JPA;
import play.db.jpa.Model;
import play.mvc.Http;
import play.mvc.Router;
import play.mvc.Scope;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Page of results for model classes (because paginate module looks too buggy).
 * It is not generic solution but it fits the needs.
 * @author <a href="mailto:[email protected]">Roman Kashitsyn</a>
 */
public class Page<M extends Model> implements PageBuilder<M> {
    
    public static final int MAX_PAGE_SIZE = 20;
    public static final int DEFAULT_PAGE_SIZE = 10;
    public static final int DEFAULT_PAGE_NUMBER = 1;
    public static final String PAGE_NUMBER_PARAM = "page";
    public static final String PAGE_SIZE_PARAM = "psize";
    public static final String ORDER_PARAM = "order";
    public static final String ORDER_BY_PARAM = "orderBy";
    
    private static final String ASC = "asc";
    private static final String DESC = "desc";
    private static final List<String> ALLOWED_ORDERS = Arrays.asList(ASC, DESC);
    private int pageSize = DEFAULT_PAGE_SIZE;
    private int pageNumber = DEFAULT_PAGE_NUMBER;
    private int total;
    private String orderBy;
    private String order;
    private final Http.Request request;
    private final Map<String, Object> params;
    private List<M> results;
    private final Class<M> clazz;
    public class SizeSwitcher {
        private final Map<String, Object> params;
        private SizeSwitcher() {
            // making defensive copy
            params = new HashMap<String, Object>(Page.this.params);
            // list of different size should always begin with 1 page
            params.put(PAGE_NUMBER_PARAM, 1);
        }
        public String urlToSwitchSize(int newSize) {
            params.put(PAGE_SIZE_PARAM, newSize);
            return Router.reverse(Page.this.request.action, params).url;
        }
        public int currentSize() {
            return Page.this.pageSize;
        }
    }
    private Page(Class<M> clazz) {
        this.clazz = clazz;
        request = Http.Request.current();
        params = new HashMap<String, Object>(Scope.Params.current().allSimple());
    }
    
    public static <M extends Model> PageBuilder<M> of(Class<M> modelClass) {
        return new Page<M>(modelClass);
    }
    
    public PageBuilder<M> withParams(Map<?, ?> params) {
        pageNumber = limit(params.get(PAGE_NUMBER_PARAM), pageNumber, Integer.MAX_VALUE);
        pageSize = limit(params.get(PAGE_SIZE_PARAM), pageSize, MAX_PAGE_SIZE);
        Object orderByParam = params.get(ORDER_BY_PARAM);
        if (orderByParam != null) {
            orderBy(getSingleValue(orderByParam).toString());
        }
        Object orderParam = params.get(ORDER_PARAM);
        if (orderParam != null) {
            String proposedValue = getSingleValue(orderParam).toString();
            if (ALLOWED_ORDERS.contains(proposedValue)) {
                order = getSingleValue(orderParam).toString();
            }
        }
        return this;
    }
    public PageBuilder<M> withNumber(int num) {
        this.pageNumber = num;
        return this;
    }
                                 
        
>private Page(Class<M> clazz)
>withParams
>of
Видно, что автор читал много сырцов либ. Хороший стиль.
>Router.reverse(Page.this.request.action , params).url
>url
Блин, даже public-поле. Одобряю.
___________
Сайтом ошибся.
> даже public-поле
Авторы play! сознательно упростили многие вещи и пишут код нет так, как "принято". Поля всех моделей, кстати, тоже public советуют делать. Борются с бойлерплэйтом, наверное.
>упростили многие вещи и пишут код нет так, как "принято"
И правильно. Раз модификатор public введен в язык, почему бы им не воспользоваться!?
я бы это в хелпер-метод вынес
пардоньте, но за ТАКОЕ надо публично на говнокод - и это при живом import play.db.jpa.JPA; -неужто нельзя было штатными JPA средствами пользоваться?