package org.bench4q.master.domain.repository;

import org.bench4q.master.domain.entity.User;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.exception.ExceptionUtils.EntityUniqueAlReadyExistException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

@Component
public class UserRepository extends AbstractRepositoty {

	public boolean attatch(User user) {
		Session session = this.getSessionHelper().openSession();
		Transaction transaction = session.beginTransaction();
		User userAlreadyExist = null;
		try {
			userAlreadyExist = (User) session.createCriteria(User.class)
					.add(Restrictions.eq("userName", user.getUserName()))
					.uniqueResult();
			if (userAlreadyExist != null) {
				return false;
			}
			session.merge(user);
			transaction.commit();
			return true;
		} catch (Exception e) {
			transaction.rollback();
			return false;
		} finally {
			releaseSession(session);
		}
	}

	@Override
	protected void guardOtherUniqueConditionForEntity(
			String uniquePropertyName, String value)
			throws EntityUniqueAlReadyExistException {
		if (getUser(value) != null) {
			throw new EntityUniqueAlReadyExistException("User with the name "
					+ value + "already exists");
		}
	}

	public boolean detach(String userName) {
		Session session = this.getSessionHelper().openSession();
		Transaction transaction = session.beginTransaction();
		try {
			User userAlreadyExist = (User) session.createCriteria(User.class)
					.add(Restrictions.eq("userName", userName)).uniqueResult();
			if (userAlreadyExist == null) {
				return false;
			}
			session.delete(userAlreadyExist);
			transaction.commit();
			return true;
		} catch (Exception e) {
			transaction.rollback();
			return false;
		} finally {
			releaseSession(session);
		}
	}

	public boolean updateEntity(User user) {
		Session session = this.getSessionHelper().openSession();
		Transaction transaction = session.beginTransaction();
		try {
			session.update(user);
			transaction.commit();
			return true;
		} catch (Exception e) {
			transaction.rollback();
			logger.error(ExceptionLog.getStackTrace(e));
			return false;
		} finally {
			releaseSession(session);
		}
	}

	public boolean isExist(User user) {
		Session session = this.getSessionHelper().openSession();
		User userAlreadyExist = (User) session.createCriteria(User.class)
				.add(Restrictions.eq("userName", user.getUserName()))
				.add(Restrictions.eq("password", user.getPassword()))
				.uniqueResult();
		releaseSession(session);
		return userAlreadyExist != null;
	}

	public User getEntity(int id) {
		User result = null;
		Session session = this.getSessionHelper().openSession();
		result = (User) session.get(User.class, id);
		releaseSession(session);
		return result;
	}

	public User getUser(String userName) {
		User result = null;
		Session session = this.getSessionHelper().openSession();
		result = doGetUser(userName, session);
		releaseSession(session);
		return result;
	}

	private User doGetUser(String userName, Session session) {
		return (User) session.createCriteria(User.class)
				.add(Restrictions.eq("userName", userName)).uniqueResult();
	}

	// public Set<Script> loadScripts(String userName) {
	// Session session = this.getSessionHelper().openSession();
	// User user = doGetUser(userName, session);
	// Set<Script> result = user.getScripts();
	// return result;
	// }
}
