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 initialize() throws IOException, SQLException {
130 LOG.info("Creation of the database.");
132 // Creation of the Lucene index
133 getIndexService().create(); // May throw IOException if the index repository is improperly configured
135 // Creation of the SIMER SQL tables
137 // Population of the database with customized data
140 _uplevel = 0; // The database is now up-to-date
143 // ==============================================================================================================================
144 // Protected member functions
145 // ==============================================================================================================================
147 public void configure(final Properties reprop) throws IOException {
148 String basepath = reprop.getProperty("repository");
149 getRepositoryService().setBasepath(basepath);
150 getIndexService().configure();
153 protected void populate() {
155 // Initialization of the schema version
156 this.setSchemaVersion("D0.3"); // TODO: Get the version name from the configuration file
158 // Creation of the default system administrator
159 // TODO: Get the username password from the Hibernate configuration
160 User.Properties uprop = new User.Properties();
161 uprop.setUsername("simer").setPassword("admin").setName(
162 "Simulation").setFirstName("Manager").setDisplayName(
163 "label.sysadmin").addRole("sysadmin").setMailAddress(
164 "noreply@salome-platform.org");
165 uprop.disableCheck();
166 getUserService().createUser(uprop);
167 } catch (Exception e) {
168 // Let's continue, hoping the best...
169 LOG.debug(e.getMessage(), e);
173 // ==============================================================================================================================
175 // ==============================================================================================================================
177 public static String getRepositoryVaultPath() {
178 return my.getRepositoryService().getRepositoryVaultPath();
184 public IndexService getIndexService() {
185 return _indexService;
191 public RepositoryService getRepositoryService() {
192 return _repositoryService;
195 public void setRepositoryService(final RepositoryService repositoryService) {
196 _repositoryService = repositoryService;
199 public void setIndexService(final IndexService indexService) {
200 _indexService = indexService;
204 * Get the userService.
205 * @return the userService
207 public UserService getUserService() {
212 * Set the userService.
213 * @param userService the userService to set
215 public void setUserService(final UserService userService) {
216 _userService = userService;