package de.fau.osr.core.db;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * This class Create the SessionFactory from hibernate.cfg.xml
 * @author Gayathery
 *
 */
public class HibernateUtil {

    static Logger logger = LoggerFactory.getLogger(HibernateUtil.class);

    private static SessionFactory sessionFactory ;
   
    static {
        logger.debug("Static call : getsession factory() start::");

        Configuration configuration = new Configuration().configure();
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
        sessionFactory = configuration.buildSessionFactory(builder.build());
        
        logger.debug("Static call : getsession factory() end::");
    }
  
    public static SessionFactory getSessionFactory() {
       return sessionFactory;
   }
   
    public static void shutdown() {
        getSessionFactory().close();
    }

    public static Session getSession() {
        return sessionFactory.openSession();
    }

    public void closeSession(Session session) {
        session.clear();
        session.close();
    }

}

@@ -22,16 +22,62 @@
 re.db;%0A%0A
+import de.fau.osr.core.db.domain.Requirement;%0A
 import o
@@ -300,16 +300,65 @@
 ctory;%0A%0A
+import java.util.HashSet;%0Aimport java.util.Set;%0A%0A
 %0A/**%0A * 
@@ -600,20 +600,16 @@
 ctory ;%0A
-   %0A
     stat
@@ -690,17 +690,28 @@
         
-C
+//generate c
 onfigura
@@ -714,18 +714,26 @@
 guration
- c
+%0A        C
 onfigura
@@ -741,15 +741,9 @@
 ion 
-= new C
+c
 onfi
@@ -754,24 +754,32 @@
 tion
-().c
+ = getC
 onfigur
-e
+ation
 ();%0A
+%0A
     
@@ -1068,374 +1068,1566 @@
   %7D%0A
+%0A
   
-%0A    public static SessionFactory getSessionFactory() %7B%0A       return sessionFactory;%0A   %7D%0A   %0A    public static void shutdown() %7B%0A        getSessionFactory().close();%0A    %7D%0A%0A    public static Session getSession() %7B%0A        return sessionFactory.openSession();%0A    %7D%0A%0A    public void closeSession(Session session) %7B%0A        session.clear();%0A        session.close()
+  /**%0A     * generates configuration from hibernate.cfg.xml and adds all persistent classes to it%0A     * @return generated configuration%0A     */%0A    public static Configuration getConfiguration() %7B%0A        Configuration configuration = new Configuration().configure();%0A        //add persistent classes%0A        for(Class%3C?%3E clazz : getPersistentClasses())%0A        %7B%0A            configuration.addAnnotatedClass(clazz);%0A        %7D%0A        return configuration;%0A    %7D%0A%0A    public static SessionFactory getSessionFactory() %7B%0A       return sessionFactory;%0A   %7D%0A   %0A    public static void shutdown() %7B%0A        getSessionFactory().close();%0A    %7D%0A%0A    public static Session getSession() %7B%0A        return sessionFactory.openSession();%0A    %7D%0A%0A    public void closeSession(Session session) %7B%0A        session.clear();%0A        session.close();%0A    %7D%0A%0A    /**%0A     * generates persistent classes set, for now hardcoded.%0A     * todo use reflection, or spring%0A     * @see %3Ca href=%22http://stackoverflow.com/questions/1413190/hibernate-mapping-package%22%3Ehttp://stackoverflow.com/questions/1413190/hibernate-mapping-package%3C/a%3E%0A     * @return set of classes are persistent%0A     */%0A    static public Set%3CClass%3C?%3E%3E getPersistentClasses()%7B%0A        //reflection way%0A//        Reflections reflections = new Reflections(%22your_package%22);%0A//        Set%3CClass%3C?%3E%3E classes = reflections.getTypesAnnotatedWith(javax.persistence.Entity.class);%0A%0A        //hardcoded way for now%0A        Set%3CClass%3C?%3E%3E classes = new HashSet%3C%3E();%0A        classes.add(Requirement.class);%0A%0A        return classes
 ;%0A  

refactor to make more useful Req-50