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