<commit_before>/**
 * 
 */

package edu.wustl.common.query.impl;

import java.util.Map.Entry;

import edu.common.dynamicextensions.domaininterface.AttributeInterface;
import edu.common.dynamicextensions.exception.DataTypeFactoryInitializationException;
import edu.common.dynamicextensions.exception.DynamicExtensionsSystemException;
import edu.wustl.common.query.impl.predicate.AbstractPredicate;
import edu.wustl.common.query.impl.predicate.PredicateGenerator;
import edu.wustl.common.query.impl.predicate.Predicates;
import edu.wustl.common.querysuite.exceptions.MultipleRootsException;
import edu.wustl.common.querysuite.queryobject.ICondition;
import edu.wustl.common.querysuite.queryobject.IExpression;
import edu.wustl.common.querysuite.queryobject.IOutputAttribute;
import edu.wustl.common.querysuite.queryobject.IParameter;
import edu.wustl.query.util.global.Constants;
import edu.wustl.query.util.global.Utility;

/**
 * @author juberahamad_patel
 *
 */
public class PassTwoXQueryGenerator extends AbstractXQueryGenerator
{

	/**
	 * 
	 * @param predicateGenerator 
	 * @return the For Clause of XQuery
	 * @throws MultipleRootsException
	 * @throws DynamicExtensionsSystemException
	 */
	@Override
	protected String buildXQueryForClause(PredicateGenerator predicateGenerator)
			throws MultipleRootsException, DynamicExtensionsSystemException
	{
		StringBuilder xqueryForClause = new StringBuilder(512);
		String variable = null;

		for (Entry<IExpression, String> entry : getForVariables().entrySet())
		{
			xqueryForClause.append(Constants.QUERY_FOR);
			IExpression expression = entry.getKey();
			variable = entry.getValue();
			if (getMainExpressions().contains(expression))
			{
				String tableName = expression.getQueryEntity().getDynamicExtensionsEntity()
						.getTableProperties().getName();

				StringBuilder rootPath = new StringBuilder().append(Constants.QUERY_XMLCOLUMN)
						.append(Constants.QUERY_OPENING_PARENTHESIS).append("\"").append(tableName)
						.append(Constants.QUERY_DOT).append(Constants.QUERY_XMLDATA).append("\"")
						.append(Constants.QUERY_CLOSING_PARENTHESIS).append('/');

				rootPath.append(expression.getQueryEntity().getDynamicExtensionsEntity().getName())
						.toString();

				xqueryForClause.append(variable).append(' ').append(Constants.IN).append(' ')
						.append(rootPath.toString());
			}
			else
			{
				IExpression parent = joinGraph.getParentList(expression).get(0);
				String parentPath = getEntityPaths().get(parent);
				xqueryForClause.append(variable).append(' ').append(Constants.IN).append(' ');
				xqueryForClause.append(parentPath).append('/').append(
						getTargetRoles().get(expression)).append('/');
				String entityName = expression.getQueryEntity().getDynamicExtensionsEntity()
						.getName();
				xqueryForClause.append(deCapitalize(entityName));
			}

			Predicates predicates = predicateGenerator.getPredicates(expression);
			if (predicates != null)
			{
				insertParameters(predicates);
				xqueryForClause.append('[').append(predicates.assemble()).append(']');
			}

		}

		return xqueryForClause.toString();

	}

	/**
	 * replace parameter placeholders with parameter names
	 * @param predicates the predicates
	 */
	private void insertParameters(Predicates predicates)
	{
		for (Entry<IParameter<ICondition>, IExpression> entry : getParameters().entrySet())
		{
			AttributeInterface attribute = entry.getKey().getParameterizedObject().getAttribute();
			String attributeAlias = Utility.getAliasFor(attribute, entry.getValue());

			for (AbstractPredicate predicate : predicates.getPredicates())
			{
				if (attributeAlias.equals(predicate.getAttributeAlias()))<commit_msg>Changes for incoporating outer join condition withtin an XML<commit_after>/**
 * 
 */

package edu.wustl.common.query.impl;

import java.util.Map.Entry;

import edu.common.dynamicextensions.domaininterface.AttributeInterface;
import edu.common.dynamicextensions.exception.DataTypeFactoryInitializationException;
import edu.common.dynamicextensions.exception.DynamicExtensionsSystemException;
import edu.wustl.common.query.impl.predicate.AbstractPredicate;
import edu.wustl.common.query.impl.predicate.PredicateGenerator;
import edu.wustl.common.query.impl.predicate.Predicates;
import edu.wustl.common.querysuite.exceptions.MultipleRootsException;
import edu.wustl.common.querysuite.queryobject.ICondition;
import edu.wustl.common.querysuite.queryobject.IExpression;
import edu.wustl.common.querysuite.queryobject.IOutputAttribute;
import edu.wustl.common.querysuite.queryobject.IParameter;
import edu.wustl.query.util.global.Constants;
import edu.wustl.query.util.global.Utility;

/**
 * @author juberahamad_patel
 *
 */
public class PassTwoXQueryGenerator extends AbstractXQueryGenerator
{

	/**
	 * 
	 * @param predicateGenerator 
	 * @return the For Clause of XQuery
	 * @throws MultipleRootsException
	 * @throws DynamicExtensionsSystemException
	 */
	@Override
	protected String buildXQueryForClause(PredicateGenerator predicateGenerator)
			throws MultipleRootsException, DynamicExtensionsSystemException
	{
		StringBuilder xqueryForClause = new StringBuilder(512);
		String variable = null;

		for (Entry<IExpression, String> entry : getForVariables().entrySet())
		{
			xqueryForClause.append(Constants.QUERY_FOR);
			IExpression expression = entry.getKey();
			variable = entry.getValue();
			if (getMainExpressions().contains(expression))
			{
				String tableName = expression.getQueryEntity().getDynamicExtensionsEntity()
						.getTableProperties().getName();

				StringBuilder rootPath = new StringBuilder().append(Constants.QUERY_XMLCOLUMN)
						.append(Constants.QUERY_OPENING_PARENTHESIS).append("\"").append(tableName)
						.append(Constants.QUERY_DOT).append(Constants.QUERY_XMLDATA).append("\"")
						.append(Constants.QUERY_CLOSING_PARENTHESIS).append('/');

				rootPath.append(expression.getQueryEntity().getDynamicExtensionsEntity().getName())
						.toString();

				xqueryForClause.append(variable).append(' ').append(Constants.IN).append(' ')
						.append(rootPath.toString());
			}
			else
			{
				StringBuffer entityPath = new StringBuffer();
				String entityName = expression.getQueryEntity().getDynamicExtensionsEntity()
				.getName();
				entityPath = entityPath.append(getTargetRoles().get(expression)).append('/').append(deCapitalize(entityName));
				IExpression parent = joinGraph.getParentList(expression).get(0);
				String parentPath = getEntityPaths().get(parent);
				xqueryForClause.append(variable).append(' ').append(Constants.IN).append(' ');
				xqueryForClause.append(parentPath).append('/').append(Constants.QUERY_OPENING_PARENTHESIS).append(entityPath.toString())
				.append(Constants.QUERY_COMMA).append(".[not(").append(entityPath.toString()).append(")]/<nothing/>").append(Constants.QUERY_CLOSING_PARENTHESIS);
			}

			Predicates predicates = predicateGenerator.getPredicates(expression);
			if (predicates != null)
			{
				insertParameters(predicates);
				xqueryForClause.append('[').append(predicates.assemble()).append(']');
			}

		}

		return xqueryForClause.toString();

	}

	/**
	 * replace parameter placeholders with parameter names
	 * @param predicates the predicates
	 */
	private void insertParameters(Predicates predicates)
	{
		for (Entry<IParameter<ICondition>, IExpression> entry : getParameters().entrySet())
		{
			AttributeInterface attribute = entry.getKey().getParameterizedObject().getAttribute();
			String attributeAlias = Utility.getAliasFor(attribute, entry.getValue());

			for (AbstractPredicate predicate : predicates.getPredicates())
			{
				if (attributeAlias.equals(predicate.getAttributeAlias()))
{'fraction_non_alphanumeric': 0.12148070907194995, 'fraction_numerical': 0.0010427528675703858, 'mean_word_length': 5.5302127659574465, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '7392039', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2234, 'n_words': 316}