DataGeneratorBean.java 1.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
package at.ac.tuwien.sepm.assignment.individual.persistence;

import java.lang.invoke.MethodHandles;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.ScriptUtils;

/**
 * This component is only created, if the profile {@code datagen} is active
 * You can activate this profile by adding {@code -Dspring.profiles.active=datagen} to your maven command line
 */
@Configuration
@Profile("datagen")
public class DataGeneratorBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
    private DataSource source;

    public DataGeneratorBean(DataSource source) {
        this.source = source;
    }

    /**
     * Executed once when the component is instantiated. Inserts some dummy data.
     */
    @PostConstruct
    void insertDummyData() {
        try {
34 35
            LOGGER.info("Inserting dummy data in database");
            ScriptUtils.executeSqlScript(source.getConnection(), new ClassPathResource("sql/insertData.sql"));
36 37 38 39 40
        } catch (Exception e) {
            LOGGER.error("Error inserting test data", e);
        }
    }
}