/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/domain/impl/MetricImpl.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 Calin Perebiceanu using IMS Development Environment (version 1.80 build 3995.22787)
// Copyright (C) 1995-2010 IMS MAXIMS. All rights reserved.

package ims.nursing.domain.impl;

import java.util.List;

import ims.core.patient.vo.PatientRefVo;
import ims.core.vitals.domain.objects.Metrics;
import ims.core.vo.domain.VSMetricsAssembler;
import ims.domain.DomainFactory;
import ims.domain.DomainObject;
import ims.domain.exceptions.DomainRuntimeException;
import ims.nursing.domain.base.impl.BaseMetricImpl;
import ims.nursing.vo.LastHeightMetricRecordVo;

public class MetricImpl extends BaseMetricImpl
{

	private static final long serialVersionUID = 1L;

	public ims.core.vo.VSMetricsCollection listPatientMetrics(ims.core.patient.vo.PatientRefVo patRef, ims.framework.utils.DateTime dteFrom, ims.framework.utils.DateTime dteTo)
	{
		if (!patRef.getID_PatientIsNotNull())
		{
			throw new DomainRuntimeException("Patient provided doesn't have and id");
		}
		String hql = "select me from Metrics as me left join me.patient as pat where"+ 
					"(me.authoringInformation.authoringDateTime between :dteFrom and :dteTo and pat.id = :patId)"; 
		List<?> domObjs = getDomainFactory().find(hql,new String[]{"dteFrom","dteTo","patId"},new Object[]{dteFrom.getJavaDate(),dteTo.getJavaDate(),patRef.getID_Patient()});
		if (domObjs == null || domObjs.size()==0)
			return null;
		return VSMetricsAssembler.createVSMetricsCollectionFromMetrics(domObjs);
	}

	public ims.core.vo.VSMetrics save(ims.core.vo.VSMetrics metric) throws ims.domain.exceptions.StaleObjectException
	{
		if (!metric.isValidated())
		{
			throw new DomainRuntimeException("Metrics provided are not validate");
		}
		DomainFactory df = getDomainFactory();
		Metrics metricsBo = VSMetricsAssembler.extractMetrics(df, metric);
		df.save(metricsBo);
		return VSMetricsAssembler.create(metricsBo);
	}

	public LastHeightMetricRecordVo getLastRecordedHeight(PatientRefVo patientRef)
	{
		if (!patientRef.getID_PatientIsNotNull())
		{
			throw new DomainRuntimeException("Invalid patient provided");
		}
		String hql = "select metr from Metrics as metr left join metr.patient as pat where (pat.id = :patId"+ 
				" and metr.isRIE is null) order by metr.authoringInformation.authoringDateTime desc";
		DomainObject dObj = getDomainFactory().findFirst(hql,new String[]{"patId"},new Object[]{patientRef.getID_Patient()});
		if (dObj instanceof Metrics)
		{
			Metrics x = (Metrics) dObj;
			LastHeightMetricRecordVo result = new LastHeightMetricRecordVo();
			result.setValue(x.getHeightValue());
			result.setMeasured(x.isHeightEstimatedMeasured());
			return result;
		}
		return null;
	}
}
