View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
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   * The <code>PageSet</code> class holds the information of a collection of pages and 
37   * has the methods for navigating through the collection  
38   * 
39   * 
40   * 
41   *@author  Antony Pulicken 
42  * @version 1.0, Feb 01, 2005
43   *
44   */
45  public class PageSet implements IMutatorListener{
46  	
47  	/**
48  	 * Holds the current page instance
49  	 */
50  	private PageImpl pageImpl;
51  	
52  	/**
53  	 * Holds the currentPage page instance
54  	 */
55  	private PageImpl currentPage;
56  	
57  	/**
58  	 * The number of pages in  the pageset
59  	 */
60  	private long pageCnt;
61  	
62  
63  	/**
64  	 * Unique Namespace for the pageset
65  	 */
66  	private String namespace;
67  	
68  	/**
69  	 * Current Page Number
70  	 */
71  	private long currentPageCnt;
72  	
73  	/**
74  	 * The type of the ILogEventModelMutator using this PageSet
75  	 */
76  	private int mutatorType;	
77  	
78  	/**
79  	 * Constructor
80  	 */
81  	public PageSet(){	
82  		this.namespace = generateUniqueNS();
83  		currentPageCnt = 1;
84  		pageCnt = 0;
85  		createNewPage();
86  		this.currentPage = pageImpl;
87  	}
88  
89  	/**
90  	 * Creates a new Page 
91  	 * @return
92  	 */
93  	private PageImpl createNewPage() {
94  		pageCnt++;
95  		this.pageImpl = PageImplFactory.getDefaultImpl();		
96  		pageImpl.init(this);
97  		// if mutator type is tailing set the current page as the last page
98  		if (this.mutatorType == ILogEventModelMutator.TAILING_MUTATOR) {
99  			getLast();
100 		}
101 		return pageImpl;
102 	}
103 
104 	/**
105 	 * Call back method to know whether the threshold is reached 
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 	 * Stores the page
117 	 */
118 	private void storePage(){
119 		pageImpl.store();
120 	}
121 
122 	/**
123 	 * Loads the page based on the pagenumber passed
124 	 * @param pageNumber The page number to be loaded
125 	 */
126 	private void loadPage(long pageNumber){
127 		currentPage.setPageNamespace(String.valueOf(pageNumber));	
128 		currentPage.load();
129 	}
130 
131 	
132 	
133 	/**
134 	 * Returns the UniqueNameSpace
135 	 * @return
136 	 */
137 	private String generateUniqueNS() {
138 		return String.valueOf(this.hashCode());
139 	}
140 
141 	/**
142 	 * 
143 	 * @return pageCnt The Page Count
144 	 */
145 	protected  long getPageCnt() {
146 		return this.pageCnt;
147 	}
148 
149 	
150 	/**
151 	 * Returns the current page 
152 	 * @return currentPage 
153 	 */	
154 	public IPage getCurrentPage() {
155 		return this.currentPage;
156 	}
157 
158 	/**
159 	 * Loads the page based on the direction 
160 	 * @param direction 
161 	 * @return currentPage
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 	 * Sets the current page count to the next page
180 	 * 
181 	 */
182 	private void getNext() {
183 		if(currentPageCnt < pageCnt){
184 			currentPageCnt = currentPageCnt + 1;	
185 		}		
186 	}
187 
188 	/**
189 	 * Sets the current page count to the previous page
190 	 * 
191 	 */
192 	private void getPrevious() {
193 		if(currentPageCnt !=1){
194 			currentPageCnt = currentPageCnt - 1;	
195 		}
196 	}
197 
198 	/**
199 	 * Sets the current page count to the first page
200 	 * 
201 	 */
202 	private void getFirst() {
203 		currentPageCnt = 1;
204 	}
205 
206 	/**
207 	 * Sets the current page count to the last page
208 	 * 
209 	 */
210 	private void getLast() {
211 		currentPageCnt = pageCnt;
212 	}
213 	/**
214 	 * @return Returns the namespace.
215 	 */
216 	protected String getNamespace() {
217 		return namespace;
218 	}
219 	/**
220 	 * @param namespace The namespace to set.
221 	 */
222 	protected void setNamespace(String namespace) {
223 		this.namespace = namespace;
224 	}
225 	
226 	/**
227 	 * @param pageNumber
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 	 * Deletes all the files/directories associated with this PageSet
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 	 * Clears all the Directories and Files created under the root cache directory
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 	 * Deletes all the files/directories under the path passed to the method
263 	 * @param fileDir The directory to be deleted
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 	 * @return Returns the lastPage.
290 	 */
291 	public IPage getLastPage() {
292 		return this.pageImpl;
293 	}
294 	
295 	/**
296 	 * Returns the current page number
297 	 * @return currentPageCnt The current pagenumber
298 	 */
299 	public long getCurrentPageCnt(){
300 		return this.currentPageCnt;
301 	}
302 
303 	/**
304 	 * Returns the total number of pages 
305 	 * @return pageCnt Total number of pages
306 	 */
307 	public long getTotalPageCnt(){
308 		return this.pageCnt;
309 	}
310 
311 	/**
312 	 * Call back method using which the controller will be notifed by 
313 	 * any LogEventModelMutators starts mutating the LogEventModel 
314 	 * @see IMutatorListener#startMutating()
315 	 */
316 	public void startMutating (int mutatorType) {
317 		this.mutatorType = mutatorType;
318 	}
319 	
320 	/**
321 	 * Call back method using which the controller will be notifed by 
322 	 * any LogEventModelMutators ends mutating the LogEventModel
323 	 * @see IMutatorListener#endMutating(int)
324 	 */
325 	public void endMutating (int infoFlag) {
326 		storePage();
327 	}
328 	
329 	/**
330 	 * Gets the type of th mutator that is using this PageSet
331 	 * @return Returns the mutatorType.
332 	 */
333 	public int getMutatorType() {
334 		return mutatorType;
335 	}
336 	
337 	/**
338 	 * Sets the mutator type for this PageSet
339 	 * @param mutatorType The mutatorType to set.
340 	 */
341 	public void setMutatorType(int mutatorType) {
342 		this.mutatorType = mutatorType;
343 	}
344 	
345 	/**
346 	 * Returns the current page number
347 	 * @return currentPageCnt The current pagenumber
348 	 */
349 	public  PageSetIterator getPageSetIterator(){
350 		return new PageSetIterator();
351 	}
352 	
353 	/**
354 	 * The <code>PageSetIterator</code> class implements Iterator and has methods for 
355 	 * iterating through the pageset  
356 	 * 
357 	 * @author antonypulicken
358 	 * 
359 	 *@author  Antony Pulicken 
360 	* @version 1.0, Feb 01, 2005
361 	 *
362 	 */
363 	private class PageSetIterator implements Iterator{
364 		
365 		/**
366 		 * Page Count
367 		 */
368 		private long iteratorPageCnt;
369 		
370 		/**
371 		 * Deafault Constructor
372 		 */
373 		public PageSetIterator() {
374 			iteratorPageCnt = 0 ;
375 		}
376 		
377 		/**
378 		 * No implementation
379 		 * @see java.util.Iterator#remove()
380 		 */
381 		public void remove() {
382 			throw new NoSuchMethodError();			
383 		}
384 
385 		/**
386 		 * Returns true if there are more pages 
387 		 * return boolean
388 		 * @see java.util.Iterator#hasNext()
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 		 * Returns the next page
401 		 * return Object 
402 		 * @see java.util.Iterator#next()
403 		 */
404 		public Object next() {
405 			loadPage(iteratorPageCnt);
406 			return currentPage;
407 		}
408 		/**
409 		 * @return Returns the iteratorPageCnt.
410 		 */
411 		public long getIteratorPageCnt () {
412 
413 			return iteratorPageCnt;
414 		}
415 	}
416 }