1 package org.splat.dal.dao.som;
5 * @author Daniel Brunier-Coulin
6 * @copyright OPEN CASCADE 2012
9 import java.io.IOException;
10 import java.sql.Connection;
11 import java.sql.DatabaseMetaData;
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.sql.Statement;
15 import java.util.Properties;
17 import org.apache.log4j.Logger;
18 import org.hibernate.Session;
19 import org.hibernate.jdbc.Work;
20 import org.splat.dal.bo.kernel.User;
21 import org.splat.service.UserService;
22 import org.splat.service.technical.IndexService;
23 import org.splat.service.technical.RepositoryService;
24 import org.springframework.transaction.annotation.Transactional;
26 public class Database extends org.splat.dal.dao.kernel.AbstractDatabase {
28 public final static Logger LOG = Logger.getLogger(org.splat.dal.dao.som.Database.class);
30 private transient int _uplevel = 0; // Level of database upgrade
31 private RepositoryService _repositoryService;
32 private IndexService _indexService;
33 private UserService _userService;
36 private static Database my = null; // Singleton instance
39 * The job-class for database schema version ckeck.
43 protected class CheckVersion implements Work {
46 * @throws SQLException
47 * @see org.hibernate.jdbc.Work#execute(java.sql.Connection)
49 public void execute(final Connection connex) throws SQLException {
50 DatabaseMetaData dbmdata = connex.getMetaData();
51 String dbname = connex.getCatalog();
52 ResultSet table = null;
53 Statement st = connex.createStatement();
54 _uplevel = -1; // Database not initialized
57 table = dbmdata.getTables(dbname, null, "study", null);
59 table = dbmdata.getTables(dbname, null, "doctype", null);
61 if (st.execute("SELECT name FROM doctype") &&
62 st.getResultSet().next() ) {
67 } catch (SQLException e) {
68 LOG.debug("Can't check the database version: ", e);
81 // ==============================================================================================================================
83 // ==============================================================================================================================
85 private static Database myDB = new Database();
87 public static Database getInstance() {
91 public static Session getCurSession() {
92 return getInstance().getSession();
95 public Database getCheckedDB() {
100 } catch (Exception error) {
101 LOG.fatal("Could not access the database, reason:", error);
111 * Check version of the database schema.
113 @Transactional(readOnly=true)
114 private void checkVersion() {
115 getSession().doWork(new CheckVersion());
118 // ==============================================================================================================================
119 // Public member functions
120 // ==============================================================================================================================
122 public boolean isInitialized() {
123 if (LOG.isDebugEnabled()) {
124 LOG.debug("The database is initialized: " + (_uplevel >= 0));
126 return (_uplevel >= 0);
129 public void reset() {
133 public void initialize() throws IOException, SQLException {
134 LOG.info("Creation of the database.");
136 // Creation of the Lucene index
137 getIndexService().create(); // May throw IOException if the index repository is improperly configured
139 // Creation of the SIMER SQL tables
141 // Population of the database with customized data
144 _uplevel = 0; // The database is now up-to-date
147 // ==============================================================================================================================
148 // Protected member functions
149 // ==============================================================================================================================
151 public void configure(final Properties reprop) throws IOException {
152 String basepath = reprop.getProperty("repository");
153 getRepositoryService().setBasepath(basepath);
154 getIndexService().configure();
157 protected void populate() {
159 // Initialization of the schema version
160 this.setSchemaVersion("D0.3"); // TODO: Get the version name from the configuration file
162 // Creation of the default system administrator
163 // TODO: Get the username password from the Hibernate configuration
164 User.Properties uprop = new User.Properties();
165 uprop.setUsername("simer").setPassword("admin").setName(
166 "Simulation").setFirstName("Manager").setDisplayName(
167 "label.sysadmin").addRole("sysadmin").setMailAddress(
168 "noreply@salome-platform.org");
169 uprop.disableCheck();
170 getUserService().createUser(uprop);
171 } catch (Exception e) {
172 // Let's continue, hoping the best...
173 LOG.debug(e.getMessage(), e);
177 // ==============================================================================================================================
179 // ==============================================================================================================================
181 public static String getRepositoryVaultPath() {
182 return my.getRepositoryService().getRepositoryVaultPath();
188 public IndexService getIndexService() {
189 return _indexService;
195 public RepositoryService getRepositoryService() {
196 return _repositoryService;
199 public void setRepositoryService(final RepositoryService repositoryService) {
200 _repositoryService = repositoryService;
203 public void setIndexService(final IndexService indexService) {
204 _indexService = indexService;
208 * Get the userService.
209 * @return the userService
211 public UserService getUserService() {
216 * Set the userService.
217 * @param userService the userService to set
219 public void setUserService(final UserService userService) {
220 _userService = userService;