All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.iintelligas.init.DataLoader Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package com.github.iintelligas.init;


import com.github.iintelligas.persist.dto.Permission;
import com.github.iintelligas.persist.dto.Role;
import com.github.iintelligas.persist.dto.User;
import com.github.iintelligas.service.PermissionService;
import com.github.iintelligas.service.RoleService;
import com.github.iintelligas.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class DataLoader implements ApplicationListener {

    private final UserService userService;

    private final RoleService roleService;

    private final PermissionService permissionService;

    private final PasswordEncoder passwordEncoder;

    @Autowired
    public DataLoader(UserService userService, RoleService roleService, PermissionService permissionService, PasswordEncoder passwordEncoder) {
        this.userService = userService;
        this.roleService = roleService;
        this.permissionService = permissionService;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        if(userService.getUser("admin") == null) {
            //Data Loading shifted to SQL file V1_01_02__PROFILE_default_data.sql
            //userDataLoader();
        }

    }

    @Transactional
    protected void userDataLoader() {
        System.out.println("Starting data loading...........");
        User adminUser = new User("admin", passwordEncoder.encode("admin"), "Admin", "Admin", "[email protected]");
        User superUser = new User("superuser", passwordEncoder.encode("superuser"), "Super", "UserEntity", "[email protected]");
        User testUser = new User("test", passwordEncoder.encode("test"), "Test", "Test", "[email protected]");
        Role adminRole = new Role("ADMIN_ROLE");
        Role userRole = new Role("USER_ROLE");
        Role superRole = new Role("SUPER_USER_ROLE");
        Permission adminPerm1 = new Permission("PERM_ADMIN_1");
        Permission adminPerm2 = new Permission("PERM_ADMIN_2");
        Permission userPerm1 = new Permission("PERM_USER_1");
        Permission userPerm2 = new Permission("PERM_USER_2");
        Permission superPerm1 = new Permission("SUPER_USER_PREM_1");
        Permission superPerm2 = new Permission("SUPER_USER_PREM_2");

        permissionService.addPermission(adminPerm1);
        permissionService.addPermission(adminPerm2);
        permissionService.addPermission(userPerm1);
        permissionService.addPermission(userPerm2);
        permissionService.addPermission(superPerm1);
        permissionService.addPermission(superPerm2);

        roleService.addRole(adminRole);
        roleService.addRole(userRole);
        roleService.addRole(superRole);

        adminUser.setEnabled(true);
        testUser.setEnabled(true);
        superUser.setEnabled(true);

        userService.addUser(adminUser);
        userService.addUser(testUser);
        userService.addUser(superUser);

        roleService.assignPermissionToRole(adminPerm1, adminRole);
        roleService.assignPermissionToRole(adminPerm2, adminRole);
        roleService.assignPermissionToRole(userPerm1, userRole);
        roleService.assignPermissionToRole(userPerm2, userRole);
        roleService.assignPermissionToRole(superPerm1, superRole);
        roleService.assignPermissionToRole(superPerm2, superRole);

        userService.assignRoleToUser(adminRole, adminUser);
        userService.assignRoleToUser(userRole, testUser);
        userService.assignRoleToUser(superRole, superUser);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy