1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.oness.common.model.util.dbunit;
17
18 import java.io.InputStream;
19 import java.sql.Connection;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import javax.sql.DataSource;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.dbunit.database.DatabaseConnection;
29 import org.dbunit.dataset.CompositeDataSet;
30 import org.dbunit.dataset.IDataSet;
31 import org.dbunit.dataset.excel.XlsDataSet;
32 import org.dbunit.dataset.xml.FlatXmlDataSet;
33 import org.dbunit.operation.DatabaseOperation;
34 import org.springframework.core.io.DefaultResourceLoader;
35 import org.springframework.core.io.Resource;
36 import org.springframework.core.io.ResourceLoader;
37
38 /***
39 * Bean that populates a database, removing previous data, from a list of
40 * resources (in Spring format) whose contents are either in
41 * {@link FlatXmlDataSet}or {@link XlsDataSet}format if they end with ".xml"
42 * or ".xls" respectively.
43 *
44 * @author Carlos Sanchez
45 * @version $Revision: 1.4 $
46 */
47 public class DatabasePopulator {
48
49 private static final Log log = LogFactory.getLog(DatabasePopulator.class);
50
51 private List locations;
52
53 private DataSource dataSource;
54
55 /***
56 * Set the locations of the dataset resources.
57 *
58 * @param locations
59 * The locations to set.
60 */
61 public void setLocations(List locations) {
62 this.locations = locations;
63 }
64
65 /***
66 * @return Returns the locations.
67 */
68 public List getLocations() {
69 return locations;
70 }
71
72 /***
73 * DataSource of the database to populate
74 *
75 * @param dataSource
76 * The dataSource to set.
77 */
78 public void setDataSource(DataSource dataSource) {
79 this.dataSource = dataSource;
80 }
81
82 /***
83 * @return Returns the dataSource.
84 */
85 public DataSource getDataSource() {
86 return dataSource;
87 }
88
89 public void execute() throws Exception {
90 if ((locations == null) || (dataSource == null))
91 return;
92
93 List dataSets = new ArrayList(locations.size());
94 ResourceLoader resourceLoader = new DefaultResourceLoader();
95 Iterator iter = locations.iterator();
96 while (iter.hasNext()) {
97 String element = (String) iter.next();
98 Resource resource = resourceLoader.getResource(element);
99 InputStream inputStream = resource.getInputStream();
100 IDataSet dataSet = null;
101 if (element.endsWith(".xls")) {
102 dataSet = new XlsDataSet(inputStream);
103 if (log.isDebugEnabled())
104 log.debug("Loading xls dataSet from " + element);
105 }
106 if (element.endsWith(".xml")) {
107 dataSet = new FlatXmlDataSet(inputStream);
108 if (log.isDebugEnabled())
109 log.debug("Loading xml dataSet from " + element);
110 }
111 if (dataSet == null) {
112 if (log.isErrorEnabled())
113 log.error("DataSet File extension unknown " + element);
114 continue;
115 }
116 dataSets.add(dataSet);
117 }
118
119 CompositeDataSet compositeDataSet = new CompositeDataSet(
120 (IDataSet[]) dataSets.toArray(new IDataSet[0]));
121
122 Connection connection = getDataSource().getConnection();
123 DatabaseConnection databaseConnection = new DatabaseConnection(
124 connection);
125 try {
126 DatabaseOperation databaseOperation = DatabaseOperation.CLEAN_INSERT;
127 databaseOperation.execute(databaseConnection, compositeDataSet);
128 if (!connection.getAutoCommit())
129 connection.commit();
130 } finally {
131 databaseConnection.close();
132 }
133
134 }
135 }