Project

General

Profile

Revision f5f02f66

IDf5f02f6690fbd07a95e30989d5831f5673ce2b32
Parent 32f1b240
Child b746bb33

Added by Cherian Mathew about 7 years ago

added methods to get schema version and check if db is empty
house-keeping and adding javadoc

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/DatabaseServiceHibernateImpl.java
10 10

  
11 11
package eu.etaxonomy.cdm.api.service;
12 12

  
13
import java.sql.Connection;
14
import java.sql.ResultSet;
15
import java.sql.SQLException;
16
import java.sql.Statement;
17

  
18
import javax.sql.DataSource;
19

  
13 20
import org.apache.log4j.Logger;
14 21
import org.hibernate.SessionFactory;
15 22
import org.springframework.beans.BeansException;
......
22 29
import org.springframework.transaction.annotation.Transactional;
23 30

  
24 31
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
32
import eu.etaxonomy.cdm.config.CdmPersistentSourceUtils;
33
import eu.etaxonomy.cdm.config.CdmSourceException;
25 34
import eu.etaxonomy.cdm.database.CdmDataSource;
26 35
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
27 36
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
......
29 38
import eu.etaxonomy.cdm.database.H2Mode;
30 39
import eu.etaxonomy.cdm.database.ICdmDataSource;
31 40
import eu.etaxonomy.cdm.model.common.init.TermNotFoundException;
41
import eu.etaxonomy.cdm.model.metadata.CdmMetaData.MetaDataPropertyName;
32 42
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
33 43

  
34 44

  
35 45

  
36 46
/**
47
 * Implementation of service which provides functionality to directly access database 
48
 * related information.
49
 * 
37 50
 * @author a.mueller
38 51
 *
39 52
 */
......
51 64
	protected ApplicationContext appContext;
52 65
	
53 66
	private CdmApplicationController application;
67
	
68
	
69
	/* (non-Javadoc)
70
	 * @see eu.etaxonomy.cdm.api.service.IDatabaseService#setApplicationController(eu.etaxonomy.cdm.api.application.CdmApplicationController)
71
	 */
54 72
	public void setApplicationController(CdmApplicationController cdmApplicationController){
55 73
		this.application = cdmApplicationController;
56 74
	}
......
73 91
		ICdmDataSource dataSource = CdmDataSource.NewInstance(databaseTypeEnum, server, database, port, username, password, code);
74 92
		CdmPersistentDataSource tmpDataSource =  saveDataSource(TMP_DATASOURCE, dataSource);
75 93
		boolean result = connectToDatasource(tmpDataSource);
76
		CdmPersistentDataSource.delete(tmpDataSource);
94
		CdmPersistentSourceUtils.delete(tmpDataSource);
77 95
		return result;
78 96
	}
79 97

  
......
102 120
		return CdmPersistentDataSource.update(strDataSourceName, dataSource);
103 121
	}
104 122

  
105
	
106
//TODO removed 04.02.2009 as spring 2.5.6 does not support DriverManagerDataSource.getDriverClassName anymore
107
//Let's see if this is not needed by any other application
108
//	/* (non-Javadoc)
109
//	 * @see eu.etaxonomy.cdm.api.service.IDatabaseService#getDatabaseTypeName()
110
//	 */
111
//	public DatabaseTypeEnum getDatabaseEnum() {
112
//		return DatabaseTypeEnum.getDatabaseEnumByDriverClass(getDataSource().getDriverClassName());
113
//	}
114
//
115
//	/* (non-Javadoc)
116
//	 * @see eu.etaxonomy.cdm.api.service.IDatabaseService#getDriverClassName()
117
//	 */
118
//	public String getDriverClassName() {
119
//		return ( getDataSource()).getDriverClassName();
120
//	}
121

  
122 123
	/* (non-Javadoc)
123 124
	 * @see eu.etaxonomy.cdm.api.service.IDatabaseService#getUrl()
124 125
	 */
......
133 134
		return getDataSource().getUsername();
134 135
	}
135 136

  
136
    //  returns the AbstractDriverBasedDataSource from hibernate 
137
	// (generalized in order to also allow using SimpleDriverDataSource)
137
	/**
138
	 * Returns the AbstractDriverBasedDataSource from hibernate,
139
	 * generalized in order to also allow using SimpleDriverDataSource.
140
	 * 
141
	 * @return the AbstractDriverBasedDataSource from the hibernate layer 
142
	 */
138 143
	private AbstractDriverBasedDataSource getDataSource(){
139 144
		AbstractDriverBasedDataSource ds = (AbstractDriverBasedDataSource)SessionFactoryUtils.getDataSource(factory);
140 145
		return ds;
141 146
	}
142 147

  
143 148

  
149
	/* (non-Javadoc)
150
	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
151
	 */
144 152
	public void setApplicationContext(ApplicationContext applicationContext)
145 153
			throws BeansException {
146 154
		this.appContext = applicationContext;
147 155
	}
156
	
157
	/* (non-Javadoc)
158
	 * @see eu.etaxonomy.cdm.api.service.IDatabaseService#getDbSchemaVersion()
159
	 */
160
	@Override
161
	public  String getDbSchemaVersion() throws CdmSourceException  {		
162
		try {
163
			return (String)getSingleValue(MetaDataPropertyName.DB_SCHEMA_VERSION.getSqlQuery());
164
		} catch (SQLException e) {
165
			throw new CdmSourceException(e.getMessage());	
166
		}
167
	}
168
	
169
    
170
	/* (non-Javadoc)
171
	 * @see eu.etaxonomy.cdm.api.service.IDatabaseService#isDbEmpty()
172
	 */
173
	@Override
174
	public boolean isDbEmpty() throws CdmSourceException {
175
		// Any CDM DB should have a schema version
176
		String dbSchemaVersion = (String) getDbSchemaVersion();		
177
		return (dbSchemaVersion == null || dbSchemaVersion.equals(""));
178
	}
179
	
180
    /**
181
     * Execute a SQL query which returns a single value
182
     * 
183
     * @param query , which returns a single value
184
     * @return
185
     * @throws SQLException
186
     */
187
    private Object getSingleValue(String query) throws SQLException{
188
        String queryString = query == null? "(null)": query;
189
        ResultSet resultSet = executeQuery(query);
190
        if (resultSet == null || resultSet.next() == false){
191
            logger.info("No record returned for query " +  queryString);
192
            return null;
193
        }
194
        if (resultSet.getMetaData().getColumnCount() != 1){
195
            logger.info("More than one column selected in query" +  queryString);
196
            //first value will be taken
197
        }
198
        Object object = resultSet.getObject(1);
199
        if (resultSet.next()){
200
            logger.info("Multiple results for query " +  queryString);
201
            //first row will be taken
202
        }
203
        return object;
204
    }
205
    
206
    /**
207
     * Executes a query and returns the ResultSet.
208
     * 
209
     * @return ResultSet for the query.
210
     * @throws SQLException
211
     */
212
    
213
    private ResultSet executeQuery (String query) throws SQLException {
214

  
215
        ResultSet resultSet;
216

  
217
        if (query == null){
218
            return null;
219
        }
220
        
221
        Connection connection = SessionFactoryUtils.getDataSource(factory).getConnection();
222
        if (connection != null){
223
            Statement statement = connection.createStatement();
224
            resultSet = statement.executeQuery(query);
225
        }else{
226
            throw new RuntimeException("Could not establish connection to database");
227
        }
228
        return resultSet;
229

  
230
    }
148 231
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IDatabaseService.java
11 11
package eu.etaxonomy.cdm.api.service;
12 12

  
13 13
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
14
import eu.etaxonomy.cdm.config.CdmSourceException;
14 15
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
15 16
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
16 17
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
......
20 21
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
21 22

  
22 23
/**
24
 * Service interface which provides functionality to directly access database 
25
 * related information.
26
 * 
23 27
 * @author a.mueller
24 28
 *
25 29
 */
26 30
public interface IDatabaseService {
27

  
28
//TODO removed 04.02.2009 as spring 2.5.6 does not support DriverManagerDataSource.getDriverClassName anymore
29
//Let's see if this is not needed by any other application1
30
//	/**
31
//	 * Returns the databaseTypeEnum
32
//	 * @return
33
//	 */
34
//	public DatabaseTypeEnum getDatabaseEnum();
35
//	
36
//	
37
//	/**
38
//	 * Returns the database driver class name
39
//	 * @return
40
//	 */
41
//	public String getDriverClassName();
42 31
	
43 32
	/**
44 33
	 * Returns the database URL
......
104 93
	 */
105 94
	public CdmPersistentDataSource updateDataSource(String strDataSourceName, CdmPersistentDataSource dataSource) throws DataSourceNotFoundException;
106 95

  
96
	/**
97
	 * @param cdmApplicationController
98
	 */
107 99
	public void setApplicationController(CdmApplicationController cdmApplicationController);
100
	
101
	/**
102
	 * Returns the CDM model schema version number
103
	 * 
104
	 * @return the CDM model schema version number
105
	 * @throws CdmSourceException , incase of an underlying SQL error
106
	 */
107
	public  String getDbSchemaVersion() throws CdmSourceException;
108
	
109
	/**
110
	 * Returns a boolean flag to indicate whether the database is empty
111
	 * 
112
	 * @return boolean flag to indicate whether the database is  empty
113
	 * @throws CdmSourceException , incase of an underlying SQL error
114
	 */
115
	public boolean isDbEmpty() throws CdmSourceException;
108 116
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)