org.kuali.kpme.core.util.LoadDatabaseDataLifeCycle.java Source code

Java tutorial

Introduction

Here is the source code for org.kuali.kpme.core.util.LoadDatabaseDataLifeCycle.java

Source

/**
 * Copyright 2004-2014 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.kpme.core.util;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.sql.DataSource;

import junit.framework.Assert;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.StatementCallback;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

public class LoadDatabaseDataLifeCycle extends SQLDataLifeCycle {
    public LoadDatabaseDataLifeCycle() {
    }

    public LoadDatabaseDataLifeCycle(Class caller) {
        super(caller);
    }

    public void loadData(final PlatformTransactionManager transactionManager, final DataSource dataSource,
            final String schemaName) {
        LOG.info("Populating tables for schema " + schemaName);
        Assert.assertNotNull("DataSource could not be located.", dataSource);

        if (schemaName == null || schemaName.equals("")) {
            Assert.fail("Empty schema name given");
        }
        new TransactionTemplate(transactionManager).execute(new TransactionCallback<Object>() {
            public Object doInTransaction(final TransactionStatus status) {
                verifyTestEnvironment(dataSource);
                return new JdbcTemplate(dataSource).execute(new StatementCallback<Object>() {
                    public Object doInStatement(Statement statement) throws SQLException {
                        if (callingTestClass != null) {
                            List<String> sqlStatements = getTestDataSQLStatements(
                                    "src/test/config/sql/" + callingTestClass.getSimpleName() + ".sql");

                            for (String sql : sqlStatements) {
                                if (!sql.startsWith("#") && !sql.startsWith("//")
                                        && !StringUtils.isEmpty(sql.trim())) {
                                    // ignore comment lines in our sql reader.
                                    statement.addBatch(sql);
                                }
                            }
                        }
                        statement.executeBatch();
                        return null;
                    }
                });
            }
        });
    }
}