package de.terrestris.shogun2.service;

import java.util.UUID;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import org.springframework.stereotype.Service;

import de.terrestris.shogun2.model.User;
import de.terrestris.shogun2.model.token.PasswordResetToken;

/**
 *
 * @author Daniel Koch
 *
 */
@Service("passwordResetService")
public class PasswordResetTokenService extends AbstractCrudService<PasswordResetToken> {

	/**
	 * The Logger
	 */
	private static final Logger LOG =
			Logger.getLogger(PasswordResetTokenService.class);

	/**
	 *
	 * @param user
	 * @return
	 */
	public PasswordResetToken findByUser(User user) {

		SimpleExpression eqEmail = Restrictions.eq("user", user);
		PasswordResetToken passwordResetToken =
				dao.findByUniqueCriteria(eqEmail);

		return passwordResetToken;
	}

	/**
	 *
	 * @return
	 */
	public PasswordResetToken findByIdAndToken(int id, String token) {

		Criterion criteria = Restrictions.and(
				Restrictions.eq("id", id),
				Restrictions.eq("token", token)
		);

		PasswordResetToken passwordResetToken = 
				dao.findByUniqueCriteria(criteria);

		return passwordResetToken;
	}

	/**
	 *
	 * @param user
	 * @return
	 */
	public PasswordResetToken generateResetPasswordToken(User user) {

		PasswordResetToken passwordResetToken;

		String token = UUID.randomUUID().toString();

		// check if the user has an open reset request / not used token
		passwordResetToken = findByUser(user);

		// if so, delete it
		if (passwordResetToken != null) {
			LOG.debug("User has an open request already, delete it first");
			dao.delete(passwordResetToken);
		}

		// and create a blank new one
		passwordResetToken = new PasswordResetToken();
		passwordResetToken.setUser(user);
		passwordResetToken.setToken(token);

		dao.saveOrUpdate(passwordResetToken);

		return passwordResetToken;
	}

}

@@ -436,16 +436,21 @@
 ordReset
+Token
 Service%22
@@ -1175,17 +1175,16 @@
 tToken =
- 
 %0A%09%09%09%09dao
@@ -1279,32 +1279,54 @@
 ser%0A%09 * @return%0A
+%09 * @throws Exception%0A
 %09 */%0A%09public Pas
@@ -1374,24 +1374,41 @@
 n(User user)
+ throws Exception
  %7B%0A%0A%09%09Passwo
@@ -1441,16 +1441,47 @@
 Token;%0A%0A
+%09%09// generate the token itself%0A
 %09%09String
@@ -1640,10 +1640,20 @@
  if 
-so
+it's present
 , de
@@ -1812,16 +1812,23 @@
 %09// and 
+try to 
 create a
@@ -1842,16 +1842,25 @@
 new one%0A
+%09%09try %7B%0A%09
 %09%09passwo
@@ -1898,24 +1898,25 @@
 tToken();%0A%09%09
+%09
 passwordRese
@@ -1937,16 +1937,17 @@
 (user);%0A
+%09
 %09%09passwo
@@ -1979,16 +1979,17 @@
 en);%0A%0A%09%09
+%09
 dao.save
@@ -2015,24 +2015,217 @@
 setToken);%0A%0A
+%09%09%09LOG.debug(%22Successfully created the reset-password token.%22);%0A%0A%09%09%7D catch(Exception e) %7B%0A%09%09%09throw new Exception(%22Could not create the reset-password %22%0A%09%09%09%09%09+ %22token: %22 + e.getMessage());%0A%09%09%7D%0A%0A
 %09%09return pas

Add try/catch