/**
 * <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 Aniruddha Phadnis
 * @version 1.00
 * Created on Jul 21, 2005
 */

package edu.wustl.catissuecore.bizlogic;

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

//import org.hibernate.HibernateException;

import edu.wustl.catissuecore.dao.DAO;
import edu.wustl.catissuecore.domain.Biohazard;
import edu.wustl.catissuecore.domain.ExternalIdentifier;
import edu.wustl.catissuecore.domain.Specimen;
import edu.wustl.catissuecore.domain.SpecimenCollectionGroup;
import edu.wustl.catissuecore.domain.StorageContainer;
import edu.wustl.common.util.dbManager.DAOException;

/**
 * NewSpecimenHDAO is used to add new specimen information into the database using Hibernate.
 * @author aniruddha_phadnis
 */
public class NewSpecimenBizLogic extends DefaultBizLogic
{
	/**
     * Saves the storageType object in the database.
     * @param session The session in which the object is saved.
     * @param obj The storageType object to be saved.
     * @throws DAOException 
     */
	protected void insert(DAO dao, Object obj) throws DAOException
	{
		Specimen specimen = (Specimen)obj;
		
		setSpecimenAttributes(dao,specimen);
				
		dao.insert(specimen.getSpecimenCharacteristics(),true);
		dao.insert(specimen,true);
		
		Collection externalIdentifierCollection = specimen.getExternalIdentifierCollection();
		if(externalIdentifierCollection != null && externalIdentifierCollection.size() > 0)
		{
			Iterator it = externalIdentifierCollection.iterator();
			
			while(it.hasNext())
			{
				ExternalIdentifier exId = (ExternalIdentifier)it.next();
				exId.setSpecimen(specimen);
				dao.insert(exId,true);
			}
		}
	}
	
	/**
     * Updates the persistent object in the database.
     * @param session The session in which the object is saved.
     * @param obj The object to be updated.
     * @throws HibernateException Exception thrown during hibernate operations.
     * @throws DAOException 
     */
    public void update(DAO dao, Object obj) throws DAOException
    {
    	Specimen specimen = (Specimen)obj;

		setSpecimenAttributes(dao,specimen);
				
		dao.update(specimen.getSpecimenCharacteristics());
		dao.update(specimen);
		
		Collection externalIdentifierCollection = specimen.getExternalIdentifierCollection();
		if(externalIdentifierCollection != null && externalIdentifierCollection.size() > 0)
		{
			Iterator it = externalIdentifierCollection.iterator();
			
			while(it.hasNext())
			{
				ExternalIdentifier exId = (ExternalIdentifier)it.next();
				exId.setSpecimen(specimen);
				dao.update(exId);
			}
		}
    }
    
    
    private void setSpecimenAttributes(DAO dao, Specimen specimen) throws DAOException
	{
    	//Load & set Specimen Collection Group if present
		if(specimen.getSpecimenCollectionGroup() != null)
		{
	    	List list = dao.retrieve(SpecimenCollectionGroup.class.getName(), "systemIdentifier", specimen.getSpecimenCollectionGroup().getSystemIdentifier());
			
			if(list!=null && list.size()!=0)
			{
				SpecimenCollectionGroup spg = (SpecimenCollectionGroup)list.get(0);
				specimen.setSpecimenCollectionGroup(spg);
			}
		}

		//Load & set Parent Specimen if present
		if(specimen.getParentSpecimen() != null)
		{
			List parentSpecimenList = dao.retrieve(Specimen.class.getName(),"systemIdentifier",specimen.getParentSpecimen().getSystemIdentifier());
			
			if(parentSpecimenList!=null && parentSpecimenList.size()!=0)
			{
				Specimen parentSpecimen = (Specimen)parentSpecimenList.get(0);
				specimen.setParentSpecimen(parentSpecimen);
			}
		}
		
		
		//Load & set Storage Container
		List scList = dao.retrieve(StorageContainer.class.getName(), "systemIdentifier", specimen.getStorageContainer().getSystemIdentifier());
		
		if(scList!=null && scList.size()!=0)
		{
			StorageContainer container = (StorageContainer)scList.get(0);
			specimen.setStorageContainer(container);
		}
		
		//Setting the Biohazard Collection
		Set set = new HashSet();
		
		Collection biohazardCollection = specimen.getBiohazardCollection();
		if(biohazardCollection != null && biohazardCollection.size() > 0)
		{
			Iterator it = biohazardCollection.iterator();

			while(it.hasNext())
			{
				Biohazard hazard = (Biohazard)it.next();
				System.out.println("hazard.getSystemIdentifier() "+hazard.getSystemIdentifier());
				Object bioObj = dao.retrieve(Biohazard.class.getName(), hazard.getSystemIdentifier());
				if(bioObj!=null)
				{
					Biohazard hazardObj = (Biohazard)bioObj;
					System.out.println("hazard.getSystemIdentifier() "+hazardObj.getSystemIdentifier());
					set.add(hazardObj);
				}
			}
		}

		specimen.setBiohazardCollection(set);
	}
}
@@ -527,54 +527,8 @@
 %0D%0A%0D%0A
-//import org.hibernate.HibernateException;%0D%0A%0D%0A
 impo
@@ -2378,18 +2378,25 @@
 n)obj;%0D%0A
-%0D%0A
+    %09%0D%0A//
 %09%09setSpe
@@ -2423,30 +2423,24 @@
 specimen);%0D%0A
-%09%09%09%09%0D%0A
 %09%09dao.update
@@ -2927,22 +2927,16 @@
 %0D%0A    %0D%0A
-    %0D%0A
     priv
@@ -3467,18 +3467,20 @@
 %7D%0D%0A%09%09%7D%0D%0A
+%09%09
 %0D%0A
-
 %09%09//Load
@@ -3911,20 +3911,16 @@
 %09%7D%0D%0A%09%09%0D%0A
-%09%09%0D%0A
 %09%09//Load
@@ -4986,16 +4986,18 @@
 %7D%0D%0A%09%09%7D%0D%0A
+%09%09
 %0D%0A%09%09spec

Edit Specimen done