package com.royalrangers.configuration;

import com.royalrangers.model.Authority;
import com.royalrangers.model.AuthorityName;
import com.royalrangers.model.User;
import com.royalrangers.security.repository.AuthorityRepository;
import com.royalrangers.security.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.stream.IntStream;

@Component
public class Bootstrap {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AuthorityRepository authorityRepository;

    @PostConstruct
    public void init() {
        initUsers();
    }

    private void initUsers() {
        List<User> users = new ArrayList<>();
        IntStream.range(1, 4).forEach(element -> {
            PasswordEncoder encoder = new BCryptPasswordEncoder();
            User user = new User();
            user.setUsername("username" + element);
            user.setEmail("user" + element + "@mail.test");
            user.setPassword(encoder.encode("password" + element));
            user.setFirstName("first" + element);
            user.setLastName("last" + element);
            user.setEnabled(true);
            user.setLastPasswordResetDate(new Date());
            Authority authority = new Authority();
            authority.setUsers(new HashSet<User>() {{ add(user); }});
            switch (element) {
                case 1:
                    authority.setName(AuthorityName.ROLE_USER);
                    break;
                case 2:
                    authority.setName(AuthorityName.ROLE_ADMIN);
                    break;
                case 3:
                    authority.setName(AuthorityName.ROLE_SUPER_ADMIN);
                    break;
            }
            authorityRepository.save(authority);
            user.setAuthorities(new HashSet<Authority>(){{ add(authority); }});
            users.add(user);
        });
        userRepository.save(users);
    }

}

@@ -68,98 +68,9 @@
 del.
-Authority;%0Aimport com.royalrangers.model.AuthorityName;%0Aimport com.royalrangers.model.User
+*
 ;%0Aim
@@ -1566,19 +1566,47 @@
 ) %7B%7B
- add(user);
+%0A                add(user);%0A           
  %7D%7D)
@@ -1750,223 +1750,1319 @@
     
-break;%0A                case 2:%0A                    authority.setName(AuthorityName.ROLE_ADMIN);%0A                    break;%0A                case 3:%0A                    authority.setName(AuthorityName.ROLE_SUPER_ADMIN
+user.setCountry(new Country(%22Ukraine%22));%0A                    user.setCity(new City(user.getCountry(), %22Cherkasy%22));%0A                    user.setGroup(new Group(user.getCity(), %22group1%22));%0A                    user.setPlatoon(new Platoon(user.getGroup(), %22platoon1%22));%0A                    user.setSection(new Section(user.getPlatoon(), %22section1%22));%0A                    break;%0A                case 2:%0A                    authority.setName(AuthorityName.ROLE_ADMIN);%0A                    user.setCountry(new Country(%22USA%22));%0A                    user.setCity(new City(user.getCountry(), %22Miami%22));%0A                    user.setGroup(new Group(user.getCity(), %22group2%22));%0A                    user.setPlatoon(new Platoon(user.getGroup(), %22platoon2%22));%0A                    user.setSection(new Section(user.getPlatoon(), %22section2%22));%0A                    break;%0A                case 3:%0A                    authority.setName(AuthorityName.ROLE_SUPER_ADMIN);%0A                    user.setCountry(new Country(%22Canada%22));%0A                    user.setCity(new City(user.getCountry(), %22Montreal%22));%0A                    user.setGroup(new Group(user.getCity(), %22group3%22));%0A                    user.setPlatoon(new Platoon(user.getGroup(), %22platoon3%22));%0A                    user.setSection(new Section(user.getPlatoon(), %22section3%22)
 );%0A 
@@ -3206,18 +3206,35 @@
 ority%3E()
+ 
 %7B%7B
+%0A               
  add(aut
@@ -3241,16 +3241,28 @@
 hority);
+%0A           
  %7D%7D);%0A  
@@ -3342,11 +3342,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Populate initial data for country, city, group, platoon and section