/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in 
 * compliance with the License.  You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */

package edu.stanford.smi.protege.server.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.table.AbstractTableModel;

import edu.stanford.smi.protege.server.RemoteSession;
import edu.stanford.smi.protege.server.Session;
import edu.stanford.smi.protege.server.framestore.background.FrameCalculatorStats;

public class UserInfoTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 7730063885739903457L;

	public enum Column {
		sessionId("Session ID"), user("User"), ipAddr("IP Adress"), transaction("In Transaction?"),
		backlog("Server Backlog");

		private String name;

		private Column(String name) {
			this.name = name;
		}

		public String getColumnName() {
			return name;
		}
	};

	Map<RemoteSession, Boolean> userInfo = new HashMap<RemoteSession, Boolean>();
	FrameCalculatorStats stats = null;
	List<RemoteSession>  sessions = new ArrayList<RemoteSession>();

	public UserInfoTableModel() {
	}

	public void setUserInfo(Map<RemoteSession, Boolean> userInfo, FrameCalculatorStats  stats) {
		this.userInfo = userInfo;
		this.stats    = stats;
		sessions  = new ArrayList<RemoteSession>();
		if (userInfo != null && stats != null) {
			for (RemoteSession session :  userInfo.keySet()) {
				sessions.add(session);
			}
		}
		fireTableDataChanged();
	}

	public int getRowCount() {
		return userInfo == null ? 0 : userInfo.size();
	}

	public int getColumnCount() {
		return Column.values().length;
	}

	public Object getValueAt(int rowIndex, int colIndex) {
		Column col = Column.values()[colIndex];
		switch (col) {
		case sessionId:
			RemoteSession remoteSession = sessions.get(rowIndex);
			if (remoteSession instanceof Session) {
				return ((Session)remoteSession).getId();
			}
			return "(none)";
		case user:
			return sessions.get(rowIndex).getUserName();
		case ipAddr:
			return sessions.get(rowIndex).getUserIpAddress();
		case transaction:
			return userInfo.get(sessions.get(rowIndex));
		case backlog:
			if (stats == null) {
				return new Integer(-1);
			} else {
				RemoteSession session = sessions.get(rowIndex);
				Integer result = stats.getPreCacheBacklog().get(session);
				if (result == null) {
					return 0;
				} else {
					return new Integer(result);
				}
			}
		default:
			throw new UnsupportedOperationException("Programmer Error");
		}
	}

	public RemoteSession getSession(int row) {
		return sessions.get(row);
	}

	@Override
	public String getColumnName(int colIndex) {
		Column col = Column.values()[colIndex];
		return col.getColumnName();
	}

	@Override
	public Class getColumnClass(int colIndex) {
		Column col = Column.values()[colIndex];
		switch (col) {
		case sessionId:
		case user:
		case ipAddr:
			return String.class;
		case transaction:
			return Boolean.class;
		case backlog:
			return Integer.class;
		default:
			throw new UnsupportedOperationException("Programmer Error");
		}
	}

	public RemoteSession getRemoteSession(int row) {
		return sessions.get(row);
	}

}
