001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.web.directive;
017
018import com.jfinal.core.Controller;
019import com.jfinal.plugin.activerecord.Page;
020import com.jfinal.template.Env;
021import com.jfinal.template.io.Writer;
022import com.jfinal.template.stat.Scope;
023import io.jboot.utils.StrUtil;
024import io.jboot.web.controller.JbootControllerContext;
025import io.jboot.web.directive.base.PaginateDirectiveBase;
026
027import javax.servlet.http.HttpServletRequest;
028import java.util.Enumeration;
029import java.util.Map;
030
031public class JbootPaginateDirective extends PaginateDirectiveBase {
032
033    private static final String URL_PAGE_INFO = "page=";
034    private static final String URL_QMARK = "?";
035    private static final String URL_AMARK = "&";
036
037    private static final String KEY_ANCHOR = "anchor";
038    private static final String KEY_PAGE_ATTR = "pageAttr";
039    private static final String DEFAULT_PAGE_ATTR = "page";
040
041    @Override
042    protected String getUrl(int pageNumber, Env env, Scope scope, Writer writer) {
043        HttpServletRequest request = JbootControllerContext.get().getRequest();
044        String queryString = request.getQueryString();
045
046        String url = request.getRequestURI();
047
048        if (StrUtil.isNotBlank(queryString)) {
049            url = url.concat(URL_QMARK).concat(queryString);
050        }
051
052        /**
053         * 锚链接
054         */
055        String anchor = getPara(KEY_ANCHOR, scope, StrUtil.EMPTY);
056        int index = url.indexOf(URL_PAGE_INFO);
057
058        /**
059         * 已经有 page=xx 参数了
060         */
061        if (index != -1) {
062            StringBuilder sb = new StringBuilder();
063            sb.append(url, 0, index).append(URL_PAGE_INFO).append(pageNumber);
064            int idx = url.indexOf(URL_AMARK, index);
065            if (idx != -1) {
066                sb.append(url.substring(idx));
067            }
068            return sb.append(anchor).toString();
069        }
070
071        /**
072         * 没有 page=xx 参数,但是已经有 query 请求
073         */
074        if (StrUtil.isNotBlank(queryString)) {
075            StringBuilder sb = new StringBuilder(url);
076            return sb.append(URL_AMARK)
077                    .append(URL_PAGE_INFO)
078                    .append(pageNumber)
079                    .append(anchor)
080                    .toString();
081        }
082
083        /**
084         * 没有 query 请求
085         */
086        else {
087            StringBuilder sb = new StringBuilder(url);
088            return sb.append(URL_QMARK)
089                    .append(URL_PAGE_INFO)
090                    .append(pageNumber)
091                    .append(anchor)
092                    .toString();
093        }
094
095    }
096
097
098    @Override
099    protected Page<?> getPage(Env env, Scope scope, Writer writer) {
100        Controller controller = JbootControllerContext.get();
101        if (controller == null) {
102            return null;
103        }
104
105        String pageAttr = getPara(KEY_PAGE_ATTR, scope, DEFAULT_PAGE_ATTR);
106        Object page = controller.getAttr(pageAttr);
107        if (page instanceof Page) {
108            return (Page<?>) page;
109        }
110
111        Enumeration<String> attrNames = controller.getAttrNames();
112        if (attrNames != null) {
113            while (attrNames.hasMoreElements()) {
114                Object attrValue = controller.getAttr(attrNames.nextElement());
115                if (attrValue instanceof Page) {
116                    return (Page<?>) attrValue;
117                }
118            }
119        }
120
121        Map rootData = scope.getRootData();
122        if (rootData != null){
123            for (Object data : rootData.values()) {
124                if (data instanceof Page){
125                    return (Page<?>) data;
126                }
127            }
128        }
129
130        return null;
131    }
132
133}