package gov.healthit.chpl.auth.user.dao.impl;


import gov.healthit.chpl.auth.user.User;
import gov.healthit.chpl.auth.user.UserImpl;
import gov.healthit.chpl.auth.user.UserRetrievalException;
import gov.healthit.chpl.auth.user.dao.UserDAO;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


@Repository(value="userDAO")
public class UserDAOImpl extends BaseDAOImpl implements UserDAO {
	
	@Transactional
	@Override
	public void create(UserImpl user) {
		
		entityManager.persist(user);
		
	}
	
	@Transactional
	@Override
	public void update(UserImpl user) {
		
		entityManager.merge(user);
		
	}
	
	@Transactional
	@Override
	public void deactivate(String uname) {
		Query query = entityManager.createQuery("UPDATE UserImpl SET deleted = true WHERE c.user_id = :uname");
		query.setParameter("uname", uname);
		query.executeUpdate();
	}
	
	@Transactional
	@Override
	public void deactivate(Long userId){
		Query query = entityManager.createQuery("UPDATE UserImpl SET deleted = true WHERE c.user_id = :userid");
		query.setParameter("userid", userId);
		query.executeUpdate();
	}
	
	@Override
	public List<UserImpl> findAll() {
		//TODO: Where not deleted
		List<UserImpl> result = entityManager.createQuery( "from UserImpl ", UserImpl.class ).getResultList();
		
		return result;
	}

	@Override
	public UserImpl getById(Long userId) throws UserRetrievalException {
		//TODO: Where not deleted
		UserImpl user = null;
		
		Query query = entityManager.createQuery( "from user where user_id = :userid", UserImpl.class );
		query.setParameter("userid", userId);
		List<UserImpl> result = query.getResultList();
		
		if (result.size() > 1){
			throw new UserRetrievalException("Data error. Duplicate user id in database.");
		}
		
		if (result.size() < 0){
			user = result.get(0);
		}
		
		return user;
	}

	@Override
	public UserImpl getByName(String uname) throws UserRetrievalException {
		
		UserImpl user = null;
		
		Query query = entityManager.createQuery( "from UserImpl where user_name = :uname", UserImpl.class );
		query.setParameter("uname", uname);
		List<UserImpl> result = query.getResultList();
		
		if (result.size() > 1){
			throw new UserRetrievalException("Data error. Duplicate user name in database.");
		}
		
		if (result.size() > 0){
			user = result.get(0);
		}
		
		return user;
	}
	
}

@@ -48,50 +48,8 @@
 %0D%0A%0D%0A
-import gov.healthit.chpl.auth.user.User;%0D%0A
 impo
@@ -1285,16 +1285,285 @@
 eleted%0D%0A
+%09%09//List%3CUserImpl%3E result = entityManager.createQuery( %22from UserImpl WHERE deleted is not true %22, UserImpl.class ).getResultList();%0D%0A%09%09//List%3CUserImpl%3E result = entityManager.createQuery( %22from UserImpl WHERE NOT (deleted = true) %22, UserImpl.class ).getResultList();%0D%0A
 %09%09List%3CU
@@ -1821,32 +1821,157 @@
 er = null;%0D%0A%09%09%0D%0A
+%09%09//Query query = entityManager.createQuery( %22from user where user_id = :userid and deleted is not true%22, UserImpl.class );%0D%0A
 %09%09Query query = 
@@ -2436,24 +2436,49 @@
 eption %7B%0D%0A%09%09
+//TODO: Where not deleted
 %0D%0A%09%09UserImpl
@@ -2494,24 +2494,26 @@
 ull;%0D%0A%09%09%0D%0A%09%09
+//
 Query query 
@@ -2580,16 +2580,277 @@
 = :uname
+ and deleted is not true%22, UserImpl.class );%0D%0A%09%09//Query query = entityManager.createQuery( %22from UserImpl where user_name = :uname AND NOT (deleted = true) %22, UserImpl.class );%0D%0A%09%09Query query = entityManager.createQuery( %22from UserImpl where user_name = :uname 
 %22, UserI

Work on DAO for deleted items