/*
 * $Id: ActiveUserListForm.java,v 1.7 2005/09/15 13:39:07 marbobo Exp $
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ActiveUserListForm.java,v $
 * Revision 1.7  2005/09/15 13:39:07  marbobo
 * + Sprachabhängige Sortierung implementiert
 *
 * Revision 1.6  2004/09/16 13:07:30  marbobo
 * + Eventhandling umgestellt
 *
 * Revision 1.5  2004/07/20 08:09:56  jonyp
 * added copyright info + cvs log
 *
 *
 *
 */
 
package vkc.administration.form;

import java.text.CollationKey;
import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionMapping;

import vkc.util.VKCStringUtils;
import vkc.view.WebShare;
import vkc.view.bean.UserTrackingInfo;
import vkc.view.form.BasicActionForm;

public class ActiveUserListForm extends BasicActionForm {
	private boolean sessionIdsVisible = false;

	private UserTrackingInfo[] userInfos = null;

	public void reset(ActionMapping mapping, HttpServletRequest request) {
	    super.reset(mapping, request);
	    
	    sessionIdsVisible = false;
	    
	    userInfos = null;
	}
	
	public UserTrackingInfo[] getUserTrackingInfos() {
		if (userInfos == null) {
			userInfos = WebShare.getActiveUsers();
			final Collator collator = Collator.getInstance(getUser().getLanguage().getLocale());

			Comparator c = new Comparator() {
				public int compare(Object o1, Object o2) {
					UserTrackingInfo u1 = (UserTrackingInfo)o1;
					UserTrackingInfo u2 = (UserTrackingInfo)o2;
					
                	CollationKey key1 = collator.getCollationKey(VKCStringUtils.decodeXmlEntities(u1.getRealName()));
                	CollationKey key2 = collator.getCollationKey(VKCStringUtils.decodeXmlEntities(u2.getRealName()));
                	
                	return key1.compareTo(key2);
				}
			};
			
			Arrays.sort(userInfos, c);
		}
		
		return userInfos;
	}
	
	public int getNumActiveUsers() {
		return getUserTrackingInfos().length;
	}

	public boolean isSessionIdsVisible() {
		return sessionIdsVisible;
	}

	public void setSessionIdsVisible(boolean b) {
		sessionIdsVisible = b;
	}
}