Salome HOME
Refactoring continues: UserService is created instead of UserDirectory. Database...
[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.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;
15
16 import org.hibernate.Session;
17 import org.hibernate.jdbc.Work;
18 import org.apache.log4j.Logger;
19
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.Database {
27
28         private int uplevel = 0; // Level of database upgrade
29         private RepositoryService _repositoryService;
30         private IndexService _indexService;
31         private UserService _userService;
32         
33
34         private static Database my = null; // Singleton instance
35
36         /**
37          * The job-class for database schema version ckeck.
38          * @author dbc
39          *
40          */
41         protected class CheckVersion implements Work {
42                 /** 
43                  * {@inheritDoc}
44                  * @see org.hibernate.jdbc.Work#execute(java.sql.Connection)
45                  */
46                 public void execute(Connection connex) throws SQLException {
47                         DatabaseMetaData dbmdata = connex.getMetaData();
48                         String dbname = connex.getCatalog();
49                         ResultSet table;
50
51                         table = dbmdata.getTables(dbname, null, "study", null);
52                         if (table.next())
53                                 return;
54                         uplevel = -1; // Database not initialized
55                 }
56         }
57
58         public final static Logger logger = org.splat.dal.dao.kernel.Database.logger;
59
60         // ==============================================================================================================================
61         // Construction
62         // ==============================================================================================================================
63
64         private static Database myDB;
65
66         public static Database getInstance() {
67                 if (myDB == null) {
68                         myDB = new Database();
69                 }
70                 return myDB; 
71         }
72         
73         public static Session getCurSession() {
74                 // -----------------------------------
75                 return getInstance().getSession();
76         }
77
78         public Database getCheckedDB() {
79                 // -------------------------------
80                 if (my == null)
81                         try {
82                                 my = this;
83                                 my.checkVersion();
84                         } catch (Exception error) {
85                                 logger.fatal("Could not access the database, reason:", error);
86                         }
87                 return my;
88         }
89
90         private Database() {
91         }
92
93         /**
94          * Check version of the database schema.
95          */
96         @Transactional(readOnly=true)
97         private void checkVersion() {
98                 getSession().doWork(new CheckVersion());
99         }
100
101         // ==============================================================================================================================
102         // Public member functions
103         // ==============================================================================================================================
104
105         public boolean isInitialized() {
106                 // -------------------------------
107                 return (uplevel >= 0);
108         }
109
110         public void initialize() throws IOException, SQLException {
111                 // -------------------------
112                 logger.info("Creation of the database.");
113
114                 // Creation of the Lucene index
115                 getIndexService().create(); // May throw IOException if the index repository is improperly configured
116
117                 // Creation of the SIMER SQL tables
118
119                 // Population of the database with customized data
120                 this.populate();
121
122                 uplevel = 0; // The database is now up-to-date
123         }
124
125         // ==============================================================================================================================
126         // Protected member functions
127         // ==============================================================================================================================
128
129         public void configure(Properties reprop) throws IOException {
130                 // --------------------------------------------
131                 String basepath = reprop.getProperty("repository");
132                 getRepositoryService().setBasepath(basepath);
133                 getIndexService().configure();
134         }
135
136         protected void populate() {
137                 // --------------------------
138                 try {
139                         // Initialization of the schema version
140                         this.setSchemaVersion("D0.3"); // TODO: Get the version name from the configuration file
141
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...
153                 }
154         }
155
156         // ==============================================================================================================================
157         // Public services
158         // ==============================================================================================================================
159
160         public static String getRepositoryVaultPath() {
161                 return my.getRepositoryService().getRepositoryVaultPath();
162         }
163
164         /**
165          * @return
166          */
167         public IndexService getIndexService() {
168                 return _indexService;
169         }
170
171         /**
172          * @return
173          */
174         public RepositoryService getRepositoryService() {
175                 return _repositoryService;
176         }
177
178         public void setRepositoryService(RepositoryService repositoryService) {
179                 _repositoryService = repositoryService;
180         }
181
182         public void setIndexService(IndexService indexService) {
183                 _indexService = indexService;
184         }
185
186         /**
187          * Get the userService.
188          * @return the userService
189          */
190         public UserService getUserService() {
191                 return _userService;
192         }
193
194         /**
195          * Set the userService.
196          * @param userService the userService to set
197          */
198         public void setUserService(UserService userService) {
199                 _userService = userService;
200         }
201 }