/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/ValueObjects/src/ims/icp/vo/domain/OutpatientEpisodeWithICPInfoVoAssembler.java
//#############################################################################
//#                                                                           #
//#  Copyright (C) <2015>  <IMS MAXIMS>                                       #
//#                                                                           #
//#  This program is free software: you can redistribute it and/or modify     #
//#  it under the terms of the GNU Affero General Public License as           #
//#  published by the Free Software Foundation, either version 3 of the       #
//#  License, or (at your option) any later version.                          # 
//#                                                                           #
//#  This program is distributed in the hope that it will be useful,          #
//#  but WITHOUT ANY WARRANTY; without even the implied warranty of           #
//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
//#  GNU Affero General Public License for more details.                      #
//#                                                                           #
//#  You should have received a copy of the GNU Affero General Public License #
//#  along with this program.  If not, see <http://www.gnu.org/licenses/>.    #
//#                                                                           #
//#  IMS MAXIMS provides absolutely NO GUARANTEE OF THE CLINICAL SAFTEY of    #
//#  this program.  Users of this software do so entirely at their own risk.  #
//#  IMS MAXIMS only ensures the Clinical Safety of unaltered run-time        #
//#  software that it builds, deploys and maintains.                          #
//#                                                                           #
//#############################################################################
//#EOH
/*
 * This code was generated
 * Copyright (C) 1995-2004 IMS MAXIMS plc. All rights reserved.
 * IMS Development Environment (version 1.80 build 5589.25814)
 * WARNING: DO NOT MODIFY the content of this file
 * Generated on 12/10/2015, 13:25
 *
 */
package ims.icp.vo.domain;

import ims.vo.domain.DomainObjectMap;
import java.util.HashMap;

import org.hibernate.proxy.HibernateProxy;

/**
 * @author Rory Fitzpatrick
 */
public class OutpatientEpisodeWithICPInfoVoAssembler
{
  	/**
	 * Copy one ValueObject to another
	 * @param valueObjectDest to be updated
	 * @param valueObjectSrc to copy values from
	 */
	 public static ims.icp.vo.OutpatientEpisodeWithICPInfoVo copy(ims.icp.vo.OutpatientEpisodeWithICPInfoVo valueObjectDest, ims.icp.vo.OutpatientEpisodeWithICPInfoVo valueObjectSrc) 
	 {
	 	if (null == valueObjectSrc) 
		{
			return valueObjectSrc;
		}
		valueObjectDest.setID_Booking_Appointment(valueObjectSrc.getID_Booking_Appointment());
	    valueObjectDest.setIsRIE(valueObjectSrc.getIsRIE());
		// Patient
		valueObjectDest.setPatient(valueObjectSrc.getPatient());
		// AppointmentTime
		valueObjectDest.setAppointmentTime(valueObjectSrc.getAppointmentTime());
		// Session
		valueObjectDest.setSession(valueObjectSrc.getSession());
		// ApptStatus
		valueObjectDest.setApptStatus(valueObjectSrc.getApptStatus());
		// AppointmentDate
		valueObjectDest.setAppointmentDate(valueObjectSrc.getAppointmentDate());
		// Activity
		valueObjectDest.setActivity(valueObjectSrc.getActivity());
		// HasElectiveList
		valueObjectDest.setHasElectiveList(valueObjectSrc.getHasElectiveList());
		// Outcome
		valueObjectDest.setOutcome(valueObjectSrc.getOutcome());
		// OutcomeActions
		valueObjectDest.setOutcomeActions(valueObjectSrc.getOutcomeActions());
		// CaseNotesPulled
		valueObjectDest.setCaseNotesPulled(valueObjectSrc.getCaseNotesPulled());
		// isCABBooking
		valueObjectDest.setIsCABBooking(valueObjectSrc.getIsCABBooking());
		// OutcomeReason
		valueObjectDest.setOutcomeReason(valueObjectSrc.getOutcomeReason());
		// OutcomeComments
		valueObjectDest.setOutcomeComments(valueObjectSrc.getOutcomeComments());
		// wasPrinted
		valueObjectDest.setWasPrinted(valueObjectSrc.getWasPrinted());
		// BookingLetter
		valueObjectDest.setBookingLetter(valueObjectSrc.getBookingLetter());
		// OutcomeHasRTTClockImpact
		valueObjectDest.setOutcomeHasRTTClockImpact(valueObjectSrc.getOutcomeHasRTTClockImpact());
		// ElectiveList
		valueObjectDest.setElectiveList(valueObjectSrc.getElectiveList());
		// LinkedAppointments
		valueObjectDest.setLinkedAppointments(valueObjectSrc.getLinkedAppointments());
		// ApptBookedOnSameDay
		valueObjectDest.setApptBookedOnSameDay(valueObjectSrc.getApptBookedOnSameDay());
	 	return valueObjectDest;
	 }

 
	/**
	 * Create the ValueObject collection to hold the set of DomainObjects.
	 * This is a convenience method only.
	 * It is intended to be used when one called to an Assembler is made.
 	 * If more than one call to an Assembler is made then #createOutpatientEpisodeWithICPInfoVoCollectionFromBooking_Appointment(DomainObjectMap, Set) should be used.
	 * @param domainObjectSet - Set of ims.scheduling.domain.objects.Booking_Appointment objects.
	 */
	public static ims.icp.vo.OutpatientEpisodeWithICPInfoVoCollection createOutpatientEpisodeWithICPInfoVoCollectionFromBooking_Appointment(java.util.Set domainObjectSet)	
	{
		return createOutpatientEpisodeWithICPInfoVoCollectionFromBooking_Appointment(new DomainObjectMap(), domainObjectSet);
	}
	
	/**
	 * Create the ValueObject collection to hold the set of DomainObjects.
	 * @param map - maps DomainObjects to created ValueObjects
	 * @param domainObjectSet - Set of ims.scheduling.domain.objects.Booking_Appointment objects.
	 */
	public static ims.icp.vo.OutpatientEpisodeWithICPInfoVoCollection createOutpatientEpisodeWithICPInfoVoCollectionFromBooking_Appointment(DomainObjectMap map, java.util.Set domainObjectSet)	
	{
		ims.icp.vo.OutpatientEpisodeWithICPInfoVoCollection voList = new ims.icp.vo.OutpatientEpisodeWithICPInfoVoCollection();
		if ( null == domainObjectSet ) 
		{
			return voList;
		}
		int rieCount=0;
		int activeCount=0;
		java.util.Iterator iterator = domainObjectSet.iterator();
		while( iterator.hasNext() ) 
		{
			ims.scheduling.domain.objects.Booking_Appointment domainObject = (ims.scheduling.domain.objects.Booking_Appointment) iterator.next();
			ims.icp.vo.OutpatientEpisodeWithICPInfoVo vo = create(map, domainObject);
			
			if (vo != null)
				voList.add(vo);
				
			if (domainObject != null)
			{				
				if (domainObject.getIsRIE() != null && domainObject.getIsRIE().booleanValue() == true)
					rieCount++;
				else
					activeCount++;
			}
		}
		voList.setRieCount(rieCount);
		voList.setActiveCount(activeCount);
		return voList;
	}

	/**
	 * Create the ValueObject collection to hold the list of DomainObjects.
	 * @param domainObjectList - List of ims.scheduling.domain.objects.Booking_Appointment objects.
	 */
	public static ims.icp.vo.OutpatientEpisodeWithICPInfoVoCollection createOutpatientEpisodeWithICPInfoVoCollectionFromBooking_Appointment(java.util.List domainObjectList) 
	{
		return createOutpatientEpisodeWithICPInfoVoCollectionFromBooking_Appointment(new DomainObjectMap(), domainObjectList);
	}
	
	/**
	 * Create the ValueObject collection to hold the list of DomainObjects.
	 * @param map - maps DomainObjects to created ValueObjects
	 * @param domainObjectList - List of ims.scheduling.domain.objects.Booking_Appointment objects.
	 */
	public static ims.icp.vo.OutpatientEpisodeWithICPInfoVoCollection createOutpatientEpisodeWithICPInfoVoCollectionFromBooking_Appointment(DomainObjectMap map, java.util.List domainObjectList) 
	{
		ims.icp.vo.OutpatientEpisodeWithICPInfoVoCollection voList = new ims.icp.vo.OutpatientEpisodeWithICPInfoVoCollection();
		if ( null == domainObjectList ) 
		{
			return voList;
		}		
		int rieCount=0;
		int activeCount=0;
		for (int i = 0; i < domainObjectList.size(); i++)
		{
			ims.scheduling.domain.objects.Booking_Appointment domainObject = (ims.scheduling.domain.objects.Booking_Appointment) domainObjectList.get(i);
			ims.icp.vo.OutpatientEpisodeWithICPInfoVo vo = create(map, domainObject);

			if (vo != null)
				voList.add(vo);
			
			if (domainObject != null)
			{
				if (domainObject.getIsRIE() != null && domainObject.getIsRIE().booleanValue() == true)
					rieCount++;
				else
					activeCount++;
			}
		}
		
		voList.setRieCount(rieCount);
		voList.setActiveCount(activeCount);
		return voList;
	}

	/**
	 * Create the ims.scheduling.domain.objects.Booking_Appointment set from the value object collection.
	 * @param domainFactory - used to create existing (persistent) domain objects.
	 * @param voCollection - the collection of value objects	 
	 */
	 public static java.util.Set extractBooking_AppointmentSet(ims.domain.ILightweightDomainFactory domainFactory, ims.icp.vo.OutpatientEpisodeWithICPInfoVoCollection voCollection) 
	 {
	 	return extractBooking_AppointmentSet(domainFactory, voCollection, null, new HashMap());
	 }
	 
	 public static java.util.Set extractBooking_AppointmentSet(ims.domain.ILightweightDomainFactory domainFactory, ims.icp.vo.OutpatientEpisodeWithICPInfoVoCollection voCollection, java.util.Set domainObjectSet, HashMap domMap) 
	 {
	 	int size = (null == voCollection) ? 0 : voCollection.size();
		if (domainObjectSet == null)
		{
			domainObjectSet = new java.util.HashSet();			
		}
		java.util.Set newSet = new java.util.HashSet();
		for(int i=0; i<size; i++) 
		{
			ims.icp.vo.OutpatientEpisodeWithICPInfoVo vo = voCollection.get(i);
			ims.scheduling.domain.objects.Booking_Appointment domainObject = OutpatientEpisodeWithICPInfoVoAssembler.extractBooking_Appointment(domainFactory, vo, domMap);

			//TODO: This can only occur in the situation of a stale object exception. For now leave it to the Interceptor to handle it.
			if (domainObject == null)
			{
				continue;
			}
			
			//Trying to avoid the hibernate collection being marked as dirty via its public interface methods. (like add)
			if (!domainObjectSet.contains(domainObject)) domainObjectSet.add(domainObject);
			newSet.add(domainObject);			
		}
		java.util.Set removedSet = new java.util.HashSet();
		java.util.Iterator iter = domainObjectSet.iterator();
		//Find out which objects need to be removed
		while (iter.hasNext())
		{
			ims.domain.DomainObject o = (ims.domain.DomainObject)iter.next();			
			if ((o == null || o.getIsRIE() == null || !o.getIsRIE().booleanValue()) && !newSet.contains(o))
			{
				removedSet.add(o);
			}
		}
		iter = removedSet.iterator();
		//Remove the unwanted objects
		while (iter.hasNext())
		{
			domainObjectSet.remove(iter.next());
		}
		return domainObjectSet;	 
	 }


	/**
	 * Create the ims.scheduling.domain.objects.Booking_Appointment list from the value object collection.
	 * @param domainFactory - used to create existing (persistent) domain objects.
	 * @param voCollection - the collection of value objects	 
	 */
	 public static java.util.List extractBooking_AppointmentList(ims.domain.ILightweightDomainFactory domainFactory, ims.icp.vo.OutpatientEpisodeWithICPInfoVoCollection voCollection) 
	 {
	 	return extractBooking_AppointmentList(domainFactory, voCollection, null, new HashMap());
	 }
	 
	 public static java.util.List extractBooking_AppointmentList(ims.domain.ILightweightDomainFactory domainFactory, ims.icp.vo.OutpatientEpisodeWithICPInfoVoCollection voCollection, java.util.List domainObjectList, HashMap domMap) 
	 {
	 	int size = (null == voCollection) ? 0 : voCollection.size();
		if (domainObjectList == null)
		{
			domainObjectList = new java.util.ArrayList();			
		}
		for(int i=0; i<size; i++) 
		{
			ims.icp.vo.OutpatientEpisodeWithICPInfoVo vo = voCollection.get(i);
			ims.scheduling.domain.objects.Booking_Appointment domainObject = OutpatientEpisodeWithICPInfoVoAssembler.extractBooking_Appointment(domainFactory, vo, domMap);

			//TODO: This can only occur in the situation of a stale object exception. For now leave it to the Interceptor to handle it.
			if (domainObject == null)
			{
				continue;
			}

			int domIdx = domainObjectList.indexOf(domainObject);
			if (domIdx == -1)
			{
				domainObjectList.add(i, domainObject);
			}
			else if (i != domIdx && i < domainObjectList.size())
			{
				Object tmp = domainObjectList.get(i);
				domainObjectList.set(i, domainObjectList.get(domIdx));
				domainObjectList.set(domIdx, tmp);
			}
		}
		
		//Remove all ones in domList where index > voCollection.size() as these should
		//now represent the ones removed from the VO collection. No longer referenced.
		int i1=domainObjectList.size();
		while (i1 > size)
		{
			domainObjectList.remove(i1-1);
			i1=domainObjectList.size();
		}
		return domainObjectList;	 
	 }

 

	/**
	 * Create the ValueObject from the ims.scheduling.domain.objects.Booking_Appointment object.
	 * @param domainObject ims.scheduling.domain.objects.Booking_Appointment
	 */
	 public static ims.icp.vo.OutpatientEpisodeWithICPInfoVo create(ims.scheduling.domain.objects.Booking_Appointment domainObject) 
	 {
	 	if (null == domainObject) 
	 	{
			return null;
		}
		DomainObjectMap map = new DomainObjectMap();
		return create(map, domainObject);
	 }
	 
	 /**
	  * Create the ValueObject from the ims.scheduling.domain.objects.Booking_Appointment object.
	  * @param map DomainObjectMap of DomainObjects to already created ValueObjects.
	  * @param domainObject
	  */
	  public static ims.icp.vo.OutpatientEpisodeWithICPInfoVo create(DomainObjectMap map, ims.scheduling.domain.objects.Booking_Appointment domainObject) 
	  {
	  		if (null == domainObject) 
	  		{
				return null;
			}
			// check if the domainObject already has a valueObject created for it
			ims.icp.vo.OutpatientEpisodeWithICPInfoVo valueObject = (ims.icp.vo.OutpatientEpisodeWithICPInfoVo) map.getValueObject(domainObject, ims.icp.vo.OutpatientEpisodeWithICPInfoVo.class);
			if ( null == valueObject ) 
			{
				valueObject = new ims.icp.vo.OutpatientEpisodeWithICPInfoVo(domainObject.getId(), domainObject.getVersion());
				map.addValueObject(domainObject, valueObject);

				valueObject = insert(map, valueObject, domainObject);
				
			}
	 		return valueObject;
	  }

	/**
	 * Update the ValueObject with the Domain Object.
	 * @param valueObject to be updated
	 * @param domainObject ims.scheduling.domain.objects.Booking_Appointment
	 */
	 public static ims.icp.vo.OutpatientEpisodeWithICPInfoVo insert(ims.icp.vo.OutpatientEpisodeWithICPInfoVo valueObject, ims.scheduling.domain.objects.Booking_Appointment domainObject) 
	 {
	 	if (null == domainObject) 
	 	{
			return valueObject;
		}
		DomainObjectMap map = new DomainObjectMap();
		return insert(map, valueObject, domainObject);
	 }
	 
	/**
	 * Update the ValueObject with the Domain Object.
	 * @param map DomainObjectMap of DomainObjects to already created ValueObjects.
	 * @param valueObject to be updated
	 * @param domainObject ims.scheduling.domain.objects.Booking_Appointment
	 */
	 public static ims.icp.vo.OutpatientEpisodeWithICPInfoVo insert(DomainObjectMap map, ims.icp.vo.OutpatientEpisodeWithICPInfoVo valueObject, ims.scheduling.domain.objects.Booking_Appointment domainObject) 
	 {
	 	if (null == domainObject) 
	 	{
			return valueObject;
		}
	 	if (null == map) 
	 	{
			map = new DomainObjectMap();
		}

		valueObject.setID_Booking_Appointment(domainObject.getId());
		valueObject.setIsRIE(domainObject.getIsRIE());
		
		// If this is a recordedInError record, and the domainObject
		// value isIncludeRecord has not been set, then we return null and
		// not the value object
		if (valueObject.getIsRIE() != null && valueObject.getIsRIE().booleanValue() == true && !domainObject.isIncludeRecord())
			return null;
			
		// If this is not a recordedInError record, and the domainObject
		// value isIncludeRecord has been set, then we return null and
		// not the value object
		if ((valueObject.getIsRIE() == null || valueObject.getIsRIE().booleanValue() == false) && domainObject.isIncludeRecord())
			return null;
			
		// Patient
		valueObject.setPatient(ims.core.vo.domain.PatientShortAssembler.create(map, domainObject.getPatient()) );
		// AppointmentTime
		String AppointmentTime = domainObject.getApptStartTime();
		if ( null != AppointmentTime ) 
		{
			valueObject.setAppointmentTime(new ims.framework.utils.Time(AppointmentTime) );
		}
		// Session
		valueObject.setSession(ims.scheduling.vo.domain.SessionLiteWithListownerVoAssembler.create(map, domainObject.getSession()) );
		// ApptStatus
		ims.domain.lookups.LookupInstance instance4 = domainObject.getApptStatus();
		if ( null != instance4 ) {
			ims.framework.utils.ImagePath img = null;
			ims.framework.utils.Color color = null;		
			img = null;
			if (instance4.getImage() != null) 
			{
				img = new ims.framework.utils.ImagePath(instance4.getImage().getImageId(), instance4.getImage().getImagePath());
			}
			color = instance4.getColor();
			if (color != null) 
				color.getValue();

			ims.scheduling.vo.lookups.Status_Reason voLookup4 = new ims.scheduling.vo.lookups.Status_Reason(instance4.getId(),instance4.getText(), instance4.isActive(), null, img, color);
			ims.scheduling.vo.lookups.Status_Reason parentVoLookup4 = voLookup4;
			ims.domain.lookups.LookupInstance parent4 = instance4.getParent();
			while (parent4 != null)
			{
				if (parent4.getImage() != null) 
				{
					img = new ims.framework.utils.ImagePath(parent4.getImage().getImageId(), parent4.getImage().getImagePath() );
				}
				else 
				{
					img = null;
				}
				color = parent4.getColor();
    			if (color != null) 
    				color.getValue();
								parentVoLookup4.setParent(new ims.scheduling.vo.lookups.Status_Reason(parent4.getId(),parent4.getText(), parent4.isActive(), null, img, color));
				parentVoLookup4 = parentVoLookup4.getParent();
								parent4 = parent4.getParent();
			}			
			valueObject.setApptStatus(voLookup4);
		}
				// AppointmentDate
		java.util.Date AppointmentDate = domainObject.getAppointmentDate();
		if ( null != AppointmentDate ) 
		{
			valueObject.setAppointmentDate(new ims.framework.utils.Date(AppointmentDate) );
		}
		// Activity
		valueObject.setActivity(ims.core.vo.domain.ActivityLiteVoAssembler.create(map, domainObject.getActivity()) );
		// HasElectiveList
		valueObject.setHasElectiveList( domainObject.isHasElectiveList() );
		// Outcome
		ims.domain.lookups.LookupInstance instance8 = domainObject.getOutcome();
		if ( null != instance8 ) {
			ims.framework.utils.ImagePath img = null;
			ims.framework.utils.Color color = null;		
			img = null;
			if (instance8.getImage() != null) 
			{
				img = new ims.framework.utils.ImagePath(instance8.getImage().getImageId(), instance8.getImage().getImagePath());
			}
			color = instance8.getColor();
			if (color != null) 
				color.getValue();

			ims.scheduling.vo.lookups.ApptOutcome voLookup8 = new ims.scheduling.vo.lookups.ApptOutcome(instance8.getId(),instance8.getText(), instance8.isActive(), null, img, color);
			ims.scheduling.vo.lookups.ApptOutcome parentVoLookup8 = voLookup8;
			ims.domain.lookups.LookupInstance parent8 = instance8.getParent();
			while (parent8 != null)
			{
				if (parent8.getImage() != null) 
				{
					img = new ims.framework.utils.ImagePath(parent8.getImage().getImageId(), parent8.getImage().getImagePath() );
				}
				else 
				{
					img = null;
				}
				color = parent8.getColor();
    			if (color != null) 
    				color.getValue();
								parentVoLookup8.setParent(new ims.scheduling.vo.lookups.ApptOutcome(parent8.getId(),parent8.getText(), parent8.isActive(), null, img, color));
				parentVoLookup8 = parentVoLookup8.getParent();
								parent8 = parent8.getParent();
			}			
			valueObject.setOutcome(voLookup8);
		}
				// OutcomeActions
		valueObject.setOutcomeActions(ims.scheduling.vo.domain.AppointmentOutcomeActionVoAssembler.createAppointmentOutcomeActionVoCollectionFromAppointmentOutcomeAction(map, domainObject.getOutcomeActions()) );
		// CaseNotesPulled
		valueObject.setCaseNotesPulled( domainObject.isCaseNotesPulled() );
		// isCABBooking
		valueObject.setIsCABBooking( domainObject.isIsCABBooking() );
		// OutcomeReason
		ims.domain.lookups.LookupInstance instance12 = domainObject.getOutcomeReason();
		if ( null != instance12 ) {
			ims.framework.utils.ImagePath img = null;
			ims.framework.utils.Color color = null;		
			img = null;
			if (instance12.getImage() != null) 
			{
				img = new ims.framework.utils.ImagePath(instance12.getImage().getImageId(), instance12.getImage().getImagePath());
			}
			color = instance12.getColor();
			if (color != null) 
				color.getValue();

			ims.admin.vo.lookups.AppointmentOutcomeReason voLookup12 = new ims.admin.vo.lookups.AppointmentOutcomeReason(instance12.getId(),instance12.getText(), instance12.isActive(), null, img, color);
			ims.admin.vo.lookups.AppointmentOutcomeReason parentVoLookup12 = voLookup12;
			ims.domain.lookups.LookupInstance parent12 = instance12.getParent();
			while (parent12 != null)
			{
				if (parent12.getImage() != null) 
				{
					img = new ims.framework.utils.ImagePath(parent12.getImage().getImageId(), parent12.getImage().getImagePath() );
				}
				else 
				{
					img = null;
				}
				color = parent12.getColor();
    			if (color != null) 
    				color.getValue();
								parentVoLookup12.setParent(new ims.admin.vo.lookups.AppointmentOutcomeReason(parent12.getId(),parent12.getText(), parent12.isActive(), null, img, color));
				parentVoLookup12 = parentVoLookup12.getParent();
								parent12 = parent12.getParent();
			}			
			valueObject.setOutcomeReason(voLookup12);
		}
				// OutcomeComments
		valueObject.setOutcomeComments(domainObject.getOutcomeComments());
		// wasPrinted
		valueObject.setWasPrinted( domainObject.isWasPrinted() );
		// BookingLetter
		valueObject.setBookingLetter(ims.core.vo.domain.PatientDocumentLiteVoAssembler.create(map, domainObject.getBookingLetter()) );
		// OutcomeHasRTTClockImpact
		valueObject.setOutcomeHasRTTClockImpact( domainObject.isOutcomeHasRTTClockImpact() );
		// ElectiveList
		if (domainObject.getElectiveList() != null)
		{
			if(domainObject.getElectiveList() instanceof HibernateProxy) // If the proxy is set, there is no need to lazy load, the proxy knows the id already. 
			{
				HibernateProxy p = (HibernateProxy) domainObject.getElectiveList();
				int id = Integer.parseInt(p.getHibernateLazyInitializer().getIdentifier().toString());				
				valueObject.setElectiveList(new ims.RefMan.vo.PatientElectiveListRefVo(id, -1));				
			}
			else
			{
				valueObject.setElectiveList(new ims.RefMan.vo.PatientElectiveListRefVo(domainObject.getElectiveList().getId(), domainObject.getElectiveList().getVersion()));
			}
		}
		// LinkedAppointments
		valueObject.setLinkedAppointments(ims.scheduling.vo.domain.LinkedAppointmentsForClinicListWithICPActionsVoAssembler.createLinkedAppointmentsForClinicListWithICPActionsVoCollectionFromBooking_Appointment(map, domainObject.getLinkedAppointments()) );
		// ApptBookedOnSameDay
		valueObject.setApptBookedOnSameDay( domainObject.isApptBookedOnSameDay() );
 		return valueObject;
	 }


	/**
	 * Create the domain object from the value object.
	 * @param domainFactory - used to create existing (persistent) domain objects.
	 * @param valueObject - extract the domain object fields from this.
	 */
	public static ims.scheduling.domain.objects.Booking_Appointment extractBooking_Appointment(ims.domain.ILightweightDomainFactory domainFactory, ims.icp.vo.OutpatientEpisodeWithICPInfoVo valueObject) 
	{
		return 	extractBooking_Appointment(domainFactory, valueObject, new HashMap());
	}

	public static ims.scheduling.domain.objects.Booking_Appointment extractBooking_Appointment(ims.domain.ILightweightDomainFactory domainFactory, ims.icp.vo.OutpatientEpisodeWithICPInfoVo valueObject, HashMap domMap) 
	{
		if (null == valueObject) 
		{
			return null;
		}
		Integer id = valueObject.getID_Booking_Appointment();
		ims.scheduling.domain.objects.Booking_Appointment domainObject = null;
		if ( null == id) 
		{
			if (domMap.get(valueObject) != null)
			{
				return (ims.scheduling.domain.objects.Booking_Appointment)domMap.get(valueObject);
			}
			// ims.icp.vo.OutpatientEpisodeWithICPInfoVo ID_Booking_Appointment field is unknown
			domainObject = new ims.scheduling.domain.objects.Booking_Appointment();
			domMap.put(valueObject, domainObject);
		}
		else 
		{
			String key = (valueObject.getClass().getName() + "__" + valueObject.getID_Booking_Appointment());
			if (domMap.get(key) != null)
			{
				return (ims.scheduling.domain.objects.Booking_Appointment)domMap.get(key);
			}
			domainObject = (ims.scheduling.domain.objects.Booking_Appointment) domainFactory.getDomainObject(ims.scheduling.domain.objects.Booking_Appointment.class, id );
			
			//TODO: Not sure how this should be handled. Effectively it must be a staleobject exception, but maybe should be handled as that further up.
			if (domainObject == null) 
				return null;

			domMap.put(key, domainObject);
		}
		domainObject.setVersion(valueObject.getVersion_Booking_Appointment());

	// SaveAsRefVO - treated as a refVo in extract methods
	ims.core.patient.domain.objects.Patient value1 = null;
		if ( null != valueObject.getPatient() ) 
		{
			if (valueObject.getPatient().getBoId() == null)
			{
				if (domMap.get(valueObject.getPatient()) != null)
				{
					value1 = (ims.core.patient.domain.objects.Patient)domMap.get(valueObject.getPatient());
				}
			}
			else
			{
				value1 = (ims.core.patient.domain.objects.Patient)domainFactory.getDomainObject(ims.core.patient.domain.objects.Patient.class, valueObject.getPatient().getBoId());
			}
		}
		domainObject.setPatient(value1);
		ims.framework.utils.Time time2 = valueObject.getAppointmentTime();
		String value2 = null;
		if ( time2 != null ) 
		{
			value2 = time2.toString();
		}
		domainObject.setApptStartTime(value2);
	// SaveAsRefVO - treated as a refVo in extract methods
	ims.scheduling.domain.objects.Sch_Session value3 = null;
		if ( null != valueObject.getSession() ) 
		{
			if (valueObject.getSession().getBoId() == null)
			{
				if (domMap.get(valueObject.getSession()) != null)
				{
					value3 = (ims.scheduling.domain.objects.Sch_Session)domMap.get(valueObject.getSession());
				}
			}
			else
			{
				value3 = (ims.scheduling.domain.objects.Sch_Session)domainFactory.getDomainObject(ims.scheduling.domain.objects.Sch_Session.class, valueObject.getSession().getBoId());
			}
		}
		domainObject.setSession(value3);
		// create LookupInstance from vo LookupType
		ims.domain.lookups.LookupInstance value4 = null;
		if ( null != valueObject.getApptStatus() ) 
		{
			value4 =
				domainFactory.getLookupInstance(valueObject.getApptStatus().getID());
		}
		domainObject.setApptStatus(value4);
		java.util.Date value5 = null;
		ims.framework.utils.Date date5 = valueObject.getAppointmentDate();		
		if ( date5 != null ) 
		{
			value5 = date5.getDate();
		}
		domainObject.setAppointmentDate(value5);
	// SaveAsRefVO - treated as a refVo in extract methods
	ims.core.resource.place.domain.objects.Activity value6 = null;
		if ( null != valueObject.getActivity() ) 
		{
			if (valueObject.getActivity().getBoId() == null)
			{
				if (domMap.get(valueObject.getActivity()) != null)
				{
					value6 = (ims.core.resource.place.domain.objects.Activity)domMap.get(valueObject.getActivity());
				}
			}
			else
			{
				value6 = (ims.core.resource.place.domain.objects.Activity)domainFactory.getDomainObject(ims.core.resource.place.domain.objects.Activity.class, valueObject.getActivity().getBoId());
			}
		}
		domainObject.setActivity(value6);
		domainObject.setHasElectiveList(valueObject.getHasElectiveList());
		// create LookupInstance from vo LookupType
		ims.domain.lookups.LookupInstance value8 = null;
		if ( null != valueObject.getOutcome() ) 
		{
			value8 =
				domainFactory.getLookupInstance(valueObject.getOutcome().getID());
		}
		domainObject.setOutcome(value8);

		// SaveAsRefVO treated as RefValueObject
		ims.scheduling.vo.AppointmentOutcomeActionRefVoCollection refCollection9 = new ims.scheduling.vo.AppointmentOutcomeActionRefVoCollection();
		if (valueObject.getOutcomeActions() != null)
		{
			for (int i9=0; i9<valueObject.getOutcomeActions().size(); i9++)
			{
				ims.scheduling.vo.AppointmentOutcomeActionRefVo ref9 = (ims.scheduling.vo.AppointmentOutcomeActionRefVo)valueObject.getOutcomeActions().get(i9);
				refCollection9.add(ref9);
			}
		}
		int size9 = (null == refCollection9) ? 0 : refCollection9.size();		
		java.util.List domainOutcomeActions9 = domainObject.getOutcomeActions();
		if (domainOutcomeActions9 == null)
		{
			domainOutcomeActions9 = new java.util.ArrayList();
		}
		for(int i=0; i < size9; i++) 
		{
			ims.scheduling.vo.AppointmentOutcomeActionRefVo vo = refCollection9.get(i);			
			ims.scheduling.domain.objects.AppointmentOutcomeAction dom = null;
			if ( null != vo ) 
			{
				if (vo.getBoId() == null)
				{
					if (domMap.get(vo) != null)
					{
						dom = (ims.scheduling.domain.objects.AppointmentOutcomeAction)domMap.get(vo);
					}
				}
				else
				{
					dom = (ims.scheduling.domain.objects.AppointmentOutcomeAction)domainFactory.getDomainObject( ims.scheduling.domain.objects.AppointmentOutcomeAction.class, vo.getBoId());
				}
			}

			int domIdx = domainOutcomeActions9.indexOf(dom);
			if (domIdx == -1)
			{
				domainOutcomeActions9.add(i, dom);
			}
			else if (i != domIdx && i < domainOutcomeActions9.size())
			{
				Object tmp = domainOutcomeActions9.get(i);
				domainOutcomeActions9.set(i, domainOutcomeActions9.get(domIdx));
				domainOutcomeActions9.set(domIdx, tmp);
			}
		}
		
		//Remove all ones in domList where index > voCollection.size() as these should
		//now represent the ones removed from the VO collection. No longer referenced.
		int i9 = domainOutcomeActions9.size();
		while (i9 > size9)
		{
			domainOutcomeActions9.remove(i9-1);
			i9 = domainOutcomeActions9.size();
		}
		
		domainObject.setOutcomeActions(domainOutcomeActions9);		
		domainObject.setCaseNotesPulled(valueObject.getCaseNotesPulled());
		domainObject.setIsCABBooking(valueObject.getIsCABBooking());
		// create LookupInstance from vo LookupType
		ims.domain.lookups.LookupInstance value12 = null;
		if ( null != valueObject.getOutcomeReason() ) 
		{
			value12 =
				domainFactory.getLookupInstance(valueObject.getOutcomeReason().getID());
		}
		domainObject.setOutcomeReason(value12);
		//This is to overcome a bug in both Sybase and Oracle which prevents them from storing an empty string correctly
		//Sybase stores it as a single space, Oracle stores it as NULL. This fix will make them consistent at least.
		if (valueObject.getOutcomeComments() != null && valueObject.getOutcomeComments().equals(""))
		{
			valueObject.setOutcomeComments(null);
		}
		domainObject.setOutcomeComments(valueObject.getOutcomeComments());
		domainObject.setWasPrinted(valueObject.getWasPrinted());
	// SaveAsRefVO - treated as a refVo in extract methods
	ims.core.documents.domain.objects.PatientDocument value15 = null;
		if ( null != valueObject.getBookingLetter() ) 
		{
			if (valueObject.getBookingLetter().getBoId() == null)
			{
				if (domMap.get(valueObject.getBookingLetter()) != null)
				{
					value15 = (ims.core.documents.domain.objects.PatientDocument)domMap.get(valueObject.getBookingLetter());
				}
			}
			else
			{
				value15 = (ims.core.documents.domain.objects.PatientDocument)domainFactory.getDomainObject(ims.core.documents.domain.objects.PatientDocument.class, valueObject.getBookingLetter().getBoId());
			}
		}
		domainObject.setBookingLetter(value15);
		domainObject.setOutcomeHasRTTClockImpact(valueObject.getOutcomeHasRTTClockImpact());
		ims.RefMan.domain.objects.PatientElectiveList value17 = null;
		if ( null != valueObject.getElectiveList() ) 
		{
			if (valueObject.getElectiveList().getBoId() == null)
			{
				if (domMap.get(valueObject.getElectiveList()) != null)
				{
					value17 = (ims.RefMan.domain.objects.PatientElectiveList)domMap.get(valueObject.getElectiveList());
				}
			}
			else if (valueObject.getBoVersion() == -1) // RefVo was not modified since obtained from the Assembler, no need to update the BO field
			{
				value17 = domainObject.getElectiveList();	
			}
			else
			{
				value17 = (ims.RefMan.domain.objects.PatientElectiveList)domainFactory.getDomainObject(ims.RefMan.domain.objects.PatientElectiveList.class, valueObject.getElectiveList().getBoId());
			}
		}
		domainObject.setElectiveList(value17);

		// SaveAsRefVO treated as RefValueObject
		ims.scheduling.vo.Booking_AppointmentRefVoCollection refCollection18 = new ims.scheduling.vo.Booking_AppointmentRefVoCollection();
		if (valueObject.getLinkedAppointments() != null)
		{
			for (int i18=0; i18<valueObject.getLinkedAppointments().size(); i18++)
			{
				ims.scheduling.vo.Booking_AppointmentRefVo ref18 = (ims.scheduling.vo.Booking_AppointmentRefVo)valueObject.getLinkedAppointments().get(i18);
				refCollection18.add(ref18);
			}
		}
		int size18 = (null == refCollection18) ? 0 : refCollection18.size();		
		java.util.Set domainLinkedAppointments18 = domainObject.getLinkedAppointments();
		if (domainLinkedAppointments18 == null)
		{
			domainLinkedAppointments18 = new java.util.HashSet();
		}
		java.util.Set newSet18  = new java.util.HashSet();
		for(int i=0; i<size18; i++) 
		{
			ims.scheduling.vo.Booking_AppointmentRefVo vo = refCollection18.get(i);					
			ims.scheduling.domain.objects.Booking_Appointment dom = null;
			if ( null != vo ) 
			{
				if (vo.getBoId() == null)
				{
					if (domMap.get(vo) != null)
					{
						dom = (ims.scheduling.domain.objects.Booking_Appointment)domMap.get(vo);
					}
				}
				else
				{
					dom = (ims.scheduling.domain.objects.Booking_Appointment)domainFactory.getDomainObject( ims.scheduling.domain.objects.Booking_Appointment.class, vo.getBoId());
				}
			}

			//Trying to avoid the hibernate collection being marked as dirty via its public interface methods. (like add)
			if (!domainLinkedAppointments18.contains(dom))
			{
				domainLinkedAppointments18.add(dom);
			}
			newSet18.add(dom);			
		}
		java.util.Set removedSet18 = new java.util.HashSet();
		java.util.Iterator iter18 = domainLinkedAppointments18.iterator();
		//Find out which objects need to be removed
		while (iter18.hasNext())
		{
			ims.domain.DomainObject o = (ims.domain.DomainObject)iter18.next();			
			if ((o == null || o.getIsRIE() == null || !o.getIsRIE().booleanValue()) && !newSet18.contains(o))
			{
				removedSet18.add(o);
			}
		}
		iter18 = removedSet18.iterator();
		//Remove the unwanted objects
		while (iter18.hasNext())
		{
			domainLinkedAppointments18.remove(iter18.next());
		}		
		
		domainObject.setLinkedAppointments(domainLinkedAppointments18);		
		domainObject.setApptBookedOnSameDay(valueObject.getApptBookedOnSameDay());

		return domainObject;
	}

}
