package cz.vavru.test.dwr.dao.ibatis;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cz.vavru.test.dwr.dao.UserDao;
import cz.vavru.test.dwr.domain.User;
/**
 * 
 * @author vlastimil@vavru.cz (http://vavru.cz)
 *
 */
public class UserDaoIbatisImpl extends SqlMapClientDaoSupport implements UserDao {

	@SuppressWarnings("unchecked")
	public List<User> getOnlineUsers() throws DataAccessException {
		return (List<User>)getSqlMapClientTemplate().queryForList("getOnlineUsers", null);
	}

	public User getUserByNick(String nick) throws DataAccessException {
		return (User) getSqlMapClientTemplate().queryForObject("getUserByNick", nick);
	}

	public User getUserById(int userId) throws DataAccessException {
		return (User) getSqlMapClientTemplate().queryForObject("getUserById", userId);
	}

	public void insertUser(User user) throws DataAccessException {
		int userId = (Integer)getSqlMapClientTemplate().insert("insertUser", user);
		user.setUserId(userId);
	}

	public void updateUserOnlineStatus(User user) throws DataAccessException {
		getSqlMapClientTemplate().update("updateUserIsOnline", user, 1);
	}
}
