1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.ui;
24  
25  import com.liferay.portal.kernel.dao.search.SearchContainer;
26  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.jsp.JspException;
32  import javax.servlet.jsp.tagext.TagSupport;
33  
34  /**
35   * <a href="PageIteratorTag.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class PageIteratorTag extends TagSupport {
41  
42      public int doStartTag() throws JspException {
43          try {
44              _pages = (int)Math.ceil((double)_total / _delta);
45  
46              HttpServletRequest request =
47                  (HttpServletRequest)pageContext.getRequest();
48  
49              request.setAttribute(
50                  "liferay-ui:page-iterator:formName", _formName);
51              request.setAttribute(
52                  "liferay-ui:page-iterator:cur", String.valueOf(_cur));
53              request.setAttribute(
54                  "liferay-ui:page-iterator:curParam", _curParam);
55              request.setAttribute(
56                  "liferay-ui:page-iterator:delta", String.valueOf(_delta));
57              request.setAttribute(
58                  "liferay-ui:page-iterator:deltaParam", _deltaParam);
59              request.setAttribute("liferay-ui:page-iterator:jsCall", _jsCall);
60              request.setAttribute(
61                  "liferay-ui:page-iterator:maxPages", String.valueOf(_maxPages));
62              request.setAttribute("liferay-ui:page-iterator:target", _target);
63              request.setAttribute(
64                  "liferay-ui:page-iterator:total", String.valueOf(_total));
65              request.setAttribute("liferay-ui:page-iterator:url", _url);
66              request.setAttribute(
67                  "liferay-ui:page-iterator:urlAnchor", _urlAnchor);
68              request.setAttribute(
69                  "liferay-ui:page-iterator:pages", String.valueOf(_pages));
70              request.setAttribute("liferay-ui:page-iterator:type", _type);
71  
72              PortalIncludeUtil.include(pageContext, getStartPage());
73  
74              return EVAL_BODY_INCLUDE;
75          }
76          catch (Exception e) {
77              throw new JspException(e);
78          }
79      }
80  
81      public int doEndTag() throws JspException {
82          try {
83              if (_pages > 1) {
84                  PortalIncludeUtil.include(pageContext, getEndPage());
85              }
86  
87              return EVAL_PAGE;
88          }
89          catch (Exception e) {
90              throw new JspException(e);
91          }
92      }
93  
94      public String getStartPage() {
95          if (Validator.isNull(_startPage)) {
96              return _START_PAGE;
97          }
98          else {
99              return _startPage;
100         }
101     }
102 
103     public void setStartPage(String startPage) {
104         _startPage = startPage;
105     }
106 
107     public String getEndPage() {
108         if (Validator.isNull(_endPage)) {
109             return _END_PAGE;
110         }
111         else {
112             return _endPage;
113         }
114     }
115 
116     public void setEndPage(String endPage) {
117         _endPage = endPage;
118     }
119 
120     public void setFormName(String formName) {
121         _formName = formName;
122     }
123 
124     public void setCur(int cur) {
125         _cur = cur;
126     }
127 
128     public void setCurParam(String curParam) {
129         _curParam = curParam;
130     }
131 
132     public void setDelta(int delta) {
133         _delta = delta;
134     }
135 
136     public void setDeltaParam(String deltaParam) {
137         _deltaParam = deltaParam;
138     }
139 
140     public void setJsCall(String jsCall) {
141         _jsCall = jsCall;
142     }
143 
144     public void setMaxPages(int maxPages) {
145         _maxPages = maxPages;
146     }
147 
148     public void setTarget(String target) {
149         _target = target;
150     }
151 
152     public void setTotal(int total) {
153         _total = total;
154     }
155 
156     public void setType(String type) {
157         _type = type;
158     }
159 
160     public void setUrl(String url) {
161         _url = url;
162         _urlAnchor = StringPool.BLANK;
163 
164         int pos = _url.indexOf("#");
165 
166         if (pos != -1) {
167             _url = url.substring(0, pos);
168             _urlAnchor = url.substring(pos, url.length());
169         }
170 
171         if (_url.indexOf("?") == -1) {
172             _url += "?";
173         }
174         else if (!_url.endsWith("&")) {
175             _url += "&";
176         }
177     }
178 
179     private static final String _START_PAGE =
180         "/html/taglib/ui/page_iterator/start.jsp";
181 
182     private static final String _END_PAGE =
183         "/html/taglib/ui/page_iterator/end.jsp";
184 
185     private String _startPage;
186     private String _endPage;
187     private String _formName = "fm";
188     private int _cur;
189     private String _curParam;
190     private int _delta = SearchContainer.DEFAULT_DELTA;
191     private String _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
192     private String _jsCall;
193     private int _maxPages = 10;
194     private String _target = "_self";
195     private int _total;
196     private String _type = "regular";
197     private String _url;
198     private String _urlAnchor;
199     private int _pages;
200 
201 }