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