1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.mindtree.techworks.insight.pagination;
25
26 import java.io.File;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30
31 import com.mindtree.techworks.insight.InsightConstants;
32 import com.mindtree.techworks.insight.model.ILogEventModelMutator;
33 import com.mindtree.techworks.insight.model.IMutatorListener;
34
35
36
37
38
39
40
41
42
43
44
45 public class PageSet implements IMutatorListener{
46
47
48
49
50 private PageImpl pageImpl;
51
52
53
54
55 private PageImpl currentPage;
56
57
58
59
60 private long pageCnt;
61
62
63
64
65
66 private String namespace;
67
68
69
70
71 private long currentPageCnt;
72
73
74
75
76 private int mutatorType;
77
78
79
80
81 public PageSet(){
82 this.namespace = generateUniqueNS();
83 currentPageCnt = 1;
84 pageCnt = 0;
85 createNewPage();
86 this.currentPage = pageImpl;
87 }
88
89
90
91
92
93 private PageImpl createNewPage() {
94 pageCnt++;
95 this.pageImpl = PageImplFactory.getDefaultImpl();
96 pageImpl.init(this);
97
98 if (this.mutatorType == ILogEventModelMutator.TAILING_MUTATOR) {
99 getLast();
100 }
101 return pageImpl;
102 }
103
104
105
106
107 protected void thresholdReached(){
108 storePage();
109 if(!String.valueOf(getCurrentPageCnt()).equals(pageImpl.getPageNamespace())){
110 pageImpl.clear();
111 }
112 createNewPage();
113 }
114
115
116
117
118 private void storePage(){
119 pageImpl.store();
120 }
121
122
123
124
125
126 private void loadPage(long pageNumber){
127 currentPage.setPageNamespace(String.valueOf(pageNumber));
128 currentPage.load();
129 }
130
131
132
133
134
135
136
137 private String generateUniqueNS() {
138 return String.valueOf(this.hashCode());
139 }
140
141
142
143
144
145 protected long getPageCnt() {
146 return this.pageCnt;
147 }
148
149
150
151
152
153
154 public IPage getCurrentPage() {
155 return this.currentPage;
156 }
157
158
159
160
161
162
163
164 public IPage navigate(String direction) {
165 if(direction.equals(InsightConstants.getLiteral("FIRST_PAGE"))){
166 getFirst();
167 }else if(direction.equals(InsightConstants.getLiteral("PREV_PAGE"))){
168 getPrevious();
169 }else if(direction.equals(InsightConstants.getLiteral("NEXT_PAGE"))){
170 getNext();
171 }else if(direction.equals(InsightConstants.getLiteral("LAST_PAGE"))){
172 getLast();
173 }
174 loadPage(currentPageCnt);
175 return currentPage;
176 }
177
178
179
180
181
182 private void getNext() {
183 if(currentPageCnt < pageCnt){
184 currentPageCnt = currentPageCnt + 1;
185 }
186 }
187
188
189
190
191
192 private void getPrevious() {
193 if(currentPageCnt !=1){
194 currentPageCnt = currentPageCnt - 1;
195 }
196 }
197
198
199
200
201
202 private void getFirst() {
203 currentPageCnt = 1;
204 }
205
206
207
208
209
210 private void getLast() {
211 currentPageCnt = pageCnt;
212 }
213
214
215
216 protected String getNamespace() {
217 return namespace;
218 }
219
220
221
222 protected void setNamespace(String namespace) {
223 this.namespace = namespace;
224 }
225
226
227
228
229 public void loadPageByNumber (long pageNumber) {
230 if (this.currentPageCnt == pageNumber) {
231 return;
232 }
233 this.currentPageCnt = pageNumber;
234 loadPage(pageNumber);
235 }
236
237
238
239
240
241 public void clear(){
242 currentPageCnt = 0;
243 pageCnt = 0;
244 pageImpl = null;
245 currentPage = null;
246 String dirToDel = System.getProperty("java.io.tmpdir") + InsightConstants.ROOT_CACHE_DIR + InsightConstants.FILE_SEPARATOR + getNamespace();
247 File file =new File(dirToDel);
248 cleanDirectory(file);
249 }
250
251
252
253
254
255
256 public static void clearAll(){
257 File rootDir = new File(System.getProperty("java.io.tmpdir") + InsightConstants.ROOT_CACHE_DIR);
258 cleanDirectory(rootDir);
259 }
260
261
262
263
264
265 private static void cleanDirectory(File fileDir){
266 List fileNames = new ArrayList();
267 if(fileDir.exists()){
268 String path = fileDir.getAbsolutePath() + InsightConstants.FILE_SEPARATOR;
269 String fileList[] = fileDir.list();
270 for(int i=0; i < fileList.length; i++){
271 File file = new File(path + fileList[i]);
272 if(file.isDirectory()){
273 fileNames.add(file);
274 }else{
275 if(file.getName().indexOf(InsightConstants.FILE_EXT) != -1){
276 file.delete();
277 }
278 }
279 }
280 for(int i=0; i < fileNames.size(); i++){
281 File dir = (File)fileNames.get(i);
282 cleanDirectory(dir);
283 }
284 fileDir.delete();
285 }
286 }
287
288
289
290
291 public IPage getLastPage() {
292 return this.pageImpl;
293 }
294
295
296
297
298
299 public long getCurrentPageCnt(){
300 return this.currentPageCnt;
301 }
302
303
304
305
306
307 public long getTotalPageCnt(){
308 return this.pageCnt;
309 }
310
311
312
313
314
315
316 public void startMutating (int mutatorType) {
317 this.mutatorType = mutatorType;
318 }
319
320
321
322
323
324
325 public void endMutating (int infoFlag) {
326 storePage();
327 }
328
329
330
331
332
333 public int getMutatorType() {
334 return mutatorType;
335 }
336
337
338
339
340
341 public void setMutatorType(int mutatorType) {
342 this.mutatorType = mutatorType;
343 }
344
345
346
347
348
349 public PageSetIterator getPageSetIterator(){
350 return new PageSetIterator();
351 }
352
353
354
355
356
357
358
359
360
361
362
363 private class PageSetIterator implements Iterator{
364
365
366
367
368 private long iteratorPageCnt;
369
370
371
372
373 public PageSetIterator() {
374 iteratorPageCnt = 0 ;
375 }
376
377
378
379
380
381 public void remove() {
382 throw new NoSuchMethodError();
383 }
384
385
386
387
388
389
390 public boolean hasNext() {
391 if(iteratorPageCnt < pageCnt){
392 iteratorPageCnt = iteratorPageCnt + 1;
393 return true;
394 }
395 loadPage(currentPageCnt);
396 return false;
397 }
398
399
400
401
402
403
404 public Object next() {
405 loadPage(iteratorPageCnt);
406 return currentPage;
407 }
408
409
410
411 public long getIteratorPageCnt () {
412
413 return iteratorPageCnt;
414 }
415 }
416 }