001    /**
002     * Copyright (c) 2000-2013 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 setId(String id) {
055                    _id = id;
056            }
057    
058            public void setJsCall(String jsCall) {
059                    _jsCall = jsCall;
060            }
061    
062            public void setMaxPages(int maxPages) {
063                    _maxPages = maxPages;
064            }
065    
066            public void setTarget(String target) {
067                    _target = target;
068            }
069    
070            public void setTotal(int total) {
071                    _total = total;
072            }
073    
074            public void setType(String type) {
075                    _type = type;
076            }
077    
078            public void setUrl(String url) {
079                    String[] urlArray = PortalUtil.stripURLAnchor(url, StringPool.POUND);
080    
081                    _url = urlArray[0];
082                    _urlAnchor = urlArray[1];
083    
084                    if (_url.indexOf(CharPool.QUESTION) == -1) {
085                            _url += "?";
086                    }
087                    else if (!_url.endsWith("&")) {
088                            _url += "&";
089                    }
090            }
091    
092            @Override
093            protected void cleanUp() {
094                    _cur = 0;
095                    _curParam = null;
096                    _delta = SearchContainer.DEFAULT_DELTA;
097                    _deltaConfigurable = SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
098                    _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
099                    _formName = "fm";
100                    _id = null;
101                    _jsCall = null;
102                    _maxPages = 10;
103                    _pages = 0;
104                    _target = "_self";
105                    _total = 0;
106                    _type = "regular";
107                    _url = null;
108                    _urlAnchor = null;
109            }
110    
111            @Override
112            protected String getEndPage() {
113                    if (_pages > 1) {
114                            return _END_PAGE;
115                    }
116                    else {
117                            return null;
118                    }
119            }
120    
121            @Override
122            protected String getStartPage() {
123                    return _START_PAGE;
124            }
125    
126            @Override
127            protected void setAttributes(HttpServletRequest request) {
128                    _pages = (int)Math.ceil((double)_total / _delta);
129    
130                    request.setAttribute(
131                            "liferay-ui:page-iterator:cur", String.valueOf(_cur));
132                    request.setAttribute("liferay-ui:page-iterator:curParam", _curParam);
133                    request.setAttribute(
134                            "liferay-ui:page-iterator:delta", String.valueOf(_delta));
135                    request.setAttribute(
136                            "liferay-ui:page-iterator:deltaConfigurable",
137                            String.valueOf(_deltaConfigurable));
138                    request.setAttribute(
139                            "liferay-ui:page-iterator:deltaParam", _deltaParam);
140                    request.setAttribute("liferay-ui:page-iterator:formName", _formName);
141                    request.setAttribute("liferay-ui:page-iterator:id", _id);
142                    request.setAttribute("liferay-ui:page-iterator:jsCall", _jsCall);
143                    request.setAttribute(
144                            "liferay-ui:page-iterator:maxPages", String.valueOf(_maxPages));
145                    request.setAttribute(
146                            "liferay-ui:page-iterator:pages", String.valueOf(_pages));
147                    request.setAttribute("liferay-ui:page-iterator:target", _target);
148                    request.setAttribute(
149                            "liferay-ui:page-iterator:total", String.valueOf(_total));
150                    request.setAttribute("liferay-ui:page-iterator:type", _type);
151                    request.setAttribute("liferay-ui:page-iterator:url", _url);
152                    request.setAttribute("liferay-ui:page-iterator:urlAnchor", _urlAnchor);
153            }
154    
155            private static final String _END_PAGE =
156                    "/html/taglib/ui/page_iterator/end.jsp";
157    
158            private static final String _START_PAGE =
159                    "/html/taglib/ui/page_iterator/start.jsp";
160    
161            private int _cur;
162            private String _curParam;
163            private int _delta = SearchContainer.DEFAULT_DELTA;
164            private boolean _deltaConfigurable =
165                    SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
166            private String _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
167            private String _formName = "fm";
168            private String _id;
169            private String _jsCall;
170            private int _maxPages = 10;
171            private int _pages;
172            private String _target = "_self";
173            private int _total;
174            private String _type = "regular";
175            private String _url;
176            private String _urlAnchor;
177    
178    }