Salome HOME
Siman codebase is refactored. Spring beans are introduced in the context.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / dao / kernel / Database.java
1 package org.splat.dal.dao.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.springframework.beans.BeansException;
15 import org.springframework.context.ApplicationContext;
16 import org.springframework.context.ApplicationContextAware;
17 import org.apache.log4j.Logger;
18
19
20 public abstract class Database  implements ApplicationContextAware {
21
22         /**
23          * The ApplicationContext.
24          */
25         private static ApplicationContext _context = null;
26
27         /**
28          * Spring will call this method for initialize the applicationContext.
29          * @param ctx the application context
30          * @throws BeansException the BeanException
31          */
32         public void setApplicationContext(final ApplicationContext ctx) throws BeansException {
33                 _context = ctx;
34         }
35
36         /**
37          * Static for getting the context.
38          * @return ApplicationContext the application context
39          */
40         public static ApplicationContext getContext() {
41                 return _context;
42         }
43         
44 //      private   static  String          CONFIG_FILE      = "/hibernate.cfg.xml";
45     private   static  SessionFactory  mySessionFactory = null;
46
47     protected class CreateTables    implements Work {
48 //  -----------------------------------------------
49       protected Statement request;
50       
51       public void execute(Connection connex) throws SQLException
52       {           
53         request = connex.createStatement();
54
55 //      Last identifier of Any objects
56         String create = "CREATE TABLE `any` (" +
57           "`rid`     int(10)  UNSIGNED NOT NULL," +
58           "`version` tinytext NOT NULL," +
59           "PRIMARY KEY (`rid`)" +
60           ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
61         request.execute(create);
62
63 //      Relation from entities
64         create = "CREATE TABLE `relation` (" +
65           "`rid`   int(10)  UNSIGNED NOT NULL," +
66           "`name`  tinytext NOT NULL," +
67           "`owner` int(10)  NOT NULL," +
68           "`refer` int(10)  NOT NULL," +
69           "PRIMARY KEY (`rid`)" +
70           ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
71         request.execute(create);
72
73 //      Attribute objects
74         create = "CREATE TABLE `attribute` (" +
75           "`rid`   int(10)  UNSIGNED NOT NULL auto_increment," +
76           "`type`  tinytext NOT NULL," +
77           "`owner` int(10)  NOT NULL," +
78           "`value` int(10)  NOT NULL," +
79           "PRIMARY KEY (`rid`)" +
80           ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
81         request.execute(create);
82
83 //      Java String objects
84         create = "CREATE TABLE `text` (" +
85           "`rid`   int(10)  UNSIGNED NOT NULL auto_increment," +
86           "`value` longtext NOT NULL," +
87           "PRIMARY KEY (`rid`)" +
88           ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
89         request.execute(create);
90
91 //      User and role objects
92         create = "CREATE TABLE `user` (" +
93           "`rid` int(10) UNSIGNED NOT NULL auto_increment," +
94           "`username` varchar(32) NOT NULL," +
95           "`password` varchar(32)," +
96           "`first`    tinytext NOT NULL," +
97           "`last`     tinytext NOT NULL," +
98           "`display`  tinytext," +
99           "`email`    tinytext," +
100           "`organid`  tinytext," +
101           "PRIMARY KEY (`rid`)" +
102           ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
103         request.execute(create);
104
105         create = "CREATE TABLE `role` (" +
106           "`username` varchar(32) NOT NULL," +
107           "`role`     varchar(32) NOT NULL," +
108           "PRIMARY KEY (`username`)" +
109           ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
110         request.execute(create);
111       }         
112     }
113
114     protected final static Logger   logger = Logger.getLogger(Database.class);
115     
116 //  ==============================================================================================================================
117 //  Public services
118 //  ==============================================================================================================================
119
120     public static Session getSession () {
121 //  -----------------------------------         
122       return getInstance().getCurrentSession();
123         }
124         
125 //  ==============================================================================================================================
126 //  Protected services
127 //  ==============================================================================================================================
128
129     protected String getSchemaVersion () {
130 //  ------------------------------------
131       return null;//TODO: Get schema version into specific object/table: getIDPool().getSchemaVersion();
132     }
133
134     protected void setSchemaVersion (String version) {
135 //  ------------------------------------------------
136 //TODO: Set schema version into specific object/table:      myIDpool = new IDPool(version);
137 //      getSession().save(myIDpool);
138     }
139
140 //  ==============================================================================================================================
141 //  Private services
142 //  ==============================================================================================================================
143         
144     private static SessionFactory getInstance () {
145 //  --------------------------------------------
146       if (mySessionFactory == null) {
147 //      org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
148         try {
149                 mySessionFactory = getContext().getBean(SessionFactory.class);
150 //          cfg.configure();   // The configuration file (hibernate.cfg.xml)) is supposed to be on the classpath
151 //          mySessionFactory = cfg.buildSessionFactory();
152         }
153         catch (Exception error) {
154           logger.fatal("Could not initialize the Hibernate configuration, reason:", error);
155         }
156       }
157       return mySessionFactory;
158     }
159 }