]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/dal/dao/som/Database.java
Salome HOME
Refactoring of Database, replacing SQL by DAOs calls. Methods for search by criteria...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / dao / som / Database.java
1 package org.splat.dal.dao.som;
2
3 /**
4  * 
5  * @author    Daniel Brunier-Coulin
6  * @copyright OPEN CASCADE 2012
7  */
8
9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 import java.util.List;
12 import java.util.Properties;
13 import java.io.File;
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;
19
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;
25
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;
35
36 public class Database extends org.splat.dal.dao.kernel.Database {
37
38         private int uplevel = 0; // Level of database upgrade
39         private RepositoryService _repositoryService;
40         private IndexService _indexService;
41         private SessionFactory _sessionFactory;
42
43         private static Database my = null; // Singleton instance
44
45         /**
46          * The job-class for database schema version ckeck.
47          * @author dbc
48          *
49          */
50         protected class CheckVersion implements Work {
51                 /** 
52                  * {@inheritDoc}
53                  * @see org.hibernate.jdbc.Work#execute(java.sql.Connection)
54                  */
55                 public void execute(Connection connex) throws SQLException {
56                         DatabaseMetaData dbmdata = connex.getMetaData();
57                         String dbname = connex.getCatalog();
58                         ResultSet table;
59
60                         table = dbmdata.getTables(dbname, null, "study", null);
61                         if (table.next())
62                                 return;
63                         uplevel = -1; // Database not initialized
64                 }
65         }
66
67         public final static Logger logger = org.splat.dal.dao.kernel.Database.logger;
68
69         // ==============================================================================================================================
70         // Construction
71         // ==============================================================================================================================
72
73         public Database getMe() {
74                 // -------------------------------
75                 if (my == null)
76                         try {
77                                 my = this;
78                                 my.checkVersion();
79                         } catch (Exception error) {
80                                 logger.fatal("Could not access the database, reason:", error);
81                         }
82                 return my;
83         }
84
85         private Database() {
86         }
87
88         /**
89          * Check version of the database schema.
90          */
91         private void checkVersion() {
92                 getSessionFactory().getCurrentSession().doWork(new CheckVersion());
93         }
94
95         // ==============================================================================================================================
96         // Public member functions
97         // ==============================================================================================================================
98
99         public boolean isInitialized() {
100                 // -------------------------------
101                 return (uplevel >= 0);
102         }
103
104         public void initialize() throws IOException, SQLException {
105                 // -------------------------
106                 logger.info("Creation of the database.");
107
108                 // Creation of the Lucene index
109                 getIndexService().create(); // May throw IOException if the index repository is improperly configured
110
111                 // Creation of the SIMER SQL tables
112                 Session session = Database.getSession();
113
114                 // Population of the database with customized data
115                 this.populate();
116
117                 session.flush();
118                 uplevel = 0; // The database is now up-to-date
119         }
120
121         // ==============================================================================================================================
122         // Protected member functions
123         // ==============================================================================================================================
124
125         public void configure(Properties reprop) throws IOException {
126                 // --------------------------------------------
127                 String basepath = reprop.getProperty("repository");
128                 getRepositoryService().setBasepath(basepath);
129                 getIndexService().configure();
130         }
131
132         protected void populate() {
133                 // --------------------------
134                 try {
135                         // Initialization of the schema version
136                         this.setSchemaVersion("D0.3"); // TODO: Get the version name from the configuration file
137
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...
149                 }
150         }
151
152         // ==============================================================================================================================
153         // Public services
154         // ==============================================================================================================================
155
156         public static String getRepositoryVaultPath() {
157                 return my.getRepositoryService().getRepositoryVaultPath();
158         }
159
160         /**
161          * @return
162          */
163         public IndexService getIndexService() {
164                 return _indexService;
165         }
166
167         /**
168          * @return
169          */
170         public RepositoryService getRepositoryService() {
171                 return _repositoryService;
172         }
173
174         public void setRepositoryService(RepositoryService repositoryService) {
175                 _repositoryService = repositoryService;
176         }
177
178         public void setIndexService(IndexService indexService) {
179                 _indexService = indexService;
180         }
181
182         /**
183          * Get the sessionFactory.
184          * 
185          * @return the sessionFactory
186          */
187         public SessionFactory getSessionFactory() {
188                 return _sessionFactory;
189         }
190
191         /**
192          * Set the sessionFactory.
193          * 
194          * @param sessionFactory
195          *            the sessionFactory to set
196          */
197         public void setSessionFactory(SessionFactory sessionFactory) {
198                 _sessionFactory = sessionFactory;
199         }
200 }