1 package org.splat.dal.dao.kernel;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
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;
20 public abstract class Database implements ApplicationContextAware {
23 * The ApplicationContext.
25 private static ApplicationContext _context = null;
28 * Spring will call this method for initialize the applicationContext.
29 * @param ctx the application context
30 * @throws BeansException the BeanException
32 public void setApplicationContext(final ApplicationContext ctx) throws BeansException {
37 * Static for getting the context.
38 * @return ApplicationContext the application context
40 public static ApplicationContext getContext() {
44 // private static String CONFIG_FILE = "/hibernate.cfg.xml";
45 private static SessionFactory mySessionFactory = null;
47 protected class CreateTables implements Work {
48 // -----------------------------------------------
49 protected Statement request;
51 public void execute(Connection connex) throws SQLException
53 request = connex.createStatement();
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);
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);
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);
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);
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," +
100 "`organid` tinytext," +
101 "PRIMARY KEY (`rid`)" +
102 ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
103 request.execute(create);
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);
114 protected final static Logger logger = Logger.getLogger(Database.class);
116 // ==============================================================================================================================
118 // ==============================================================================================================================
120 public static Session getSession () {
121 // -----------------------------------
122 return getInstance().getCurrentSession();
125 // ==============================================================================================================================
126 // Protected services
127 // ==============================================================================================================================
129 protected String getSchemaVersion () {
130 // ------------------------------------
131 return null;//TODO: Get schema version into specific object/table: getIDPool().getSchemaVersion();
134 protected void setSchemaVersion (String version) {
135 // ------------------------------------------------
136 //TODO: Set schema version into specific object/table: myIDpool = new IDPool(version);
137 // getSession().save(myIDpool);
140 // ==============================================================================================================================
142 // ==============================================================================================================================
144 private static SessionFactory getInstance () {
145 // --------------------------------------------
146 if (mySessionFactory == null) {
147 // org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
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();
153 catch (Exception error) {
154 logger.fatal("Could not initialize the Hibernate configuration, reason:", error);
157 return mySessionFactory;