<commit_before>/* Copyright (C) 2011 SpringSource
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.codehaus.groovy.grails.orm.hibernate

import grails.orm.HibernateCriteriaBuilder
import org.codehaus.groovy.grails.commons.DomainClassArtefactHandler
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.codehaus.groovy.grails.commons.GrailsDomainClass
import org.codehaus.groovy.grails.commons.metaclass.StaticMethodInvocation
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil
import org.codehaus.groovy.grails.orm.hibernate.cfg.HibernateNamedQueriesBuilder
import org.grails.datastore.gorm.GormEnhancer
import org.grails.datastore.gorm.GormInstanceApi
import org.grails.datastore.gorm.GormStaticApi
import org.grails.datastore.gorm.GormValidationApi
import org.hibernate.criterion.Projections
import org.hibernate.criterion.Restrictions
import org.hibernate.engine.EntityEntry
import org.hibernate.engine.SessionImplementor
import org.hibernate.proxy.HibernateProxy
import org.springframework.beans.SimpleTypeConverter
import org.springframework.core.convert.ConversionService
import org.springframework.dao.DataAccessException
import org.springframework.datastore.mapping.model.PersistentEntity
import org.springframework.orm.hibernate3.HibernateCallback
import org.springframework.orm.hibernate3.HibernateTemplate
import org.springframework.orm.hibernate3.SessionHolder
import org.springframework.transaction.PlatformTransactionManager
import org.springframework.transaction.support.TransactionSynchronizationManager
import org.codehaus.groovy.grails.orm.hibernate.metaclass.*
import org.hibernate.*

 /**
 * Extended GORM Enhancer that fills out the remaining
 * GORM for Hibernate methods and
 * implements string-based query support via HQL
 * 
 * @author <NAME>
 * @since 1.0
 *
 */
class HibernateGormEnhancer extends GormEnhancer{

	private ClassLoader classLoader
	private GrailsApplication grailsApplication
	
	public HibernateGormEnhancer(HibernateDatastore datastore,
			PlatformTransactionManager transactionManager) {
		super(datastore, transactionManager);
		
		def mappingContext = datastore.mappingContext
		
		if(mappingContext instanceof org.codehaus.groovy.grails.domain.GrailsDomainClassMappingContext) {
			grailsApplication = mappingContext.grailsApplication
			classLoader = grailsApplication.classLoader
		}
		else {
			classLoader = Thread.currentThread().contextClassLoader
		}
		
	}

    protected GormValidationApi getValidationApi(Class cls) {
		def validateApi = new HibernateGormValidationApi(cls, datastore)
		validateApi.classLoader = classLoader
		return validateApi
	}
	
	@Override
	protected GormStaticApi getStaticApi(Class cls) {
		def staticApi = new HibernateGormStaticApi(cls, datastore)
		staticApi.classLoader = classLoader
		return staticApi
	}

	@Override
	protected GormInstanceApi getInstanceApi(Class cls) {
		def instanceApi = new HibernateGormInstanceApi(cls, datastore)
		instanceApi.classLoader = classLoader
		return instanceApi
	}

    @Override
	protected void registerNamedQueries( PersistentEntity entity, Object namedQueries ) {
		if(grailsApplication != null) {
			SessionFactory sessionFactory = datastore.sessionFactory
			def domainClass = grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, entity.name)
			if(domainClass != null) {
				def builder = new HibernateNamedQueriesBuilder(domainClass, grailsApplication, sessionFactory)
				builder.evaluate((Closure)namedQueries)
			}
		}

	}

}

/**
 * The implementation of the GORM static method contract for Hibernate
 * 
 * @author <NAME>
 * @since 1.0
 */
class HibernateGormStaticApi extends GormStaticApi {
	private static final EMPTY_ARRAY = [] as Object[]
	
	private HibernateTemplate hibernateTemplate
	private SessionFactory sessionFactory
	private ConversionService conversionService
	private Class identityType
	private ListPersistentMethod listMethod
	private FindAllPersistentMethod findAllMethod
	private FindPersistentMethod findMethod
	private ExecuteQueryPersistentMethod executeQueryMethod
	private ExecuteUpdatePersistentMethod executeUpdateMethod
	private MergePersistentMethod mergeMethod
	private ClassLoader classLoader = Thread.currentThread().getContextClassLoader()
    private List<StaticMethodInvocation> dynamicMethods = []
	
	public HibernateGormStaticApi(Class persistentClass, HibernateDatastore datastore) {
		super(persistentClass, datastore);
		this.sessionFactory = datastore.getSessionFactory()
		this.hibernateTemplate = new HibernateTemplate(sessionFactory)
		this.conversionService = datastore.mappingContext.conversionService


		this.executeQueryMethod = new ExecuteQueryPersistentMethod( sessionFactory, classLoader )
		this.executeUpdateMethod = new ExecuteUpdatePersistentMethod( sessionFactory, classLoader )
		this.findMethod = new FindPersistentMethod( sessionFactory, classLoader )
		this.findAllMethod = new FindAllPersistentMethod( sessionFactory, classLoader )
		identityType = persistentEntity.identity.type
		
		def mappingContext = datastore.mappingContext
		if(mappingContext instanceof org.codehaus.groovy.grails.domain.GrailsDomainClassMappingContext) {
			org.codehaus.groovy.grails.domain.GrailsDomainClassMappingContext domainClassMappingContext = mappingContext<commit_msg>pass a List<FinderMethod) to the HibernateGormStaticApi ctor and its superclass.<commit_after>/* Copyright (C) 2011 SpringSource
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.codehaus.groovy.grails.orm.hibernate

import grails.orm.HibernateCriteriaBuilder
import org.codehaus.groovy.grails.commons.DomainClassArtefactHandler
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.codehaus.groovy.grails.commons.GrailsDomainClass
import org.codehaus.groovy.grails.commons.metaclass.StaticMethodInvocation
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil
import org.codehaus.groovy.grails.orm.hibernate.cfg.HibernateNamedQueriesBuilder
import org.codehaus.groovy.grails.orm.hibernate.metaclass.*
import org.grails.datastore.gorm.finders.FinderMethod
import org.grails.datastore.gorm.GormEnhancer
import org.grails.datastore.gorm.GormInstanceApi
import org.grails.datastore.gorm.GormStaticApi
import org.grails.datastore.gorm.GormValidationApi
import org.hibernate.*
import org.hibernate.criterion.Projections
import org.hibernate.criterion.Restrictions
import org.hibernate.engine.EntityEntry
import org.hibernate.engine.SessionImplementor
import org.hibernate.proxy.HibernateProxy
import org.springframework.beans.SimpleTypeConverter
import org.springframework.core.convert.ConversionService
import org.springframework.dao.DataAccessException
import org.springframework.datastore.mapping.model.PersistentEntity
import org.springframework.orm.hibernate3.HibernateCallback
import org.springframework.orm.hibernate3.HibernateTemplate
import org.springframework.orm.hibernate3.SessionHolder
import org.springframework.transaction.PlatformTransactionManager
import org.springframework.transaction.support.TransactionSynchronizationManager

 /**
 * Extended GORM Enhancer that fills out the remaining
 * GORM for Hibernate methods and
 * implements string-based query support via HQL
 * 
 * @author <NAME>
 * @since 1.0
 *
 */
class HibernateGormEnhancer extends GormEnhancer{

	private ClassLoader classLoader
	private GrailsApplication grailsApplication
	
	public HibernateGormEnhancer(HibernateDatastore datastore,
			PlatformTransactionManager transactionManager) {
		super(datastore, transactionManager);
		
		def mappingContext = datastore.mappingContext
		
		if(mappingContext instanceof org.codehaus.groovy.grails.domain.GrailsDomainClassMappingContext) {
			grailsApplication = mappingContext.grailsApplication
			classLoader = grailsApplication.classLoader
		}
		else {
			classLoader = Thread.currentThread().contextClassLoader
		}
		
	}

    protected GormValidationApi getValidationApi(Class cls) {
		def validateApi = new HibernateGormValidationApi(cls, datastore)
		validateApi.classLoader = classLoader
		return validateApi
	}
	
	@Override
	protected GormStaticApi getStaticApi(Class cls) {
		def staticApi = new HibernateGormStaticApi(cls, datastore, finders)
		staticApi.classLoader = classLoader
		return staticApi
	}

	@Override
	protected GormInstanceApi getInstanceApi(Class cls) {
		def instanceApi = new HibernateGormInstanceApi(cls, datastore)
		instanceApi.classLoader = classLoader
		return instanceApi
	}

    @Override
	protected void registerNamedQueries( PersistentEntity entity, Object namedQueries ) {
		if(grailsApplication != null) {
			SessionFactory sessionFactory = datastore.sessionFactory
			def domainClass = grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, entity.name)
			if(domainClass != null) {
				def builder = new HibernateNamedQueriesBuilder(domainClass, grailsApplication, sessionFactory)
				builder.evaluate((Closure)namedQueries)
			}
		}

	}

}

/**
 * The implementation of the GORM static method contract for Hibernate
 * 
 * @author <NAME>
 * @since 1.0
 */
class HibernateGormStaticApi extends GormStaticApi {
	private static final EMPTY_ARRAY = [] as Object[]
	
	private HibernateTemplate hibernateTemplate
	private SessionFactory sessionFactory
	private ConversionService conversionService
	private Class identityType
	private ListPersistentMethod listMethod
	private FindAllPersistentMethod findAllMethod
	private FindPersistentMethod findMethod
	private ExecuteQueryPersistentMethod executeQueryMethod
	private ExecuteUpdatePersistentMethod executeUpdateMethod
	private MergePersistentMethod mergeMethod
	private ClassLoader classLoader = Thread.currentThread().getContextClassLoader()
    private List<StaticMethodInvocation> dynamicMethods = []
	
	public HibernateGormStaticApi(Class persistentClass, HibernateDatastore datastore, List<FinderMethod> finders) {
		super(persistentClass, datastore, finders);
		this.sessionFactory = datastore.getSessionFactory()
		this.hibernateTemplate = new HibernateTemplate(sessionFactory)
		this.conversionService = datastore.mappingContext.conversionService


		this.executeQueryMethod = new ExecuteQueryPersistentMethod( sessionFactory, classLoader )
		this.executeUpdateMethod = new ExecuteUpdatePersistentMethod( sessionFactory, classLoader )
		this.findMethod = new FindPersistentMethod( sessionFactory, classLoader )
		this.findAllMethod = new FindAllPersistentMethod( sessionFactory, classLoader )
		identityType = persistentEntity.identity.type
		
		def mappingContext = datastore.mappingContext
		if(mappingContext instanceof org.codehaus.groovy.grails.domain.GrailsDomainClassMappingContext) {
			org.codehaus.groovy.grails.domain.GrailsDomainClassMappingContext domainClassMappingContext = mappingContext
{'fraction_non_alphanumeric': 0.06469939794793522, 'fraction_numerical': 0.0025438819638768763, 'mean_word_length': 6.173965936739659, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5941494', 'n_tokens_mistral': 3441, 'n_tokens_neox': 3136, 'n_words': 744}