<commit_before>/**
 * <p>Title: NewSpecimenHDAO Class>
 * <p>Description:	NewSpecimenBizLogicHDAO is used to add new specimen information into the database using Hibernate.</p>
 * Copyright:    Copyright (c) year
 * Company: Washington University, School of Medicine, St. Louis.
 * @author <NAME>
 * @version 1.00
 * Created on Jul 21, 2005
 */

package edu.wustl.catissuecore.bizlogic;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
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 net.sf.hibernate.HibernateException;
import edu.wustl.catissuecore.domain.Address;
import edu.wustl.catissuecore.domain.Biohazard;
import edu.wustl.catissuecore.domain.CellSpecimen;
import edu.wustl.catissuecore.domain.CollectionEventParameters;
import edu.wustl.catissuecore.domain.DisposalEventParameters;
import edu.wustl.catissuecore.domain.DistributedItem;
import edu.wustl.catissuecore.domain.ExternalIdentifier;
import edu.wustl.catissuecore.domain.FluidSpecimen;
import edu.wustl.catissuecore.domain.MolecularSpecimen;
import edu.wustl.catissuecore.domain.QuantityInCount;
import edu.wustl.catissuecore.domain.QuantityInGram;
import edu.wustl.catissuecore.domain.QuantityInMicrogram;
import edu.wustl.catissuecore.domain.QuantityInMilliliter;
import edu.wustl.catissuecore.domain.ReceivedEventParameters;
import edu.wustl.catissuecore.domain.Specimen;
import edu.wustl.catissuecore.domain.SpecimenCharacteristics;
import edu.wustl.catissuecore.domain.SpecimenCollectionGroup;
import edu.wustl.catissuecore.domain.SpecimenEventParameters;
import edu.wustl.catissuecore.domain.StorageContainer;
import edu.wustl.catissuecore.domain.TissueSpecimen;
import edu.wustl.catissuecore.domain.User;
import edu.wustl.catissuecore.integration.IntegrationManager;
import edu.wustl.catissuecore.integration.IntegrationManagerFactory;
import edu.wustl.catissuecore.util.ApiSearchUtil;
import edu.wustl.catissuecore.util.StorageContainerUtil;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.catissuecore.util.global.Utility;
import edu.wustl.common.beans.SessionDataBean;
import edu.wustl.common.cde.CDEManager;
import edu.wustl.common.dao.DAO;
import edu.wustl.common.dao.DAOFactory;
import edu.wustl.common.dao.JDBCDAO;
import edu.wustl.common.domain.AbstractDomainObject;
import edu.wustl.common.exception.BizLogicException;
import edu.wustl.common.security.SecurityManager;
import edu.wustl.common.security.exceptions.SMException;
import edu.wustl.common.security.exceptions.UserNotAuthorizedException;
import edu.wustl.common.util.dbManager.DAOException;
import edu.wustl.common.util.global.ApplicationProperties;
import edu.wustl.common.util.global.Validator;
import edu.wustl.common.util.logger.Logger;

/**
 * NewSpecimenHDAO is used to add new specimen information into the database using Hibernate.
 * @author aniruddha_phadnis
 */
public class NewSpecimenBizLogic extends IntegrationBizLogic
{

	/**
	 * Saves the storageType object in the database.
	 * @param obj The storageType object to be saved.
	 * @param session The session in which the object is saved.
	 * @throws DAOException 
	 */
	protected void insert(Object obj, DAO dao, SessionDataBean sessionDataBean) throws DAOException, UserNotAuthorizedException
	{
		if (obj instanceof Map)
		{
			insertMultipleSpecimen((Map) obj, dao, sessionDataBean);
		}
		else
		{
			insertSingleSpecimen((Specimen) obj, dao, sessionDataBean, false);

			//Inserting authorization data 
			Specimen specimen = (Specimen) obj;
			Set protectionObjects = new HashSet();
			protectionObjects.add(specimen);
			if (specimen.getSpecimenCharacteristics() != null)
			{
				protectionObjects.add(specimen.getSpecimenCharacteristics());
			}
			try
			{
				SecurityManager.getInstance(this.getClass()).insertAuthorizationData(null, protectionObjects, getDynamicGroups(specimen));
			}
			catch (SMException e)
			{
				throw handleSMException(e);
			}

		}
	}

	/**
	 * Insert multiple specimen into the data base.
	 * @param specimenList
	 * @param dao
	 * @param sessionDataBean
	 * @throws DAOException
	 * @throws UserNotAuthorizedException
	 */
	private void insertMultipleSpecimen(Map specimenMap, DAO dao, SessionDataBean sessionDataBean) throws DAOException, UserNotAuthorizedException
	{
		List specimenList = new ArrayList();
		Iterator specimenIterator = specimenMap.keySet().iterator();
		int count = 0;
		while (specimenIterator.hasNext())
		{
			count++;
			Specimen specimen = (Specimen) specimenIterator.next();

			/**
			 * Name : <NAME>
			 * Reviewer's Name: <NAME>
			 * Bug ID: 3262
			 * Patch ID: 3262_3
			 * See also: 1-4
			 * Description: Adding Default Events if user has not entered them
			 */
			if(specimen.getSpecimenEventCollection() == null || specimen.getSpecimenEventCollection().isEmpty())
			{
				Collection specimenEventColl = new HashSet();
				User user = new User();
				user.setId(sessionDataBean.getUserId());
				CollectionEventParameters collectionEventParameters = populateCollectionEventParameters(specimen,user);				
				specimenEventColl.add(collectionEventParameters);
				
				ReceivedEventParameters receivedEventParameters = populateReceivedEventParameters(specimen,user);								
				specimenEventColl.add(receivedEventParameters);
				
				specimen.setSpecimenEventCollection(specimenEventColl);
			}
			/**
			 * Start: Change for API Search   --- Jitendra 06/10/2006
			 * In Case of Api Search, previoulsy it was failing since there was default class level initialization 
			 * on domain object. For example in User object, it was initialized as protected String lastName=""; 
			 * So we removed default class level initialization on domain object and are initializing in method
			 * setAllValues() of domain object. But in case of Api Search, default values will not get set 
			 * since setAllValues() method of domainObject will not get called. To avoid null pointer exception,
			 * we are setting the default values same as we were setting in setAllValues() method of domainObject.
			 */
			ApiSearchUtil.setSpecimenDefault(specimen);
			//End:- Change for API Search

			Long parentSpecimenId = specimen.getId();

			resetId(specimen);
<commit_msg>Set created date to collected event date if this specimen is not derived one in case of multiple specimen.<commit_after>/**
 * <p>Title: NewSpecimenHDAO Class>
 * <p>Description:	NewSpecimenBizLogicHDAO is used to add new specimen information into the database using Hibernate.</p>
 * Copyright:    Copyright (c) year
 * Company: Washington University, School of Medicine, St. Louis.
 * @author <NAME>
 * @version 1.00
 * Created on Jul 21, 2005
 */

package edu.wustl.catissuecore.bizlogic;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
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 net.sf.hibernate.HibernateException;
import edu.wustl.catissuecore.domain.Address;
import edu.wustl.catissuecore.domain.Biohazard;
import edu.wustl.catissuecore.domain.CellSpecimen;
import edu.wustl.catissuecore.domain.CollectionEventParameters;
import edu.wustl.catissuecore.domain.DisposalEventParameters;
import edu.wustl.catissuecore.domain.DistributedItem;
import edu.wustl.catissuecore.domain.ExternalIdentifier;
import edu.wustl.catissuecore.domain.FluidSpecimen;
import edu.wustl.catissuecore.domain.MolecularSpecimen;
import edu.wustl.catissuecore.domain.QuantityInCount;
import edu.wustl.catissuecore.domain.QuantityInGram;
import edu.wustl.catissuecore.domain.QuantityInMicrogram;
import edu.wustl.catissuecore.domain.QuantityInMilliliter;
import edu.wustl.catissuecore.domain.ReceivedEventParameters;
import edu.wustl.catissuecore.domain.Specimen;
import edu.wustl.catissuecore.domain.SpecimenCharacteristics;
import edu.wustl.catissuecore.domain.SpecimenCollectionGroup;
import edu.wustl.catissuecore.domain.SpecimenEventParameters;
import edu.wustl.catissuecore.domain.StorageContainer;
import edu.wustl.catissuecore.domain.TissueSpecimen;
import edu.wustl.catissuecore.domain.User;
import edu.wustl.catissuecore.integration.IntegrationManager;
import edu.wustl.catissuecore.integration.IntegrationManagerFactory;
import edu.wustl.catissuecore.util.ApiSearchUtil;
import edu.wustl.catissuecore.util.StorageContainerUtil;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.catissuecore.util.global.Utility;
import edu.wustl.common.beans.SessionDataBean;
import edu.wustl.common.cde.CDEManager;
import edu.wustl.common.dao.DAO;
import edu.wustl.common.dao.DAOFactory;
import edu.wustl.common.dao.JDBCDAO;
import edu.wustl.common.domain.AbstractDomainObject;
import edu.wustl.common.exception.BizLogicException;
import edu.wustl.common.security.SecurityManager;
import edu.wustl.common.security.exceptions.SMException;
import edu.wustl.common.security.exceptions.UserNotAuthorizedException;
import edu.wustl.common.util.dbManager.DAOException;
import edu.wustl.common.util.global.ApplicationProperties;
import edu.wustl.common.util.global.Validator;
import edu.wustl.common.util.logger.Logger;

/**
 * NewSpecimenHDAO is used to add new specimen information into the database using Hibernate.
 * @author aniruddha_phadnis
 */
public class NewSpecimenBizLogic extends IntegrationBizLogic
{

	/**
	 * Saves the storageType object in the database.
	 * @param obj The storageType object to be saved.
	 * @param session The session in which the object is saved.
	 * @throws DAOException 
	 */
	protected void insert(Object obj, DAO dao, SessionDataBean sessionDataBean) throws DAOException, UserNotAuthorizedException
	{
		if (obj instanceof Map)
		{
			insertMultipleSpecimen((Map) obj, dao, sessionDataBean);
		}
		else
		{
			insertSingleSpecimen((Specimen) obj, dao, sessionDataBean, false);

			//Inserting authorization data 
			Specimen specimen = (Specimen) obj;
			Set protectionObjects = new HashSet();
			protectionObjects.add(specimen);
			if (specimen.getSpecimenCharacteristics() != null)
			{
				protectionObjects.add(specimen.getSpecimenCharacteristics());
			}
			try
			{
				SecurityManager.getInstance(this.getClass()).insertAuthorizationData(null, protectionObjects, getDynamicGroups(specimen));
			}
			catch (SMException e)
			{
				throw handleSMException(e);
			}

		}
	}

	/**
	 * Insert multiple specimen into the data base.
	 * @param specimenList
	 * @param dao
	 * @param sessionDataBean
	 * @throws DAOException
	 * @throws UserNotAuthorizedException
	 */
	private void insertMultipleSpecimen(Map specimenMap, DAO dao, SessionDataBean sessionDataBean) throws DAOException, UserNotAuthorizedException
	{
		List specimenList = new ArrayList();
		Iterator specimenIterator = specimenMap.keySet().iterator();
		int count = 0;
		while (specimenIterator.hasNext())
		{
			count++;
			Specimen specimen = (Specimen) specimenIterator.next();

			/**
			 * Name : <NAME>
			 * Reviewer's Name: <NAME>
			 * Bug ID: 3262
			 * Patch ID: 3262_3
			 * See also: 1-4
			 * Description: Adding Default Events if user has not entered them
			 */
			if(specimen.getSpecimenEventCollection() == null || specimen.getSpecimenEventCollection().isEmpty())
			{
				Collection specimenEventColl = new HashSet();
				User user = new User();
				user.setId(sessionDataBean.getUserId());
				CollectionEventParameters collectionEventParameters = populateCollectionEventParameters(specimen,user);				
				specimenEventColl.add(collectionEventParameters);
				
				ReceivedEventParameters receivedEventParameters = populateReceivedEventParameters(specimen,user);								
				specimenEventColl.add(receivedEventParameters);
				
				specimen.setSpecimenEventCollection(specimenEventColl);
			}
			/**
			 * Start: Change for API Search   --- Jitendra 06/10/2006
			 * In Case of Api Search, previoulsy it was failing since there was default class level initialization 
			 * on domain object. For example in User object, it was initialized as protected String lastName=""; 
			 * So we removed default class level initialization on domain object and are initializing in method
			 * setAllValues() of domain object. But in case of Api Search, default values will not get set 
			 * since setAllValues() method of domainObject will not get called. To avoid null pointer exception,
			 * we are setting the default values same as we were setting in setAllValues() method of domainObject.
			 */
			ApiSearchUtil.setSpecimenDefault(specimen);
			//End:- Change for API Search

			Long parentSpecimenId = specimen.getId();

			resetId(specimen);
            
            /**
              * Patch ID: 3835_1_21
              * See also: 1_1 to 1_5
              * Description : set created date to collected event date if this specimen is not derived one in case of multiple specimen.
              */
            
            if((specimen.getParentSpecimen() == null ))
            {              
                Collection specimenEventsCollection = specimen.getSpecimenEventCollection();
                Iterator specimenEventsCollectionIterator = specimenEventsCollection.iterator();
                while (specimenEventsCollectionIterator.hasNext())
                {               
                      Object eventObject = specimenEventsCollectionIterator.next();
                      if (eventObject instanceof CollectionEventParameters)
                      {
                          CollectionEventParameters collEventParam=(CollectionEventParameters)eventObject;
                          specimen.setCreatedOn(collEventParam.getTimestamp());
                      }
                }               
            }
            

{'fraction_non_alphanumeric': 0.0882875204204844, 'fraction_numerical': 0.004900916258256978, 'mean_word_length': 4.297215951843492, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '6615251', 'n_tokens_mistral': 4585, 'n_tokens_neox': 4082, 'n_words': 964}