<commit_before>package edu.wustl.catissuecore.querysuite.metadata;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import edu.common.dynamicextensions.domaininterface.AttributeInterface;
import edu.wustl.common.util.global.Constants;


public class AddPermissibleValue
{
	private Connection connection = null;
	private HashMap<Long, List<AttributeInterface>> entityIDAttributeListMap = new HashMap<Long, List<AttributeInterface>>();
	private HashMap<String, List<String>> entityAttributesToAdd = new HashMap<String, List<String>>();
	private HashMap<String, String> attributeDatatypeMap = new HashMap<String, String>();
	private List<String> entityNameList = new ArrayList<String>();
	private Map<String, Long> entityIDMap = new HashMap<String, Long>();
	private HashMap<String, List> permissibleValueToAddMap = new HashMap<String, List>();
	
	public List<String> addPermissibleValue() throws SQLException, IOException
	{
		List<String> addAttributeSQL=new ArrayList<String>();
		List<String> addSQL;
		populateEntityList();
		populateAttributesToAddMap();
		populatePermissibleValueToAddMap();
		populateEntityIDList();
		entityIDAttributeListMap = UpdateMetadataUtil.populateEntityAttributeMap(connection, entityIDMap);
		populateAttributeDatatypeMap();
		Set<String> keySet = entityIDMap.keySet();
		Long identifier;
		for(String  key : keySet)
		{
			identifier = entityIDMap.get(key);
			List<AttributeInterface> attibuteList = entityIDAttributeListMap.get(identifier);
			for(AttributeInterface attribute : attibuteList)
			{
				if(isAttributeToAdd(key, attribute.getName()))
				{
					addSQL = addPermissibleValue(identifier, attribute);
					addAttributeSQL.addAll(addSQL);
				}
			}
		}
		return addAttributeSQL;
	}

	private List<String> addPermissibleValue(Long identifier, AttributeInterface attribute) throws SQLException, IOException
	{
		List<String> deleteSQL = new ArrayList<String>();
				
		String dataType = getDataTypeOfAttribute(attribute.getName());
		String tableName=null;
		if(dataType.equals("long"))
		{
			tableName= "dyextn_long_concept_value";
		}
		else if(dataType.equals("string"))
		{
			tableName = "dyextn_string_concept_value";
		}
		else if(dataType.equals("integer"))
		{
			tableName = "dyextn_integer_concept_value";
		}
		else if(dataType.equals("double"))
		{
			tableName = "dyextn_double_concept_value";
		}
		else if(dataType.equals("boolean"))
		{
			tableName = "dyextn_boolean_concept_value";
		}
		
		insertPermissibleValue(tableName, attribute);
		
		return deleteSQL;
	}

	private void insertPermissibleValue(String tableName, AttributeInterface attribute) throws IOException, SQLException
	{
		String sql;
		long permissibleValueId=0;
		long dataElementId=0;
		sql = "select max(IDENTIFIER) from dyextn_permissible_value";
		Statement stmt = connection.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		if(rs.next())
		{
			permissibleValueId = rs.getLong(1)+1;
		}
		stmt.close();
		
		sql = "select IDENTIFIER from dyextn_data_element where ATTRIBUTE_TYPE_INFO_ID = "+attribute.getAttributeTypeInformation().getDataElement().getId();
		stmt = connection.createStatement();
		rs = stmt.executeQuery(sql);
		if(rs.next())
		{
			dataElementId = rs.getLong(1);
		}
		stmt.close();
		
		if(dataElementId==0)
		{
			stmt = connection.createStatement();
			sql = "select max(IDENTIFIER) from dyextn_data_element";
			stmt = connection.createStatement();
			rs = stmt.executeQuery(sql);
			if(rs.next())
			{
				dataElementId = rs.getLong(1)+1;
			}
			stmt.close();
			
			sql = "insert into dyextn_data_element(IDENTIFIER,ATTRIBUTE_TYPE_INFO_ID,CATEGORY_ATTRIBUTE_ID) values ("+dataElementId+","+attribute.getAttributeTypeInformation().getDataElement().getId()+",null)";
			if(Constants.MSSQLSERVER_DATABASE.equalsIgnoreCase(UpdateMetadata.DATABASE_TYPE))
			{
				sql = UpdateMetadataUtil.getIndentityInsertStmtForMsSqlServer(sql,"dyextn_data_element");
			}
			UpdateMetadataUtil.executeInsertSQL(sql, connection.createStatement());
		
			sql = "insert into dyextn_userdefined_de values ("+dataElementId+")";
			UpdateMetadataUtil.executeInsertSQL(sql, connection.createStatement());
		}
		
		List permissibleValueList = permissibleValueToAddMap.get(attribute.getName());
		
		for(Object value : permissibleValueList)
		{
			if (value  instanceof String)
			{
				value = "'"+(String) value+"'";
			}
			sql = "insert into dyextn_permissible_value(IDENTIFIER,DESCRIPTION,ATTRIBUTE_TYPE_INFO_ID,CATEGORY_ATTRIBUTE_ID) values ("+permissibleValueId+",null,"+attribute.getAttributeTypeInformation().getDataElement().getId()+",null)";
			if(Constants.MSSQLSERVER_DATABASE.equalsIgnoreCase(UpdateMetadata.DATABASE_TYPE))
			{
				sql = UpdateMetadataUtil.getIndentityInsertStmtForMsSqlServer(sql,"dyextn_permissible_value");<commit_msg>5551 - Sorting of PV's in order of insertion in category.<commit_after>package edu.wustl.catissuecore.querysuite.metadata;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import edu.common.dynamicextensions.domaininterface.AttributeInterface;
import edu.wustl.common.util.global.Constants;


public class AddPermissibleValue
{
	private Connection connection = null;
	private HashMap<Long, List<AttributeInterface>> entityIDAttributeListMap = new HashMap<Long, List<AttributeInterface>>();
	private HashMap<String, List<String>> entityAttributesToAdd = new HashMap<String, List<String>>();
	private HashMap<String, String> attributeDatatypeMap = new HashMap<String, String>();
	private List<String> entityNameList = new ArrayList<String>();
	private Map<String, Long> entityIDMap = new HashMap<String, Long>();
	private HashMap<String, List> permissibleValueToAddMap = new HashMap<String, List>();
	
	public List<String> addPermissibleValue() throws SQLException, IOException
	{
		List<String> addAttributeSQL=new ArrayList<String>();
		List<String> addSQL;
		populateEntityList();
		populateAttributesToAddMap();
		populatePermissibleValueToAddMap();
		populateEntityIDList();
		entityIDAttributeListMap = UpdateMetadataUtil.populateEntityAttributeMap(connection, entityIDMap);
		populateAttributeDatatypeMap();
		Set<String> keySet = entityIDMap.keySet();
		Long identifier;
		for(String  key : keySet)
		{
			identifier = entityIDMap.get(key);
			List<AttributeInterface> attibuteList = entityIDAttributeListMap.get(identifier);
			for(AttributeInterface attribute : attibuteList)
			{
				if(isAttributeToAdd(key, attribute.getName()))
				{
					addSQL = addPermissibleValue(identifier, attribute);
					addAttributeSQL.addAll(addSQL);
				}
			}
		}
		return addAttributeSQL;
	}

	private List<String> addPermissibleValue(Long identifier, AttributeInterface attribute) throws SQLException, IOException
	{
		List<String> deleteSQL = new ArrayList<String>();
				
		String dataType = getDataTypeOfAttribute(attribute.getName());
		String tableName=null;
		if(dataType.equals("long"))
		{
			tableName= "dyextn_long_concept_value";
		}
		else if(dataType.equals("string"))
		{
			tableName = "dyextn_string_concept_value";
		}
		else if(dataType.equals("integer"))
		{
			tableName = "dyextn_integer_concept_value";
		}
		else if(dataType.equals("double"))
		{
			tableName = "dyextn_double_concept_value";
		}
		else if(dataType.equals("boolean"))
		{
			tableName = "dyextn_boolean_concept_value";
		}
		
		insertPermissibleValue(tableName, attribute);
		
		return deleteSQL;
	}

	private void insertPermissibleValue(String tableName, AttributeInterface attribute) throws IOException, SQLException
	{
		String sql;
		long permissibleValueId=0;
		long dataElementId=0;
		sql = "select max(IDENTIFIER) from dyextn_permissible_value";
		Statement stmt = connection.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		if(rs.next())
		{
			permissibleValueId = rs.getLong(1)+1;
		}
		stmt.close();
		
		sql = "select IDENTIFIER from dyextn_data_element where ATTRIBUTE_TYPE_INFO_ID = "+attribute.getAttributeTypeInformation().getDataElement().getId();
		stmt = connection.createStatement();
		rs = stmt.executeQuery(sql);
		if(rs.next())
		{
			dataElementId = rs.getLong(1);
		}
		stmt.close();
		
		if(dataElementId==0)
		{
			stmt = connection.createStatement();
			sql = "select max(IDENTIFIER) from dyextn_data_element";
			stmt = connection.createStatement();
			rs = stmt.executeQuery(sql);
			if(rs.next())
			{
				dataElementId = rs.getLong(1)+1;
			}
			stmt.close();
			
			sql = "insert into dyextn_data_element(IDENTIFIER,ATTRIBUTE_TYPE_INFO_ID,CATEGORY_ATTRIBUTE_ID) values ("+dataElementId+","+attribute.getAttributeTypeInformation().getDataElement().getId()+",null)";
			if(Constants.MSSQLSERVER_DATABASE.equalsIgnoreCase(UpdateMetadata.DATABASE_TYPE))
			{
				sql = UpdateMetadataUtil.getIndentityInsertStmtForMsSqlServer(sql,"dyextn_data_element");
			}
			UpdateMetadataUtil.executeInsertSQL(sql, connection.createStatement());
		
			sql = "insert into dyextn_userdefined_de values (" +dataElementId +",1)";
			UpdateMetadataUtil.executeInsertSQL(sql, connection.createStatement());
		}
		
		List permissibleValueList = permissibleValueToAddMap.get(attribute.getName());
		
		for(Object value : permissibleValueList)
		{
			if (value  instanceof String)
			{
				value = "'"+(String) value+"'";
			}
			sql = "insert into dyextn_permissible_value(IDENTIFIER,DESCRIPTION,ATTRIBUTE_TYPE_INFO_ID,CATEGORY_ATTRIBUTE_ID) values ("+permissibleValueId+",null,"+attribute.getAttributeTypeInformation().getDataElement().getId()+",null)";
			if(Constants.MSSQLSERVER_DATABASE.equalsIgnoreCase(UpdateMetadata.DATABASE_TYPE))
			{
				sql = UpdateMetadataUtil.getIndentityInsertStmtForMsSqlServer(sql,"dyextn_permissible_value");
{'fraction_non_alphanumeric': 0.11492655815799921, 'fraction_numerical': 0.0020841603811036125, 'mean_word_length': 5.859768550034037, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, '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': '6002896', 'n_tokens_mistral': 3481, 'n_tokens_neox': 3099, 'n_words': 529}