gt: 		DomainFactory factory = getDomainFactory();
context: //#############################################################################
  //#                                                                           #
  //#  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 by Peter Martin using IMS Development Environment (version 1.62 build 2929.28798)
  // Copyright (C) 1995-2008 IMS MAXIMS plc. All rights reserved.
  
  package ims.clinical.domain.impl;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import ims.admin.domain.GPAdmin;
  import ims.admin.domain.MosAdmin;
  import ims.admin.domain.OrganisationAndLocation;
  import ims.admin.domain.PracticeSearch;
  import ims.admin.domain.impl.GPAdminImpl;
  import ims.admin.domain.impl.MosAdminImpl;
  import ims.admin.domain.impl.OrganisationAndLocationImpl;
  import ims.admin.domain.impl.PracticeSearchImpl;
  import ims.clinical.domain.objects.ReferralsRecording;
  import ims.clinical.vo.ReferralsRecordingRefVo;
  import ims.clinical.vo.ReferralsRecordingVo;
  import ims.clinical.vo.ReferralsRecordingVoCollection;
  import ims.clinical.vo.domain.ReferralsRecordingShortVoAssembler;
  import ims.clinical.vo.domain.ReferralsRecordingVoAssembler;
  import ims.clinical.vo.lookups.PrimaryPreceeding;
  import ims.clinical.vo.lookups.ReferralCategory;
  import ims.core.clinical.vo.ServiceRefVo;
  import ims.core.resource.people.vo.GpRefVo;
  import ims.core.resource.people.vo.HcpRefVo;
  import ims.core.resource.people.vo.MemberOfStaffRefVo;
  import ims.core.vo.GP;
  import ims.core.vo.GpShortVoCollection;
  import ims.core.vo.HcpLiteVo;
  import ims.core.vo.LocShortVo;
  import ims.core.vo.LocShortVoCollection;
  import ims.core.vo.LocationServiceVo;
  import ims.core.vo.LocationServiceVoCollection;
  import ims.core.vo.MemberOfStaffShortVo;
  import ims.core.vo.domain.HcpLiteVoAssembler;
  import ims.core.vo.domain.LocationServiceVoAssembler;
  import ims.domain.DomainFactory;
  import ims.domain.exceptions.DomainRuntimeException;
  import ims.framework.exceptions.CodingRuntimeException;
  import ims.clinical.domain.base.impl.BaseDemoreferralDetailsImpl;;
  
  public class DemoreferralDetailsImpl extends BaseDemoreferralDetailsImpl
  {
  
  	private static final long serialVersionUID = 1L;
  
  	/**
  	* saveReferralsRecording
  	*/
  	public ims.clinical.vo.ReferralsRecordingVo saveReferralsRecording(ims.clinical.vo.ReferralsRecordingVo referVo) throws ims.domain.exceptions.StaleObjectException
  	{
  		DomainFactory factory = getDomainFactory();
  		ReferralsRecording doRR = ReferralsRecordingVoAssembler.extractReferralsRecording(factory, referVo);
  		factory.save(doRR);
  		return ReferralsRecordingVoAssembler.create(doRR);
  	}
  
  	/**
  	* geStagingClassification
  	*/
  	public ims.clinical.vo.ReferralsRecordingVo getPrimaryPreecedingReferralRecording(ims.core.admin.vo.CareSpellRefVo csRefVo)
  	{
  		if(csRefVo == null)
  			throw new RuntimeException("Cannot get ReferralsRecordingVo for null CareContextRefVo");
  		
  		DomainFactory factory = getDomainFactory();
  		StringBuffer hql = new StringBuffer(" ");
  		String query = "from ReferralsRecording rr ";
  		ArrayList markers = new ArrayList();
  		ArrayList values = new ArrayList();
  		String andStr = " ";
  		
  		if (csRefVo != null)
  		{
  			hql.append(andStr + " rr.careSpell.id = :id_CareSpell");
  			markers.add("id_CareSpell");
  			values.add(csRefVo.getID_CareSpell());			
  			andStr = " and ";
  		}
  
  		hql.append(andStr + " rr.primaryPreceeding = :pp_PRIMARY");
  		markers.add("pp_PRIMARY");
  		values.add(getDomLookup(PrimaryPreceeding.PRIMARY));			
  		andStr = " and ";
  		
  		hql.append(andStr + " rr.referralCategory = :rc_PRECEEDEDEPISODE");
  		markers.add("rc_PRECEEDEDEPISODE");
  		values.add(getDomLookup(ReferralCategory.PRECEEDEDEPISODE));			
  		andStr = " and ";
  
  		if (markers.size() > 0) query += " where ";
  			query += hql.toString();
  		
  		List rrList = factory.find(query, markers, values);	
  		ReferralsRecordingVoCollection coll = ReferralsRecordingVoAssembler.createReferralsRecordingVoCollectionFromReferralsRecording(rrList);
  		if(coll != null && coll.size() > 0)
  			return coll.get(0);
  		else
  			return null;
  	}
  
  	/**
  	* listReferralsRecording
  	*/
  	public ims.clinical.vo.ReferralsRecordingShortVoCollection listReferralsRecording(ims.core.admin.vo.CareContextRefVo careContextRefVo)
  	{
  		DomainFactory factory = getDomainFactory();
  		String hql = "from ReferralsRecording rr where rr.careContext.id = " + careContextRefVo.getID_CareContext();
  		
  		return ReferralsRecordingShortVoAssembler.
  			createReferralsRecordingShortVoCollectionFromReferralsRecording((factory.find(hql))).sort();
  	}
  
  	/**
  	* List Organisations for given search criteria
  	*/
  	public ims.core.vo.OrganisationVoCollection listOrganisation(ims.core.vo.OrganisationVo organisation)
  	{
  		OrganisationAndLocation org = (OrganisationAndLocation) getDomainImpl(OrganisationAndLocationImpl.class);
  		return org.listOrganisation(organisation, Boolean.TRUE, Boolean.TRUE);
  	}
  
  	/**
  	* list Hcps by a HcpFilter and then a subtype(e.g) MedicType - The class will be detemined by the hcptype populated in the HcpFilter
  	*/
  	public ims.core.vo.HcpCollection listHcps(ims.core.vo.HcpFilter filter, ims.core.vo.lookups.HcpDisType subType)
  	{
  		MosAdmin impl = (MosAdmin) getDomainImpl(MosAdminImpl.class);
  		return impl.listHcps(filter);
  	}
  
  	/**
  	* Returns a MosList based on the search criteria in the passed in filter
  	*/
  	public ims.core.vo.MemberOfStaffShortVoCollection listMembersOfStaff(ims.core.vo.MemberOfStaffShortVo filter)
  	{
  		MosAdmin mosAdminIml = (MosAdmin)getDomainImpl(MosAdminImpl.class);
  		return mosAdminIml.listMembersOfStaff(filter);
  	}
  
  	public ims.core.vo.OrganisationWithSitesVoCollection listPractice(ims.admin.vo.PracticeSearchCriteriaVo filter)
  	{
  		PracticeSearch prac = (PracticeSearch) getDomainImpl(PracticeSearchImpl.class);
  		return prac.listPractice(filter);
  	}
  
  	public GpShortVoCollection listGPsBySurname(String surname) 
  	{
  		GPAdmin gps = (GPAdmin) getDomainImpl(GPAdminImpl.class);
  		return gps.listGPsBySurname(surname);
  	}
  
  	public ReferralsRecordingVo getReferralRecording(ReferralsRecordingRefVo rrRefVo) 
  	{
  		return ReferralsRecordingVoAssembler.create((ReferralsRecording)getDomainFactory().getDomainObject(ReferralsRecording.class, rrRefVo.getID_ReferralsRecording()));
  	}
  
  	public GP getGP(GpRefVo gpRefVo) 
  	{
  		GPAdmin gps = (GPAdmin) getDomainImpl(GPAdminImpl.class);
  		return gps.getGP(gpRefVo.getID_Gp());
  	}
  
  	public MemberOfStaffShortVo getMemberOfStaff(MemberOfStaffRefVo mosRefVo) 
  	{
  		MosAdmin mosadm = (MosAdmin) getDomainImpl(MosAdminImpl.class);
  		MemberOfStaffShortVo mosVo = new MemberOfStaffShortVo();
  		mosVo.setID_MemberOfStaff(mosRefVo.getID_MemberOfStaff());
  		return mosadm.getMemberOfStaff(mosVo);
  	}
  
  	public LocationServiceVo getLocationService(LocShortVo locShort, ServiceRefVo service) {
  		if (locShort == null || service == null)
  			throw new DomainRuntimeException("Failed to get LocationService record, location and/or service not supplied");
  
  		DomainFactory factory = getDomainFactory();
  
  		List lstLocService = factory.find("from LocationService locService where locService.location.id = :locationId and locService.service.id = :serviceId", new String[]{"locationId", "serviceId"}, new Object[]{locShort.getID_Location(), service.getID_Service()});
  		LocationServiceVoCollection voCollLocServices = LocationServiceVoAssembler.createLocationServiceVoCollectionFromLocationService(lstLocService);
  
  		if (voCollLocServices != null && voCollLocServices.size() > 0)
  			return voCollLocServices.get(0);
  
  		return null;
  	}
  
  	public LocShortVoCollection listLocationsForMemberOfStaff(MemberOfStaffRefVo mos) {
  		MosAdmin impl = (MosAdmin) getDomainImpl(MosAdminImpl.class);
  		return impl.listLocationsForMemberOfStaff(mos,Boolean.TRUE); //WDEV-19532
  	}
  
  	public HcpLiteVo getHcpLiteById(HcpRefVo refId) {
  		if(refId == null)
  			throw new CodingRuntimeException("null refId passed to getHcpById() !");
  
  