1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.api.service;
|
12
|
|
13
|
import java.sql.Connection;
|
14
|
import java.sql.ResultSet;
|
15
|
import java.sql.SQLException;
|
16
|
import java.sql.Statement;
|
17
|
import java.util.HashMap;
|
18
|
import java.util.Map;
|
19
|
|
20
|
import org.apache.log4j.Logger;
|
21
|
import org.hibernate.SessionFactory;
|
22
|
import org.springframework.beans.BeansException;
|
23
|
import org.springframework.beans.factory.annotation.Autowired;
|
24
|
import org.springframework.context.ApplicationContext;
|
25
|
import org.springframework.context.ApplicationContextAware;
|
26
|
import org.springframework.jdbc.datasource.AbstractDriverBasedDataSource;
|
27
|
import org.springframework.orm.hibernate5.SessionFactoryUtils;
|
28
|
import org.springframework.stereotype.Service;
|
29
|
import org.springframework.transaction.annotation.Transactional;
|
30
|
|
31
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
32
|
import eu.etaxonomy.cdm.config.CdmPersistentSourceUtils;
|
33
|
import eu.etaxonomy.cdm.config.CdmSourceException;
|
34
|
import eu.etaxonomy.cdm.database.CdmDataSource;
|
35
|
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
|
36
|
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
|
37
|
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
|
38
|
import eu.etaxonomy.cdm.database.H2Mode;
|
39
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
40
|
import eu.etaxonomy.cdm.model.common.init.TermNotFoundException;
|
41
|
import eu.etaxonomy.cdm.model.metadata.CdmMetaData.MetaDataPropertyName;
|
42
|
|
43
|
|
44
|
|
45
|
/**
|
46
|
* Implementation of service which provides functionality to directly access database
|
47
|
* related information.
|
48
|
*
|
49
|
* @author a.mueller
|
50
|
*
|
51
|
*/
|
52
|
@Service
|
53
|
@Transactional(readOnly = true)
|
54
|
public class DatabaseServiceHibernateImpl implements IDatabaseService, ApplicationContextAware {
|
55
|
private static final Logger logger = Logger.getLogger(DatabaseServiceHibernateImpl.class);
|
56
|
|
57
|
private static final String TMP_DATASOURCE = "tmp";
|
58
|
|
59
|
@Autowired
|
60
|
private SessionFactory factory;
|
61
|
|
62
|
@Autowired
|
63
|
protected ApplicationContext appContext;
|
64
|
|
65
|
private CdmApplicationController application;
|
66
|
|
67
|
@Override
|
68
|
public void setApplicationController(CdmApplicationController cdmApplicationController){
|
69
|
this.application = cdmApplicationController;
|
70
|
}
|
71
|
|
72
|
@Override
|
73
|
public boolean connectToDatasource(CdmPersistentDataSource dataSource) throws TermNotFoundException{
|
74
|
this.application.changeDataSource(dataSource);
|
75
|
logger.debug("DataSource changed to " + dataSource.getName());
|
76
|
return true;
|
77
|
}
|
78
|
|
79
|
@Override
|
80
|
public boolean connectToDatabase(DatabaseTypeEnum databaseTypeEnum, String server,
|
81
|
String database, String username, String password, int port, String filePath, H2Mode mode) throws TermNotFoundException {
|
82
|
ICdmDataSource dataSource = CdmDataSource.NewInstance(databaseTypeEnum, server, database, port, username, password);
|
83
|
CdmPersistentDataSource tmpDataSource = saveDataSource(TMP_DATASOURCE, dataSource);
|
84
|
boolean result = connectToDatasource(tmpDataSource);
|
85
|
CdmPersistentSourceUtils.delete(tmpDataSource);
|
86
|
return result;
|
87
|
}
|
88
|
|
89
|
@Override
|
90
|
public boolean connectToDatabase(DatabaseTypeEnum databaseTypeEnum, String server,
|
91
|
String database, String username, String password) throws TermNotFoundException {
|
92
|
return connectToDatabase(databaseTypeEnum, server, database, username, password, databaseTypeEnum.getDefaultPort(), null, null) ;
|
93
|
}
|
94
|
|
95
|
@Override
|
96
|
public CdmPersistentDataSource saveDataSource(String strDataSourceName,
|
97
|
ICdmDataSource dataSource) {
|
98
|
return CdmPersistentDataSource.save(strDataSourceName, dataSource);
|
99
|
}
|
100
|
|
101
|
@Override
|
102
|
public CdmPersistentDataSource updateDataSource(String strDataSourceName,
|
103
|
CdmPersistentDataSource dataSource) throws DataSourceNotFoundException {
|
104
|
return CdmPersistentDataSource.update(strDataSourceName, dataSource);
|
105
|
}
|
106
|
|
107
|
@Override
|
108
|
public String getUrl() {
|
109
|
return getDataSource().getUrl();
|
110
|
}
|
111
|
|
112
|
@Override
|
113
|
public String getUsername() {
|
114
|
return getDataSource().getUsername();
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* Returns the AbstractDriverBasedDataSource from hibernate,
|
119
|
* generalized in order to also allow using SimpleDriverDataSource.
|
120
|
*
|
121
|
* @return the AbstractDriverBasedDataSource from the hibernate layer
|
122
|
*/
|
123
|
private AbstractDriverBasedDataSource getDataSource(){
|
124
|
AbstractDriverBasedDataSource ds = (AbstractDriverBasedDataSource)SessionFactoryUtils.getDataSource(factory);
|
125
|
return ds;
|
126
|
}
|
127
|
|
128
|
@Override
|
129
|
public void setApplicationContext(ApplicationContext applicationContext)
|
130
|
throws BeansException {
|
131
|
this.appContext = applicationContext;
|
132
|
}
|
133
|
|
134
|
@Override
|
135
|
public String getDbSchemaVersion() throws CdmSourceException {
|
136
|
try {
|
137
|
return (String)getSingleValue(MetaDataPropertyName.DB_SCHEMA_VERSION.getSqlQuery());
|
138
|
} catch (SQLException e) {
|
139
|
throw new CdmSourceException(e.getMessage());
|
140
|
}
|
141
|
}
|
142
|
|
143
|
@Override
|
144
|
public boolean isDbEmpty() throws CdmSourceException {
|
145
|
// Any CDM DB should have a schema version
|
146
|
String dbSchemaVersion = getDbSchemaVersion();
|
147
|
return (dbSchemaVersion == null || dbSchemaVersion.equals(""));
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* Execute a SQL query which returns a single value
|
152
|
*
|
153
|
* @param query , which returns a single value
|
154
|
* @return
|
155
|
* @throws SQLException
|
156
|
*/
|
157
|
private Object getSingleValue(String query) throws SQLException {
|
158
|
String queryString = query == null? "(null)": query;
|
159
|
//ResultSet resultSet = executeQuery(query);
|
160
|
ResultSet resultSet = null;
|
161
|
|
162
|
Connection connection = SessionFactoryUtils.getDataSource(factory).getConnection();
|
163
|
if (connection != null){
|
164
|
|
165
|
Statement statement = connection.createStatement();
|
166
|
resultSet = statement.executeQuery(query);
|
167
|
|
168
|
if (resultSet == null || resultSet.next() == false){
|
169
|
logger.info("No record returned for query " + queryString);
|
170
|
return null;
|
171
|
}
|
172
|
if (resultSet.getMetaData().getColumnCount() != 1){
|
173
|
logger.info("More than one column selected in query" + queryString);
|
174
|
//first value will be taken
|
175
|
}
|
176
|
Object object = resultSet.getObject(1);
|
177
|
if (resultSet.next()){
|
178
|
logger.info("Multiple results for query " + queryString);
|
179
|
//first row will be taken
|
180
|
}
|
181
|
// making sure we close all resources so we don't run out of
|
182
|
// connections in the connection pool
|
183
|
resultSet.close();
|
184
|
statement.close();
|
185
|
connection.close();
|
186
|
|
187
|
return object;
|
188
|
}else{
|
189
|
throw new RuntimeException("Could not establish connection to database");
|
190
|
}
|
191
|
|
192
|
}
|
193
|
|
194
|
|
195
|
@Override
|
196
|
public Map<MetaDataPropertyName, String> getCdmMetadataMap() throws CdmSourceException {
|
197
|
Map<MetaDataPropertyName, String> cdmMetaDataMap = new HashMap<MetaDataPropertyName, String>();
|
198
|
|
199
|
for(MetaDataPropertyName mdpn : MetaDataPropertyName.values()){
|
200
|
String value = null;
|
201
|
try {
|
202
|
value = (String)getSingleValue(mdpn.getSqlQuery());
|
203
|
} catch (SQLException e) {
|
204
|
throw new CdmSourceException(e.getMessage());
|
205
|
}
|
206
|
if(value != null) {
|
207
|
cdmMetaDataMap.put(mdpn, value);
|
208
|
}
|
209
|
}
|
210
|
return cdmMetaDataMap;
|
211
|
}
|
212
|
|
213
|
}
|