Application.java 3.31 KB
package etunicorn;

import etunicorn.entity.Permission;
import etunicorn.entity.Personne;
import etunicorn.entity.Role;
import etunicorn.entity.Session;
import etunicorn.repository.PermissionRepository;
import etunicorn.repository.PersonneRepository;
import etunicorn.repository.RoleRepository;
import etunicorn.repository.SessionRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * etunicorn-server
 * Copyright © 2017 Le Club Info Polytech Lille
 * Tous droits réservés
 */
@Configuration
@SpringBootApplication
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner demo(PermissionRepository permissionRepository,
                                  RoleRepository roleRepository,
                                  PersonneRepository personneRepository,
                                  SessionRepository sessionRepository) {
        return (args) -> {
            permissionRepository.save(new Permission("PERSONNE_ADD"));
            permissionRepository.save(new Permission("PERSONNE_EDIT"));
            permissionRepository.save(new Permission("PERSONNE_GET"));
            permissionRepository.save(new Permission("PERSONNE_LIST"));
            permissionRepository.save(new Permission("PERSONNE_REMOVE"));
            permissionRepository.save(new Permission("PERSONNE_ROLE"));
            permissionRepository.save(new Permission("ROLE_ADD"));
            permissionRepository.save(new Permission("ROLE_DELETE"));
            permissionRepository.save(new Permission("ROLE_PERMISSION_ADD"));
            permissionRepository.save(new Permission("ROLE_PERMISSION_LIST"));
            permissionRepository.save(new Permission("ROLE_PERMISSION_REMOVE"));
            // ...

            roleRepository.save(new Role("admin", (List<Permission>) permissionRepository.findAll()));
            roleRepository.save(new Role("etudiant", new ArrayList<>()));
            personneRepository.save(new Personne("carte", new Date(), "gbontoux", roleRepository.findByNom("admin")));
            sessionRepository.save(new Session(personneRepository.findByLogin("gbontoux"), "A", new Date(new Date().getTime() + 1000 * 60 * 10)));

        };
    }

    // @Bean
    // public DataSource dataSource() {
    //     DriverManagerDataSource dataSource = new DriverManagerDataSource();
    //     dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    //     dataSource.setUrl("jdbc:mysql://localhost/etunicorn");
    //     dataSource.setUsername("etunicorn");
    //     dataSource.setPassword("etunicorn");
    //     return dataSource;
    // }

    // Properties additionalProperties() {
    //     Properties properties = new Properties();
    //     properties.setProperty("spring.jpa.hibernale.ddl-auto", "create-drop");
    //     properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
    //     return properties;
    // }

}