Salome HOME
f4772060f72bdb6d9c988675b8ec4739f3609e14
[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-2014
7  */
8
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;
16
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;
25
26 public class Database extends org.splat.dal.dao.kernel.AbstractDatabase {
27
28         public final static Logger LOG = Logger.getLogger(org.splat.dal.dao.som.Database.class);
29
30         private transient int _uplevel = 0; // Level of database upgrade
31         private RepositoryService _repositoryService;
32         private IndexService _indexService;
33         private UserService _userService;
34         
35
36         private static Database my = null; // Singleton instance
37
38         /**
39          * The job-class for database schema version ckeck.
40          * @author dbc
41          *
42          */
43         protected class CheckVersion implements Work {
44                 /** 
45                  * {@inheritDoc}
46                  * @throws SQLException 
47                  * @see org.hibernate.jdbc.Work#execute(java.sql.Connection)
48                  */
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
55
56                         try {
57                                 table = dbmdata.getTables(dbname, null, "study", null);
58                                 if (table.next()) {
59                                         table = dbmdata.getTables(dbname, null, "doctype", null);
60                                         if (table.next()) {
61                                                 if (st.execute("SELECT name FROM doctype") &&
62                                                         st.getResultSet().next() ) {
63                                                         _uplevel = 0;
64                                                 }
65                                         }
66                                 }
67                         } catch (SQLException e) {
68                                 LOG.debug("Can't check the database version: ", e);
69                                 throw e;
70                         } finally {
71                                 if (table != null) {
72                                         table.close();
73                                 }
74                                 if (st != null) {
75                                         st.close();
76                                 }
77                         }
78                 }
79         }
80
81         // ==============================================================================================================================
82         // Construction
83         // ==============================================================================================================================
84
85         private static Database myDB = new Database();
86
87         public static Database getInstance() {
88                 return myDB; 
89         }
90         
91         public static Session getCurSession() {
92                 return getInstance().getSession();
93         }
94
95         public Database getCheckedDB() {
96                 if (my == null) {
97                         try {
98                                 my = this;
99                                 my.checkVersion();
100                         } catch (Exception error) {
101                                 LOG.fatal("Could not access the database, reason:", error);
102                         }
103                 }
104                 return my;
105         }
106
107         private Database() {
108         }
109
110         /**
111          * Check version of the database schema.
112          */
113         @Transactional(readOnly=true)
114         private void checkVersion() {
115                 getSession().doWork(new CheckVersion());
116         }
117
118         // ==============================================================================================================================
119         // Public member functions
120         // ==============================================================================================================================
121
122         public boolean isInitialized() {
123                 if (LOG.isDebugEnabled()) {
124                         LOG.debug("The database is initialized: " + (_uplevel >= 0));
125                 }
126                 return (_uplevel >= 0);
127         }
128
129         public void reset() {
130                 _uplevel = -1;
131         }
132         
133         public void initialize() throws IOException, SQLException {
134                 LOG.info("Creation of the database.");
135
136                 // Creation of the Lucene index
137                 getIndexService().create(); // May throw IOException if the index repository is improperly configured
138
139                 // Creation of the SIMER SQL tables
140
141                 // Population of the database with customized data
142                 this.populate();
143
144                 _uplevel = 0; // The database is now up-to-date
145         }
146
147         // ==============================================================================================================================
148         // Protected member functions
149         // ==============================================================================================================================
150
151         public void configure(final Properties reprop) throws IOException {
152                 String basepath = reprop.getProperty("repository");
153                 getRepositoryService().setBasepath(basepath);
154                 getIndexService().configure();
155         }
156
157         protected void populate() {
158                 try {
159                         // Initialization of the schema version
160                         this.setSchemaVersion("D0.3"); // TODO: Get the version name from the configuration file
161
162                         // Creation of the default system administrator
163                         // TODO: Get the username password from the Hibernate configuration
164                         User.Properties uprop = new User.Properties();
165                         uprop.setUsername("simer").setPassword("admin").setName(
166                                         "Simulation").setFirstName("Manager").setDisplayName(
167                                         "label.sysadmin").addRole("sysadmin").setMailAddress(
168                                         "noreply@salome-platform.org");
169                         uprop.disableCheck();
170                         getUserService().createUser(uprop);
171                 } catch (Exception e) {
172                         // Let's continue, hoping the best...
173                         LOG.debug(e.getMessage(), e);
174                 }
175         }
176
177         // ==============================================================================================================================
178         // Public services
179         // ==============================================================================================================================
180
181         public static String getRepositoryVaultPath() {
182                 return my.getRepositoryService().getRepositoryVaultPath();
183         }
184
185         /**
186          * @return
187          */
188         public IndexService getIndexService() {
189                 return _indexService;
190         }
191
192         /**
193          * @return
194          */
195         public RepositoryService getRepositoryService() {
196                 return _repositoryService;
197         }
198
199         public void setRepositoryService(final RepositoryService repositoryService) {
200                 _repositoryService = repositoryService;
201         }
202
203         public void setIndexService(final IndexService indexService) {
204                 _indexService = indexService;
205         }
206
207         /**
208          * Get the userService.
209          * @return the userService
210          */
211         public UserService getUserService() {
212                 return _userService;
213         }
214
215         /**
216          * Set the userService.
217          * @param userService the userService to set
218          */
219         public void setUserService(final UserService userService) {
220                 _userService = userService;
221         }
222 }