<commit_before>/*
 * <p>Title: SpecimenArrayBizLogic Class </p>
 * <p>Description:This class performs business level logic for Specimen Array</p>
 * Copyright: Copyright (c) year 2006
 * Company: Washington University, School of Medicine, St. Louis.
 * @version 1.1
 * Created on Aug 28,2006
 */

package edu.wustl.catissuecore.bizlogic;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import edu.wustl.catissuecore.domain.CellSpecimen;
import edu.wustl.catissuecore.domain.FluidSpecimen;
import edu.wustl.catissuecore.domain.MolecularSpecimen;
import edu.wustl.catissuecore.domain.QuantityInMicrogram;
import edu.wustl.catissuecore.domain.Specimen;
import edu.wustl.catissuecore.domain.SpecimenArray;
import edu.wustl.catissuecore.domain.SpecimenArrayContent;
import edu.wustl.catissuecore.domain.SpecimenArrayType;
import edu.wustl.catissuecore.domain.TissueSpecimen;
import edu.wustl.catissuecore.util.ApiSearchUtil;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.common.beans.SessionDataBean;
import edu.wustl.common.bizlogic.DefaultBizLogic;
import edu.wustl.common.dao.DAO;
import edu.wustl.common.security.exceptions.UserNotAuthorizedException;
import edu.wustl.common.util.dbManager.DAOException;

/**
 * <p>This class initializes the fields of SpecimenArrayBizLogic.java</p>
 * @author <NAME>
 * @version 1.1
 */
public class SpecimenArrayBizLogic extends DefaultBizLogic
{

	/**
	 * @see edu.wustl.common.bizlogic.AbstractBizLogic#insert(java.lang.Object, edu.wustl.common.dao.DAO, edu.wustl.common.beans.SessionDataBean)
	 */
	protected void insert(Object obj, DAO dao, SessionDataBean sessionDataBean)
			throws DAOException, UserNotAuthorizedException
	{
		SpecimenArray specimenArray = (SpecimenArray) obj;		
		doUpdateSpecimenArrayContents(specimenArray, dao, sessionDataBean, true);

		dao.insert(specimenArray.getCapacity(), sessionDataBean, true, false);
		dao.insert(specimenArray, sessionDataBean, true, false);
		SpecimenArrayContent specimenArrayContent = null;
		// TODO move this method to HibernateDAOImpl for common use (for collection insertion)
		for (Iterator iter = specimenArray.getSpecimenArrayContentCollection().iterator(); iter
				.hasNext();)
		{
			specimenArrayContent = (SpecimenArrayContent) iter.next();
			specimenArrayContent.setSpecimenArray(specimenArray);
			dao.insert(specimenArrayContent, sessionDataBean, true, false);
		}
	}

	/**
	 * @see edu.wustl.common.bizlogic.AbstractBizLogic#update(edu.wustl.common.dao.DAO, java.lang.Object, java.lang.Object, edu.wustl.common.beans.SessionDataBean)
	 */
	protected void update(DAO dao, Object obj, Object oldObj, SessionDataBean sessionDataBean)
			throws DAOException, UserNotAuthorizedException
	{
		SpecimenArray specimenArray = (SpecimenArray) obj;
		doUpdateSpecimenArrayContents(specimenArray, dao, sessionDataBean, false);
		dao.update(specimenArray.getCapacity(), sessionDataBean, true, false, false);
		dao.update(specimenArray, sessionDataBean, true, false, false);
		SpecimenArrayContent specimenArrayContent = null;
		//SpecimenArray oldSpecimenArray = (SpecimenArray) oldObj;
		Collection oldSpecArrayContents = ((SpecimenArray) oldObj)
				.getSpecimenArrayContentCollection();

		for (Iterator iter = specimenArray.getSpecimenArrayContentCollection().iterator(); iter
				.hasNext();)
		{
			specimenArrayContent = (SpecimenArrayContent) iter.next();
			specimenArrayContent.setSpecimenArray(specimenArray);
			// increment by 1 because of array index starts from 0.
			if (specimenArrayContent.getPositionDimensionOne() != null)
			{
				specimenArrayContent.setPositionDimensionOne(new Integer(specimenArrayContent
						.getPositionDimensionOne().intValue() + 1));
				specimenArrayContent.setPositionDimensionTwo(new Integer(specimenArrayContent
						.getPositionDimensionTwo().intValue() + 1));
			}

			if (isNewSpecimenArrayContent(specimenArrayContent, oldSpecArrayContents))
			{
				dao.insert(specimenArrayContent, sessionDataBean, true, false);
			}
			else
			{
				dao.update(specimenArrayContent, sessionDataBean, true, false, false);
			}
		}
	}

	/**
	 * @param specimenArrayContent array contents
	 * @param oldSpecArrayContents old spec array contents
	 * @return whether it is new or old
	 */
	private boolean isNewSpecimenArrayContent(SpecimenArrayContent specimenArrayContent,
			Collection oldSpecArrayContents)
	{
		boolean isNew = true;
		SpecimenArrayContent arrayContent = null;

		for (Iterator iter = oldSpecArrayContents.iterator(); iter.hasNext();)
		{
			arrayContent = (SpecimenArrayContent) iter.next();

			if (specimenArrayContent.getId() == null)
			{
				isNew = true;
				break;
			}
			else if (arrayContent.getId().longValue() == specimenArrayContent.getId().longValue())
			{
				isNew = false;
				break;
			}
		}
		return isNew;
	}

	/**
	 * @param specimenArray specimen array
	 * @param dao dao
	 * @param sessionDataBean session data bean
	 * @param isInsertOperation is insert operation
	 * @throws DAOException 
	 * @throws UserNotAuthorizedException
	 */
	private void doUpdateSpecimenArrayContents(SpecimenArray specimenArray, DAO dao,
			SessionDataBean sessionDataBean, boolean isInsertOperation) throws DAOException,
			UserNotAuthorizedException
	{
		Collection specimenArrayContentCollection = specimenArray
				.getSpecimenArrayContentCollection();
		Collection updatedSpecArrayContentCollection = new HashSet();
		SpecimenArrayContent specimenArrayContent = null;
		Specimen specimen = null;
		if (specimenArrayContentCollection != null && !specimenArrayContentCollection.isEmpty())
		{
			double quantity = 0.0;
			// fetch array type to check specimen class
			List arrayTypes = dao.retrieve(SpecimenArrayType.class.getName(),
					Constants.SYSTEM_IDENTIFIER, specimenArray.getSpecimenArrayType().getId());
			SpecimenArrayType arrayType = null;

			if ((arrayTypes != null) && (!arrayTypes.isEmpty()))
			{
				arrayType = (SpecimenArrayType) arrayTypes.get(0);
			}

			for (Iterator iter = specimenArrayContentCollection.iterator(); iter.hasNext();)
			{
				specimenArrayContent = (SpecimenArrayContent) iter.next();
				
				/**
				 * 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.setSpecimenArrayContentDefault(specimenArrayContent);
				//End:- Change for API Search 
				
				specimen = getSpecimen(dao, specimenArrayContent);
				if (specimen != null)
				{
					// check whether array & specimen are compatible on the basis of class
					if (!isArrayAndSpecimenCompatibile(arrayType, specimen))
					{
						throw new DAOException(
								Constants.ARRAY_SPEC_NOT_COMPATIBLE_EXCEPTION_MESSAGE);<commit_msg>Comment ApiSearchUtil.setSpecimenArrayContentDefault(specimenArrayContent) to remove error while adding aliquots.<commit_after>/*
 * <p>Title: SpecimenArrayBizLogic Class </p>
 * <p>Description:This class performs business level logic for Specimen Array</p>
 * Copyright: Copyright (c) year 2006
 * Company: Washington University, School of Medicine, St. Louis.
 * @version 1.1
 * Created on Aug 28,2006
 */

package edu.wustl.catissuecore.bizlogic;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import edu.wustl.catissuecore.domain.CellSpecimen;
import edu.wustl.catissuecore.domain.FluidSpecimen;
import edu.wustl.catissuecore.domain.MolecularSpecimen;
import edu.wustl.catissuecore.domain.QuantityInMicrogram;
import edu.wustl.catissuecore.domain.Specimen;
import edu.wustl.catissuecore.domain.SpecimenArray;
import edu.wustl.catissuecore.domain.SpecimenArrayContent;
import edu.wustl.catissuecore.domain.SpecimenArrayType;
import edu.wustl.catissuecore.domain.TissueSpecimen;
import edu.wustl.catissuecore.util.ApiSearchUtil;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.common.beans.SessionDataBean;
import edu.wustl.common.bizlogic.DefaultBizLogic;
import edu.wustl.common.dao.DAO;
import edu.wustl.common.security.exceptions.UserNotAuthorizedException;
import edu.wustl.common.util.dbManager.DAOException;

/**
 * <p>This class initializes the fields of SpecimenArrayBizLogic.java</p>
 * @author <NAME>
 * @version 1.1
 */
public class SpecimenArrayBizLogic extends DefaultBizLogic
{

	/**
	 * @see edu.wustl.common.bizlogic.AbstractBizLogic#insert(java.lang.Object, edu.wustl.common.dao.DAO, edu.wustl.common.beans.SessionDataBean)
	 */
	protected void insert(Object obj, DAO dao, SessionDataBean sessionDataBean)
			throws DAOException, UserNotAuthorizedException
	{
		SpecimenArray specimenArray = (SpecimenArray) obj;		
		doUpdateSpecimenArrayContents(specimenArray, dao, sessionDataBean, true);

		dao.insert(specimenArray.getCapacity(), sessionDataBean, true, false);
		dao.insert(specimenArray, sessionDataBean, true, false);
		SpecimenArrayContent specimenArrayContent = null;
		// TODO move this method to HibernateDAOImpl for common use (for collection insertion)
		for (Iterator iter = specimenArray.getSpecimenArrayContentCollection().iterator(); iter
				.hasNext();)
		{
			specimenArrayContent = (SpecimenArrayContent) iter.next();
			specimenArrayContent.setSpecimenArray(specimenArray);
			dao.insert(specimenArrayContent, sessionDataBean, true, false);
		}
	}

	/**
	 * @see edu.wustl.common.bizlogic.AbstractBizLogic#update(edu.wustl.common.dao.DAO, java.lang.Object, java.lang.Object, edu.wustl.common.beans.SessionDataBean)
	 */
	protected void update(DAO dao, Object obj, Object oldObj, SessionDataBean sessionDataBean)
			throws DAOException, UserNotAuthorizedException
	{
		SpecimenArray specimenArray = (SpecimenArray) obj;
		doUpdateSpecimenArrayContents(specimenArray, dao, sessionDataBean, false);
		dao.update(specimenArray.getCapacity(), sessionDataBean, true, false, false);
		dao.update(specimenArray, sessionDataBean, true, false, false);
		SpecimenArrayContent specimenArrayContent = null;
		//SpecimenArray oldSpecimenArray = (SpecimenArray) oldObj;
		Collection oldSpecArrayContents = ((SpecimenArray) oldObj)
				.getSpecimenArrayContentCollection();

		for (Iterator iter = specimenArray.getSpecimenArrayContentCollection().iterator(); iter
				.hasNext();)
		{
			specimenArrayContent = (SpecimenArrayContent) iter.next();
			specimenArrayContent.setSpecimenArray(specimenArray);
			// increment by 1 because of array index starts from 0.
			if (specimenArrayContent.getPositionDimensionOne() != null)
			{
				specimenArrayContent.setPositionDimensionOne(new Integer(specimenArrayContent
						.getPositionDimensionOne().intValue() + 1));
				specimenArrayContent.setPositionDimensionTwo(new Integer(specimenArrayContent
						.getPositionDimensionTwo().intValue() + 1));
			}

			if (isNewSpecimenArrayContent(specimenArrayContent, oldSpecArrayContents))
			{
				dao.insert(specimenArrayContent, sessionDataBean, true, false);
			}
			else
			{
				dao.update(specimenArrayContent, sessionDataBean, true, false, false);
			}
		}
	}

	/**
	 * @param specimenArrayContent array contents
	 * @param oldSpecArrayContents old spec array contents
	 * @return whether it is new or old
	 */
	private boolean isNewSpecimenArrayContent(SpecimenArrayContent specimenArrayContent,
			Collection oldSpecArrayContents)
	{
		boolean isNew = true;
		SpecimenArrayContent arrayContent = null;

		for (Iterator iter = oldSpecArrayContents.iterator(); iter.hasNext();)
		{
			arrayContent = (SpecimenArrayContent) iter.next();

			if (specimenArrayContent.getId() == null)
			{
				isNew = true;
				break;
			}
			else if (arrayContent.getId().longValue() == specimenArrayContent.getId().longValue())
			{
				isNew = false;
				break;
			}
		}
		return isNew;
	}

	/**
	 * @param specimenArray specimen array
	 * @param dao dao
	 * @param sessionDataBean session data bean
	 * @param isInsertOperation is insert operation
	 * @throws DAOException 
	 * @throws UserNotAuthorizedException
	 */
	private void doUpdateSpecimenArrayContents(SpecimenArray specimenArray, DAO dao,
			SessionDataBean sessionDataBean, boolean isInsertOperation) throws DAOException,
			UserNotAuthorizedException
	{
		Collection specimenArrayContentCollection = specimenArray
				.getSpecimenArrayContentCollection();
		Collection updatedSpecArrayContentCollection = new HashSet();
		SpecimenArrayContent specimenArrayContent = null;
		Specimen specimen = null;
		if (specimenArrayContentCollection != null && !specimenArrayContentCollection.isEmpty())
		{
			double quantity = 0.0;
			// fetch array type to check specimen class
			List arrayTypes = dao.retrieve(SpecimenArrayType.class.getName(),
					Constants.SYSTEM_IDENTIFIER, specimenArray.getSpecimenArrayType().getId());
			SpecimenArrayType arrayType = null;

			if ((arrayTypes != null) && (!arrayTypes.isEmpty()))
			{
				arrayType = (SpecimenArrayType) arrayTypes.get(0);
			}

			for (Iterator iter = specimenArrayContentCollection.iterator(); iter.hasNext();)
			{
				specimenArrayContent = (SpecimenArrayContent) iter.next();
				
				/**
				 * 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.setSpecimenArrayContentDefault(specimenArrayContent);
				//End:- Change for API Search 
				
				specimen = getSpecimen(dao, specimenArrayContent);
				if (specimen != null)
				{
					// check whether array & specimen are compatible on the basis of class
					if (!isArrayAndSpecimenCompatibile(arrayType, specimen))
					{
						throw new DAOException(
								Constants.ARRAY_SPEC_NOT_COMPATIBLE_EXCEPTION_MESSAGE);
{'fraction_non_alphanumeric': 0.09298863021115322, 'fraction_numerical': 0.00392528424472117, 'mean_word_length': 4.8778838504375495, 'pattern_counts': {'":': 0, '<': 17, '<?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': '2405641', 'n_tokens_mistral': 4926, 'n_tokens_neox': 4309, 'n_words': 1024}