<commit_before>/**
 * <p>Title: SimpleQueryBizLogic Class>
 * <p>Description:	SimpleQueryBizLogic contains the bizlogic required for simple query interface.</p>
 * Copyright:    Copyright (c) year
 * Company: Washington University, School of Medicine, St. Louis.
 * @author <NAME>
 * @version 1.00
 */

package edu.wustl.catissuecore.bizlogic;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;

import edu.wustl.catissuecore.dao.DAOFactory;
import edu.wustl.catissuecore.dao.JDBCDAO;
import edu.wustl.catissuecore.query.DataElement;
import edu.wustl.catissuecore.query.Query;
import edu.wustl.catissuecore.query.SimpleConditionsNode;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.common.beans.QueryResultObjectData;
import edu.wustl.common.util.dbManager.DAOException;
import edu.wustl.common.util.dbManager.HibernateMetaData;
import edu.wustl.common.util.logger.Logger;

/**
 * SimpleQueryBizLogic contains the bizlogic required for simple query interface.
 * @author gautam_shetty
 */
public class SimpleQueryBizLogic extends DefaultBizLogic
{
    /**
	 * Adds single quotes (') for string and date type attributes in the condition collecion 
	 * and the returns the Set of objects to which the condition attributes belong.
     * @param simpleConditionNodeCollection The condition collection.
     * @return the Set of objects to which the condition attributes belong.
     * @throws DAOException
     */
    public List handleStringAndDateConditions(Collection simpleConditionNodeCollection, List fromTables) throws DAOException
    {
        //Adding single quotes to strings and date values.
        Iterator iterator = simpleConditionNodeCollection.iterator();
        while (iterator.hasNext())
        {
            SimpleConditionsNode simpleConditionsNode = (SimpleConditionsNode) iterator.next();
            
        	// Add all the objects selected in UI to the fromtables Set. 
        	addInListIfNotPresent(fromTables, simpleConditionsNode.getCondition().getDataElement().getTableAliasName());
        	
        	// Adds single quotes to the value of attributes whose type is string or date.
            String tableInPath = addSingleQuotes(simpleConditionsNode);
        	
        	//Get the tables in path for this field and add it in the fromTables Set. 
        	if (tableInPath != null)
        	{
        	    addTablesInPathToFromSet(fromTables, tableInPath);
        	}
        	
        	addInListIfNotPresent(fromTables, simpleConditionsNode.getCondition().getDataElement().getTableAliasName());
        }
        return fromTables;
    }

    /**
     * Gets the alias names of the tables in path and adds them in the fromTables Set passed.
     * @param fromTables The Set to which the alias names are to be added.
     * @param tableInPath The ids of tables in path separated by :
     * @throws DAOException
     */
    private void addTablesInPathToFromSet(List fromTables, String tableInPath) throws DAOException
    {
        StringTokenizer tableInPathTokenizer = new StringTokenizer(tableInPath, ":");
        while (tableInPathTokenizer.hasMoreTokens())
        {
            Long tableId = Long.valueOf(tableInPathTokenizer.nextToken());
            QueryBizLogic bizLogic = (QueryBizLogic)BizLogicFactory
            							.getBizLogic(Constants.SIMPLE_QUERY_INTERFACE_ID);
            String aliasName = bizLogic.getAliasNameFromTableId(tableId);
            
            if (aliasName != null)
        	{
                addInListIfNotPresent(fromTables, aliasName);
        	}
        }
    }

    /**
     * Adds quotes to the value of the attribute whose type is string or date 
     * and returns the tables in path for that object.
     * @param simpleConditionsNode The conditio node to be checked. 
     * @return The tables in path for that object.
     */
    private String addSingleQuotes(SimpleConditionsNode simpleConditionsNode)
    {
        String columnName = simpleConditionsNode.getCondition().getDataElement().getField();
        StringTokenizer stringToken = new StringTokenizer(columnName, ".");
        simpleConditionsNode.getCondition().getDataElement().setTableName(stringToken.nextToken());
        simpleConditionsNode.getCondition().getDataElement().setField(stringToken.nextToken());
        simpleConditionsNode.getCondition().getDataElement().setFieldType(stringToken.nextToken());
        String tableInPath = null;
        
        if (stringToken.hasMoreTokens())
        {
            tableInPath = stringToken.nextToken();
        }
        
        return tableInPath;
    }

    /**
	 * Adds the activity status conditions for all the objects in the from clause.
     * @param simpleConditionNodeCollection The SimpleConditionsNode Collection.
     * @param fromTables Set of tables in the from clause of the query.
     * @param simpleConditionsNode The last condition in the simpleConditionNode's Collection.
     */
    public void addActivityStatusConditions(Collection simpleConditionNodeCollection, Set fromTables) throws DAOException,ClassNotFoundException
    {
        // Creating aliasName set with full package names.
        // Required for checking the activityStatus.
        Set fromTablesWithPackageNames = new HashSet();
        Iterator fromTableSetIterator = fromTables.iterator();
        while (fromTableSetIterator.hasNext())
        {
            String tableName = getClassName((String)fromTableSetIterator.next());
            fromTablesWithPackageNames.add(tableName);
        }
        // Check and get the activity status conditions for all the objects in the conditions.
        List activityStatusConditionList = new ArrayList();<commit_msg>changes to set table correctly<commit_after>/**
 * <p>Title: SimpleQueryBizLogic Class>
 * <p>Description:	SimpleQueryBizLogic contains the bizlogic required for simple query interface.</p>
 * Copyright:    Copyright (c) year
 * Company: Washington University, School of Medicine, St. Louis.
 * @author <NAME>
 * @version 1.00
 */

package edu.wustl.catissuecore.bizlogic;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;

import edu.wustl.catissuecore.dao.DAOFactory;
import edu.wustl.catissuecore.dao.JDBCDAO;
import edu.wustl.catissuecore.query.DataElement;
import edu.wustl.catissuecore.query.Query;
import edu.wustl.catissuecore.query.SimpleConditionsNode;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.common.beans.QueryResultObjectData;
import edu.wustl.common.util.dbManager.DAOException;
import edu.wustl.common.util.dbManager.HibernateMetaData;
import edu.wustl.common.util.logger.Logger;

/**
 * SimpleQueryBizLogic contains the bizlogic required for simple query interface.
 * @author gautam_shetty
 */
public class SimpleQueryBizLogic extends DefaultBizLogic
{
    /**
	 * Adds single quotes (') for string and date type attributes in the condition collecion 
	 * and the returns the Set of objects to which the condition attributes belong.
     * @param simpleConditionNodeCollection The condition collection.
     * @return the Set of objects to which the condition attributes belong.
     * @throws DAOException
     */
    public List handleStringAndDateConditions(Collection simpleConditionNodeCollection, List fromTables) throws DAOException
    {
        //Adding single quotes to strings and date values.
        Iterator iterator = simpleConditionNodeCollection.iterator();
        while (iterator.hasNext())
        {
            SimpleConditionsNode simpleConditionsNode = (SimpleConditionsNode) iterator.next();
            
        	// Add all the objects selected in UI to the fromtables Set. 
        	addInListIfNotPresent(fromTables, simpleConditionsNode.getCondition().getDataElement().getTableAliasName());
        	
        	// Adds single quotes to the value of attributes whose type is string or date.
            String tableInPath = addSingleQuotes(simpleConditionsNode);
        	
        	//Get the tables in path for this field and add it in the fromTables Set. 
        	if (tableInPath != null)
        	{
        	    addTablesInPathToFromSet(fromTables, tableInPath);
        	}
        	
        	addInListIfNotPresent(fromTables, simpleConditionsNode.getCondition().getDataElement().getTableAliasName());
        }
        return fromTables;
    }

    /**
     * Gets the alias names of the tables in path and adds them in the fromTables Set passed.
     * @param fromTables The Set to which the alias names are to be added.
     * @param tableInPath The ids of tables in path separated by :
     * @throws DAOException
     */
    private void addTablesInPathToFromSet(List fromTables, String tableInPath) throws DAOException
    {
        StringTokenizer tableInPathTokenizer = new StringTokenizer(tableInPath, ":");
        while (tableInPathTokenizer.hasMoreTokens())
        {
            Long tableId = Long.valueOf(tableInPathTokenizer.nextToken());
            QueryBizLogic bizLogic = (QueryBizLogic)BizLogicFactory
            							.getBizLogic(Constants.SIMPLE_QUERY_INTERFACE_ID);
            String aliasName = bizLogic.getAliasNameFromTableId(tableId);
            
            if (aliasName != null)
        	{
                addInListIfNotPresent(fromTables, aliasName);
        	}
        }
    }

    /**
     * Adds quotes to the value of the attribute whose type is string or date 
     * and returns the tables in path for that object.
     * @param simpleConditionsNode The conditio node to be checked. 
     * @return The tables in path for that object.
     */
    private String addSingleQuotes(SimpleConditionsNode simpleConditionsNode)
    {
        String columnName = simpleConditionsNode.getCondition().getDataElement().getField();
        Logger.out.debug(" columnName:"+columnName);
        StringTokenizer stringToken = new StringTokenizer(columnName, ".");
        simpleConditionsNode.getCondition().getDataElement().setTable(stringToken.nextToken());
        Logger.out.debug("tablename:"+simpleConditionsNode.getCondition().getDataElement().getTableAliasName());
        simpleConditionsNode.getCondition().getDataElement().setField(stringToken.nextToken());
        simpleConditionsNode.getCondition().getDataElement().setFieldType(stringToken.nextToken());
        String tableInPath = null;
        
        if (stringToken.hasMoreTokens())
        {
            tableInPath = stringToken.nextToken();
        }
        
        Logger.out.debug("^^^^^^^^^^^Condition:"+simpleConditionsNode.getCondition());
        
        return tableInPath;
    }

    /**
	 * Adds the activity status conditions for all the objects in the from clause.
     * @param simpleConditionNodeCollection The SimpleConditionsNode Collection.
     * @param fromTables Set of tables in the from clause of the query.
     * @param simpleConditionsNode The last condition in the simpleConditionNode's Collection.
     */
    public void addActivityStatusConditions(Collection simpleConditionNodeCollection, Set fromTables) throws DAOException,ClassNotFoundException
    {
        // Creating aliasName set with full package names.
        // Required for checking the activityStatus.
        Set fromTablesWithPackageNames = new HashSet();
        Iterator fromTableSetIterator = fromTables.iterator();
        while (fromTableSetIterator.hasNext())
        {
            String tableName = getClassName((String)fromTableSetIterator.next());
            fromTablesWithPackageNames.add(tableName);
        }
        // Check and get the activity status conditions for all the objects in the conditions.
        List activityStatusConditionList = new ArrayList();
{'fraction_non_alphanumeric': 0.0794032324906755, 'fraction_numerical': 0.0004973062577704103, 'mean_word_length': 3.218881118881119, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 13, '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': '4154453', 'n_tokens_mistral': 3217, 'n_tokens_neox': 3177, 'n_words': 875}