/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/domain/impl/ClinicalAdmissionImpl.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 by Cornel Ventuneac using IMS Development Environment (version 1.71 build 3763.19232)
// Copyright (C) 1995-2010 IMS MAXIMS. All rights reserved.

package ims.nursing.domain.impl;

import ims.core.admin.domain.objects.CareContext;
import ims.core.admin.pas.domain.objects.AdmissionDetail;
import ims.core.admin.pas.domain.objects.PASEvent;
import ims.core.admin.pas.vo.PASEventRefVo;
import ims.core.admin.vo.CareContextRefVo;
import ims.core.clinical.domain.objects.ClinicalAdmission;
import ims.core.clinical.vo.ClinicalAdmissionRefVo;
import ims.core.patient.vo.PatientRefVo;
import ims.core.resource.people.vo.HcpRefVo;
import ims.core.vo.AdmissionDetailVo;
import ims.core.vo.AdmissionDetailVoCollection;
import ims.core.vo.CareContextShortVo;
import ims.core.vo.NurseVo;
import ims.core.vo.NurseVoCollection;
import ims.core.vo.domain.AdmissionDetailVoAssembler;
import ims.core.vo.domain.CareContextShortVoAssembler;
import ims.core.vo.domain.NurseVoAssembler;
import ims.domain.DomainFactory;
import ims.framework.exceptions.CodingRuntimeException;
import ims.nursing.domain.base.impl.BaseClinicalAdmissionImpl;
import ims.nursing.vo.AdmissionDetailForClinicalAdmissionVo;
import ims.nursing.vo.CarePlanCollection;
import ims.nursing.vo.ClinicalAdmissionListVoCollection;
import ims.nursing.vo.ClinicalAdmissionVo;
import ims.nursing.vo.ClinicalAdmissionVoCollection;
import ims.nursing.vo.PasEventForClinicalAdmissionVo;
import ims.nursing.vo.domain.AdmissionDetailForClinicalAdmissionVoAssembler;
import ims.nursing.vo.domain.CarePlanAssembler;
import ims.nursing.vo.domain.ClinicalAdmissionListVoAssembler;
import ims.nursing.vo.domain.ClinicalAdmissionVoAssembler;
import ims.nursing.vo.domain.PasEventForClinicalAdmissionVoAssembler;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ClinicalAdmissionImpl extends BaseClinicalAdmissionImpl
{

	private static final long serialVersionUID = 1L;

	/**
	* get clinical admission
	*/
	public ims.nursing.vo.ClinicalAdmissionVo getClinicalAdmission(ims.core.admin.vo.CareContextRefVo careContextRefvo)
	{
		if(careContextRefvo == null)
			throw new CodingRuntimeException("careContextRefVo Filter not provided for list call. ");

		if(careContextRefvo != null)
		{
			DomainFactory factory = getDomainFactory();
			StringBuffer hql = new StringBuffer(" from ClinicalAdmission ca where "); 
			String andStr = " ";
		
			ArrayList<String> markers = new ArrayList<String>();
			ArrayList<Serializable> values = new ArrayList<Serializable>();
		
			hql.append(andStr + " ca.careContext.id = :careContextId");
 			markers.add("careContextId");
			values.add(careContextRefvo.getID_CareContext());
			andStr = " and ";	

			List list = factory.find(hql.toString(), markers,values);
			if(list != null && list.size() > 0)
			{ 
				ClinicalAdmissionVoCollection voColl = ClinicalAdmissionVoAssembler.createClinicalAdmissionVoCollectionFromClinicalAdmission(list);
				if(voColl != null && voColl.size() > 0)
					return voColl.get(0);
			}
		}
		return null;
		
	}

	/**
	* saveClinicalAdmission
	*/
	public ims.nursing.vo.ClinicalAdmissionVo saveClinicalAdmission(ims.nursing.vo.ClinicalAdmissionVo clinAdmVo) throws ims.domain.exceptions.DomainInterfaceException, ims.domain.exceptions.StaleObjectException,ims.domain.exceptions.UniqueKeyViolationException
	{
		if(clinAdmVo == null)
			throw new CodingRuntimeException("ClinicalAdmissionVo is null");
		
		if(!clinAdmVo.isValidated())
			throw new CodingRuntimeException("ClinicalAdmissionVo Value Object has not been validated");

		DomainFactory factory = getDomainFactory();
		ClinicalAdmission  doCA = ClinicalAdmissionVoAssembler.extractClinicalAdmission(factory, clinAdmVo);

		factory.save(doCA);
		return ClinicalAdmissionVoAssembler.create(doCA);
	}


	public AdmissionDetailVo getAdmissionDetail(Integer pasEventId) 
	{
		if(pasEventId == null)
			return null;

		if(pasEventId != null)
		{
			DomainFactory factory = getDomainFactory();
			StringBuffer hql = new StringBuffer(" select a1_1 from AdmissionDetail as a1_1 left join a1_1.pasEvent as p1_1 where  "); 
			String andStr = " ";
		
			ArrayList<String> markers = new ArrayList<String>();
			ArrayList<Serializable> values = new ArrayList<Serializable>();
		
			hql.append(andStr + " p1_1.id = :pasEventId");
 			markers.add("pasEventId");
			values.add(pasEventId);
			andStr = " and ";	

			List list = factory.find(hql.toString(), markers,values);
			if(list != null && list.size() > 0)
			{ 
				AdmissionDetailVoCollection voColl = AdmissionDetailVoAssembler.createAdmissionDetailVoCollectionFromAdmissionDetail(list);
				if(voColl != null && voColl.size() > 0)
					return voColl.get(0);
			}
		}
		return null;
	}

	
	public ClinicalAdmissionVo getPreviousClinicalAdmission(Integer patientId)
	{
		if(patientId == null)
			return null;

		if(patientId != null)
		{
			DomainFactory factory = getDomainFactory();
			StringBuffer hql = new StringBuffer(" select c1_1 from ClinicalAdmission as c1_1 left join c1_1.careContext as c2_1 left join c2_1.episodeOfCare as e1_1 left join e1_1.careSpell as c3_1 left join c3_1.patient as p1_1	where(c3_1.patient.id = :PID) order by c1_1.authoringInformation.authoringDateTime desc "); 
			
		
			ArrayList<String> markers = new ArrayList<String>();
			ArrayList<Serializable> values = new ArrayList<Serializable>();
					
 			markers.add("PID");
			values.add(patientId);
			

			//List list = factory.find(hql.toString(), markers,values,1);
			List list = factory.find(hql.toString(), markers,values);
			if(list != null && list.size() > 0)
			{ 
				ClinicalAdmissionVoCollection voColl = ClinicalAdmissionVoAssembler.createClinicalAdmissionVoCollectionFromClinicalAdmission(list);
				if(voColl != null && voColl.size() > 0)
					return voColl.get(0);
			}
		}
		return null;
		
	}

	
	public CarePlanCollection getCareplans(CareContextRefVo careContextrefVo) 
	{
		if(careContextrefVo == null)
			throw new CodingRuntimeException("careContextRefVo Filter not provided for list call. ");

		if(careContextrefVo != null)
		{
			DomainFactory factory = getDomainFactory();
			StringBuffer hql = new StringBuffer(" select c1_1 from CarePlan as c1_1 left join c1_1.careContext as c2_1 where  "); 
			String andStr = " ";
		
			ArrayList<String> markers = new ArrayList<String>();
			ArrayList<Serializable> values = new ArrayList<Serializable>();
		
			hql.append(andStr + " c2_1.id = :careContextId");
 			markers.add("careContextId");
			values.add(careContextrefVo.getID_CareContext());
			andStr = " and ";	

			List list = factory.find(hql.toString(), markers,values);
			if(list != null && list.size() > 0)
			{ 
				CarePlanCollection voColl = CarePlanAssembler.createCarePlanCollectionFromCarePlan(list);
				if(voColl != null && voColl.size() > 0)
					return voColl;
			}
		}
		return null;
	}

	public ClinicalAdmissionListVoCollection listClinicalAdmissions(PatientRefVo patientRefVo) 
	{
		//WDEV-13846 Florin
		if(patientRefVo == null)
			throw new CodingRuntimeException("PatientRefVo Filter not provided for list call. ");

		DomainFactory factory = getDomainFactory();
		String hql = "from ClinicalAdmission ca where ca.careContext.episodeOfCare.careSpell.patient.id = :idPatient order by ca.systemInformation.creationDateTime desc";
		List<?> list = factory.find(hql, new String[] {"idPatient"}, new Object[] {patientRefVo.getID_Patient()});
		
		return ClinicalAdmissionListVoAssembler.createClinicalAdmissionListVoCollectionFromClinicalAdmission(list).sort();
	}

	public CareContextShortVo getCareContextShort(CareContextRefVo careContexxtRefVo)
	{
		DomainFactory factory = getDomainFactory();

		CareContext cc = (CareContext) factory.getDomainObject(CareContext.class, careContexxtRefVo.getID_CareContext());
		return CareContextShortVoAssembler.create(cc);
	}

	
	public NurseVo getNurse(HcpRefVo id) 
	{	
		if(id == null)
			throw new CodingRuntimeException("HCP id must not be null ");
		
		DomainFactory factory = getDomainFactory();
		String hql = "from Nurse as n1_1 where n1_1.id = :idHCP";
		List list = factory.find(hql, new String[] {"idHCP"}, new Object[] {id.getID_Hcp()});
		if(list != null && list.size() > 0)
		{
			NurseVoCollection voColl =  NurseVoAssembler.createNurseVoCollectionFromNurse(list);
			if(voColl != null && voColl.size() > 0)
				return voColl.get(0);
		}
		return null;
	}

	//WDEV-10976
	public AdmissionDetailForClinicalAdmissionVo getPASAdmissionDetails(PASEventRefVo pasEvent) 
	{
		if(pasEvent == null || pasEvent.getID_PASEvent() == null)
			throw new CodingRuntimeException("Can not get AdmissionDetail on null PASEvent Id.");
		
		DomainFactory factory = getDomainFactory();
		String query = "from AdmissionDetail as ad where ad.pasEvent.id = :PASEvent";
		
		List<?> list = factory.find(query, new String[] {"PASEvent"}, new Object[] {pasEvent.getID_PASEvent()});
		
		if(list != null && list.size() > 0)
			return AdmissionDetailForClinicalAdmissionVoAssembler.create((AdmissionDetail) list.get(0));
		
		return null;
	}

	public PasEventForClinicalAdmissionVo getPASEvent(PASEventRefVo pasEvent)
	{
		if(pasEvent == null || pasEvent.getID_PASEvent() == null)
			throw new CodingRuntimeException("Can not get AdmissionDetail on null PASEvent Id.");
		
		return PasEventForClinicalAdmissionVoAssembler.create((PASEvent) getDomainFactory().getDomainObject(PASEvent.class, pasEvent.getID_PASEvent()));
	}

	 //WDEV-13846
	public ClinicalAdmissionVo getClinicalAdmissionById(ClinicalAdmissionRefVo clinicalAdmissionId)
	{
		if (clinicalAdmissionId == null || clinicalAdmissionId.getID_ClinicalAdmission()== null)
		{
			throw new CodingRuntimeException("Cannot get ClinicalAdmissionRefVo on null Id ");
		}

		DomainFactory factory = getDomainFactory();
		ClinicalAdmission domainClinicalAdmission = (ClinicalAdmission) factory.getDomainObject(ClinicalAdmission.class, clinicalAdmissionId.getID_ClinicalAdmission());
		
		return ClinicalAdmissionVoAssembler.create(domainClinicalAdmission);
	}
}
