Salome HOME
91f4ce1820394de19d1250f7705cc91ed634b790
[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.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 initialize() throws IOException, SQLException {
130                 LOG.info("Creation of the database.");
131
132                 // Creation of the Lucene index
133                 getIndexService().create(); // May throw IOException if the index repository is improperly configured
134
135                 // Creation of the SIMER SQL tables
136
137                 // Population of the database with customized data
138                 this.populate();
139
140                 _uplevel = 0; // The database is now up-to-date
141         }
142
143         // ==============================================================================================================================
144         // Protected member functions
145         // ==============================================================================================================================
146
147         public void configure(final Properties reprop) throws IOException {
148                 String basepath = reprop.getProperty("repository");
149                 getRepositoryService().setBasepath(basepath);
150                 getIndexService().configure();
151         }
152
153         protected void populate() {
154                 try {
155                         // Initialization of the schema version
156                         this.setSchemaVersion("D0.3"); // TODO: Get the version name from the configuration file
157
158                         // Creation of the default system administrator
159                         // TODO: Get the username password from the Hibernate configuration
160                         User.Properties uprop = new User.Properties();
161                         uprop.setUsername("simer").setPassword("admin").setName(
162                                         "Simulation").setFirstName("Manager").setDisplayName(
163                                         "label.sysadmin").addRole("sysadmin").setMailAddress(
164                                         "noreply@salome-platform.org");
165                         uprop.disableCheck();
166                         getUserService().createUser(uprop);
167                 } catch (Exception e) {
168                         // Let's continue, hoping the best...
169                         LOG.debug(e.getMessage(), e);
170                 }
171         }
172
173         // ==============================================================================================================================
174         // Public services
175         // ==============================================================================================================================
176
177         public static String getRepositoryVaultPath() {
178                 return my.getRepositoryService().getRepositoryVaultPath();
179         }
180
181         /**
182          * @return
183          */
184         public IndexService getIndexService() {
185                 return _indexService;
186         }
187
188         /**
189          * @return
190          */
191         public RepositoryService getRepositoryService() {
192                 return _repositoryService;
193         }
194
195         public void setRepositoryService(final RepositoryService repositoryService) {
196                 _repositoryService = repositoryService;
197         }
198
199         public void setIndexService(final IndexService indexService) {
200                 _indexService = indexService;
201         }
202
203         /**
204          * Get the userService.
205          * @return the userService
206          */
207         public UserService getUserService() {
208                 return _userService;
209         }
210
211         /**
212          * Set the userService.
213          * @param userService the userService to set
214          */
215         public void setUserService(final UserService userService) {
216                 _userService = userService;
217         }
218 }