Salome HOME
Refactoring of Database, replacing SQL by DAOs calls. Methods for search by criteria...
[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 org.hibernate.Session;
9 import org.hibernate.SessionFactory;
10 import org.springframework.beans.BeansException;
11 import org.springframework.context.ApplicationContext;
12 import org.springframework.context.ApplicationContextAware;
13 import org.apache.log4j.Logger;
14
15
16 public abstract class Database implements ApplicationContextAware {
17
18         /**
19          * The ApplicationContext.
20          */
21         private static ApplicationContext _context = null;
22
23         /**
24          * Spring will call this method for initialize the applicationContext.
25          * @param ctx the application context
26          * @throws BeansException the BeanException
27          */
28         public void setApplicationContext(final ApplicationContext ctx) throws BeansException {
29                 _context = ctx;
30         }
31
32         /**
33          * Static for getting the context.
34          * @return ApplicationContext the application context
35          */
36         public static ApplicationContext getContext() {
37                 return _context;
38         }
39         
40 //      private   static  String          CONFIG_FILE      = "/hibernate.cfg.xml";
41     private   static  SessionFactory  mySessionFactory = null;
42
43     protected final static Logger   logger = Logger.getLogger(Database.class);
44     
45 //  ==============================================================================================================================
46 //  Public services
47 //  ==============================================================================================================================
48
49     public static Session getSession () {
50 //  -----------------------------------         
51       return getInstance().getCurrentSession();
52         }
53         
54 //  ==============================================================================================================================
55 //  Protected services
56 //  ==============================================================================================================================
57
58     protected String getSchemaVersion () {
59 //  ------------------------------------
60       return null;//TODO: Get schema version into specific object/table: getIDPool().getSchemaVersion();
61     }
62
63     protected void setSchemaVersion (String version) {
64 //  ------------------------------------------------
65 //TODO: Set schema version into specific object/table:      myIDpool = new IDPool(version);
66 //      getSession().save(myIDpool);
67     }
68
69 //  ==============================================================================================================================
70 //  Private services
71 //  ==============================================================================================================================
72         
73     private static SessionFactory getInstance () {
74 //  --------------------------------------------
75       if (mySessionFactory == null) {
76 //      org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
77         try {
78                 mySessionFactory = getContext().getBean(SessionFactory.class);
79 //          cfg.configure();   // The configuration file (hibernate.cfg.xml)) is supposed to be on the classpath
80 //          mySessionFactory = cfg.buildSessionFactory();
81         }
82         catch (Exception error) {
83           logger.fatal("Could not initialize the Hibernate configuration, reason:", error);
84         }
85       }
86       return mySessionFactory;
87     }
88 }