1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PageIteratorTag extends TagSupport {
40  
41      public int doStartTag() throws JspException {
42          try {
43              _pages = (int)Math.ceil((double)_total / _delta);
44  
45              HttpServletRequest request =
46                  (HttpServletRequest)pageContext.getRequest();
47  
48              request.setAttribute(
49                  "liferay-ui:page-iterator:formName", _formName);
50              request.setAttribute(
51                  "liferay-ui:page-iterator:cur", String.valueOf(_cur));
52              request.setAttribute(
53                  "liferay-ui:page-iterator:curParam", _curParam);
54              request.setAttribute(
55                  "liferay-ui:page-iterator:delta", String.valueOf(_delta));
56              request.setAttribute(
57                  "liferay-ui:page-iterator:deltaParam", _deltaParam);
58              request.setAttribute("liferay-ui:page-iterator:jsCall", _jsCall);
59              request.setAttribute(
60                  "liferay-ui:page-iterator:maxPages", String.valueOf(_maxPages));
61              request.setAttribute("liferay-ui:page-iterator:target", _target);
62              request.setAttribute(
63                  "liferay-ui:page-iterator:total", String.valueOf(_total));
64              request.setAttribute("liferay-ui:page-iterator:url", _url);
65              request.setAttribute(
66                  "liferay-ui:page-iterator:urlAnchor", _urlAnchor);
67              request.setAttribute(
68                  "liferay-ui:page-iterator:pages", String.valueOf(_pages));
69              request.setAttribute("liferay-ui:page-iterator:type", _type);
70  
71              PortalIncludeUtil.include(pageContext, getStartPage());
72  
73              return EVAL_BODY_INCLUDE;
74          }
75          catch (Exception e) {
76              throw new JspException(e);
77          }
78      }
79  
80      public int doEndTag() throws JspException {
81          try {
82              if (_pages > 1) {
83                  PortalIncludeUtil.include(pageContext, getEndPage());
84              }
85  
86              return EVAL_PAGE;
87          }
88          catch (Exception e) {
89              throw new JspException(e);
90          }
91      }
92  
93      public String getStartPage() {
94          if (Validator.isNull(_startPage)) {
95              return _START_PAGE;
96          }
97          else {
98              return _startPage;
99          }
100     }
101 
102     public void setStartPage(String startPage) {
103         _startPage = startPage;
104     }
105 
106     public String getEndPage() {
107         if (Validator.isNull(_endPage)) {
108             return _END_PAGE;
109         }
110         else {
111             return _endPage;
112         }
113     }
114 
115     public void setEndPage(String endPage) {
116         _endPage = endPage;
117     }
118 
119     public void setFormName(String formName) {
120         _formName = formName;
121     }
122 
123     public void setCur(int cur) {
124         _cur = cur;
125     }
126 
127     public void setCurParam(String curParam) {
128         _curParam = curParam;
129     }
130 
131     public void setDelta(int delta) {
132         _delta = delta;
133     }
134 
135     public void setDeltaParam(String deltaParam) {
136         _deltaParam = deltaParam;
137     }
138 
139     public void setJsCall(String jsCall) {
140         _jsCall = jsCall;
141     }
142 
143     public void setMaxPages(int maxPages) {
144         _maxPages = maxPages;
145     }
146 
147     public void setTarget(String target) {
148         _target = target;
149     }
150 
151     public void setTotal(int total) {
152         _total = total;
153     }
154 
155     public void setType(String type) {
156         _type = type;
157     }
158 
159     public void setUrl(String url) {
160         _url = url;
161         _urlAnchor = StringPool.BLANK;
162 
163         int pos = _url.indexOf("#");
164 
165         if (pos != -1) {
166             _url = url.substring(0, pos);
167             _urlAnchor = url.substring(pos, url.length());
168         }
169 
170         if (_url.indexOf("?") == -1) {
171             _url += "?";
172         }
173         else if (!_url.endsWith("&")) {
174             _url += "&";
175         }
176     }
177 
178     private static final String _START_PAGE =
179         "/html/taglib/ui/page_iterator/start.jsp";
180 
181     private static final String _END_PAGE =
182         "/html/taglib/ui/page_iterator/end.jsp";
183 
184     private String _startPage;
185     private String _endPage;
186     private String _formName = "fm";
187     private int _cur;
188     private String _curParam;
189     private int _delta = SearchContainer.DEFAULT_DELTA;
190     private String _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
191     private String _jsCall;
192     private int _maxPages = 10;
193     private String _target = "_self";
194     private int _total;
195     private String _type = "regular";
196     private String _url;
197     private String _urlAnchor;
198     private int _pages;
199 
200 }