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.util.Properties;
16 import org.apache.log4j.Logger;
17 import org.hibernate.Session;
18 import org.hibernate.jdbc.Work;
19 import org.splat.dal.bo.kernel.User;
20 import org.splat.service.UserService;
21 import org.splat.service.technical.IndexService;
22 import org.splat.service.technical.RepositoryService;
23 import org.springframework.transaction.annotation.Transactional;
25 public class Database extends org.splat.dal.dao.kernel.AbstractDatabase {
27 public final static Logger LOG = org.splat.dal.dao.som.Database.LOG;
29 private transient int _uplevel = 0; // Level of database upgrade
30 private RepositoryService _repositoryService;
31 private IndexService _indexService;
32 private UserService _userService;
35 private static Database my = null; // Singleton instance
38 * The job-class for database schema version ckeck.
42 protected class CheckVersion implements Work {
45 * @throws SQLException
46 * @see org.hibernate.jdbc.Work#execute(java.sql.Connection)
48 public void execute(final Connection connex) throws SQLException {
49 DatabaseMetaData dbmdata = connex.getMetaData();
50 String dbname = connex.getCatalog();
51 ResultSet table = null;
54 table = dbmdata.getTables(dbname, null, "study", null);
58 } catch (SQLException e) {
59 LOG.debug("Can't check the database version: ", e);
66 _uplevel = -1; // Database not initialized
70 // ==============================================================================================================================
72 // ==============================================================================================================================
74 private static Database myDB = new Database();
76 public static Database getInstance() {
80 public static Session getCurSession() {
81 return getInstance().getSession();
84 public Database getCheckedDB() {
89 } catch (Exception error) {
90 LOG.fatal("Could not access the database, reason:", error);
100 * Check version of the database schema.
102 @Transactional(readOnly=true)
103 private void checkVersion() {
104 getSession().doWork(new CheckVersion());
107 // ==============================================================================================================================
108 // Public member functions
109 // ==============================================================================================================================
111 public boolean isInitialized() {
112 return (_uplevel >= 0);
115 public void initialize() throws IOException, SQLException {
116 LOG.info("Creation of the database.");
118 // Creation of the Lucene index
119 getIndexService().create(); // May throw IOException if the index repository is improperly configured
121 // Creation of the SIMER SQL tables
123 // Population of the database with customized data
126 _uplevel = 0; // The database is now up-to-date
129 // ==============================================================================================================================
130 // Protected member functions
131 // ==============================================================================================================================
133 public void configure(final Properties reprop) throws IOException {
134 String basepath = reprop.getProperty("repository");
135 getRepositoryService().setBasepath(basepath);
136 getIndexService().configure();
139 protected void populate() {
141 // Initialization of the schema version
142 this.setSchemaVersion("D0.3"); // TODO: Get the version name from the configuration file
144 // Creation of the default system administrator
145 // TODO: Get the username password from the Hibernate configuration
146 User.Properties uprop = new User.Properties();
147 uprop.setUsername("simer").setPassword("admin").setName(
148 "Simulation").setFirstName("Manager").setDisplayName(
149 "label.sysadmin").addRole("sysadmin").setMailAddress(
150 "noreply@salome-platform.org");
151 uprop.disableCheck();
152 getUserService().createUser(uprop);
153 } catch (Exception e) {
154 // Let's continue, hoping the best...
155 LOG.debug(e.getMessage(), e);
159 // ==============================================================================================================================
161 // ==============================================================================================================================
163 public static String getRepositoryVaultPath() {
164 return my.getRepositoryService().getRepositoryVaultPath();
170 public IndexService getIndexService() {
171 return _indexService;
177 public RepositoryService getRepositoryService() {
178 return _repositoryService;
181 public void setRepositoryService(final RepositoryService repositoryService) {
182 _repositoryService = repositoryService;
185 public void setIndexService(final IndexService indexService) {
186 _indexService = indexService;
190 * Get the userService.
191 * @return the userService
193 public UserService getUserService() {
198 * Set the userService.
199 * @param userService the userService to set
201 public void setUserService(final UserService userService) {
202 _userService = userService;