+package org.argeo.slc.support.deploy.db;\r
+\r
+import java.sql.SQLException;\r
+\r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
+import org.argeo.slc.core.SlcException;\r
+import org.argeo.slc.core.UnsupportedException;\r
+import org.argeo.slc.core.build.Distribution;\r
+import org.argeo.slc.core.deploy.DeployedSystem;\r
+import org.argeo.slc.core.deploy.Deployment;\r
+import org.argeo.slc.core.deploy.DeploymentData;\r
+import org.argeo.slc.core.deploy.TargetData;\r
+import org.dbunit.DataSourceDatabaseTester;\r
+import org.dbunit.DatabaseUnitException;\r
+import org.dbunit.IDatabaseTester;\r
+import org.dbunit.database.IDatabaseConnection;\r
+import org.dbunit.dataset.IDataSet;\r
+import org.dbunit.operation.DatabaseOperation;\r
+\r
+public class DbUnitDeployment implements Deployment {\r
+ private static Log log = LogFactory.getLog(DbUnitDeployment.class);\r
+\r
+ private JdbcAware mxDatabase;\r
+ private DbUnitDeploymentData deploymentData;\r
+ private DbModel dbModel;\r
+\r
+ public void execute() {\r
+ try {\r
+ IDatabaseTester databaseTester = new DataSourceDatabaseTester(\r
+ mxDatabase.getDataSource());\r
+ databaseTester.setSetUpOperation(new DatabaseOperation() {\r
+\r
+ @Override\r
+ public void execute(IDatabaseConnection connection,\r
+ IDataSet dataSet) throws DatabaseUnitException,\r
+ SQLException {\r
+ if (dbModel != null) {\r
+ dbModel.createSchema(connection.getConnection());\r
+ DatabaseOperation.INSERT.execute(connection, dataSet);\r
+ } else {\r
+ DatabaseOperation.UPDATE.execute(connection, dataSet);\r
+ }\r
+ }\r
+\r
+ });\r
+ databaseTester.setDataSet(deploymentData.createDataSet());\r
+ databaseTester.onSetup();\r
+ databaseTester.onTearDown();\r
+\r
+ log.info("Database deployed.");\r
+ } catch (Exception e) {\r
+ throw new SlcException("Could not initialize the database", e);\r
+ }\r
+ }\r
+\r
+ public DeployedSystem getDeployedSystem() {\r
+ // TODO: think of a more generic approach. MxDtaabse deployed system?\r
+ // (with deployment id etc.)\r
+ throw new UnsupportedException("Method not supported");\r
+ }\r
+\r
+ public void setDbModel(DbModel dbModel) {\r
+ this.dbModel = dbModel;\r
+ }\r
+\r
+ public void setDeploymentData(DeploymentData deploymentData) {\r
+ this.deploymentData = (DbUnitDeploymentData) deploymentData;\r
+ }\r
+\r
+ public void setTargetData(TargetData targetData) {\r
+ this.mxDatabase = (JdbcAware) targetData;\r
+\r
+ }\r
+\r
+ public void setDistribution(Distribution distribution) {\r
+ throw new UnsupportedException("Method not supported");\r
+ }\r
+\r
+}\r