/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/domain/impl/NutritionalRiskAssessmentDialogImpl.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 Vasile Purdila using IMS Development Environment (version 1.22 build 41210.1630)
// Copyright (C) 1995-2004 IMS MAXIMS plc. All rights reserved.

package ims.nursing.domain.impl;

import java.util.ArrayList;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainInterfaceException;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.impl.DomainImpl;
import ims.framework.enumerations.SortOrder;
import ims.framework.utils.Date;
import ims.nursing.assessmenttools.domain.objects.NutritionalRiskAssessment;
import ims.nursing.vo.AssessmentComponent;
import ims.nursing.vo.AssessmentHeaderInfo;
import ims.nursing.vo.NutritionalRiskAssessmentVo;
import ims.nursing.vo.domain.NutritionalRiskAssessmentVoAssembler;
import ims.spinalinjuries.vo.NurAssessmentEatingVo;
import ims.core.admin.vo.CareContextRefVo;
import ims.core.vitals.domain.objects.VitalSigns;
import ims.core.vo.ClinicalContactShortVo;
import ims.core.vo.VitalSignsVo;
import ims.core.vo.VitalSignsVoCollection;
import ims.core.vo.domain.VitalSignsVoAssembler;

public class NutritionalRiskAssessmentDialogImpl extends DomainImpl implements ims.nursing.domain.NutritionalRiskAssessmentDialog, ims.domain.impl.Transactional
{

	public NutritionalRiskAssessmentVo getRecord(Integer recordId) throws DomainInterfaceException
	{
		DomainFactory factory = getDomainFactory();
		
		NutritionalRiskAssessment domAssess = (NutritionalRiskAssessment)factory.getDomainObject(NutritionalRiskAssessment.class, recordId);
		return(NutritionalRiskAssessmentVoAssembler.create(domAssess));
	}

	public AssessmentHeaderInfo saveRecord(NutritionalRiskAssessmentVo nutritionalRiskRecord, AssessmentHeaderInfo assessment, AssessmentComponent component, VitalSignsVo vitalSign) throws StaleObjectException
	{
		// Ensure the Value Object was validated
		if (!nutritionalRiskRecord.isValidated())
			throw new DomainRuntimeException("This Nutritional Risk Assessment has not been validated");
			
		DomainFactory factory = getDomainFactory();			
		NutritionalRiskAssessment domAssessment = NutritionalRiskAssessmentVoAssembler.extractNutritionalRiskAssessment(factory, nutritionalRiskRecord);
		
		factory.save(domAssessment);
		
		if(vitalSign != null)
			saveVitalSign(vitalSign);
		
		if (assessment != null)
		{
			if(component != null && component instanceof NurAssessmentEatingVo)
			{
				NutritionalRiskAssessmentVo nutritionalRisk = NutritionalRiskAssessmentVoAssembler.create(domAssessment);
				nutritionalRisk.validate();
				
				((NurAssessmentEatingVo)component).setNutritionalRiskAssessment(nutritionalRisk);
				component.validate();
			}

			assessment.validate();

			return saveEatingComponent(assessment, component, null);
		}
		return null;  // It otherwise expects assessment header
	}

	public Boolean isPartOfEatingAssessment(NutritionalRiskAssessmentVo nutritionalRiskRecord) throws DomainInterfaceException
	{
		DomainFactory factory = getDomainFactory();
		
		NutritionalRiskAssessment domNutritionalRisk = NutritionalRiskAssessmentVoAssembler.extractNutritionalRiskAssessment(factory, nutritionalRiskRecord);
		java.util.List water = factory.find("from NurEatingComponent n where n.nutritionalRiskAssessment = :nr", "nr", domNutritionalRisk);
		
		if(water == null || water.size() == 0)
			return Boolean.FALSE;
		
		return Boolean.TRUE;
	}

	public AssessmentHeaderInfo saveEatingComponent(AssessmentHeaderInfo assessment, AssessmentComponent component, ClinicalContactShortVo voClinicalContactShort) throws StaleObjectException 
	{
		return null;
	}

	public VitalSignsVoCollection getLastMetrics(ClinicalContactShortVo voClinicalContactShort, Date recordingDate) throws DomainInterfaceException
	{
		DomainFactory factory = getDomainFactory();
		StringBuffer filterString = new StringBuffer();
		String query = "from VitalSigns vs ";
		ArrayList markers= new ArrayList();
		ArrayList values = new ArrayList();
		String andStr = " ";

		if (voClinicalContactShort != null)
		{
			filterString.append(andStr + " vs.clinicalContact.id = :rcc");
			markers.add("rcc");
			values.add(voClinicalContactShort.getID_ClinicalContact());
			andStr = " and ";
		}
		
		if(recordingDate != null)
		{
			Date filterDate = new Date(recordingDate);
			Date tofilterDate = new Date(recordingDate).addDay(1);
			
			filterString.append(andStr + " vs.authoringDateTime > :fromDate and vs.authoringDateTime < :toDate");
			markers.add("fromDate");
			markers.add("toDate");
			values.add(filterDate.getDate());
			values.add(tofilterDate.getDate());
			andStr = " and ";

		}
		
		if (markers.size() > 0) query += " where ";
		query += filterString.toString();	
		java.util.List vitals = factory.find(query,markers,values);
		VitalSignsVoCollection voVitalsColl = VitalSignsVoAssembler.createVitalSignsVoCollectionFromVitalSigns(vitals);
		if(voVitalsColl != null && voVitalsColl.size() > 0)
		{
				voVitalsColl.sort(SortOrder.DESCENDING);
				return voVitalsColl;
		}
		else
			return null;
	}
	
	public VitalSignsVo saveVitalSign(VitalSignsVo vitalSign) throws ims.domain.exceptions.StaleObjectException
	{
		if (!vitalSign.isValidated())
		{
			throw new DomainRuntimeException("This Vital Sign has not been validated");
		}
		
		DomainFactory factory = getDomainFactory();

		VitalSigns doVitals = VitalSignsVoAssembler.extractVitalSigns(factory, vitalSign);
		
		factory.save(doVitals);
		return VitalSignsVoAssembler.create(doVitals);
	}

	public VitalSignsVoCollection getLastMetricsByCareContext(CareContextRefVo voCareContextRef, Date recordingDate)
	{
		DomainFactory factory = getDomainFactory();
		StringBuffer filterString = new StringBuffer();
		String query = "from VitalSigns vs ";
		ArrayList markers= new ArrayList();
		ArrayList values = new ArrayList();
		String andStr = " ";

		if (voCareContextRef != null)
		{
			filterString.append(andStr + " vs.careContext.id = :rcc");
			markers.add("rcc");
			values.add(voCareContextRef.getID_CareContext());
			andStr = " and ";
		}
		
		if(recordingDate != null)
		{
			Date filterDate = new Date(recordingDate);
			Date tofilterDate = new Date(recordingDate).addDay(1);
			
			filterString.append(andStr + " vs.authoringDateTime > :fromDate and vs.authoringDateTime < :toDate");
			markers.add("fromDate");
			markers.add("toDate");
			values.add(filterDate.getDate());
			values.add(tofilterDate.getDate());
			andStr = " and ";

		}
		
		filterString.append(andStr + " vs.clinicalContact.id is null");
		
		if (markers.size() > 0) query += " where ";
		query += filterString.toString();	
		java.util.List vitals = factory.find(query,markers,values);
		VitalSignsVoCollection voVitalsColl = VitalSignsVoAssembler.createVitalSignsVoCollectionFromVitalSigns(vitals);
		if(voVitalsColl != null && voVitalsColl.size() > 0)
		{
				voVitalsColl.sort(SortOrder.DESCENDING);
				return voVitalsColl;
		}
		else
			return null;
	}

	public VitalSignsVoCollection getLastMetrics(Integer idPatient)
	{
		if (idPatient == null)
			return null;

		DomainFactory factory = getDomainFactory();
		StringBuffer filterString = new StringBuffer();
		String query = "from VitalSigns vs ";
		ArrayList markers= new ArrayList();
		ArrayList values = new ArrayList();

		filterString.append(" vs.careContext.episodeOfCare.careSpell.patient.id = :idPat");
		markers.add("idPat");
		values.add(idPatient);
		
		if (markers.size() > 0) query += " where ";
		query += filterString.toString();	
		java.util.List vitals = factory.find(query,markers,values);
		VitalSignsVoCollection voVitalsColl = VitalSignsVoAssembler.createVitalSignsVoCollectionFromVitalSigns(vitals);
		if(voVitalsColl != null && voVitalsColl.size() > 0)
		{
				voVitalsColl.sort(SortOrder.DESCENDING);
				return voVitalsColl;
		}
		else
			return null;
	}

}
