<commit_before>/**
 * <p>Title: UserHDAO Class>
 * <p>Description:	UserHDAO is used to add user 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 Apr 13, 2005
 */

package edu.wustl.catissuecore.bizlogic;

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.Stack;

import edu.wustl.catissuecore.domain.CollectionEventParameters;
import edu.wustl.catissuecore.domain.CollectionProtocol;
import edu.wustl.catissuecore.domain.CollectionProtocolEvent;
import edu.wustl.catissuecore.domain.CollectionProtocolRegistration;
import edu.wustl.catissuecore.domain.ConsentTierStatus;
import edu.wustl.catissuecore.domain.Participant;
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.User;
import edu.wustl.catissuecore.util.ApiSearchUtil;
import edu.wustl.catissuecore.util.CollectionProtocolUtil;
import edu.wustl.catissuecore.util.EventsUtil;
import edu.wustl.catissuecore.util.WithdrawConsentUtil;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.catissuecore.util.global.Utility;
import edu.wustl.common.beans.SessionDataBean;
import edu.wustl.common.bizlogic.DefaultBizLogic;
import edu.wustl.common.cde.CDEManager;
import edu.wustl.common.dao.AbstractDAO;
import edu.wustl.common.dao.DAO;
import edu.wustl.common.dao.DAOFactory;
import edu.wustl.common.dao.HibernateDAO;
import edu.wustl.common.domain.AbstractDomainObject;
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;

/**
 * UserHDAO is used to add user information into the database using Hibernate.
 * 
 * @author kapil_kaveeshwar
 */
public class SpecimenCollectionGroupBizLogic extends DefaultBizLogic
{
	/**
	 * Saves the user object in the database.
	 * 
	 * @param obj
	 *            The user 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
	{
		SpecimenCollectionGroup specimenCollectionGroup = (SpecimenCollectionGroup) obj;

		if (specimenCollectionGroup.getSpecimenCollectionSite() != null)
		{
			Object siteObj = dao.retrieve(Site.class.getName(), specimenCollectionGroup.getSpecimenCollectionSite().getId());
			if (siteObj != null)
			{
				// check for closed Site
				checkStatus(dao, specimenCollectionGroup.getSpecimenCollectionSite(), "Site");

				specimenCollectionGroup.setSpecimenCollectionSite((Site) siteObj);
			}
		}
		Object collectionProtocolEventObj = dao.retrieve(CollectionProtocolEvent.class.getName(), specimenCollectionGroup
				.getCollectionProtocolEvent().getId());
		if (collectionProtocolEventObj != null)
		{
			CollectionProtocolEvent cpe = (CollectionProtocolEvent) collectionProtocolEventObj;

			// check for closed CollectionProtocol
			checkStatus(dao, cpe.getCollectionProtocol(), "Collection Protocol");

			specimenCollectionGroup.setCollectionProtocolEvent(cpe);
		}

		setCollectionProtocolRegistration(dao, specimenCollectionGroup, null);

		dao.insert(specimenCollectionGroup, sessionDataBean, true, true);

		insertAuthData(specimenCollectionGroup);
	}

	/**
	 * @param specimenCollectionGroup
	 * @throws DAOException
	 */
	public void insertAuthData(SpecimenCollectionGroup specimenCollectionGroup) throws DAOException
	{
		try
		{
			SecurityManager.getInstance(this.getClass()).insertAuthorizationData(null, getProtectionObjects(specimenCollectionGroup),
					getDynamicGroups(specimenCollectionGroup));
		}
		catch (SMException e)
		{
			throw handleSMException(e);
		}
	}

	private Set getProtectionObjects(AbstractDomainObject obj)
	{
		Set protectionObjects = new HashSet();

		SpecimenCollectionGroup specimenCollectionGroup = (SpecimenCollectionGroup) obj;
		protectionObjects.add(specimenCollectionGroup);

		Logger.out.debug(protectionObjects.toString());
		return protectionObjects;
	}

	protected String[] getDynamicGroups(AbstractDomainObject obj) throws SMException<commit_msg>new function added in scgbizlogic to get scgobject from id<commit_after>/**
 * <p>Title: UserHDAO Class>
 * <p>Description:	UserHDAO is used to add user 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 Apr 13, 2005
 */

package edu.wustl.catissuecore.bizlogic;

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.Stack;

import edu.wustl.catissuecore.domain.CollectionEventParameters;
import edu.wustl.catissuecore.domain.CollectionProtocol;
import edu.wustl.catissuecore.domain.CollectionProtocolEvent;
import edu.wustl.catissuecore.domain.CollectionProtocolRegistration;
import edu.wustl.catissuecore.domain.ConsentTierStatus;
import edu.wustl.catissuecore.domain.Participant;
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.User;
import edu.wustl.catissuecore.util.ApiSearchUtil;
import edu.wustl.catissuecore.util.CollectionProtocolUtil;
import edu.wustl.catissuecore.util.EventsUtil;
import edu.wustl.catissuecore.util.WithdrawConsentUtil;
import edu.wustl.catissuecore.util.global.Constants;
import edu.wustl.catissuecore.util.global.Utility;
import edu.wustl.common.beans.SessionDataBean;
import edu.wustl.common.bizlogic.DefaultBizLogic;
import edu.wustl.common.cde.CDEManager;
import edu.wustl.common.dao.AbstractDAO;
import edu.wustl.common.dao.DAO;
import edu.wustl.common.dao.DAOFactory;
import edu.wustl.common.dao.HibernateDAO;
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;

/**
 * UserHDAO is used to add user information into the database using Hibernate.
 * 
 * @author kapil_kaveeshwar
 */
public class SpecimenCollectionGroupBizLogic extends DefaultBizLogic
{
	/**
	 * Saves the user object in the database.
	 * 
	 * @param obj
	 *            The user 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
	{
		SpecimenCollectionGroup specimenCollectionGroup = (SpecimenCollectionGroup) obj;

		if (specimenCollectionGroup.getSpecimenCollectionSite() != null)
		{
			Object siteObj = dao.retrieve(Site.class.getName(), specimenCollectionGroup.getSpecimenCollectionSite().getId());
			if (siteObj != null)
			{
				// check for closed Site
				checkStatus(dao, specimenCollectionGroup.getSpecimenCollectionSite(), "Site");

				specimenCollectionGroup.setSpecimenCollectionSite((Site) siteObj);
			}
		}
		Object collectionProtocolEventObj = dao.retrieve(CollectionProtocolEvent.class.getName(), specimenCollectionGroup
				.getCollectionProtocolEvent().getId());
		if (collectionProtocolEventObj != null)
		{
			CollectionProtocolEvent cpe = (CollectionProtocolEvent) collectionProtocolEventObj;

			// check for closed CollectionProtocol
			checkStatus(dao, cpe.getCollectionProtocol(), "Collection Protocol");

			specimenCollectionGroup.setCollectionProtocolEvent(cpe);
		}

		setCollectionProtocolRegistration(dao, specimenCollectionGroup, null);

		dao.insert(specimenCollectionGroup, sessionDataBean, true, true);

		insertAuthData(specimenCollectionGroup);
	}

	/**
	 * @param specimenCollectionGroup
	 * @throws DAOException
	 */
	public void insertAuthData(SpecimenCollectionGroup specimenCollectionGroup) throws DAOException
	{
		try
		{
			SecurityManager.getInstance(this.getClass()).insertAuthorizationData(null, getProtectionObjects(specimenCollectionGroup),
					getDynamicGroups(specimenCollectionGroup));
		}
		catch (SMException e)
		{
			throw handleSMException(e);
		}
	}

	/**
	 * This function used to get specimenCollectionGroup object from id and 
	 * populate all its associated entities.
	 * @param scgId 				SpecimenCollectionGroup id
	 * @param bean 					SessionDataBean
	 * @param retrieveAssociates	flag for retrieve associated entities or not.
	 * @return object of CollectionProtocol
	 * @throws BizLogicException If fails to retrieve any of the required entity.
	 */
	public SpecimenCollectionGroup getSCGFromId(
			Long scgId, SessionDataBean bean,boolean retrieveAssociates)
		throws BizLogicException
	{
		AbstractDAO dao = DAOFactory.getInstance().getDAO(Constants.HIBERNATE_DAO);
		try
		{
			dao.openSession(bean);

			List cpList = dao.retrieve(SpecimenCollectionGroup.class.getName(),
					Constants.SYSTEM_IDENTIFIER, scgId);

			if(cpList == null || cpList.isEmpty())
			{
				throw new BizLogicException("Cannot find CP. Failed to find " +
						"SCG for id " + scgId);
			}
			SpecimenCollectionGroup specCollGroup = 
							(SpecimenCollectionGroup) cpList.get(0);
			if(retrieveAssociates)
			{
				retreiveAssociates(scgId, specCollGroup);
			}
			return specCollGroup;
		}
		catch(DAOException exception)
		{
			throw new BizLogicException(exception.getMessage(),exception);
		}
		finally
		{
			try
			{
				dao.closeSession();
			}
			catch(DAOException daoException)
			{
				Logger.out.fatal("Failed to close session due to "
						+ daoException.getMessage(), daoException);
			}
		}
	}

	/**
	 * 
	 * @param scgId
	 * @param specCollGroup
	 * @throws BizLogicException
	 */
	private void retreiveAssociates(Long scgId, SpecimenCollectionGroup specCollGroup)
			throws BizLogicException
	{
		CollectionProtocolRegistration collProtReg = 
			specCollGroup.getCollectionProtocolRegistration();
		
		if(collProtReg == null)
		{
			throw new BizLogicException("Cannot find CP. CPR for " +
					"SCG id " + scgId + " is unexpectedly null.");
		}
		CollectionProtocol collProt = collProtReg.getCollectionProtocol();
		if(collProt == null)
		{
			throw new BizLogicException("Cannot find CP. for SCG id " + scgId);
		}
		Long id =collProt.getId();
		
		Collection<Specimen> specimenCollection = specCollGroup.getSpecimenCollection();
		retrieveSpecimens(specimenCollection);
		
		
	}

	/**
	 * @param specimenCollection
	 */
	private void retrieveSpecimens(Collection<Specimen> specimenCollection)
	{
		if (specimenCollection == null)
		{
			return;
		}
		
		Iterator<Specimen> specIterator = specimenCollection.iterator();
		while(specIterator.hasNext())
		{
			Specimen specimen = specIterator.next();
			Collection<Specimen> childSpecimenCollection = 
				specimen.getChildrenSpecimen();
			retrieveSpecimens(childSpecimenCollection);
		}
	}
	
	private Set getProtectionObjects(AbstractDomainObject obj)
	{
		Set protectionObjects = new HashSet();

		SpecimenCollectionGroup specimenCollectionGroup = (SpecimenCollectionGroup) obj;
		protectionObjects.add(specimenCollectionGroup);

		Logger.out.debug(protectionObjects.toString());
		return protectionObjects;
	}

	protected String[] getDynamicGroups(AbstractDomainObject obj) throws SMException
{'fraction_non_alphanumeric': 0.09383313180169287, 'fraction_numerical': 0.0015316404675534058, 'mean_word_length': 5.2092092092092095, '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': '4558602', 'n_tokens_mistral': 4112, 'n_tokens_neox': 3745, 'n_words': 740}