1 package org.splat.dal.dao.som;
5 * @author Daniel Brunier-Coulin
6 * @copyright OPEN CASCADE 2012
9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 import java.util.List;
12 import java.util.Properties;
14 import java.io.IOException;
15 import java.sql.Connection;
16 import java.sql.DatabaseMetaData;
17 import java.sql.ResultSet;
18 import java.sql.SQLException;
20 import org.hibernate.Query;
21 import org.hibernate.Session;
22 import org.hibernate.SessionFactory;
23 import org.hibernate.jdbc.Work;
24 import org.apache.log4j.Logger;
26 import org.splat.dal.bo.kernel.User;
27 import org.splat.dal.bo.som.IDBuilder;
28 import org.splat.dal.bo.som.ProgressState;
29 import org.splat.dal.bo.som.SimulationContext;
30 import org.splat.dal.bo.som.SimulationContextType;
31 import org.splat.kernel.UserDirectory;
32 import org.splat.kernel.InvalidPropertyException;
33 import org.splat.service.technical.IndexService;
34 import org.splat.service.technical.RepositoryService;
36 public class Database extends org.splat.dal.dao.kernel.Database {
38 private int uplevel = 0; // Level of database upgrade
39 private RepositoryService _repositoryService;
40 private IndexService _indexService;
41 private SessionFactory _sessionFactory;
43 private static Database my = null; // Singleton instance
46 * The job-class for database schema version ckeck.
50 protected class CheckVersion implements Work {
53 * @see org.hibernate.jdbc.Work#execute(java.sql.Connection)
55 public void execute(Connection connex) throws SQLException {
56 DatabaseMetaData dbmdata = connex.getMetaData();
57 String dbname = connex.getCatalog();
60 table = dbmdata.getTables(dbname, null, "study", null);
63 uplevel = -1; // Database not initialized
67 public final static Logger logger = org.splat.dal.dao.kernel.Database.logger;
69 // ==============================================================================================================================
71 // ==============================================================================================================================
73 public Database getMe() {
74 // -------------------------------
79 } catch (Exception error) {
80 logger.fatal("Could not access the database, reason:", error);
89 * Check version of the database schema.
91 private void checkVersion() {
92 getSessionFactory().getCurrentSession().doWork(new CheckVersion());
95 // ==============================================================================================================================
96 // Public member functions
97 // ==============================================================================================================================
99 public boolean isInitialized() {
100 // -------------------------------
101 return (uplevel >= 0);
104 public void initialize() throws IOException, SQLException {
105 // -------------------------
106 logger.info("Creation of the database.");
108 // Creation of the Lucene index
109 getIndexService().create(); // May throw IOException if the index repository is improperly configured
111 // Creation of the SIMER SQL tables
112 Session session = Database.getSession();
114 // Population of the database with customized data
118 uplevel = 0; // The database is now up-to-date
121 // ==============================================================================================================================
122 // Protected member functions
123 // ==============================================================================================================================
125 public void configure(Properties reprop) throws IOException {
126 // --------------------------------------------
127 String basepath = reprop.getProperty("repository");
128 getRepositoryService().setBasepath(basepath);
129 getIndexService().configure();
132 protected void populate() {
133 // --------------------------
135 // Initialization of the schema version
136 this.setSchemaVersion("D0.3"); // TODO: Get the version name from the configuration file
138 // Creation of the default system administrator
139 // TODO: Get the username password from the Hibernate configuration
140 User.Properties uprop = new User.Properties();
141 uprop.setUsername("simer").setPassword("admin").setName(
142 "Simulation").setFirstName("Manager").setDisplayName(
143 "label.sysadmin").addRole("sysadmin").setMailAddress(
144 "noreply@salome-platform.org");
145 uprop.disableCheck();
146 UserDirectory.createUser(uprop);
147 } catch (Exception e) {
148 // Let's continue, hoping the best...
152 // ==============================================================================================================================
154 // ==============================================================================================================================
156 public static String getRepositoryVaultPath() {
157 return my.getRepositoryService().getRepositoryVaultPath();
163 public IndexService getIndexService() {
164 return _indexService;
170 public RepositoryService getRepositoryService() {
171 return _repositoryService;
174 public void setRepositoryService(RepositoryService repositoryService) {
175 _repositoryService = repositoryService;
178 public void setIndexService(IndexService indexService) {
179 _indexService = indexService;
183 * Get the sessionFactory.
185 * @return the sessionFactory
187 public SessionFactory getSessionFactory() {
188 return _sessionFactory;
192 * Set the sessionFactory.
194 * @param sessionFactory
195 * the sessionFactory to set
197 public void setSessionFactory(SessionFactory sessionFactory) {
198 _sessionFactory = sessionFactory;