1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.database;
|
11
|
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import javax.annotation.PostConstruct;
|
17
|
import javax.sql.DataSource;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.dbunit.database.DatabaseConfig;
|
21
|
import org.dbunit.database.DatabaseConnection;
|
22
|
import org.dbunit.database.IDatabaseConnection;
|
23
|
import org.dbunit.dataset.IDataSet;
|
24
|
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
|
25
|
import org.dbunit.ext.h2.H2DataTypeFactory;
|
26
|
import org.dbunit.operation.DatabaseOperation;
|
27
|
import org.springframework.beans.factory.annotation.Autowired;
|
28
|
import org.springframework.core.io.Resource;
|
29
|
import org.springframework.transaction.TransactionStatus;
|
30
|
|
31
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
32
|
import eu.etaxonomy.cdm.model.common.VocabularyEnum;
|
33
|
|
34
|
public class TestingTermInitializer extends PersistentTermInitializer {
|
35
|
private static final Logger logger = Logger.getLogger(TestingTermInitializer.class);
|
36
|
|
37
|
private DataSource dataSource;
|
38
|
|
39
|
private Resource termsDataSet;
|
40
|
|
41
|
private Resource termsDtd;
|
42
|
|
43
|
public void setTermsDataSet(Resource termsDataSet) {
|
44
|
this.termsDataSet = termsDataSet;
|
45
|
}
|
46
|
|
47
|
public void setTermsDtd(Resource termsDtd) {
|
48
|
this.termsDtd = termsDtd;
|
49
|
}
|
50
|
|
51
|
@Autowired
|
52
|
public void setDataSource(DataSource dataSource) {
|
53
|
this.dataSource = dataSource;
|
54
|
}
|
55
|
|
56
|
|
57
|
@PostConstruct
|
58
|
@Override
|
59
|
public void initialize() {
|
60
|
super.initialize();
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
public void doInitialize(){
|
65
|
logger.info("TestingTermInitializer initialize start ...");
|
66
|
if (isOmit()){
|
67
|
logger.info("TestingTermInitializer.omit == true, returning without initializing terms");
|
68
|
return;
|
69
|
} else {
|
70
|
TransactionStatus txStatus = transactionManager.getTransaction(txDefinition);
|
71
|
IDatabaseConnection connection = null;
|
72
|
|
73
|
try {
|
74
|
|
75
|
connection = getConnection();
|
76
|
|
77
|
// MultiSchemaXmlDataSetFactory dataSetFactory = new MultiSchemaXmlDataSetFactory();
|
78
|
// MultiSchemaDataSet multiSchemaDataset = dataSetFactory.createDataSet(termsDataSet.getFile());
|
79
|
//
|
80
|
// if(multiSchemaDataset != null){
|
81
|
// for (String name : multiSchemaDataset.getSchemaNames()) {
|
82
|
// IDataSet clearDataSet = multiSchemaDataset.getDataSetForSchema(name);
|
83
|
// DatabaseOperation.CLEAN_INSERT.execute(connection, clearDataSet);
|
84
|
// }
|
85
|
// }
|
86
|
|
87
|
// logger.info("loading data base schema from " + termsDtd.getFile().getAbsolutePath());
|
88
|
// logger.info("loading data set from " + termsDataSet.getFile().getAbsolutePath());
|
89
|
|
90
|
|
91
|
//old: IDataSet dataSet = new FlatXmlDataSet(new InputStreamReader(termsDataSet.getInputStream()),new InputStreamReader(termsDtd.getInputStream()));
|
92
|
|
93
|
IDataSet dataSet = new FlatXmlDataSetBuilder()
|
94
|
.setMetaDataSetFromDtd(termsDtd.getInputStream())
|
95
|
.build(termsDataSet.getInputStream());
|
96
|
|
97
|
|
98
|
//ITable definedTermBase = dataSet.getTable("DEFINEDTERMBASE");
|
99
|
// for(int rowId = 0; rowId < definedTermBase.getRowCount(); rowId++) {
|
100
|
// System.err.println(rowId + " : " + definedTermBase.getValue(rowId, "CREATEDBY_ID"));
|
101
|
// }
|
102
|
DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
|
103
|
|
104
|
} catch (Exception e) {
|
105
|
logger.error(e);
|
106
|
for(StackTraceElement ste : e.getStackTrace()) {
|
107
|
logger.error(ste);
|
108
|
}
|
109
|
} finally {
|
110
|
try {
|
111
|
if (connection != null){
|
112
|
connection.close();
|
113
|
}
|
114
|
} catch (SQLException sqle) {
|
115
|
logger.error(sqle);
|
116
|
}
|
117
|
}
|
118
|
|
119
|
transactionManager.commit(txStatus);
|
120
|
|
121
|
txStatus = transactionManager.getTransaction(txDefinition);
|
122
|
|
123
|
for(VocabularyEnum vocabularyType : VocabularyEnum.values()) {
|
124
|
initializeAndStore(vocabularyType, new HashMap<UUID,DefinedTermBase>(), null);
|
125
|
}
|
126
|
transactionManager.commit(txStatus);
|
127
|
//txStatus = transactionManager.getTransaction(txDefinition);
|
128
|
}
|
129
|
logger.info("TestingTermInitializer initialize end ...");
|
130
|
}
|
131
|
|
132
|
protected IDatabaseConnection getConnection() throws SQLException {
|
133
|
IDatabaseConnection connection = null;
|
134
|
try {
|
135
|
connection = new DatabaseConnection(dataSource.getConnection());
|
136
|
|
137
|
DatabaseConfig config = connection.getConfig();
|
138
|
//FIXME must use unitils.properties: org.unitils.core.dbsupport.DbSupport.implClassName & database.dialect to find configured DataTypeFactory
|
139
|
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,new H2DataTypeFactory());
|
140
|
} catch (Exception e) {
|
141
|
logger.error(e);
|
142
|
}
|
143
|
return connection;
|
144
|
}
|
145
|
}
|