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.springframework.orm.hibernate3.support.HibernateDaoSupport;
18 import org.apache.log4j.Logger;
21 public abstract class Database implements ApplicationContextAware {
24 * The ApplicationContext.
26 private static ApplicationContext _context = null;
29 * Spring will call this method for initialize the applicationContext.
30 * @param ctx the application context
31 * @throws BeansException the BeanException
33 public void setApplicationContext(final ApplicationContext ctx) throws BeansException {
38 * Static for getting the context.
39 * @return ApplicationContext the application context
41 public static ApplicationContext getContext() {
45 // private static String CONFIG_FILE = "/hibernate.cfg.xml";
46 private static SessionFactory mySessionFactory = null;
48 protected class CreateTables implements Work {
49 // -----------------------------------------------
50 protected Statement request;
52 public void execute(Connection connex) throws SQLException
54 request = connex.createStatement();
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);
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);
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);
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);
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);
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);
115 protected final static Logger logger = Logger.getLogger(Database.class);
117 // ==============================================================================================================================
119 // ==============================================================================================================================
121 public static Session getSession () {
122 // -----------------------------------
123 return getInstance().getCurrentSession();
126 // ==============================================================================================================================
127 // Protected services
128 // ==============================================================================================================================
130 protected String getSchemaVersion () {
131 // ------------------------------------
132 return null;//TODO: Get schema version into specific object/table: getIDPool().getSchemaVersion();
135 protected void setSchemaVersion (String version) {
136 // ------------------------------------------------
137 //TODO: Set schema version into specific object/table: myIDpool = new IDPool(version);
138 // getSession().save(myIDpool);
141 // ==============================================================================================================================
143 // ==============================================================================================================================
145 private static SessionFactory getInstance () {
146 // --------------------------------------------
147 if (mySessionFactory == null) {
148 // org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
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();
154 catch (Exception error) {
155 logger.fatal("Could not initialize the Hibernate configuration, reason:", error);
158 return mySessionFactory;