/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.portal.dao.orm.hibernate;

import aQute.bnd.annotation.ProviderType;

import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.portlet.PortletClassLoaderUtil;
import com.liferay.portal.security.lang.DoPrivilegedUtil;
import com.liferay.portal.spring.hibernate.PortletHibernateConfiguration;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;

/**
 * @author Shuyang Zhou
 * @author Alexander Chow
 */
@ProviderType
public class PortletSessionFactoryImpl extends SessionFactoryImpl {

	/**
	 * @deprecated As of Judson (7.1.x), with no direct replacement
	 */
	@Deprecated
	public void setDataSource(DataSource dataSource) {
		_dataSource = dataSource;
	}

	/**
	 * @deprecated As of Judson (7.1.x), with no direct replacement
	 */
	@Deprecated
	protected SessionFactory createSessionFactory(DataSource dataSource) {
		PortletHibernateConfiguration portletHibernateConfiguration =
			new PortletHibernateConfiguration(
				getSessionFactoryClassLoader(), dataSource);

		portletHibernateConfiguration.setDataSource(dataSource);

		SessionFactory sessionFactory = null;

		try {
			sessionFactory =
				portletHibernateConfiguration.buildSessionFactory();
		}
		catch (Exception e) {
			_log.error(e, e);

			return null;
		}

		return sessionFactory;
	}

	/**
	 * @deprecated As of Judson (7.1.x), with no direct replacement
	 */
	@Deprecated
	protected DataSource getDataSource() {
		return _dataSource;
	}

	protected ClassLoader getDefaultSessionFactoryClassLoader() {
		return PortletClassLoaderUtil.getClassLoader();
	}

	/**
	 * @deprecated As of Judson (7.1.x), with no direct replacement
	 */
	@Deprecated
	protected SessionFactory getSessionFactory() {
		return getSessionFactoryImplementor();
	}

	@Override
	protected Session wrapSession(org.hibernate.Session session) {
		return DoPrivilegedUtil.wrapWhenActive(super.wrapSession(session));
	}

	private static final Log _log = LogFactoryUtil.getLog(
		PortletSessionFactoryImpl.class);

	private DataSource _dataSource;

}