net.przemkovv.sphinx.config.PopulateDatabaseOnStartup.java Source code

Java tutorial

Introduction

Here is the source code for net.przemkovv.sphinx.config.PopulateDatabaseOnStartup.java

Source

/*
 * The MIT License
 *
 * Copyright 2013 Przemyslaw Walkowiak <przemyslaw.walkowiak@put.poznan.pl>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package net.przemkovv.sphinx.config;

import net.przemkovv.sphinx.dao.PermissionDAO;
import net.przemkovv.sphinx.dao.RoleDAO;
import net.przemkovv.sphinx.model.Permission;
import net.przemkovv.sphinx.model.Role;
import net.przemkovv.sphinx.model.Task;
import net.przemkovv.sphinx.model.User;
import net.przemkovv.sphinx.service.RoleService;
import net.przemkovv.sphinx.service.TaskService;
import net.przemkovv.sphinx.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Przemyslaw Walkowiak <przemyslaw.walkowiak@put.poznan.pl>
 */
@Component
@Transactional
public class PopulateDatabaseOnStartup implements ApplicationListener<ContextRefreshedEvent> {

    private static final Logger logger = LoggerFactory.getLogger(PopulateDatabaseOnStartup.class);

    boolean was_executed = false;

    @Autowired
    RoleDAO roleDAO;

    @Autowired
    PermissionDAO permissionDAO;

    @Autowired
    RoleService roleService;

    @Autowired
    UserService userService;

    @Autowired
    TaskService taskService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (!was_executed) {
            was_executed = true;
        } else {
            return;
        }
        logger.debug("Context name: {}, {}", event.getApplicationContext().getId(),
                event.getApplicationContext().getDisplayName());

        Permission permission_new_task = new Permission("NEW_TASK");
        Permission permission_edit_task = new Permission("UPDATE_TASK");
        Permission permission_remove_task = new Permission("REMOVE_TASK");

        Role role = new Role("TASK_MANAGER");
        role.getPermissions().add(permission_new_task);
        role.getPermissions().add(permission_edit_task);
        role.getPermissions().add(permission_remove_task);

        roleService.save(role);
        Role role2 = new Role("TASK2_MANAGER");
        role2.getPermissions().add(permission_remove_task);
        roleService.save(role2);

        User userJohn = new User();
        userJohn.setEmail("john@gmail.com");
        userJohn.setFirstName("John");
        userJohn.setLastName("Malkovic");
        userService.setPassword(userJohn, "john");
        userService.save(userJohn);

        User userBob = new User();
        userBob.setEmail("bob@gmail.com");
        userBob.setFirstName("Bob");
        userBob.setLastName("Marley");
        userService.setPassword(userBob, "bob");
        userService.save(userBob);

        taskService.createTask("Hello World!", "Display \"Hello World!\"");
        taskService.createTask("2+2", "Return value of 2+2");
    }

}