<commit_before>package gov.nih.nci.common.util;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;

public class NestedCriteria2HQL
{

	private NestedCriteria criteria;
	private Configuration cfg;
	private Session session;
	private Query query;
	private Query countQuery;

	private static Logger log = Logger.getLogger(NestedCriteria2HQL.class);

	private List paramList = new ArrayList();

	public NestedCriteria2HQL(NestedCriteria crit, Configuration cfg, Session session)
	{
		this.criteria = crit;
		this.cfg = cfg;
		this.session = session;
	}

	public Query translate() throws Exception
	{
		StringBuffer hql = new StringBuffer();
		String srcAlias = "";
		String destAlias = getAlias(criteria.getTargetObjectName(),1);

		hql.append("from ");
		hql.append(criteria.getTargetObjectName()).append(" ").append(destAlias);;
		hql.append(" where ");

		NestedCriteria temp = criteria;
		Stack closingStack = new Stack();
		while (temp != null)
		{
			srcAlias = getAlias(temp.getSourceName(),1);
			destAlias = getAlias(temp.getTargetObjectName(),1);
			hql.append(destAlias).append(".id in ");
			hql.append("(");
			hql.append("select ").append(destAlias).append(".id ");
			hql.append(" from ").append(temp.getTargetObjectName()).append(" ").append(destAlias);

			//Source and Target will be same in the case of it is the only node in the linked list.
			if (!temp.getSourceName().equals(temp.getTargetObjectName()))
			{
				hql.append(",").append(temp.getSourceName()).append(" ").append(srcAlias);
				hql.append(" where ");
				if (temp.isTargetCollection())
					hql.append(destAlias).append(" in elements(").append(srcAlias).append(".").append(temp.getRoleName()).append(")");
				else
					hql.append(destAlias).append("=").append(srcAlias).append(".").append(temp.getRoleName());

				hql.append(" and ");
			} else
			{
				hql.append(" where ");
			}

			//If it is the last node then process the attached object list collection
			if (temp.getInternalNestedCriteria() == null)
			{
				for (Iterator i = temp.getSourceObjectList().iterator(); i.hasNext();)
				{
					Object obj = i.next();
					hql.append(srcAlias).append(".id in ").append("(");
					hql.append(getObjectCriterion(obj, cfg));
					hql.append(")");
					if (i.hasNext())
						hql.append(" or ");
				}
			}

			closingStack.push(")");
			temp = temp.getInternalNestedCriteria();
		}

		while (!closingStack.empty())
			hql.append(closingStack.pop());

		log.debug("HQL Query :"+query.getQueryString());
		return prepareQuery(hql);
	}
	
	private Query prepareQuery(StringBuffer hql)<commit_msg>moved log statement<commit_after>package gov.nih.nci.common.util;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;

public class NestedCriteria2HQL
{

	private NestedCriteria criteria;
	private Configuration cfg;
	private Session session;
	private Query query;
	private Query countQuery;

	private static Logger log = Logger.getLogger(NestedCriteria2HQL.class);

	private List paramList = new ArrayList();

	public NestedCriteria2HQL(NestedCriteria crit, Configuration cfg, Session session)
	{
		this.criteria = crit;
		this.cfg = cfg;
		this.session = session;
	}

	public Query translate() throws Exception
	{
		StringBuffer hql = new StringBuffer();
		String srcAlias = "";
		String destAlias = getAlias(criteria.getTargetObjectName(),1);

		hql.append("from ");
		hql.append(criteria.getTargetObjectName()).append(" ").append(destAlias);;
		hql.append(" where ");

		NestedCriteria temp = criteria;
		Stack closingStack = new Stack();
		while (temp != null)
		{
			srcAlias = getAlias(temp.getSourceName(),1);
			destAlias = getAlias(temp.getTargetObjectName(),1);
			hql.append(destAlias).append(".id in ");
			hql.append("(");
			hql.append("select ").append(destAlias).append(".id ");
			hql.append(" from ").append(temp.getTargetObjectName()).append(" ").append(destAlias);

			//Source and Target will be same in the case of it is the only node in the linked list.
			if (!temp.getSourceName().equals(temp.getTargetObjectName()))
			{
				hql.append(",").append(temp.getSourceName()).append(" ").append(srcAlias);
				hql.append(" where ");
				if (temp.isTargetCollection())
					hql.append(destAlias).append(" in elements(").append(srcAlias).append(".").append(temp.getRoleName()).append(")");
				else
					hql.append(destAlias).append("=").append(srcAlias).append(".").append(temp.getRoleName());

				hql.append(" and ");
			} else
			{
				hql.append(" where ");
			}

			//If it is the last node then process the attached object list collection
			if (temp.getInternalNestedCriteria() == null)
			{
				for (Iterator i = temp.getSourceObjectList().iterator(); i.hasNext();)
				{
					Object obj = i.next();
					hql.append(srcAlias).append(".id in ").append("(");
					hql.append(getObjectCriterion(obj, cfg));
					hql.append(")");
					if (i.hasNext())
						hql.append(" or ");
				}
			}

			closingStack.push(")");
			temp = temp.getInternalNestedCriteria();
		}

		while (!closingStack.empty())
			hql.append(closingStack.pop());

		query = prepareQuery(hql);
		log.debug("HQL Query :"+query.getQueryString());
		return query;
	}
	
	private Query prepareQuery(StringBuffer hql)
{'fraction_non_alphanumeric': 0.1504085376021344, 'fraction_numerical': 0.002334500583625146, 'mean_word_length': 4.3362989323843415, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2397474', 'n_tokens_mistral': 2131, 'n_tokens_neox': 1846, 'n_words': 405}