<commit_before>/*
 * Created on Jul 29, 2005
 *<p>SpecimenEventParametersBizLogic Class</p>
 * This class contains the Biz Logic for all EventParameters Classes.
 * This will be the class which will be used for data transactions of the EventParameters.
 */

package edu.wustl.catissuecore.bizlogic;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import edu.wustl.catissuecore.domain.AbstractSpecimen;
import edu.wustl.catissuecore.domain.CheckInCheckOutEventParameter;
import edu.wustl.catissuecore.domain.CollectionEventParameters;
import edu.wustl.catissuecore.domain.DisposalEventParameters;
import edu.wustl.catissuecore.domain.Distribution;
import edu.wustl.catissuecore.domain.DistributionEventParameters;
import edu.wustl.catissuecore.domain.EmbeddedEventParameters;
import edu.wustl.catissuecore.domain.FixedEventParameters;
import edu.wustl.catissuecore.domain.FrozenEventParameters;
import edu.wustl.catissuecore.domain.ReceivedEventParameters;
import edu.wustl.catissuecore.domain.Site;
import edu.wustl.catissuecore.domain.Specimen;
import edu.wustl.catissuecore.domain.SpecimenCollectionGroup;
import edu.wustl.catissuecore.domain.SpecimenEventParameters;
import edu.wustl.catissuecore.domain.SpecimenPosition;
import edu.wustl.catissuecore.domain.StorageContainer;
import edu.wustl.catissuecore.domain.TissueSpecimenReviewEventParameters;
import edu.wustl.catissuecore.domain.TransferEventParameters;
import edu.wustl.catissuecore.domain.User;
import edu.wustl.catissuecore.util.ApiSearchUtil;
import edu.wustl.catissuecore.util.CatissueCoreCacheManager;
import edu.wustl.catissuecore.util.Position;
import edu.wustl.catissuecore.util.SpecimenUtil;
import edu.wustl.catissuecore.util.StorageContainerUtil;
import edu.wustl.catissuecore.util.global.AppUtility;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.common.beans.SessionDataBean;
import edu.wustl.common.bizlogic.DefaultBizLogic;
import edu.wustl.common.cde.CDEManager;
import edu.wustl.common.exception.ApplicationException;
import edu.wustl.common.exception.BizLogicException;
import edu.wustl.common.exception.ErrorKey;
import edu.wustl.common.factory.AbstractFactoryConfig;
import edu.wustl.common.factory.IFactory;
import edu.wustl.common.util.global.ApplicationProperties;
import edu.wustl.common.util.global.CommonUtilities;
import edu.wustl.common.util.global.Status;
import edu.wustl.common.util.global.Validator;
import edu.wustl.common.util.logger.Logger;
import edu.wustl.dao.DAO;
import edu.wustl.dao.HibernateDAO;
import edu.wustl.dao.JDBCDAO;
import edu.wustl.dao.QueryWhereClause;
import edu.wustl.dao.condition.EqualClause;
import edu.wustl.dao.condition.NotEqualClause;
import edu.wustl.dao.exception.DAOException;
import edu.wustl.dao.query.generator.DBTypes;
import edu.wustl.dao.util.HibernateMetaData;
import edu.wustl.dao.util.NamedQueryParam;
import edu.wustl.security.exception.UserNotAuthorizedException;

/**
 * @author mandar_deshmukh
 * This class contains the Business Logic for all EventParameters Classes.
 * This will be the class which will be used for data transactions of the EventParameters.
 */
public class SpecimenEventParametersBizLogic extends CatissueDefaultBizLogic
{

	/**
	 * Logger object.
	 */
	private static final Logger LOGGER = Logger
			.getCommonLogger(SpecimenEventParametersBizLogic.class);
	/**
	 * storageContainerIds.
	 */
	private HashSet<String> storageContainerIds = new HashSet<String>();

	/**
	 * Saves the FrozenEventParameters object in the database.
	 * @param obj The FrozenEventParameters object to be saved.
	 * @param dao DAO Object
	 * @param sessionDataBean SessionDataBean object
	 * @throws BizLogicException throws BizLogicException.
	 */
	@Override
	protected void insert(Object obj, DAO dao, SessionDataBean sessionDataBean)
			throws BizLogicException
	{
		try
		{
			final IFactory factory = AbstractFactoryConfig.getInstance().getBizLogicFactory();
			final NewSpecimenBizLogic newSpecimenBizLogic = (NewSpecimenBizLogic) factory
					.getBizLogic(Constants.NEW_SPECIMEN_FORM_ID);
			final List specimenIds = new ArrayList();
			
			if (obj instanceof List)
			{
				final List eventObjectsList = (List) obj;
				checkStatusAndGetUserId((SpecimenEventParameters)eventObjectsList.get(0), dao);
				for (int i = 0; i < eventObjectsList.size(); i++)
				{
					specimenIds.add(((SpecimenEventParameters) eventObjectsList.get(i)).getSpecimen()
							.getId());
				}
	
				for (int i = 0; i < eventObjectsList.size(); i++)
				{
					this.insertEvent(eventObjectsList.get(i), dao, sessionDataBean,
							newSpecimenBizLogic, specimenIds);
				}
			}
			else
			{
				checkStatusAndGetUserId((SpecimenEventParameters)obj, dao);
				this.insertEvent(obj, dao, sessionDataBean, newSpecimenBizLogic, specimenIds);
			}
		}
		catch (final DAOException daoExp)
		{
			this.LOGGER.error(daoExp.getMessage(), daoExp);
			throw this
					.getBizLogicException(daoExp, daoExp.getErrorKeyName(), daoExp.getMsgValues());
		}
	}
	/**
	 * Saves the SpecimenEventParameters object in the database.
	 * @param specimenIdentifiers Single Specimen identifier OR Multiple Comma Sperated Specimen Identifier
	 * @param obj SpecimenEventParameter Object
	 * @param sessionDataBean SessionDataBean object
	 * @return return Long of newly created Event Identifier
	 * @throws BizLogicException throws CloneNotSupportedException,ApplicationException.
	 */
	public Long insertBulkSpecimenEvents(String specimenIdentifiers,SpecimenEventParameters obj,
			SessionDataBean sessionDataBean) throws CloneNotSupportedException, ApplicationException
	{
		Long createdObjectId=null;
		DAO dao = null;
		final IFactory factory = AbstractFactoryConfig.getInstance().getBizLogicFactory();
		final NewSpecimenBizLogic newSpecimenBizLogic = (NewSpecimenBizLogic) factory
				.getBizLogic(Constants.NEW_SPECIMEN_FORM_ID);
		//List<String> idList=getSpecimenIdList(specimenIdentifiers);
		List<String> idList=AppUtility.getListOnCommaToken(specimenIdentifiers);
		Iterator<String> specimenIdIt=idList.iterator();
		final List<String> specimenIds = new ArrayList<String>();
		//add user validation
		dao = AppUtility.openDAOSession(sessionDataBean);
		checkStatusAndGetUserId(obj, dao);
		dao.closeSession();
		while(specimenIdIt.hasNext())
		{
			try 
			{
				dao = AppUtility.openDAOSession(sessionDataBean);
				SpecimenEventParameters specimenEventParameters=(SpecimenEventParameters) obj.clone();
				Specimen specimen=new Specimen();
				specimen.setId(Long.valueOf(specimenIdIt.next()));
				specimenEventParameters.setSpecimen(specimen);
				insertEvent(specimenEventParameters, dao, sessionDataBean, newSpecimenBizLogic, specimenIds);
				createdObjectId=specimenEventParameters.getId();
				dao.commit();
			}catch (final DAOException daoException) {
				LOGGER.error(daoException.getMessage(),daoException);
				throw AppUtility.getApplicationException(daoException, daoException.getErrorKeyName(),
						daoException.getMsgValues());	
			}
			finally
			{
				AppUtility.closeDAOSession(dao);
			}
		}
		return createdObjectId;
	}
	
	/**<commit_msg>removed the dao.openSession and dao.commit from the while loop. modified the code to use single session for all the events. https://catissueplus.atlassian.net/browse/PLUS-79<commit_after>/*
 * Created on Jul 29, 2005
 *<p>SpecimenEventParametersBizLogic Class</p>
 * This class contains the Biz Logic for all EventParameters Classes.
 * This will be the class which will be used for data transactions of the EventParameters.
 */

package edu.wustl.catissuecore.bizlogic;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import edu.wustl.catissuecore.domain.AbstractSpecimen;
import edu.wustl.catissuecore.domain.CheckInCheckOutEventParameter;
import edu.wustl.catissuecore.domain.CollectionEventParameters;
import edu.wustl.catissuecore.domain.DisposalEventParameters;
import edu.wustl.catissuecore.domain.Distribution;
import edu.wustl.catissuecore.domain.DistributionEventParameters;
import edu.wustl.catissuecore.domain.EmbeddedEventParameters;
import edu.wustl.catissuecore.domain.FixedEventParameters;
import edu.wustl.catissuecore.domain.FrozenEventParameters;
import edu.wustl.catissuecore.domain.ReceivedEventParameters;
import edu.wustl.catissuecore.domain.Site;
import edu.wustl.catissuecore.domain.Specimen;
import edu.wustl.catissuecore.domain.SpecimenCollectionGroup;
import edu.wustl.catissuecore.domain.SpecimenEventParameters;
import edu.wustl.catissuecore.domain.SpecimenPosition;
import edu.wustl.catissuecore.domain.StorageContainer;
import edu.wustl.catissuecore.domain.TissueSpecimenReviewEventParameters;
import edu.wustl.catissuecore.domain.TransferEventParameters;
import edu.wustl.catissuecore.domain.User;
import edu.wustl.catissuecore.util.ApiSearchUtil;
import edu.wustl.catissuecore.util.CatissueCoreCacheManager;
import edu.wustl.catissuecore.util.Position;
import edu.wustl.catissuecore.util.SpecimenUtil;
import edu.wustl.catissuecore.util.StorageContainerUtil;
import edu.wustl.catissuecore.util.global.AppUtility;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.common.beans.SessionDataBean;
import edu.wustl.common.bizlogic.DefaultBizLogic;
import edu.wustl.common.cde.CDEManager;
import edu.wustl.common.exception.ApplicationException;
import edu.wustl.common.exception.BizLogicException;
import edu.wustl.common.exception.ErrorKey;
import edu.wustl.common.factory.AbstractFactoryConfig;
import edu.wustl.common.factory.IFactory;
import edu.wustl.common.util.global.ApplicationProperties;
import edu.wustl.common.util.global.CommonUtilities;
import edu.wustl.common.util.global.Status;
import edu.wustl.common.util.global.Validator;
import edu.wustl.common.util.logger.Logger;
import edu.wustl.dao.DAO;
import edu.wustl.dao.HibernateDAO;
import edu.wustl.dao.JDBCDAO;
import edu.wustl.dao.QueryWhereClause;
import edu.wustl.dao.condition.EqualClause;
import edu.wustl.dao.condition.NotEqualClause;
import edu.wustl.dao.exception.DAOException;
import edu.wustl.dao.query.generator.DBTypes;
import edu.wustl.dao.util.HibernateMetaData;
import edu.wustl.dao.util.NamedQueryParam;
import edu.wustl.security.exception.UserNotAuthorizedException;

/**
 * @author mandar_deshmukh
 * This class contains the Business Logic for all EventParameters Classes.
 * This will be the class which will be used for data transactions of the EventParameters.
 */
public class SpecimenEventParametersBizLogic extends CatissueDefaultBizLogic
{

	/**
	 * Logger object.
	 */
	private static final Logger LOGGER = Logger
			.getCommonLogger(SpecimenEventParametersBizLogic.class);
	/**
	 * storageContainerIds.
	 */
	private HashSet<String> storageContainerIds = new HashSet<String>();

	/**
	 * Saves the FrozenEventParameters object in the database.
	 * @param obj The FrozenEventParameters object to be saved.
	 * @param dao DAO Object
	 * @param sessionDataBean SessionDataBean object
	 * @throws BizLogicException throws BizLogicException.
	 */
	@Override
	protected void insert(Object obj, DAO dao, SessionDataBean sessionDataBean)
			throws BizLogicException
	{
		try
		{
			final IFactory factory = AbstractFactoryConfig.getInstance().getBizLogicFactory();
			final NewSpecimenBizLogic newSpecimenBizLogic = (NewSpecimenBizLogic) factory
					.getBizLogic(Constants.NEW_SPECIMEN_FORM_ID);
			final List specimenIds = new ArrayList();
			
			if (obj instanceof List)
			{
				final List eventObjectsList = (List) obj;
				checkStatusAndGetUserId((SpecimenEventParameters)eventObjectsList.get(0), dao);
				for (int i = 0; i < eventObjectsList.size(); i++)
				{
					specimenIds.add(((SpecimenEventParameters) eventObjectsList.get(i)).getSpecimen()
							.getId());
				}
	
				for (int i = 0; i < eventObjectsList.size(); i++)
				{
					this.insertEvent(eventObjectsList.get(i), dao, sessionDataBean,
							newSpecimenBizLogic, specimenIds);
				}
			}
			else
			{
				checkStatusAndGetUserId((SpecimenEventParameters)obj, dao);
				this.insertEvent(obj, dao, sessionDataBean, newSpecimenBizLogic, specimenIds);
			}
		}
		catch (final DAOException daoExp)
		{
			this.LOGGER.error(daoExp.getMessage(), daoExp);
			throw this
					.getBizLogicException(daoExp, daoExp.getErrorKeyName(), daoExp.getMsgValues());
		}
	}
	/**
	 * Saves the SpecimenEventParameters object in the database.
	 * @param specimenIdentifiers Single Specimen identifier OR Multiple Comma Sperated Specimen Identifier
	 * @param obj SpecimenEventParameter Object
	 * @param sessionDataBean SessionDataBean object
	 * @return return Long of newly created Event Identifier
	 * @throws BizLogicException throws CloneNotSupportedException,ApplicationException.
	 */
	public Long insertBulkSpecimenEvents(String specimenIdentifiers,SpecimenEventParameters obj,
			SessionDataBean sessionDataBean) throws CloneNotSupportedException, ApplicationException
	{
		Long createdObjectId=null;
		DAO dao = null;
		final IFactory factory = AbstractFactoryConfig.getInstance().getBizLogicFactory();
		final NewSpecimenBizLogic newSpecimenBizLogic = (NewSpecimenBizLogic) factory
				.getBizLogic(Constants.NEW_SPECIMEN_FORM_ID);
		//List<String> idList=getSpecimenIdList(specimenIdentifiers);
		List<String> idList=AppUtility.getListOnCommaToken(specimenIdentifiers);
		Iterator<String> specimenIdIt=idList.iterator();
		final List<String> specimenIds = new ArrayList<String>();
		//add user validation
		try 
		{
			dao = AppUtility.openDAOSession(sessionDataBean);
			checkStatusAndGetUserId(obj, dao);
			while(specimenIdIt.hasNext())
			{
			
					SpecimenEventParameters specimenEventParameters=(SpecimenEventParameters) obj.clone();
					Specimen specimen=new Specimen();
					specimen.setId(Long.valueOf(specimenIdIt.next()));
					specimenEventParameters.setSpecimen(specimen);
					insertEvent(specimenEventParameters, dao, sessionDataBean, newSpecimenBizLogic, specimenIds);
					createdObjectId=specimenEventParameters.getId();
					
			}
			dao.commit();
		}
		catch (final DAOException daoException) {
				LOGGER.error(daoException.getMessage(),daoException);
				throw AppUtility.getApplicationException(daoException, daoException.getErrorKeyName(),
						daoException.getMsgValues());	
			}
		
			finally
			{
				AppUtility.closeDAOSession(dao);
			}
		
		return createdObjectId;
	}
	
	/**
{'fraction_non_alphanumeric': 0.09327237991266375, 'fraction_numerical': 0.0013646288209606986, 'mean_word_length': 5.543303571428571, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 21, 'https://': 1, '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': '5015363', 'n_tokens_mistral': 5067, 'n_tokens_neox': 4684, 'n_words': 857}