package edu.ucsf.lava.core.session.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import edu.ucsf.lava.core.controller.BaseListComponentHandler;
import edu.ucsf.lava.core.controller.ScrollablePagedListHolder;
import edu.ucsf.lava.core.dao.LavaDaoFilter;
import edu.ucsf.lava.core.session.SessionManager;
import edu.ucsf.lava.core.session.model.LavaServerInstance;
import edu.ucsf.lava.core.session.model.LavaSession;


public class CurrentLavaSessionsHandler extends BaseLavaSessionsHandler {
	public CurrentLavaSessionsHandler() {
	super();
	this.setHandledList("currentLavaSessions","lavaSession");
	CurrentLavaSessionsSourceProvider sourceProvider = new CurrentLavaSessionsSourceProvider(this);
	sourceProvider.setSessionManager(this.sessionManager);
	this.setSourceProvider(sourceProvider);
}

public static class CurrentLavaSessionsSourceProvider extends BaseListComponentHandler.BaseListSourceProvider {
	protected SessionManager sessionManager;
	
	public CurrentLavaSessionsSourceProvider(BaseListComponentHandler listHandler) {
		super(listHandler);
		this.listHandler.setPageSize(1000);
	}
	
	public SessionManager getSessionManager() {
		return sessionManager;
	}

	public void setSessionManager(SessionManager sessionManager) {
		this.sessionManager = sessionManager;
	}

	public List loadElements(Locale locale, Object filter) {
			//LavaDaoFilter daoFilter = (LavaDaoFilter)filter;
			//LavaServerInstance serverInstance = ((CurrentLavaSessionsHandler)listHandler).getServerInstance();
			
			//daoFilter.convertParamsToDaoParams();
			//return 	LavaSession.MANAGER.getCurrent(serverInstance,daoFilter);
		List results = new ArrayList<LavaSession>();
		results.addAll(sessionManager.getLavaSessions().values());
		
		//sort is descending order by time
		Collections.sort(results, 
						new Comparator<LavaSession>() {
							public int compare(LavaSession session1, LavaSession session2) {
								return -1 * session1.getAccessTimestamp().compareTo(session2.getAccessTimestamp());
							}
						});
		return results;
	}
	
	public List loadList(Locale locale, Object filter) {
		LavaDaoFilter daoFilter = (LavaDaoFilter)filter;
		//LavaServerInstance serverInstance = ((CurrentLavaSessionsHandler)listHandler).getServerInstance();
		//daoFilter.convertParamsToDaoParams();
		
		List results = new ArrayList<LavaSession>();
		results.addAll(sessionManager.getLavaSessions().values());
		//sort in descending order by time
		Collections.sort(results, 
						new Comparator<LavaSession>() {
							public int compare(LavaSession session1, LavaSession session2) {
								return -1 * session1.getAccessTimestamp().compareTo(session2.getAccessTimestamp());
							}
						});
		
		return ScrollablePagedListHolder.createSourceList(results,daoFilter);
	}
	
	
}







}

@@ -182,16 +182,228 @@
 Locale;%0D
+%0Aimport java.util.Map;%0D%0A%0D%0Aimport javax.servlet.http.HttpServletRequest;%0D%0A%0D%0Aimport org.springframework.webflow.context.servlet.ServletExternalContext;%0D%0Aimport org.springframework.webflow.execution.RequestContext;%0D
 %0A%0D%0Aimpor
@@ -933,59 +933,31 @@
 ;%0D%0A%09
-CurrentLavaSessionsSourceProvider sourceProvider = 
+this.setSourceProvider(
 new 
@@ -999,27 +999,105 @@
 his)
+)
 ;%0D%0A
-%09sourceProvider.
+%7D%0D%0A%09%0D%0Apublic SessionManager getSessionManager() %7B%0D%0A%09return sessionManager;%0D%0A%7D%0D%0A%0D%0Apublic void 
 setS
@@ -1110,21 +1110,31 @@
 Manager(
-this.
+SessionManager 
 sessionM
@@ -1140,17 +1140,18 @@
 Manager)
-;
+ %7B
 %0D%0A%09this.
@@ -1156,47 +1156,45 @@
 s.se
-tSourceProvider(sourceProvid
+ssionManager = sessionManag
 er
-)
 ;%0D%0A%7D%0D%0A%0D%0A
 publ
@@ -1185,24 +1185,26 @@
 ager;%0D%0A%7D%0D%0A%0D%0A
+%0D%0A
 public stati
@@ -1308,54 +1308,8 @@
  %7B%0D%0A
-%09protected SessionManager sessionManager;%0D%0A%09%0D%0A
 %09pub
@@ -1460,198 +1460,8 @@
 %0A%09%0D%0A
-%09public SessionManager getSessionManager() %7B%0D%0A%09%09return sessionManager;%0D%0A%09%7D%0D%0A%0D%0A%09public void setSessionManager(SessionManager sessionManager) %7B%0D%0A%09%09this.sessionManager = sessionManager;%0D%0A%09%7D%0D%0A%0D%0A
 %09pub
@@ -1853,33 +1853,78 @@
 %09results.addAll(
-s
+((CurrentLavaSessionsHandler)listHandler).getS
 essionManager.ge
@@ -1912,32 +1912,34 @@
 etSessionManager
+()
 .getLavaSessions
@@ -2600,17 +2600,62 @@
 .addAll(
-s
+((CurrentLavaSessionsHandler)listHandler).getS
 essionMa
@@ -2659,16 +2659,18 @@
 nManager
+()
 .getLava

fix to last change