001
014
015 package com.liferay.portal.kernel.dao.search;
016
017 import com.liferay.portal.kernel.util.DeterminateKeyGenerator;
018 import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
019 import com.liferay.portal.kernel.util.GetterUtil;
020 import com.liferay.portal.kernel.util.OrderByComparator;
021 import com.liferay.portal.kernel.util.ParamUtil;
022 import com.liferay.portal.kernel.util.PortalUtil;
023 import com.liferay.portal.kernel.util.PropsKeys;
024 import com.liferay.portal.kernel.util.PropsUtil;
025 import com.liferay.portal.kernel.util.SearchContainerReference;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.StringUtil;
028 import com.liferay.portal.kernel.util.TextFormatter;
029 import com.liferay.portal.kernel.util.Validator;
030 import com.liferay.portal.kernel.util.WebKeys;
031
032 import java.util.ArrayList;
033 import java.util.List;
034 import java.util.Map;
035
036 import javax.portlet.PortletRequest;
037 import javax.portlet.PortletURL;
038
039 import javax.servlet.http.HttpServletRequest;
040
041
044 public class SearchContainer<R> {
045
046 public static final int DEFAULT_CUR = 1;
047
048 public static final String DEFAULT_CUR_PARAM = "cur";
049
050
053 @Deprecated
054 public static final int DEFAULT_CUR_VALUE = DEFAULT_CUR;
055
056 public static final int DEFAULT_DELTA = GetterUtil.getInteger(
057 PropsUtil.get(PropsKeys.SEARCH_CONTAINER_PAGE_DEFAULT_DELTA));
058
059 public static final boolean DEFAULT_DELTA_CONFIGURABLE = true;
060
061 public static final String DEFAULT_DELTA_PARAM = "delta";
062
063 public static final String DEFAULT_DEPRECATED_TOTAL_VAR = "deprecatedTotal";
064
065
068 @Deprecated
069 public static final int DEFAULT_MAX_PAGES = 25;
070
071 public static final String DEFAULT_ORDER_BY_COL_PARAM = "orderByCol";
072
073 public static final String DEFAULT_ORDER_BY_TYPE_PARAM = "orderByType";
074
075 public static final String DEFAULT_RESULTS_VAR = "results";
076
077 public static final String DEFAULT_TOTAL_VAR = "total";
078
079 public static final String DEFAULT_VAR = "searchContainer";
080
081 public static final int MAX_DELTA = 200;
082
083 public SearchContainer() {
084 _curParam = DEFAULT_CUR_PARAM;
085 _displayTerms = null;
086 _portletRequest = null;
087 _searchTerms = null;
088 }
089
090 public SearchContainer(
091 PortletRequest portletRequest, DisplayTerms displayTerms,
092 DisplayTerms searchTerms, String curParam, int cur, int delta,
093 PortletURL iteratorURL, List<String> headerNames,
094 String emptyResultsMessage) {
095
096 this (
097 portletRequest, displayTerms, searchTerms, curParam, cur, delta,
098 iteratorURL, headerNames, emptyResultsMessage, StringPool.BLANK);
099 }
100
101 public SearchContainer(
102 PortletRequest portletRequest, DisplayTerms displayTerms,
103 DisplayTerms searchTerms, String curParam, int cur, int delta,
104 PortletURL iteratorURL, List<String> headerNames,
105 String emptyResultsMessage, String cssClass) {
106
107 _portletRequest = portletRequest;
108 _displayTerms = displayTerms;
109 _searchTerms = searchTerms;
110
111 _curParam = curParam;
112
113 boolean resetCur = ParamUtil.getBoolean(portletRequest, "resetCur");
114
115 if (resetCur) {
116 _cur = DEFAULT_CUR;
117 }
118 else {
119 if (cur < 1) {
120 _cur = ParamUtil.getInteger(
121 portletRequest, _curParam, DEFAULT_CUR);
122
123 if (_cur < 1) {
124 _cur = DEFAULT_CUR;
125 }
126 }
127 else {
128 _cur = cur;
129 }
130 }
131
132 if (!_curParam.equals(DEFAULT_CUR_PARAM)) {
133 _deltaParam =
134 DEFAULT_DELTA_PARAM +
135 StringUtil.replace(
136 _curParam, DEFAULT_CUR_PARAM, StringPool.BLANK);
137 }
138
139 setDelta(ParamUtil.getInteger(portletRequest, _deltaParam, delta));
140
141 _iteratorURL = iteratorURL;
142
143 _iteratorURL.setParameter(_curParam, String.valueOf(_cur));
144 _iteratorURL.setParameter(_deltaParam, String.valueOf(_delta));
145
146 _setParameter(DisplayTerms.KEYWORDS);
147 _setParameter(DisplayTerms.ADVANCED_SEARCH);
148 _setParameter(DisplayTerms.AND_OPERATOR);
149
150 if (headerNames != null) {
151 _headerNames = new ArrayList<>(headerNames.size());
152
153 _headerNames.addAll(headerNames);
154
155 _buildNormalizedHeaderNames(_headerNames);
156 }
157
158 _emptyResultsMessage = emptyResultsMessage;
159
160 SearchContainerReference searchContainerReference =
161 (SearchContainerReference)portletRequest.getAttribute(
162 WebKeys.SEARCH_CONTAINER_REFERENCE);
163
164 if (searchContainerReference != null) {
165 searchContainerReference.register(this);
166 }
167
168 if (Validator.isNotNull(cssClass)) {
169 _cssClass = cssClass;
170 }
171 }
172
173 public SearchContainer(
174 PortletRequest portletRequest, DisplayTerms displayTerms,
175 DisplayTerms searchTerms, String curParam, int delta,
176 PortletURL iteratorURL, List<String> headerNames,
177 String emptyResultsMessage) {
178
179 this (
180 portletRequest, displayTerms, searchTerms, curParam, 0, delta,
181 iteratorURL, headerNames, emptyResultsMessage);
182 }
183
184 public SearchContainer(
185 PortletRequest portletRequest, PortletURL iteratorURL,
186 List<String> headerNames, String emptyResultsMessage) {
187
188 this(
189 portletRequest, null, null, DEFAULT_CUR_PARAM, DEFAULT_DELTA,
190 iteratorURL, headerNames, emptyResultsMessage);
191 }
192
193 public String getClassName() {
194 return _className;
195 }
196
197 public String getCssClass() {
198 return _cssClass;
199 }
200
201 public int getCur() {
202 return _cur;
203 }
204
205 public String getCurParam() {
206 return _curParam;
207 }
208
209
212 @Deprecated
213 public int getCurValue() {
214 return getCur();
215 }
216
217 public int getDelta() {
218 return _delta;
219 }
220
221 public String getDeltaParam() {
222 return _deltaParam;
223 }
224
225 public DisplayTerms getDisplayTerms() {
226 return _displayTerms;
227 }
228
229 public String getEmptyResultsMessage() {
230 return _emptyResultsMessage;
231 }
232
233 public int getEnd() {
234 return _end;
235 }
236
237 public List<String> getHeaderNames() {
238 return _headerNames;
239 }
240
241 public String getId(HttpServletRequest request, String namespace) {
242 if (_uniqueId) {
243 return _id;
244 }
245
246 if (Validator.isNotNull(_id)) {
247 _id = PortalUtil.getUniqueElementId(request, namespace, _id);
248 _uniqueId = true;
249
250 return _id;
251 }
252
253 String id = null;
254
255 if (Validator.isNotNull(_className)) {
256 String simpleClassName = _className;
257
258 int pos = simpleClassName.lastIndexOf(StringPool.PERIOD);
259
260 if (pos != -1) {
261 simpleClassName = simpleClassName.substring(pos + 1);
262 }
263
264 String variableCasingSimpleClassName = TextFormatter.format(
265 simpleClassName, TextFormatter.I);
266
267 id = TextFormatter.formatPlural(variableCasingSimpleClassName);
268
269 id = id.concat("SearchContainer");
270
271 _id = PortalUtil.getUniqueElementId(request, namespace, id);
272 _uniqueId = true;
273
274 return _id;
275 }
276
277 id = DeterminateKeyGenerator.generate("taglib_search_container");
278
279 _id = id.concat("SearchContainer");
280 _uniqueId = true;
281
282 return _id;
283 }
284
285 public PortletURL getIteratorURL() {
286 return _iteratorURL;
287 }
288
289
292 @Deprecated
293 public int getMaxPages() {
294 return _maxPages;
295 }
296
297 public List<String> getNormalizedHeaderNames() {
298 return _normalizedHeaderNames;
299 }
300
301 public Map<String, String> getOrderableHeaders() {
302 return _orderableHeaders;
303 }
304
305 public String getOrderByCol() {
306 return _orderByCol;
307 }
308
309 public String getOrderByColParam() {
310 return _orderByColParam;
311 }
312
313 public OrderByComparator<R> getOrderByComparator() {
314 return _orderByComparator;
315 }
316
317 public String getOrderByJS() {
318 return _orderByJS;
319 }
320
321 public String getOrderByType() {
322 return _orderByType;
323 }
324
325 public String getOrderByTypeParam() {
326 return _orderByTypeParam;
327 }
328
329 public PortletRequest getPortletRequest() {
330 return _portletRequest;
331 }
332
333 public int getResultEnd() {
334 return _resultEnd;
335 }
336
337 public List<ResultRow> getResultRows() {
338 return _resultRows;
339 }
340
341 public List<R> getResults() {
342 return _results;
343 }
344
345 public RowChecker getRowChecker() {
346 return _rowChecker;
347 }
348
349 public RowMover getRowMover() {
350 return _rowMover;
351 }
352
353 public DisplayTerms getSearchTerms() {
354 return _searchTerms;
355 }
356
357 public int getStart() {
358 return _start;
359 }
360
361 public int getTotal() {
362 return _total;
363 }
364
365 public String getTotalVar() {
366 return _totalVar;
367 }
368
369 public boolean hasResults() {
370 return !_results.isEmpty();
371 }
372
373 public boolean isDeltaConfigurable() {
374 return _deltaConfigurable;
375 }
376
377 public boolean isHover() {
378 return _hover;
379 }
380
381 public boolean isRecalculateCur() {
382 if ((_total == 0) && (_cur == DEFAULT_CUR)) {
383 return false;
384 }
385
386 if (((_cur - 1) * _delta) >= _total) {
387 return true;
388 }
389
390 return false;
391 }
392
393 public boolean isSearch() {
394 if (_searchTerms != null) {
395 return _searchTerms.isSearch();
396 }
397
398 return _search;
399 }
400
401 public void setClassName(String className) {
402 _className = className;
403 }
404
405 public void setCssClass(String cssClass) {
406 _cssClass = cssClass;
407 }
408
409 public void setDelta(int delta) {
410 if (delta <= 0) {
411 _delta = DEFAULT_DELTA;
412 }
413 else if (delta > MAX_DELTA) {
414 _delta = MAX_DELTA;
415 }
416 else {
417 _delta = delta;
418 }
419
420 _calculateStartAndEnd();
421 }
422
423 public void setDeltaConfigurable(boolean deltaConfigurable) {
424 _deltaConfigurable = deltaConfigurable;
425 }
426
427 public void setDeltaParam(String deltaParam) {
428 _deltaParam = deltaParam;
429 }
430
431 public void setEmptyResultsMessage(String emptyResultsMessage) {
432 _emptyResultsMessage = emptyResultsMessage;
433 }
434
435 public void setHeaderNames(List<String> headerNames) {
436 _headerNames = headerNames;
437
438 _buildNormalizedHeaderNames(headerNames);
439 }
440
441 public void setHover(boolean hover) {
442 _hover = hover;
443 }
444
445 public void setId(String id) {
446 _id = id;
447 }
448
449 public void setIteratorURL(PortletURL iteratorURL) {
450 _iteratorURL = iteratorURL;
451 }
452
453
456 @Deprecated
457 public void setMaxPages(int maxPages) {
458 _maxPages = maxPages;
459 }
460
461 public void setOrderableHeaders(Map<String, String> orderableHeaders) {
462 _orderableHeaders = orderableHeaders;
463 }
464
465 public void setOrderByCol(String orderByCol) {
466 _orderByCol = orderByCol;
467
468 _iteratorURL.setParameter(_orderByColParam, _orderByCol);
469 }
470
471 public void setOrderByColParam(String orderByColParam) {
472 _orderByColParam = orderByColParam;
473 }
474
475 public void setOrderByComparator(OrderByComparator<R> orderByComparator) {
476 _orderByComparator = orderByComparator;
477 }
478
479 public void setOrderByJS(String orderByJS) {
480 _orderByJS = orderByJS;
481 }
482
483 public void setOrderByType(String orderByType) {
484 _orderByType = orderByType;
485
486 _iteratorURL.setParameter(_orderByTypeParam, _orderByType);
487 }
488
489 public void setOrderByTypeParam(String orderByTypeParam) {
490 _orderByTypeParam = orderByTypeParam;
491 }
492
493 public void setResults(List<R> results) {
494 _results = results;
495 }
496
497 public void setRowChecker(RowChecker rowChecker) {
498 _rowChecker = rowChecker;
499 }
500
501 public void setRowMover(RowMover rowMover) {
502 _rowMover = rowMover;
503 }
504
505 public void setSearch(boolean search) {
506 _search = search;
507 }
508
509 public void setTotal(int total) {
510 _total = total;
511
512 _calculateCur();
513 _calculateStartAndEnd();
514 }
515
516 public void setTotalVar(String totalVar) {
517 _totalVar = totalVar;
518 }
519
520 private void _buildNormalizedHeaderNames(List<String> headerNames) {
521 if (headerNames == null) {
522 return;
523 }
524
525 _normalizedHeaderNames = new ArrayList<>(headerNames.size());
526
527 for (String headerName : headerNames) {
528 _normalizedHeaderNames.add(
529 FriendlyURLNormalizerUtil.normalize(headerName));
530 }
531 }
532
533 private void _calculateCur() {
534 if (_total == 0) {
535 _cur = DEFAULT_CUR;
536
537 return;
538 }
539
540 if (isRecalculateCur()) {
541 if ((_total % _delta) == 0) {
542 _cur = (_total / _delta);
543 }
544 else {
545 _cur = (_total / _delta) + 1;
546 }
547 }
548 }
549
550 private void _calculateStartAndEnd() {
551 int[] startAndEnd = SearchPaginationUtil.calculateStartAndEnd(
552 _cur, _delta);
553
554 _start = startAndEnd[0];
555 _end = startAndEnd[1];
556
557 _resultEnd = _end;
558
559 if (_resultEnd > _total) {
560 _resultEnd = _total;
561 }
562 }
563
564 private void _setParameter(String name) {
565 String value = _portletRequest.getParameter(name);
566
567 if (value != null) {
568 _iteratorURL.setParameter(name, value);
569 }
570 }
571
572 private String _className;
573 private String _cssClass = StringPool.BLANK;
574 private int _cur;
575 private final String _curParam;
576 private int _delta = DEFAULT_DELTA;
577 private boolean _deltaConfigurable = DEFAULT_DELTA_CONFIGURABLE;
578 private String _deltaParam = DEFAULT_DELTA_PARAM;
579 private final DisplayTerms _displayTerms;
580 private String _emptyResultsMessage;
581 private int _end;
582 private List<String> _headerNames;
583 private boolean _hover = true;
584 private String _id;
585 private PortletURL _iteratorURL;
586
587
590 @Deprecated
591 private int _maxPages = DEFAULT_MAX_PAGES;
592
593 private List<String> _normalizedHeaderNames;
594 private Map<String, String> _orderableHeaders;
595 private String _orderByCol;
596 private String _orderByColParam = DEFAULT_ORDER_BY_COL_PARAM;
597 private OrderByComparator<R> _orderByComparator;
598 private String _orderByJS;
599 private String _orderByType;
600 private String _orderByTypeParam = DEFAULT_ORDER_BY_TYPE_PARAM;
601 private final PortletRequest _portletRequest;
602 private int _resultEnd;
603 private final List<ResultRow> _resultRows = new ArrayList<>();
604 private List<R> _results = new ArrayList<>();
605 private RowChecker _rowChecker;
606 private RowMover _rowMover;
607 private boolean _search;
608 private final DisplayTerms _searchTerms;
609 private int _start;
610 private int _total;
611 private String _totalVar;
612 private boolean _uniqueId;
613
614 }