1 package org.splat.dal.dao.som;
5 * @author Daniel Brunier-Coulin
6 * @copyright OPEN CASCADE 2012
9 import java.util.Properties;
10 import java.io.IOException;
11 import java.sql.Connection;
12 import java.sql.DatabaseMetaData;
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
16 import org.hibernate.Session;
17 import org.hibernate.jdbc.Work;
18 import org.apache.log4j.Logger;
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.Database {
28 private int uplevel = 0; // Level of database upgrade
29 private RepositoryService _repositoryService;
30 private IndexService _indexService;
31 private UserService _userService;
34 private static Database my = null; // Singleton instance
37 * The job-class for database schema version ckeck.
41 protected class CheckVersion implements Work {
44 * @see org.hibernate.jdbc.Work#execute(java.sql.Connection)
46 public void execute(Connection connex) throws SQLException {
47 DatabaseMetaData dbmdata = connex.getMetaData();
48 String dbname = connex.getCatalog();
51 table = dbmdata.getTables(dbname, null, "study", null);
54 uplevel = -1; // Database not initialized
58 public final static Logger logger = org.splat.dal.dao.kernel.Database.logger;
60 // ==============================================================================================================================
62 // ==============================================================================================================================
64 private static Database myDB;
66 public static Database getInstance() {
68 myDB = new Database();
73 public static Session getCurSession() {
74 // -----------------------------------
75 return getInstance().getSession();
78 public Database getCheckedDB() {
79 // -------------------------------
84 } catch (Exception error) {
85 logger.fatal("Could not access the database, reason:", error);
94 * Check version of the database schema.
96 @Transactional(readOnly=true)
97 private void checkVersion() {
98 getSession().doWork(new CheckVersion());
101 // ==============================================================================================================================
102 // Public member functions
103 // ==============================================================================================================================
105 public boolean isInitialized() {
106 // -------------------------------
107 return (uplevel >= 0);
110 public void initialize() throws IOException, SQLException {
111 // -------------------------
112 logger.info("Creation of the database.");
114 // Creation of the Lucene index
115 getIndexService().create(); // May throw IOException if the index repository is improperly configured
117 // Creation of the SIMER SQL tables
119 // Population of the database with customized data
122 uplevel = 0; // The database is now up-to-date
125 // ==============================================================================================================================
126 // Protected member functions
127 // ==============================================================================================================================
129 public void configure(Properties reprop) throws IOException {
130 // --------------------------------------------
131 String basepath = reprop.getProperty("repository");
132 getRepositoryService().setBasepath(basepath);
133 getIndexService().configure();
136 protected void populate() {
137 // --------------------------
139 // Initialization of the schema version
140 this.setSchemaVersion("D0.3"); // TODO: Get the version name from the configuration file
142 // Creation of the default system administrator
143 // TODO: Get the username password from the Hibernate configuration
144 User.Properties uprop = new User.Properties();
145 uprop.setUsername("simer").setPassword("admin").setName(
146 "Simulation").setFirstName("Manager").setDisplayName(
147 "label.sysadmin").addRole("sysadmin").setMailAddress(
148 "noreply@salome-platform.org");
149 uprop.disableCheck();
150 getUserService().createUser(uprop);
151 } catch (Exception e) {
152 // Let's continue, hoping the best...
156 // ==============================================================================================================================
158 // ==============================================================================================================================
160 public static String getRepositoryVaultPath() {
161 return my.getRepositoryService().getRepositoryVaultPath();
167 public IndexService getIndexService() {
168 return _indexService;
174 public RepositoryService getRepositoryService() {
175 return _repositoryService;
178 public void setRepositoryService(RepositoryService repositoryService) {
179 _repositoryService = repositoryService;
182 public void setIndexService(IndexService indexService) {
183 _indexService = indexService;
187 * Get the userService.
188 * @return the userService
190 public UserService getUserService() {
195 * Set the userService.
196 * @param userService the userService to set
198 public void setUserService(UserService userService) {
199 _userService = userService;