/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/RACPC/src/ims/racpc/domain/impl/RACPCPMHExamImpl.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 Peter Martin using IMS Development Environment (version 1.71 build 3642.24101)
// Copyright (C) 1995-2010 IMS MAXIMS. All rights reserved.

package ims.racpc.domain.impl;

import java.util.ArrayList;
import java.util.List;

import ims.clinical.domain.objects.RACPPastMedicalHistory;
import ims.clinical.vo.RACPPastMedicalHistoryVoCollection;
import ims.clinical.vo.domain.RACPPastMedicalHistoryVoAssembler;
import ims.core.admin.vo.CareContextRefVo;
import ims.core.vitals.domain.objects.VitalSigns;
import ims.core.vo.VitalSignsVo;
import ims.core.vo.VitalSignsVoCollection;
import ims.core.vo.domain.VitalSignsVoAssembler;
import ims.domain.DomainFactory;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.exceptions.CodingRuntimeException;
import ims.racpc.domain.RACPCSummary;
import ims.racpc.domain.base.impl.BaseRACPCPMHExamImpl;

public class RACPCPMHExamImpl extends BaseRACPCPMHExamImpl
{

	private static final long serialVersionUID = 1L;

	public ims.clinical.vo.RACPPastMedicalHistoryVo getRACPPastMedicalHistoryForrefCareContext(CareContextRefVo refCareContextRefVo)
	{
		if (refCareContextRefVo == null)
			throw new CodingRuntimeException("Cannot get RACPSymptomRiskFactorsVo for null ClinicalContactRefVo");
		
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer(" from RACPPastMedicalHistory pmh where "); 
	
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();
	
		hql.append(" pmh.careContext.id = :ccId");
		markers.add("ccId");
		values.add(refCareContextRefVo.getID_CareContext());

		List listRACPPastMedicalHistory = factory.find(hql.toString(), markers,values);
		if(listRACPPastMedicalHistory != null && listRACPPastMedicalHistory.size() > 0)
		{ 
			RACPPastMedicalHistoryVoCollection voColl = RACPPastMedicalHistoryVoAssembler.createRACPPastMedicalHistoryVoCollectionFromRACPPastMedicalHistory(listRACPPastMedicalHistory);
			if(voColl != null && voColl.size() > 0)
				return voColl.get(0);
		}
		return null;
	}

	public ims.clinical.vo.RACPPastMedicalHistoryVo saveRACPPastMedicalHistoryVo(ims.clinical.vo.RACPPastMedicalHistoryVo voRACPPastMedicalHistory, VitalSignsVo voVitalSigns, Boolean bUpdateVitalSigns) throws ims.domain.exceptions.StaleObjectException
	{
		if (voRACPPastMedicalHistory == null  || !voRACPPastMedicalHistory.isValidated())
			throw new CodingRuntimeException("Past Medical History is null or has not been validated");
		if (voVitalSigns == null  || !voVitalSigns.isValidated())
			throw new CodingRuntimeException("Vital Signs is null or has not been validated");
		
		DomainFactory factory=getDomainFactory();
		
		if(bUpdateVitalSigns){
			VitalSigns doVitalSigns = VitalSignsVoAssembler.extractVitalSigns(factory, voVitalSigns);
			factory.save(doVitalSigns);
		}
					
		RACPPastMedicalHistory doRACPPastMedicalHistory = RACPPastMedicalHistoryVoAssembler.extractRACPPastMedicalHistory(factory, voRACPPastMedicalHistory);
		factory.save(doRACPPastMedicalHistory);
		return RACPPastMedicalHistoryVoAssembler.create(doRACPPastMedicalHistory);
	}

	public VitalSignsVo saveVitalSignsForCareContext(VitalSignsVo voVitalSigns) throws StaleObjectException {
		if (voVitalSigns == null  || !voVitalSigns.isValidated())
			throw new CodingRuntimeException("Vital Signs is null or has not been validated");
		DomainFactory factory=getDomainFactory();
		
		VitalSigns doRACPPastMedicalHistory = VitalSignsVoAssembler.extractVitalSigns(factory, voVitalSigns);
		factory.save(doRACPPastMedicalHistory);
		return VitalSignsVoAssembler.create(doRACPPastMedicalHistory);
	}

	public VitalSignsVo getVitalSignsForCareContext(CareContextRefVo refVoCareContext) {
		if (refVoCareContext == null)
			throw new CodingRuntimeException("Cannot get CareContext for null CareContextRefVo");
		
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer(" from VitalSigns vs where "); 
	
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();
	
		hql.append(" vs.careContext.id = :ccId");
		markers.add("ccId");
		values.add(refVoCareContext.getID_CareContext());
		
		hql.append(" order by vs.vitalsTakenDateTime");

		List listVitalSigns = factory.find(hql.toString(), markers,values);
		if(listVitalSigns != null && listVitalSigns.size() > 0)
		{ 
			VitalSignsVoCollection voColl = VitalSignsVoAssembler.createVitalSignsVoCollectionFromVitalSigns(listVitalSigns);
			if(voColl != null && voColl.size() > 0)
				return voColl.get(0);
		}
		return null;
	}

	//WDEV-13437
	public Boolean isRACPCSummaryComplete(CareContextRefVo careContext) 
	{
		RACPCSummary impl = (RACPCSummary)getDomainImpl(RACPCSummaryImpl.class);
		return impl.isRACPCSummaryComplete(careContext);
	}
}
