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

ai.digital.integration.server.common.tasks.database.ImportDbUnitDataTask.kt Maven / Gradle / Ivy

There is a newer version: 23.3.0-1025.941
Show newest version
package ai.digital.integration.server.common.tasks.database

import ai.digital.integration.server.common.constant.PluginConstant
import ai.digital.integration.server.common.util.DbConfigurationUtil
import ai.digital.integration.server.common.util.DbUtil
import ai.digital.integration.server.common.util.IntegrationServerUtil
import ai.digital.integration.server.common.util.PostgresDbUtil
import ai.digital.integration.server.deploy.internals.DeployExtensionUtil
import ai.digital.integration.server.deploy.tasks.server.DownloadAndExtractDbUnitDataDistTask
import org.dbunit.dataset.xml.FlatXmlDataSet
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder
import org.dbunit.operation.DatabaseOperation
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import java.io.FileInputStream
import java.nio.file.Paths

open class ImportDbUnitDataTask : DefaultTask() {

    companion object {
        const val NAME = "importDbUnitData"
    }

    init {
        this.group = PluginConstant.PLUGIN_GROUP
        this.dependsOn(DownloadAndExtractDbUnitDataDistTask.NAME)
        this.onlyIf {
            !DbUtil.isDerby(project) && DeployExtensionUtil.getExtension(project).xldIsDataVersion != null
        }
    }

    private fun getConfiguration(): Triple {
        val username = DbUtil.getDbPropValue(project, "db-username")
        val password = DbUtil.getDbPropValue(project, "db-password")
        val url = DbUtil.getDbPropValue(project, "db-url")

        return Triple(username, password, url)
    }

    private fun configureDataSet(): FlatXmlDataSet? {
        val provider = FlatXmlDataSetBuilder()
        provider.isColumnSensing = true
        provider.isCaseSensitiveTableNames = true
        val version = DeployExtensionUtil.getExtension(project).xldIsDataVersion
        val dataFile = Paths.get("${IntegrationServerUtil.getDist(project)}/xld-is-data-${version}-repository/data.xml")
        return provider.build(FileInputStream(dataFile.toFile()))
    }

    @TaskAction
    fun runImport() {
        val dbname = DbUtil.databaseName(project)
        val dbDependency = DbUtil.detectDbDependencies(dbname)
        val dbConfig = getConfiguration()
        val properties = DbConfigurationUtil.connectionProperties(dbConfig.first, dbConfig.second)

        val driverConnection =
            DbConfigurationUtil.createDriverConnection(dbDependency.driverClass.orEmpty(), dbConfig.third, properties)
        val connection = DbConfigurationUtil.configureConnection(driverConnection, dbDependency)
        try {
            val dataSet = configureDataSet()
            DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet)
            if (dbname == DbUtil.POSTGRES) {
                PostgresDbUtil.resetSequences(project, driverConnection)
            }
        } finally {
            connection.close()
            driverConnection.close()
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy