1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.wcm.publication;
18
19 import org.exoplatform.commons.exception.ExoMessageException;
20 import org.exoplatform.commons.utils.PageList;
21 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26
27
28
29
30
31
32 @SuppressWarnings({ "deprecation", "unchecked" })
33 public class PaginatedResultIterator extends PageList {
34
35
36 protected Result result;
37
38
39
40
41
42
43 public PaginatedResultIterator(int pageSize) {
44 super(pageSize);
45 }
46
47 public PaginatedResultIterator(Result result, int pageSize) {
48 super(pageSize);
49 this.result = result;
50 this.setAvailablePage((int)result.getNumTotal());
51 this.currentListPage_ = null;
52 }
53
54
55
56
57 protected void populateCurrentPage(int page) throws Exception {
58 if(page == currentPage_) {
59 if(currentListPage_ != null)
60 return;
61 }
62 currentListPage_ = new ArrayList();
63
64 WCMComposer composer = WCMCoreUtils.getService(WCMComposer.class);
65 result.getFiltersDescriber().put(WCMComposer.FILTER_LIMIT, ""+this.getPageSize());
66 result.getFiltersDescriber().put(WCMComposer.FILTER_OFFSET, ""+(this.getPageSize()*(page-1)));
67 result.getFiltersDescriber().put(WCMComposer.FILTER_TOTAL, ""+this.result.getNumTotal());
68 result = composer.getPaginatedContents(result.getNodeLocationDescriber(),
69 result.getFiltersDescriber(),
70 WCMCoreUtils.getUserSessionProvider());
71
72 currentListPage_ = result.getNodes();
73
74 currentPage_ = page;
75 }
76
77
78
79
80
81
82 public int getTotalPages() { return getAvailablePage(); }
83
84
85
86
87
88
89 public int getNodesPerPage() { return getPageSize(); }
90
91
92
93
94
95
96 public long getTotalNodes() {
97 return result.getNumTotal();
98 }
99
100
101
102
103
104
105
106
107 public List getCurrentPageData() throws Exception {
108 return currentPage();
109 }
110
111
112
113
114 public List getPage(int page) throws Exception {
115 if (page < 1 || page > availablePage_) {
116 Object[] args = { Integer.toString(page), Integer.toString(availablePage_) };
117 throw new ExoMessageException("PageList.page-out-of-range", args);
118 }
119 populateCurrentPage(page);
120 while (currentListPage_ != null && currentListPage_.size() == 0 && page > 1) {
121 page--;
122 result.setNumTotal(page * this.getPageSize());
123 populateCurrentPage(page);
124 availablePage_ = page;
125 }
126
127 return currentListPage_;
128
129 }
130
131
132
133
134
135
136
137
138 public void changePage(int page) throws Exception {
139 populateCurrentPage(page);
140 }
141
142
143
144
145 public List getAll() throws Exception {
146 throw new UnsupportedOperationException();
147 }
148 }