package com.cyb.tms.dao.impl;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Repository;

import com.cyb.tms.dao.TmsUsersDAO;
import com.cyb.tms.entity.TmsUsers;
import com.cyb.tms.util.HibernateUtil;

@Repository
public class TmsUsersDAOImpl implements TmsUsersDAO {

private static final String USER_NAME = "userName";
	
	@Autowired
    private HibernateUtil hibernateUtil;
	
	@Autowired
	private BCryptPasswordEncoder passwordEncoder;

	@Override
	public TmsUsers findByUsername(String username) {
		return hibernateUtil.findByUsername(USER_NAME, username, TmsUsers.class);
	}

	@Override
	public long createUser(TmsUsers user) {
		user.setPassword(passwordEncoder.encode(user.getPassword()));
		return (Long) hibernateUtil.create(user);
	}

	@Override
	public TmsUsers updateUser(TmsUsers tmsUser) {
		return hibernateUtil.update(tmsUser);
	}

	@Override
	public void deleteUser(long id) {
		TmsUsers user = new TmsUsers();
		user.setId(id);
        hibernateUtil.delete(user);
	}

	@Override
	public List<TmsUsers> getAllUsers() {
		return hibernateUtil.fetchAll(TmsUsers.class);
	}

	@Override
	public TmsUsers getUser(long id) {
		return hibernateUtil.fetchById(id, TmsUsers.class);
	}
	

	@SuppressWarnings("unchecked")
	@Override
	public List<TmsUsers> getUsersByStatus(long projectId) {
		List<TmsUsers> users = hibernateUtil.getCurrentSession().createCriteria(TmsUsers.class, "tu")
				              .createAlias("tmsProject", "proj")
			                  .add(Restrictions.eq("proj.pid", projectId))
			                  .add(Restrictions.ne("userRole", "MANAGER"))
			                  .add(Restrictions.eq("tu.isActive", "ACTIVE")).list();
		return users;
	}


}

@@ -1548,16 +1548,58 @@
 tId) %7B%0D%0A
+%09%09Object%5B%5D roles = %7B%22MANAGER%22, %22ADMIN%22%7D;%0D%0A
 %09%09List%3CT
@@ -1848,17 +1848,34 @@
 ctions.n
-e
+ot(Restrictions.in
 (%22userRo
@@ -1879,25 +1879,22 @@
 rRole%22, 
-%22MANAGER%22
+roles)
 ))%0D%0A%09%09%09 

Update userlist query