]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/support/deploy/db/DbUnitDeployment.java
Refactor package names
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / support / deploy / db / DbUnitDeployment.java
1 package org.argeo.slc.support.deploy.db;
2
3 import java.sql.SQLException;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.argeo.slc.SlcException;
8 import org.argeo.slc.UnsupportedException;
9 import org.argeo.slc.build.Distribution;
10 import org.argeo.slc.deploy.DeployedSystem;
11 import org.argeo.slc.deploy.Deployment;
12 import org.argeo.slc.deploy.DeploymentData;
13 import org.argeo.slc.deploy.TargetData;
14 import org.dbunit.DataSourceDatabaseTester;
15 import org.dbunit.DatabaseUnitException;
16 import org.dbunit.IDatabaseTester;
17 import org.dbunit.database.IDatabaseConnection;
18 import org.dbunit.dataset.IDataSet;
19 import org.dbunit.operation.DatabaseOperation;
20
21 public class DbUnitDeployment implements Deployment {
22 private static Log log = LogFactory.getLog(DbUnitDeployment.class);
23
24 private JdbcAware mxDatabase;
25 private DbUnitDeploymentData deploymentData;
26 private DbModel dbModel;
27
28 public void execute() {
29 try {
30 IDatabaseTester databaseTester = new DataSourceDatabaseTester(
31 mxDatabase.getDataSource());
32 databaseTester.setSetUpOperation(new DatabaseOperation() {
33
34 @Override
35 public void execute(IDatabaseConnection connection,
36 IDataSet dataSet) throws DatabaseUnitException,
37 SQLException {
38 if (dbModel != null) {
39 dbModel.createSchema(connection.getConnection());
40 DatabaseOperation.INSERT.execute(connection, dataSet);
41 } else {
42 DatabaseOperation.UPDATE.execute(connection, dataSet);
43 }
44 }
45
46 });
47 databaseTester.setDataSet(deploymentData.createDataSet());
48 databaseTester.onSetup();
49 databaseTester.onTearDown();
50
51 log.info("Database deployed.");
52 } catch (Exception e) {
53 throw new SlcException("Could not initialize the database", e);
54 }
55 }
56
57 public DeployedSystem getDeployedSystem() {
58 // TODO: think of a more generic approach. MxDtaabse deployed system?
59 // (with deployment id etc.)
60 throw new UnsupportedException("Method not supported");
61 }
62
63 public void setDbModel(DbModel dbModel) {
64 this.dbModel = dbModel;
65 }
66
67 public void setDeploymentData(DeploymentData deploymentData) {
68 this.deploymentData = (DbUnitDeploymentData) deploymentData;
69 }
70
71 public void setTargetData(TargetData targetData) {
72 this.mxDatabase = (JdbcAware) targetData;
73
74 }
75
76 public void setDistribution(Distribution distribution) {
77 throw new UnsupportedException("Method not supported");
78 }
79
80 }