net.groupbuy.template.directive.PaginationDirective.java Source code

Java tutorial

Introduction

Here is the source code for net.groupbuy.template.directive.PaginationDirective.java

Source

/*
 * Copyright 2005-2013 shopxx.net. All rights reserved.
 * Support: http://www.shopxx.net
 * License: http://www.shopxx.net/license
 */
package net.groupbuy.template.directive;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.groupbuy.util.FreemarkerUtils;

import org.springframework.stereotype.Component;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * ? - 
 * 
 * @author SHOP++ Team
 * @version 3.0
 */
@Component("paginationDirective")
public class PaginationDirective extends BaseDirective {

    /** "?"??? */
    private static final String PATTERN_PARAMETER_NAME = "pattern";

    /** "?"??? */
    private static final String PAGE_NUMBER_PARAMETER_NAME = "pageNumber";

    /** ""??? */
    private static final String TOTAL_PAGES_PARAMETER_NAME = "totalPages";

    /** "?"??? */
    private static final String SEGMENT_COUNT_PARAMETER_NAME = "segmentCount";

    /** "?"???? */
    private static final String PATTERN_VARIABLE_NAME = "pattern";

    /** "?"???? */
    private static final String PAGE_NUMBER_VARIABLE_NAME = "pageNumber";

    /** ""???? */
    private static final String PAGE_COUNT_VARIABLE_NAME = "totalPages";

    /** "?"???? */
    private static final String SEGMENT_COUNT_VARIABLE_NAME = "segmentCount";

    /** "?"???? */
    private static final String HAS_PREVIOUS_VARIABLE_NAME = "hasPrevious";

    /** "?"???? */
    private static final String HAS_NEXT_VARIABLE_NAME = "hasNext";

    /** "?"???? */
    private static final String IS_FIRST_VARIABLE_NAME = "isFirst";

    /** "?"???? */
    private static final String IS_LAST_VARIABLE_NAME = "isLast";

    /** "?"???? */
    private static final String PREVIOUS_PAGE_NUMBER_VARIABLE_NAME = "previousPageNumber";

    /** "?"???? */
    private static final String NEXT_PAGE_NUMBER_VARIABLE_NAME = "nextPageNumber";

    /** "?"???? */
    private static final String FIRST_PAGE_NUMBER_VARIABLE_NAME = "firstPageNumber";

    /** "?"???? */
    private static final String LAST_PAGE_NUMBER_VARIABLE_NAME = "lastPageNumber";

    /** "?"???? */
    private static final String SEGMENT_VARIABLE_NAME = "segment";

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
            throws TemplateException, IOException {
        String pattern = FreemarkerUtils.getParameter(PATTERN_PARAMETER_NAME, String.class, params);
        Integer pageNumber = FreemarkerUtils.getParameter(PAGE_NUMBER_PARAMETER_NAME, Integer.class, params);
        Integer totalPages = FreemarkerUtils.getParameter(TOTAL_PAGES_PARAMETER_NAME, Integer.class, params);
        Integer segmentCount = FreemarkerUtils.getParameter(SEGMENT_COUNT_PARAMETER_NAME, Integer.class, params);

        if (pageNumber == null || pageNumber < 1) {
            pageNumber = 1;
        }
        if (totalPages == null || totalPages < 1) {
            totalPages = 1;
        }
        if (segmentCount == null || segmentCount < 1) {
            segmentCount = 5;
        }
        boolean hasPrevious = pageNumber > 1;
        boolean hasNext = pageNumber < totalPages;
        boolean isFirst = pageNumber == 1;
        boolean isLast = pageNumber.equals(totalPages);
        int previousPageNumber = pageNumber - 1;
        int nextPageNumber = pageNumber + 1;
        int firstPageNumber = 1;
        int lastPageNumber = totalPages;
        int startSegmentPageNumber = pageNumber - (int) Math.floor((segmentCount - 1) / 2D);
        int endSegmentPageNumber = pageNumber + (int) Math.ceil((segmentCount - 1) / 2D);
        if (startSegmentPageNumber < 1) {
            startSegmentPageNumber = 1;
        }
        if (endSegmentPageNumber > totalPages) {
            endSegmentPageNumber = totalPages;
        }
        List<Integer> segment = new ArrayList<Integer>();
        for (int i = startSegmentPageNumber; i <= endSegmentPageNumber; i++) {
            segment.add(i);
        }

        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put(PATTERN_VARIABLE_NAME, pattern);
        variables.put(PAGE_NUMBER_VARIABLE_NAME, pageNumber);
        variables.put(PAGE_COUNT_VARIABLE_NAME, totalPages);
        variables.put(SEGMENT_COUNT_VARIABLE_NAME, segmentCount);
        variables.put(HAS_PREVIOUS_VARIABLE_NAME, hasPrevious);
        variables.put(HAS_NEXT_VARIABLE_NAME, hasNext);
        variables.put(IS_FIRST_VARIABLE_NAME, isFirst);
        variables.put(IS_LAST_VARIABLE_NAME, isLast);
        variables.put(PREVIOUS_PAGE_NUMBER_VARIABLE_NAME, previousPageNumber);
        variables.put(NEXT_PAGE_NUMBER_VARIABLE_NAME, nextPageNumber);
        variables.put(FIRST_PAGE_NUMBER_VARIABLE_NAME, firstPageNumber);
        variables.put(LAST_PAGE_NUMBER_VARIABLE_NAME, lastPageNumber);
        variables.put(SEGMENT_VARIABLE_NAME, segment);
        setLocalVariables(variables, env, body);
    }

}