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:deltaConfigurable",
58                  String.valueOf(_deltaConfigurable));
59              request.setAttribute(
60                  "liferay-ui:page-iterator:deltaParam", _deltaParam);
61              request.setAttribute("liferay-ui:page-iterator:jsCall", _jsCall);
62              request.setAttribute(
63                  "liferay-ui:page-iterator:maxPages", String.valueOf(_maxPages));
64              request.setAttribute("liferay-ui:page-iterator:target", _target);
65              request.setAttribute(
66                  "liferay-ui:page-iterator:total", String.valueOf(_total));
67              request.setAttribute("liferay-ui:page-iterator:url", _url);
68              request.setAttribute(
69                  "liferay-ui:page-iterator:urlAnchor", _urlAnchor);
70              request.setAttribute(
71                  "liferay-ui:page-iterator:pages", String.valueOf(_pages));
72              request.setAttribute("liferay-ui:page-iterator:type", _type);
73  
74              PortalIncludeUtil.include(pageContext, getStartPage());
75  
76              return EVAL_BODY_INCLUDE;
77          }
78          catch (Exception e) {
79              throw new JspException(e);
80          }
81      }
82  
83      public int doEndTag() throws JspException {
84          try {
85              if (_pages > 1) {
86                  PortalIncludeUtil.include(pageContext, getEndPage());
87              }
88  
89              return EVAL_PAGE;
90          }
91          catch (Exception e) {
92              throw new JspException(e);
93          }
94      }
95  
96      public String getStartPage() {
97          if (Validator.isNull(_startPage)) {
98              return _START_PAGE;
99          }
100         else {
101             return _startPage;
102         }
103     }
104 
105     public void setStartPage(String startPage) {
106         _startPage = startPage;
107     }
108 
109     public String getEndPage() {
110         if (Validator.isNull(_endPage)) {
111             return _END_PAGE;
112         }
113         else {
114             return _endPage;
115         }
116     }
117 
118     public void setEndPage(String endPage) {
119         _endPage = endPage;
120     }
121 
122     public void setFormName(String formName) {
123         _formName = formName;
124     }
125 
126     public void setCur(int cur) {
127         _cur = cur;
128     }
129 
130     public void setCurParam(String curParam) {
131         _curParam = curParam;
132     }
133 
134     public void setDelta(int delta) {
135         _delta = delta;
136     }
137 
138     public void setDeltaConfigurable(boolean deltaConfigurable) {
139         _deltaConfigurable = deltaConfigurable;
140     }
141 
142     public void setDeltaParam(String deltaParam) {
143         _deltaParam = deltaParam;
144     }
145 
146     public void setJsCall(String jsCall) {
147         _jsCall = jsCall;
148     }
149 
150     public void setMaxPages(int maxPages) {
151         _maxPages = maxPages;
152     }
153 
154     public void setTarget(String target) {
155         _target = target;
156     }
157 
158     public void setTotal(int total) {
159         _total = total;
160     }
161 
162     public void setType(String type) {
163         _type = type;
164     }
165 
166     public void setUrl(String url) {
167         _url = url;
168         _urlAnchor = StringPool.BLANK;
169 
170         int pos = _url.indexOf("#");
171 
172         if (pos != -1) {
173             _url = url.substring(0, pos);
174             _urlAnchor = url.substring(pos, url.length());
175         }
176 
177         if (_url.indexOf("?") == -1) {
178             _url += "?";
179         }
180         else if (!_url.endsWith("&")) {
181             _url += "&";
182         }
183     }
184 
185     private static final String _START_PAGE =
186         "/html/taglib/ui/page_iterator/start.jsp";
187 
188     private static final String _END_PAGE =
189         "/html/taglib/ui/page_iterator/end.jsp";
190 
191     private String _startPage;
192     private String _endPage;
193     private String _formName = "fm";
194     private int _cur;
195     private String _curParam;
196     private int _delta = SearchContainer.DEFAULT_DELTA;
197     private boolean _deltaConfigurable =
198         SearchContainer.DEFAULT_DELTA_CONFIGURABLE;
199     private String _deltaParam = SearchContainer.DEFAULT_DELTA_PARAM;
200     private String _jsCall;
201     private int _maxPages = 10;
202     private String _target = "_self";
203     private int _total;
204     private String _type = "regular";
205     private String _url;
206     private String _urlAnchor;
207     private int _pages;
208 
209 }