Salome HOME
SIMAN Eclipse workspace first version
[tools/siman.git] / Workspace / SPlat / src / org / splat / kernel / Database.java
1 package org.splat.kernel;
2 /**
3  * 
4  * @author    Daniel Brunier-Coulin
5  * @copyright OPEN CASCADE 2012
6  */
7
8 import java.sql.Connection;
9 import java.sql.SQLException;
10 import java.sql.Statement;
11 import org.hibernate.Session;
12 import org.hibernate.SessionFactory;
13 import org.hibernate.jdbc.Work;
14 import org.apache.log4j.Logger;
15
16
17 public abstract class Database {
18
19 //      private   static  String          CONFIG_FILE      = "/hibernate.cfg.xml";
20     private   static  SessionFactory  mySessionFactory = null;
21     protected static  IDPool          myIDpool         = null;
22
23     protected class CreateTables    implements Work {
24 //  -----------------------------------------------
25       protected Statement request;
26       
27       public void execute(Connection connex) throws SQLException
28       {           
29         request = connex.createStatement();
30
31 //      Last identifier of Any objects
32         String create = "CREATE TABLE `any` (" +
33           "`rid`     int(10)  UNSIGNED NOT NULL," +
34           "`version` tinytext NOT NULL," +
35           "PRIMARY KEY (`rid`)" +
36           ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
37         request.execute(create);
38
39 //      Relation from entities
40         create = "CREATE TABLE `relation` (" +
41           "`rid`   int(10)  UNSIGNED NOT NULL," +
42           "`name`  tinytext NOT NULL," +
43           "`owner` int(10)  NOT NULL," +
44           "`refer` int(10)  NOT NULL," +
45           "PRIMARY KEY (`rid`)" +
46           ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
47         request.execute(create);
48
49 //      Attribute objects
50         create = "CREATE TABLE `attribute` (" +
51           "`rid`   int(10)  UNSIGNED NOT NULL auto_increment," +
52           "`type`  tinytext NOT NULL," +
53           "`owner` int(10)  NOT NULL," +
54           "`value` int(10)  NOT NULL," +
55           "PRIMARY KEY (`rid`)" +
56           ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
57         request.execute(create);
58
59 //      Java String objects
60         create = "CREATE TABLE `text` (" +
61           "`rid`   int(10)  UNSIGNED NOT NULL auto_increment," +
62           "`value` longtext NOT NULL," +
63           "PRIMARY KEY (`rid`)" +
64           ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
65         request.execute(create);
66
67 //      User and role objects
68         create = "CREATE TABLE `user` (" +
69           "`rid` int(10) UNSIGNED NOT NULL auto_increment," +
70           "`username` varchar(32) NOT NULL," +
71           "`password` varchar(32)," +
72           "`first`    tinytext NOT NULL," +
73           "`last`     tinytext NOT NULL," +
74           "`display`  tinytext," +
75           "`email`    tinytext," +
76           "`organid`  tinytext," +
77           "PRIMARY KEY (`rid`)" +
78           ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
79         request.execute(create);
80
81         create = "CREATE TABLE `role` (" +
82           "`username` varchar(32) NOT NULL," +
83           "`role`     varchar(32) NOT NULL," +
84           "PRIMARY KEY (`username`)" +
85           ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
86         request.execute(create);
87       }         
88     }
89
90     protected final static Logger   logger = Logger.getLogger(Database.class);
91     
92 //  ==============================================================================================================================
93 //  Public services
94 //  ==============================================================================================================================
95
96     public static Session getSession () {
97 //  -----------------------------------         
98       return getInstance().getCurrentSession();
99         }
100         
101         public static void close () {
102 //  ---------------------------         
103       if (mySessionFactory != null) mySessionFactory.close();
104       mySessionFactory = null;
105         }
106
107         public static IDPool getIDPool () {
108 //  ---------------------------------
109           if (myIDpool == null) myIDpool = new IDPool(getSession());
110           return myIDpool;
111         }
112
113 //  ==============================================================================================================================
114 //  Protected services
115 //  ==============================================================================================================================
116
117     protected String getSchemaVersion () {
118 //  ------------------------------------
119       return getIDPool().getSchemaVersion();
120     }
121
122         protected void setIDPoolSize (int size) {
123 //  ---------------------------------------
124           IDPool.setPoolSize(size);
125         }
126
127     protected void setSchemaVersion (String version) {
128 //  ------------------------------------------------
129       myIDpool = new IDPool(version);
130       getSession().save(myIDpool);
131     }
132
133 //  ==============================================================================================================================
134 //  Private services
135 //  ==============================================================================================================================
136         
137     private static SessionFactory getInstance () {
138 //  --------------------------------------------
139       if (mySessionFactory == null) {
140         org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
141         try {          
142           cfg.configure();   // The configuration file (hibernate.cfg.xml)) is supposed to be on the classpath
143           mySessionFactory = cfg.buildSessionFactory();
144         }
145         catch (Exception error) {
146           logger.fatal("Could not initialize the Hibernate configuration, reason:", error);
147         }
148       }
149       return mySessionFactory;
150     }
151 }