001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.dao.search.SearchContainer;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.taglib.util.IncludeTag;
022    
023    import javax.servlet.http.HttpServletRequest;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class PageIteratorTag extends IncludeTag {
029    
030            public void setCur(int cur) {
031                    _cur = cur;
032            }
033    
034            public void setCurParam(String curParam) {
035                    _curParam = curParam;
036            }
037    
038            public void setDelta(int delta) {
039                    _delta = delta;
040            }
041    
042            public void setDeltaConfigurable(boolean deltaConfigurable) {
043                    _deltaConfigurable = deltaConfigurable;
044            }
045    
046            public void setDeltaParam(String deltaParam) {
047                    _deltaParam = deltaParam;
048            }
049    
050            public void setFormName(String formName) {
051                    _formName = formName;
052            }
053    
054            public void setJsCall(String jsCall) {
055                    _jsCall = jsCall;
056            }
057    
058            public void setMaxPages(int maxPages) {
059                    _maxPages = maxPages;
060            }
061    
062            public void setTarget(String target) {
063                    _target = target;
064            }
065    
066            public void setTotal(int total) {
067                    _total = total;
068            }
069    
070            public void setType(String type) {
071                    _type = type;
072            }
073    
074            public void setUrl(String url) {
075                    String[] urlArray = PortalUtil.stripURLAnchor(url, StringPool.POUND);
076    
077                    _url = urlArray[0];
078                    _urlAnchor = urlArray[1];
079    
080                    if (_url.indexOf(CharPool.QUESTION) == -1) {
081                            _url += "?";
082                    }
083                    else if (!_url.endsWith("&")) {
084                            _url += "&";
085                    }
086            }
087    
088            @Override
089            protected void cleanUp() {
090                    _cur = 0;
091                    _curParam = null;
092                    _delta = SearchContainer.DEFAULT_DELTA;
093                    _deltaConfigurable = SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
094                    _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
095                    _formName = "fm";
096                    _jsCall = null;
097                    _maxPages = 10;
098                    _pages = 0;
099                    _target = "_self";
100                    _total = 0;
101                    _type = "regular";
102                    _url = null;
103                    _urlAnchor = null;
104            }
105    
106            @Override
107            protected String getEndPage() {
108                    if (_pages > 1) {
109                            return _END_PAGE;
110                    }
111                    else {
112                            return null;
113                    }
114            }
115    
116            @Override
117            protected String getStartPage() {
118                    return _START_PAGE;
119            }
120    
121            @Override
122            protected void setAttributes(HttpServletRequest request) {
123                    _pages = (int)Math.ceil((double)_total / _delta);
124    
125                    request.setAttribute(
126                            "liferay-ui:page-iterator:cur", String.valueOf(_cur));
127                    request.setAttribute("liferay-ui:page-iterator:curParam", _curParam);
128                    request.setAttribute(
129                            "liferay-ui:page-iterator:delta", String.valueOf(_delta));
130                    request.setAttribute(
131                            "liferay-ui:page-iterator:deltaConfigurable",
132                            String.valueOf(_deltaConfigurable));
133                    request.setAttribute(
134                            "liferay-ui:page-iterator:deltaParam", _deltaParam);
135                    request.setAttribute("liferay-ui:page-iterator:formName", _formName);
136                    request.setAttribute("liferay-ui:page-iterator:jsCall", _jsCall);
137                    request.setAttribute(
138                            "liferay-ui:page-iterator:maxPages", String.valueOf(_maxPages));
139                    request.setAttribute(
140                            "liferay-ui:page-iterator:pages", String.valueOf(_pages));
141                    request.setAttribute("liferay-ui:page-iterator:target", _target);
142                    request.setAttribute(
143                            "liferay-ui:page-iterator:total", String.valueOf(_total));
144                    request.setAttribute("liferay-ui:page-iterator:type", _type);
145                    request.setAttribute("liferay-ui:page-iterator:url", _url);
146                    request.setAttribute("liferay-ui:page-iterator:urlAnchor", _urlAnchor);
147            }
148    
149            private static final String _END_PAGE =
150                    "/html/taglib/ui/page_iterator/end.jsp";
151    
152            private static final String _START_PAGE =
153                    "/html/taglib/ui/page_iterator/start.jsp";
154    
155            private int _cur;
156            private String _curParam;
157            private int _delta = SearchContainer.DEFAULT_DELTA;
158            private boolean _deltaConfigurable =
159                    SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
160            private String _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
161            private String _formName = "fm";
162            private String _jsCall;
163            private int _maxPages = 10;
164            private int _pages;
165            private String _target = "_self";
166            private int _total;
167            private String _type = "regular";
168            private String _url;
169            private String _urlAnchor;
170    
171    }