001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.portlet.PortletURL;
024    
025    import javax.servlet.http.HttpServletRequest;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class PageIteratorTag extends IncludeTag {
031    
032            public void setCur(int cur) {
033                    _cur = cur;
034            }
035    
036            public void setCurParam(String curParam) {
037                    _curParam = curParam;
038            }
039    
040            public void setDelta(int delta) {
041                    _delta = delta;
042            }
043    
044            public void setDeltaConfigurable(boolean deltaConfigurable) {
045                    _deltaConfigurable = deltaConfigurable;
046            }
047    
048            public void setDeltaParam(String deltaParam) {
049                    _deltaParam = deltaParam;
050            }
051    
052            public void setForcePost(boolean forcePost) {
053                    _forcePost = forcePost;
054            }
055    
056            public void setFormName(String formName) {
057                    _formName = formName;
058            }
059    
060            public void setId(String id) {
061                    _id = id;
062            }
063    
064            public void setJsCall(String jsCall) {
065                    _jsCall = jsCall;
066            }
067    
068            public void setMaxPages(int maxPages) {
069                    _maxPages = maxPages;
070            }
071    
072            public void setPortletURL(PortletURL portletURL) {
073                    _portletURL = portletURL;
074            }
075    
076            public void setTarget(String target) {
077                    _target = target;
078            }
079    
080            public void setTotal(int total) {
081                    _total = total;
082            }
083    
084            public void setType(String type) {
085                    _type = type;
086            }
087    
088            /**
089             * @deprecated As of 7.0.0, replaced by {@link #setPortletURL(PortletURL)}
090             */
091            @Deprecated
092            public void setUrl(String url) {
093                    String[] urlArray = PortalUtil.stripURLAnchor(url, StringPool.POUND);
094    
095                    _url = urlArray[0];
096                    _urlAnchor = urlArray[1];
097    
098                    if (_url.indexOf(CharPool.QUESTION) == -1) {
099                            _url += "?";
100                    }
101                    else if (!_url.endsWith("&")) {
102                            _url += "&";
103                    }
104            }
105    
106            @Override
107            protected void cleanUp() {
108                    _cur = 0;
109                    _curParam = null;
110                    _delta = SearchContainer.DEFAULT_DELTA;
111                    _deltaConfigurable = SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
112                    _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
113                    _forcePost = SearchContainer.DEFAULT_FORCE_POST;
114                    _formName = "fm";
115                    _id = null;
116                    _jsCall = null;
117                    _maxPages = 10;
118                    _pages = 0;
119                    _portletURL = null;
120                    _target = "_self";
121                    _total = 0;
122                    _type = "regular";
123                    _url = null;
124                    _urlAnchor = null;
125            }
126    
127            @Override
128            protected String getEndPage() {
129                    if (_pages > 1) {
130                            return _END_PAGE;
131                    }
132                    else {
133                            return null;
134                    }
135            }
136    
137            @Override
138            protected String getStartPage() {
139                    return _START_PAGE;
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(
157                            "liferay-ui:page-iterator:forcePost", String.valueOf(_forcePost));
158                    request.setAttribute("liferay-ui:page-iterator:formName", _formName);
159                    request.setAttribute("liferay-ui:page-iterator:id", _id);
160                    request.setAttribute("liferay-ui:page-iterator:jsCall", _jsCall);
161                    request.setAttribute(
162                            "liferay-ui:page-iterator:maxPages", String.valueOf(_maxPages));
163                    request.setAttribute(
164                            "liferay-ui:page-iterator:pages", String.valueOf(_pages));
165                    request.setAttribute(
166                            "liferay-ui:page-iterator:portletURL", _portletURL);
167                    request.setAttribute("liferay-ui:page-iterator:target", _target);
168                    request.setAttribute(
169                            "liferay-ui:page-iterator:total", String.valueOf(_total));
170                    request.setAttribute("liferay-ui:page-iterator:type", _type);
171                    request.setAttribute("liferay-ui:page-iterator:url", _url);
172                    request.setAttribute("liferay-ui:page-iterator:urlAnchor", _urlAnchor);
173            }
174    
175            private static final String _END_PAGE =
176                    "/html/taglib/ui/page_iterator/end.jsp";
177    
178            private static final String _START_PAGE =
179                    "/html/taglib/ui/page_iterator/start.jsp";
180    
181            private int _cur;
182            private String _curParam;
183            private int _delta = SearchContainer.DEFAULT_DELTA;
184            private boolean _deltaConfigurable =
185                    SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
186            private String _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
187            private boolean _forcePost = SearchContainer.DEFAULT_FORCE_POST;
188            private String _formName = "fm";
189            private String _id;
190            private String _jsCall;
191            private int _maxPages = 10;
192            private int _pages;
193            private PortletURL _portletURL;
194            private String _target = "_self";
195            private int _total;
196            private String _type = "regular";
197            private String _url;
198            private String _urlAnchor;
199    
200    }