001    /**
002     * Copyright (c) 2000-present 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.kernel.util.Validator;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.taglib.util.IncludeTag;
023    
024    import javax.servlet.http.HttpServletRequest;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class PageIteratorTag extends IncludeTag {
030    
031            public void setCur(int cur) {
032                    _cur = cur;
033            }
034    
035            public void setCurParam(String curParam) {
036                    _curParam = curParam;
037            }
038    
039            public void setDelta(int delta) {
040                    _delta = delta;
041            }
042    
043            public void setDeltaConfigurable(boolean deltaConfigurable) {
044                    _deltaConfigurable = deltaConfigurable;
045            }
046    
047            public void setDeltaParam(String deltaParam) {
048                    _deltaParam = deltaParam;
049            }
050    
051            public void setFormName(String formName) {
052                    _formName = formName;
053            }
054    
055            public void setId(String id) {
056                    _id = id;
057            }
058    
059            public void setJsCall(String jsCall) {
060                    _jsCall = jsCall;
061            }
062    
063            public void setMarkupView(String markupView) {
064                    _markupView = markupView;
065            }
066    
067            public void setMaxPages(int maxPages) {
068                    _maxPages = maxPages;
069            }
070    
071            public void setTarget(String target) {
072                    _target = target;
073            }
074    
075            public void setTotal(int total) {
076                    _total = total;
077            }
078    
079            public void setType(String type) {
080                    _type = type;
081            }
082    
083            public void setUrl(String url) {
084                    String[] urlArray = PortalUtil.stripURLAnchor(url, StringPool.POUND);
085    
086                    _url = urlArray[0];
087                    _urlAnchor = urlArray[1];
088    
089                    if (_url.indexOf(CharPool.QUESTION) == -1) {
090                            _url += "?";
091                    }
092                    else if (!_url.endsWith("&")) {
093                            _url += "&";
094                    }
095            }
096    
097            @Override
098            protected void cleanUp() {
099                    _cur = 0;
100                    _curParam = null;
101                    _delta = SearchContainer.DEFAULT_DELTA;
102                    _deltaConfigurable = SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
103                    _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
104                    _formName = "fm";
105                    _id = null;
106                    _jsCall = null;
107                    _markupView = null;
108                    _maxPages = 10;
109                    _pages = 0;
110                    _target = "_self";
111                    _total = 0;
112                    _type = "regular";
113                    _url = null;
114                    _urlAnchor = null;
115            }
116    
117            @Override
118            protected String getEndPage() {
119                    if (_pages > 1) {
120                            if (Validator.isNotNull(_markupView)) {
121                                    return "/html/taglib/ui/page_iterator/" + _markupView +
122                                            "/end.jsp";
123                            }
124    
125                            return "/html/taglib/ui/page_iterator/end.jsp";
126                    }
127                    else {
128                            return null;
129                    }
130            }
131    
132            @Override
133            protected String getStartPage() {
134                    if (Validator.isNotNull(_markupView)) {
135                            return "/html/taglib/ui/page_iterator/" + _markupView +
136                                    "/start.jsp";
137                    }
138    
139                    return "/html/taglib/ui/page_iterator/start.jsp";
140            }
141    
142            @Override
143            protected void setAttributes(HttpServletRequest request) {
144                    _pages = (int)Math.ceil((double)_total / _delta);
145    
146                    request.setAttribute(
147                            "liferay-ui:page-iterator:cur", String.valueOf(_cur));
148                    request.setAttribute("liferay-ui:page-iterator:curParam", _curParam);
149                    request.setAttribute(
150                            "liferay-ui:page-iterator:delta", String.valueOf(_delta));
151                    request.setAttribute(
152                            "liferay-ui:page-iterator:deltaConfigurable",
153                            String.valueOf(_deltaConfigurable));
154                    request.setAttribute(
155                            "liferay-ui:page-iterator:deltaParam", _deltaParam);
156                    request.setAttribute("liferay-ui:page-iterator:formName", _formName);
157                    request.setAttribute("liferay-ui:page-iterator:id", _id);
158                    request.setAttribute("liferay-ui:page-iterator:jsCall", _jsCall);
159                    request.setAttribute(
160                            "liferay-ui:page-iterator:maxPages", String.valueOf(_maxPages));
161                    request.setAttribute(
162                            "liferay-ui:page-iterator:pages", String.valueOf(_pages));
163                    request.setAttribute("liferay-ui:page-iterator:target", _target);
164                    request.setAttribute(
165                            "liferay-ui:page-iterator:total", String.valueOf(_total));
166                    request.setAttribute("liferay-ui:page-iterator:type", _type);
167                    request.setAttribute("liferay-ui:page-iterator:url", _url);
168                    request.setAttribute("liferay-ui:page-iterator:urlAnchor", _urlAnchor);
169            }
170    
171            private int _cur;
172            private String _curParam;
173            private int _delta = SearchContainer.DEFAULT_DELTA;
174            private boolean _deltaConfigurable =
175                    SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
176            private String _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
177            private String _formName = "fm";
178            private String _id;
179            private String _jsCall;
180            private String _markupView;
181            private int _maxPages = 10;
182            private int _pages;
183            private String _target = "_self";
184            private int _total;
185            private String _type = "regular";
186            private String _url;
187            private String _urlAnchor;
188    
189    }