<commit_before>/**
 * 
 */
package edu.wustl.catissuecore.util.querysuite;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import javax.servlet.http.HttpSession;

import edu.common.dynamicextensions.domaininterface.AssociationInterface;
import edu.common.dynamicextensions.domaininterface.AttributeInterface;
import edu.common.dynamicextensions.domaininterface.EntityInterface;
import edu.common.dynamicextensions.domaininterface.RoleInterface;
import edu.common.dynamicextensions.entitymanager.EntityManager;
import edu.common.dynamicextensions.entitymanager.EntityManagerInterface;
import edu.common.dynamicextensions.exception.DynamicExtensionsSystemException;
import edu.wustl.cab2b.server.cache.EntityCache;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.catissuecore.util.global.Variables;
import edu.wustl.common.beans.QueryResultObjectDataBean;
import edu.wustl.common.beans.SessionDataBean;
import edu.wustl.common.dao.DAOFactory;
import edu.wustl.common.dao.JDBCDAO;
import edu.wustl.common.querysuite.queryobject.IQuery;
import edu.wustl.common.querysuite.queryobject.IQueryEntity;
import edu.wustl.common.querysuite.queryobject.impl.OutputTreeDataNode;
import edu.wustl.common.querysuite.queryobject.impl.metadata.QueryOutputTreeAttributeMetadata;
import edu.wustl.common.util.dbManager.DAOException;
import edu.wustl.common.util.global.ApplicationProperties;
import edu.wustl.common.util.logger.Logger;


/**
 * @author supriya_dankh
 * This class contains all the methods required for CSM integration in query.
 */
public abstract class QueryCSMUtil
{
	
	/**This method will check if main objects for all the dependent objects are present in query or not.
	 * If yes then will create map of entity as key and main entity list as value.
	 * If not then will set error message in session.
	 * @param query
	 * @param session
	 * @param uniqueIdNodesMap
	 * @return
	 */
	public static Map<EntityInterface, List<EntityInterface>> setMainObjectErrorMessage(
			IQuery query, HttpSession session, Map<String, OutputTreeDataNode> uniqueIdNodesMap)
	{
		int status;
		Map<EntityInterface ,List<EntityInterface>> mainEntityMap = getMainEntitiesForAllQueryNodes(uniqueIdNodesMap);
		List<Long> queryDeEntities = getAllDEEntities(query);
		String errorMessg = getErrorMessage(queryDeEntities, mainEntityMap);
		if(!errorMessg.equals(""))
		{
			session.setAttribute(Constants.NO_MAIN_OBJECT_IN_QUERY, errorMessg);
			return null;
		}
		else
			session.setAttribute(Constants.MAIN_ENTITY_MAP, mainEntityMap);
		return mainEntityMap;
	}

	/**This method will return error message for main object.
	 * @param queryDeEntities list of ids of all the DE entities present in query.
	 * @param mainEntityMap main entity map.
	 */
	private static String getErrorMessage(List<Long> queryDeEntities,
			Map<EntityInterface, List<EntityInterface>> mainEntityMap)
	{
		String errorMsg = "";
		
		for (Iterator iterator = mainEntityMap.entrySet().iterator(); iterator.hasNext();)
		{
			String mainEntityName = "";
			String entityName = "";
			String mainEntityNames = "";
			Map.Entry<EntityInterface ,List<EntityInterface>> mapValue = (Map.Entry<EntityInterface ,List<EntityInterface>>) iterator.next();
			String name = mapValue.getKey().getName();
			if(name!=null && !name.equals(""))
			{
			  entityName = name.substring(name.lastIndexOf(".")+1,name.length());
			}
			boolean isMainEntityPresent = false;
			for(EntityInterface mainEntity : mapValue.getValue())
			{
				mainEntityName = mainEntity.getName();
				if(mainEntityName!=null && !mainEntityName.equals(""))
					mainEntityName = mainEntityName.substring(mainEntityName.lastIndexOf(".")+1,mainEntityName.length());
				mainEntityNames = mainEntityNames + mainEntityName+" or ";
				if(queryDeEntities.contains(mainEntity.getId()))
				{
					isMainEntityPresent = true;
					break;
				}
			} 
			if(!isMainEntityPresent)
			{
				mainEntityNames = mainEntityNames.substring(0, mainEntityNames.lastIndexOf("r")-1);
				errorMsg = entityName+" "
						+ ApplicationProperties.getValue("query.mainObjectErrorPartPre")
						+ " "+mainEntityNames + ApplicationProperties.getValue("query.mainObjectErrorPartPost");
				break;
			}
		}<commit_msg>Checks if the main entity is present in the uniqueNodeIdMap . If main entity is not present, returns error message.<commit_after>/**
 * 
 */
package edu.wustl.catissuecore.util.querysuite;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import javax.servlet.http.HttpSession;

import edu.common.dynamicextensions.domaininterface.AssociationInterface;
import edu.common.dynamicextensions.domaininterface.AttributeInterface;
import edu.common.dynamicextensions.domaininterface.EntityInterface;
import edu.common.dynamicextensions.domaininterface.RoleInterface;
import edu.common.dynamicextensions.entitymanager.EntityManager;
import edu.common.dynamicextensions.entitymanager.EntityManagerInterface;
import edu.common.dynamicextensions.exception.DynamicExtensionsSystemException;
import edu.wustl.cab2b.server.cache.EntityCache;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.catissuecore.util.global.Variables;
import edu.wustl.common.beans.QueryResultObjectDataBean;
import edu.wustl.common.beans.SessionDataBean;
import edu.wustl.common.dao.DAOFactory;
import edu.wustl.common.dao.JDBCDAO;
import edu.wustl.common.querysuite.queryobject.IQuery;
import edu.wustl.common.querysuite.queryobject.IQueryEntity;
import edu.wustl.common.querysuite.queryobject.impl.OutputTreeDataNode;
import edu.wustl.common.querysuite.queryobject.impl.metadata.QueryOutputTreeAttributeMetadata;
import edu.wustl.common.util.dbManager.DAOException;
import edu.wustl.common.util.global.ApplicationProperties;
import edu.wustl.common.util.logger.Logger;


/**
 * @author supriya_dankh
 * This class contains all the methods required for CSM integration in query.
 */
public abstract class QueryCSMUtil
{
	
	/**This method will check if main objects for all the dependent objects are present in query or not.
	 * If yes then will create map of entity as key and main entity list as value.
	 * If not then will set error message in session.
	 * @param query
	 * @param session
	 * @param uniqueIdNodesMap
	 * @return
	 */
	public static Map<EntityInterface, List<EntityInterface>> setMainObjectErrorMessage(
			IQuery query, HttpSession session, Map<String, OutputTreeDataNode> uniqueIdNodesMap)
	{
		int status;
		Map<EntityInterface ,List<EntityInterface>> mainEntityMap = getMainEntitiesForAllQueryNodes(uniqueIdNodesMap);
		List<Long> queryDeEntities = getAllDEEntities(query);
		String errorMessg = getErrorMessage(mainEntityMap, uniqueIdNodesMap);
		if(!errorMessg.equals(""))
		{
			session.setAttribute(Constants.NO_MAIN_OBJECT_IN_QUERY, errorMessg);
			return null;
		}
		else
			session.setAttribute(Constants.MAIN_ENTITY_MAP, mainEntityMap);
		return mainEntityMap;
	}
	
	/**
	 * This method will return error message for main object.
	 * @param queryDeEntities list of ids of all the DE entities present in query.
	 * @param mainEntityMap main entity map.
	 * @param uniqueIdNodesMap contains all the entities in the query 
	 * @return
	 */
	private static String getErrorMessage(Map<EntityInterface, List<EntityInterface>> mainEntityMap, 
			Map<String, OutputTreeDataNode> uniqueIdNodesMap)
	{
		String errorMsg = "";
		
		for (Iterator iterator = mainEntityMap.entrySet().iterator(); iterator.hasNext();)
		{
			String mainEntityName = "";
			String entityName = "";
			String mainEntityNames = "";
			Map.Entry<EntityInterface ,List<EntityInterface>> mapValue = (Map.Entry<EntityInterface ,List<EntityInterface>>) iterator.next();
			String name = mapValue.getKey().getName();
			if(name!=null && !name.equals(""))
			{
			  entityName = name.substring(name.lastIndexOf(".")+1,name.length());
			}
			boolean isMainEntityPresent = false;
			for(EntityInterface mainEntity : mapValue.getValue())
			{
				mainEntityName = mainEntity.getName();
				if(mainEntityName!=null && !mainEntityName.equals(""))
					mainEntityName = mainEntityName.substring(mainEntityName.lastIndexOf(".")+1,mainEntityName.length());
				mainEntityNames = mainEntityNames + mainEntityName+" or ";
				// iterate through uniqueIdNodesMap and check if main entity is present.
				for (Iterator idMapIterator = uniqueIdNodesMap.entrySet().iterator(); idMapIterator.hasNext();)
				{	
					Map.Entry<String, OutputTreeDataNode> IdmapValue = (Map.Entry<String, OutputTreeDataNode>) idMapIterator.next();
					OutputTreeDataNode node = IdmapValue.getValue();				
					EntityInterface mapEntity = node.getOutputEntity().getDynamicExtensionsEntity();
					if (mainEntity.getName().equals(mapEntity.getName()))
					{
						isMainEntityPresent = true;
						break;
					}
				}
			} 
			if(!isMainEntityPresent)
			{
				mainEntityNames = mainEntityNames.substring(0, mainEntityNames.lastIndexOf("r")-1);
				String message = ApplicationProperties.getValue("query.mainObjectError");
				Object[] arguments = new Object[]{entityName,mainEntityNames};
				errorMsg = MessageFormat.format(message, arguments);
				break;
			}
		}
{'fraction_non_alphanumeric': 0.10397096498719044, 'fraction_numerical': 0.001067463706233988, 'mean_word_length': 5.2543391188251, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '4264726', 'n_tokens_mistral': 3054, 'n_tokens_neox': 2784, 'n_words': 570}