1 package net.sourceforge.mavenplugins.dbunit;
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 import org.apache.commons.lang.StringUtils;
60 import org.dbunit.DatabaseUnitException;
61 import org.dbunit.database.DatabaseSequenceFilter;
62 import org.dbunit.database.IDatabaseConnection;
63 import org.dbunit.dataset.DataSetException;
64 import org.dbunit.dataset.FilteredDataSet;
65 import org.dbunit.dataset.IDataSet;
66 import org.dbunit.dataset.filter.ITableFilter;
67 import org.dbunit.ext.mssql.InsertIdentityOperation;
68 import org.dbunit.operation.DatabaseOperation;
69
70 import java.io.IOException;
71 import java.sql.SQLException;
72
73 public class DataSetTool extends DbUnitTool
74 {
75 /** the type of data set being processed */
76 private String dataSetFormat;
77 /** the operation to perform on the data set */
78 private String operation;
79 /** dbunit database operation based on the string */
80 private DatabaseOperation dbOperation;
81 /** list of comma separated table names to export */
82 private String exportTables;
83 /** export psuedo operation */
84 private static final String EXPORT = "EXPORT";
85 /** format for excel files */
86 private static final String EXCEL = "EXCEL";
87
88 /**
89 * @return the format of the data set.
90 */
91 public String getDataSetFormat()
92 {
93 return dataSetFormat;
94 }
95
96 /**
97 * TODO better document this
98 * Set the format of the dataset, e.g. EXCEL
99 * @param name the dbunit dataset format
100 */
101 public void setDataSetFormat(String name)
102 {
103 dataSetFormat = name;
104 }
105
106 /**
107 * The operation to perform on a dataset, e.g.
108 * <code>INSERT</code>, <code>CLEAN_INSERT</code>, <code>DELETE</code> or
109 * <code>EXPORT</code>
110 */
111 public String getOperation()
112 {
113 return operation;
114 }
115
116 /**
117 * @return the operation to perform on a dataset
118 * @see #getOperation()
119 */
120 public void setOperation(String name)
121 {
122 operation = name;
123 if (operation == null) operation = "INSERT";
124 if (operation.equalsIgnoreCase("CLEAN_INSERT")) dbOperation = DatabaseOperation.CLEAN_INSERT;
125 if (operation.equalsIgnoreCase("DELETE")) dbOperation = DatabaseOperation.DELETE;
126 if (operation.equalsIgnoreCase("INSERT")) dbOperation = DatabaseOperation.INSERT;
127 if (operation.equalsIgnoreCase("MSSQL_CLEAN_INSERT")) dbOperation = InsertIdentityOperation.CLEAN_INSERT;
128 }
129
130 /**
131 * @return a comma separated list of table names
132 */
133 public String getExportTables()
134 {
135 return exportTables;
136 }
137
138 /**
139 * set the list of tables to be exported
140 * @param tables a comma separated list of table names
141 */
142 public void setExportTables(String tables)
143 {
144 exportTables = tables;
145 }
146
147 /**
148 * @return the dataset to operate on from the given connection
149 */
150 private IDataSet getDataSet(IDatabaseConnection connection) throws SQLException
151 {
152 IDataSet dataSet = null;
153 if (getExportTables() == null)
154 {
155 dataSet = connection.createDataSet();
156 }
157 else
158 {
159 String[] tables = StringUtils.split(getExportTables(), ",");
160 dataSet = connection.createDataSet(tables);
161 }
162 try
163 {
164 ITableFilter filter = new DatabaseSequenceFilter(connection);
165 return new FilteredDataSet(filter, dataSet);
166 }
167 catch (DataSetException e)
168 {
169 throw new SQLException(e.getMessage());
170 }
171 }
172
173 /**
174 * Process a dataset using a DbUnit operation
175 */
176 public void process() throws ClassNotFoundException, SQLException, IOException, DataSetException, DatabaseUnitException
177 {
178 if (getOperation().equalsIgnoreCase(EXPORT))
179 {
180 IDatabaseConnection connection= getConnection();
181 writeDataSetToFile(getFileName(), getDataSetFormat(), getDataSet(connection));
182 connection.close();
183 }
184 else if (dbOperation != null)
185 {
186
187 IDataSet dataset = getDataSetFromFile(getFileName(), getDataSetFormat());
188 dbOperation.execute(getConnection(), dataset);
189 }
190 else
191 {
192 throw new IllegalArgumentException("Invalid value for maven.dbunit.importType");
193 }
194 }
195
196 }