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