package org.ngrinder.infra.init;

import javax.annotation.PostConstruct;

import org.ngrinder.model.Role;
import org.ngrinder.model.User;
import org.ngrinder.user.repository.UserRepository;
import org.ngrinder.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DBInit {
	@Autowired
	private UserRepository userRepository;

	@Autowired
	private UserService userService;

	@PostConstruct
	@Transactional
	public void init() {
		createDefaultUserIfNecessary();
	}

	private void createDefaultUserIfNecessary() {
		// If there is no users.. make admin and user and U, S, A roles.
		if (userRepository.count() == 0) {
			User user = new User();
			user.setUserId("admin");
			user.setPassword("admin");
			user.setRole(Role.ADMIN);
			userService.saveUser(user);

			User user2 = new User();
			user2.setUserId("user");
			user2.setPassword("user");
			user2.setRole(Role.USER);
			userService.saveUser(user2);
		}
	}
}

@@ -187,54 +187,8 @@
 ry;%0A
-import org.ngrinder.user.service.UserService;%0A
 impo
@@ -246,16 +246,16 @@
 owired;%0A
+
 import o
@@ -447,55 +447,8 @@
 y;%0A%0A
-%09@Autowired%0A%09private UserService userService;%0A%0A
 %09@Po
@@ -807,32 +807,31 @@
 %0A%09%09%09user
-Service
+Repository
 .save
-User
 (user);%0A
@@ -957,24 +957,23 @@
 user
-Service
+Repository
 .save
-User
 (use

Add default user if there is no user in db. If we use userService to save, The ModelAspect will work to get current user from context and set it as created user. But at this time, there is no user in context because not logging in.