package org.ros.chatto.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Service;
import org.ros.chatto.model.ChatUser;

import org.ros.chatto.repository.UserRepositoryCustom;

@Service
class UserRepositoryCustomImpl implements UserRepositoryCustom{
	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public List<String> getAllUserNames(String userName) {
		List<String> userNamesList = null;
//		Session session = null;
		try {
			CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
			CriteriaQuery<String> criteriaQuery = criteriaBuilder.createQuery(String.class);
			Root<ChatUser> root = criteriaQuery.from(ChatUser.class);
			criteriaQuery.select(root.get("userName"));
			criteriaQuery.where(criteriaBuilder.notEqual(root.get("userName"), userName));
			
			userNamesList = entityManager.createQuery(criteriaQuery).getResultList();
//			for(String un: userNamesList)
//			{
//				System.out.println(un);
//			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return userNamesList;
	}
	
}
