<commit_before>
package edu.wustl.query.util.global;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import edu.common.dynamicextensions.domaininterface.EntityInterface;
import edu.common.dynamicextensions.domaininterface.TaggedValueInterface;
import edu.common.dynamicextensions.entitymanager.EntityManagerConstantsInterface;
import edu.wustl.common.beans.QueryResultObjectData;
import edu.wustl.common.beans.QueryResultObjectDataBean;
import edu.wustl.common.beans.SessionDataBean;
import edu.wustl.common.bizlogic.QueryBizLogic;
import edu.wustl.common.dao.QuerySessionData;
import edu.wustl.common.dao.queryExecutor.PagenatedResultData;
import edu.wustl.common.querysuite.queryobject.LogicalOperator;
import edu.wustl.common.querysuite.queryobject.RelationalOperator;
import edu.wustl.common.util.dbManager.DAOException;
import edu.wustl.common.util.logger.Logger;
import edu.wustl.query.bizlogic.QueryOutputSpreadsheetBizLogic;

public class Utility extends edu.wustl.common.util.Utility
{

	private static String pattern = "MM-dd-yyyy";

	public static Object toNewGridFormat(Object obj)
	{
		obj = toGridFormat(obj);
		if (obj instanceof String)
		{
			String objString = (String) obj;
			StringBuffer tokenedString = new StringBuffer();

			StringTokenizer tokenString = new StringTokenizer(objString, ",");

			while (tokenString.hasMoreTokens())
			{
				tokenedString.append(tokenString.nextToken() + " ");
			}
			String gridFormattedStr = new String(tokenedString);
			obj = gridFormattedStr;
		}

		return obj;
	}

	/**
	 * Adds the attribute values in the list in sorted order and returns the
	 * list containing the attribute values in proper order
	 * 
	 * @param dataType -
	 *            data type of the attribute value
	 * @param value1 -
	 *            first attribute value
	 * @param value2 -
	 *            second attribute value
	 * @return List containing value1 and valu2 in sorted order
	 */
	public static ArrayList<String> getAttributeValuesInProperOrder(String dataType, String value1,
			String value2)
	{
		String v1 = value1;
		String v2 = value2;
		ArrayList<String> attributeValues = new ArrayList<String>();
		if (dataType.equalsIgnoreCase(EntityManagerConstantsInterface.DATE_ATTRIBUTE_TYPE))
		{
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			try
			{
				Date date1 = df.parse(value1);
				Date date2 = df.parse(value2);
				if (date1.after(date2))
				{
					v1 = value2;
					v2 = value1;
				}
			}
			catch (ParseException e)
			{
				Logger.out
						.error("Can not parse the given date in getAttributeValuesInProperOrder() method :"
								+ e.getMessage());
				e.printStackTrace();
			}
		}
		else
		{
			if (dataType.equalsIgnoreCase(EntityManagerConstantsInterface.INTEGER_ATTRIBUTE_TYPE)
					|| dataType
							.equalsIgnoreCase(EntityManagerConstantsInterface.LONG_ATTRIBUTE_TYPE))
			{
				if (Long.parseLong(value1) > Long.parseLong(value2))
				{
					v1 = value2;
					v2 = value1;
				}

			}
			else
			{
				if (dataType
						.equalsIgnoreCase(EntityManagerConstantsInterface.DOUBLE_ATTRIBUTE_TYPE))
				{
					if (Double.parseDouble(value1) > Double.parseDouble(value2))
					{
						v1 = value2;
						v2 = value1;
					}

				}
			}
		}
		attributeValues.add(v1);
		attributeValues.add(v2);
		return attributeValues;
	}

	public static Object toNewGridFormatWithHref(List<String> row,
			Map<Integer, QueryResultObjectData> hyperlinkColumnMap, int index)
	{
		Object obj = row.get(index);

		if (obj instanceof String)
		{
			obj = toNewGridFormat(obj);

			QueryResultObjectData queryResultObjectData = hyperlinkColumnMap.get(index);

			if (queryResultObjectData != null)// This column is to be shown as
			// hyperlink.
			{
				if (obj == null || obj.equals(""))
				{
					obj = "NA";
				}

				/**
				 * Name : Prafull Bug ID: 4223 Patch ID: 4223_1 Description:
				 * Edit User: password fields empty & error on submitting new
				 * password Added PageOf Attribute as request parameter in the
				 * link.
				 */<commit_msg>Methods added for SpreadSheetviewAction<commit_after>
package edu.wustl.query.util.global;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import edu.common.dynamicextensions.domaininterface.EntityInterface;
import edu.common.dynamicextensions.domaininterface.TaggedValueInterface;
import edu.common.dynamicextensions.entitymanager.EntityManagerConstantsInterface;
import edu.wustl.common.beans.QueryResultObjectData;
import edu.wustl.common.beans.QueryResultObjectDataBean;
import edu.wustl.common.beans.SessionDataBean;
import edu.wustl.common.bizlogic.QueryBizLogic;
import edu.wustl.common.dao.QuerySessionData;
import edu.wustl.common.dao.queryExecutor.PagenatedResultData;
import edu.wustl.common.querysuite.queryobject.LogicalOperator;
import edu.wustl.common.querysuite.queryobject.RelationalOperator;
import edu.wustl.common.util.dbManager.DAOException;
import edu.wustl.common.util.logger.Logger;
import edu.wustl.query.bizlogic.QueryOutputSpreadsheetBizLogic;

public class Utility extends edu.wustl.common.util.Utility
{

	private static String pattern = "MM-dd-yyyy";

	public static Object toNewGridFormat(Object obj)
	{
		obj = toGridFormat(obj);
		if (obj instanceof String)
		{
			String objString = (String) obj;
			StringBuffer tokenedString = new StringBuffer();

			StringTokenizer tokenString = new StringTokenizer(objString, ",");

			while (tokenString.hasMoreTokens())
			{
				tokenedString.append(tokenString.nextToken() + " ");
			}
			String gridFormattedStr = new String(tokenedString);
			obj = gridFormattedStr;
		}

		return obj;
	}

	/**
	 * Adds the attribute values in the list in sorted order and returns the
	 * list containing the attribute values in proper order
	 * 
	 * @param dataType -
	 *            data type of the attribute value
	 * @param value1 -
	 *            first attribute value
	 * @param value2 -
	 *            second attribute value
	 * @return List containing value1 and valu2 in sorted order
	 */
	public static ArrayList<String> getAttributeValuesInProperOrder(String dataType, String value1,
			String value2)
	{
		String v1 = value1;
		String v2 = value2;
		ArrayList<String> attributeValues = new ArrayList<String>();
		if (dataType.equalsIgnoreCase(EntityManagerConstantsInterface.DATE_ATTRIBUTE_TYPE))
		{
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			try
			{
				Date date1 = df.parse(value1);
				Date date2 = df.parse(value2);
				if (date1.after(date2))
				{
					v1 = value2;
					v2 = value1;
				}
			}
			catch (ParseException e)
			{
				Logger.out
						.error("Can not parse the given date in getAttributeValuesInProperOrder() method :"
								+ e.getMessage());
				e.printStackTrace();
			}
		}
		else
		{
			if (dataType.equalsIgnoreCase(EntityManagerConstantsInterface.INTEGER_ATTRIBUTE_TYPE)
					|| dataType
							.equalsIgnoreCase(EntityManagerConstantsInterface.LONG_ATTRIBUTE_TYPE))
			{
				if (Long.parseLong(value1) > Long.parseLong(value2))
				{
					v1 = value2;
					v2 = value1;
				}

			}
			else
			{
				if (dataType
						.equalsIgnoreCase(EntityManagerConstantsInterface.DOUBLE_ATTRIBUTE_TYPE))
				{
					if (Double.parseDouble(value1) > Double.parseDouble(value2))
					{
						v1 = value2;
						v2 = value1;
					}

				}
			}
		}
		attributeValues.add(v1);
		attributeValues.add(v2);
		return attributeValues;
	}

	
	public static String getmyData(List dataList)
	{
		String myData =	"[";
		int i=0;
		if(dataList !=null && dataList.size()!=0)
		{
			for (i=0;i<(dataList.size()-1);i++)
			{
				List row = (List)dataList.get(i);
				int j;
				myData=myData+"\"";
				for (j=0;j < (row.size()-1);j++)
				{
					myData=myData+Utility.toNewGridFormat(row.get(j)).toString();
					myData=myData+",";
				}
				myData=myData+Utility.toNewGridFormat(row.get(j)).toString();
				myData=myData+"\"";
				myData=myData+",";
			}
		
			List row = (List)dataList.get(i);
			int j;
			myData=myData+"\"";
			for (j=0;j < (row.size()-1);j++)
			{
				myData=myData+Utility.toNewGridFormat(row.get(j)).toString();
				myData=myData+",";
			}
			myData=myData+Utility.toNewGridFormat(row.get(j)).toString();
			myData=myData+"\"";
		}
		myData=myData+"]";
		return myData;
	}
	public static String getcolumns(List columnList) 
	{
		String columns="\"";
		int col;
		if(columnList!=null)
		{
			for(col=0;col<(columnList.size()-1);col++)
			{
				columns=columns+columnList.get(col);
				columns=columns+",";
			}
			columns=columns+columnList.get(col);
		}
		columns=columns+"\"";
		return columns;
	}
	public static String getcolWidth(List columnList, boolean isWidthInPercent)
	{
	
		String colWidth="\"";
		int col;
		if(columnList!=null)
		{
			String fixedColWidth = null;
			if(isWidthInPercent)
			{
				fixedColWidth = String.valueOf(100/columnList.size());
			}
			else
			{
				fixedColWidth="100";
			}
			for(col=0;col<(columnList.size()-1);col++)
			{
				colWidth=colWidth+fixedColWidth;
				colWidth=colWidth+",";
			}
			colWidth=colWidth+fixedColWidth;
		}
		colWidth=colWidth+"\"";
		return colWidth;
	}
	public static String getcolTypes(List dataList)
	{
		StringBuffer colTypes= new StringBuffer(); 
		colTypes.append("\"");
		colTypes.append(Variables.prepareColTypes(dataList));
		colTypes.append("\"");
		return colTypes.toString();
	}
	
	public static void setGridData(List dataList, List columnList, HttpServletRequest request)
	{
		request.setAttribute("myData",getmyData(dataList));
		request.setAttribute("columns", getcolumns(columnList));
		boolean isWidthInPercent=false;
		if( columnList.size()<10)
		{
			isWidthInPercent=true;
		}
		request.setAttribute("colWidth",getcolWidth(columnList,isWidthInPercent));
		request.setAttribute("isWidthInPercent",isWidthInPercent);
		request.setAttribute("colTypes",getcolTypes(dataList));
		int heightOfGrid = 100;
		if(dataList!=null)
		{
			int noOfRows = dataList.size();
			heightOfGrid = (noOfRows + 2) * 20;
			if(heightOfGrid > 240)
			{
				heightOfGrid = 230;
			}
		}
		request.setAttribute("heightOfGrid", heightOfGrid);
		int col = 0;
		int i=0;
		String hiddenColumnNumbers="";
		if(columnList!=null)
		{
			for(col=0;col<columnList.size();col++)
			{
				if (columnList.get(col).toString().trim().equals("ID") || columnList.get(col).toString().trim().equals("Status")
						|| columnList.get(col).toString().trim().equals("Site Name")|| columnList.get(col).toString().trim().equals("Report Collection Date"))
				{
					hiddenColumnNumbers=hiddenColumnNumbers+"hiddenColumnNumbers["+i+"] = "+col+";";
					i++;
				}
			}
		}
		request.setAttribute("hiddenColumnNumbers", hiddenColumnNumbers);
	}
	
	
	public static Object toNewGridFormatWithHref(List<String> row,
			Map<Integer, QueryResultObjectData> hyperlinkColumnMap, int index)
	{
		Object obj = row.get(index);

		if (obj instanceof String)
		{
			obj = toNewGridFormat(obj);

			QueryResultObjectData queryResultObjectData = hyperlinkColumnMap.get(index);

			if (queryResultObjectData != null)// This column is to be shown as
			// hyperlink.
			{
				if (obj == null || obj.equals(""))
				{
					obj = "NA";
				}

				/**
				 * Name : Prafull Bug ID: 4223 Patch ID: 4223_1 Description:
				 * Edit User: password fields empty & error on submitting new
				 * password Added PageOf Attribute as request parameter in the
				 * link.
				 */
{'fraction_non_alphanumeric': 0.11797040169133192, 'fraction_numerical': 0.010232558139534883, 'mean_word_length': 3.844735764031135, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 18, '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': '4664513', 'n_tokens_mistral': 4557, 'n_tokens_neox': 3831, 'n_words': 744}