/*
 * Copyright © 2013 Jarett Creason
 *
 * This file is part of BrewNinja.
 *
 * BrewNinja is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * BrewNinja is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with BrewNinja in the file named COPYING in the root directory.
 * If not, see <http://www.gnu.org/licenses/>.
 */

package com.europabrewing.util;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import java.util.List;
import java.util.Map;

/**
 * @author jcreason - [email protected]
 * @date July 2013
 *
 * Please see the README and/or documentation associated
 */
public class HibernateUtil {

	private static final SessionFactory sessionFactory = buildSessionFactory();

	/**
	 * Connect to the database and create a sessionFactory object
	 *
	 * @return the newly created session factory
	 */
	private static SessionFactory buildSessionFactory() {
		try {
			Configuration configuration = new Configuration();
			// avoid slow startup: http://stackoverflow.com/questions/10075081/hibernate-slow-to-acquire-postgres-connection
//			configuration.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
			configuration.configure();

			ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();

			return configuration.buildSessionFactory(serviceRegistry);

		} catch (Throwable ex) {
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	/**
	 * Return the SessionFactory object
	 *
	 * @return SessionFactory
	 */
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Return a newly created Session
	 *
	 * @return Session
	 * @throws org.hibernate.HibernateException
	 */
	public static Session getSession() throws HibernateException {
		return getSessionFactory().openSession();
	}

	/**
	 * Helper function to have one place to suppress warnings
	 *
	 * @param q
	 * @param <T>
	 * @return
	 */
	public static <T> List<T> listAndCast(Query q) {
		@SuppressWarnings("unchecked")
		List list = q.list();
		return list;
	}

	/**
	 * Print all data that is held in this database
	 *
	 * @throws org.hibernate.HibernateException
	 */
	public static void printAllData() throws HibernateException {
		Session session = getSession();

		try {
			System.out.println("querying all the managed entities...");

			final Map<String, ClassMetadata> metadataMap = session.getSessionFactory().getAllClassMetadata();

			for (String key : metadataMap.keySet()) {
				final ClassMetadata classMetadata = metadataMap.get(key);
				final String entityName = classMetadata.getEntityName();
				final Query query = session.createQuery("from " + entityName);

				System.out.println("executing: " + query.getQueryString());
				for (Object o : query.list()) {
					System.out.println("  " + o);
				}
			}

		} finally {
			session.close();
		}
	}
}