package de.terrestris.shogun2.service;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import org.springframework.stereotype.Service;

import de.terrestris.shogun2.model.User;

/**
 * Service class for the {@link User} model.
 * 
 * @author Nils Bühner
 * @see AbstractCrudService
 * 
 */
@Service("userService")
public class UserService extends AbstractExtDirectCrudService<User> {

	/**
	 * 
	 * @param accountName
	 * @return
	 */
	public User findByAccountName(String accountName) {

		SimpleExpression eqAccountName = Restrictions.eq("accountName",
				accountName);
		List<User> userList = dao.findByCriteria(eqAccountName);

		if (userList.size() == 1) {
			return userList.get(0);
		}

		return null;
	}

}

@@ -37,32 +37,8 @@
 e;%0A%0A
-import java.util.List;%0A%0A
 impo
@@ -127,16 +127,148 @@
 ession;%0A
+import org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.security.crypto.password.PasswordEncoder;%0A
 import o
@@ -400,17 +400,16 @@
 odel.%0A *
- 
 %0A * @aut
@@ -454,17 +454,16 @@
 rvice%0A *
- 
 %0A */%0A@Se
@@ -559,51 +559,281 @@
 %7B%0A%0A%09
-/**%0A%09 * %0A%09 * @param accountName%0A%09 * @return
+@Autowired%0A%09private PasswordEncoder passwordEncoder;%0A%0A%09/**%0A%09 * Returns the user for the given (unique) account name.%0A%09 * If no user was found, null will be returned.%0A%09 *%0A%09 * @param accountName A unique account name.%0A%09 * @return The unique user for the account name or null.
 %0A%09 *
@@ -978,27 +978,17 @@
 ;%0A%09%09
-List%3C
 User
-%3E
  user
-List
  = d
@@ -996,16 +996,22 @@
 o.findBy
+Unique
 Criteria
@@ -1034,81 +1034,991 @@
 %0A%0A%09%09
-if (userList.size() == 1
+return user;%0A%09%7D%0A%0A%09/**%0A%09 * Persists a new user in the database.%0A%09 *%0A%09 * @param user%0A%09 *            The user to create%0A%09 * @param encryptPassword%0A%09 *            Whether or not the current password of the user object should%0A%09 *            be encrypted or not before the object is persisted in the db%0A%09 *%0A%09 * @return The persisted user object (incl. ID value)%0A%09 */%0A%09public User createNewUser(User user, boolean encryptPassword) %7B%0A%0A%09%09if(user.getId() != null
 ) %7B%0A%09%09%09
-return userList.get(0);%0A%09%09%7D%0A%0A%09%09return null
+// to be sure that we are in the%0A%09%09%09// %22create%22 case, the id must be null%0A%09%09%09return user;%0A%09%09%7D%0A%0A%09%09if(encryptPassword)%7B%0A%09%09%09user.setPassword(passwordEncoder.encode(user.getPassword()));%0A%09%09%7D%0A%0A%09%09dao.saveOrUpdate(user);%0A%0A%09%09return user;%0A%09%7D%0A%0A%09/**%0A%09 * @return the passwordEncoder%0A%09 */%0A%09public PasswordEncoder getPasswordEncoder() %7B%0A%09%09return passwordEncoder;%0A%09%7D%0A%0A%09/**%0A%09 * @param passwordEncoder the passwordEncoder to set%0A%09 */%0A%09public void setPasswordEncoder(PasswordEncoder passwordEncoder) %7B%0A%09%09this.passwordEncoder = passwordEncoder
 ;%0A%09%7D

Refactor and extend the UserService