/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/domain/impl/MUSTImpl.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 Charlotte Murphy using IMS Development Environment (version 1.45 build 2377.15764)
// Copyright (C) 1995-2006 IMS MAXIMS plc. All rights reserved.

package ims.nursing.domain.impl;

import ims.admin.domain.HcpAdmin;
import ims.admin.domain.impl.HcpAdminImpl;
import ims.core.admin.vo.CareContextRefVo;
import ims.core.domain.VitalSignsMetrics;
import ims.core.domain.impl.VitalSignsImpl;
import ims.core.vo.HcpFilter;
import ims.core.vo.HcpLiteVoCollection;
import ims.core.vo.VSMetrics;
import ims.core.vo.VitalSignsVoCollection;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.StaleObjectException;
import ims.nursing.assessmenttools.domain.objects.MUST;
import ims.nursing.domain.base.impl.BaseMUSTImpl;
import ims.nursing.vo.MUSTVo;
import ims.nursing.vo.MUSTVoCollection;
import ims.nursing.vo.domain.MUSTVoAssembler;

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

public class MUSTImpl extends BaseMUSTImpl
{

	private static final long serialVersionUID = 1L;

	public HcpLiteVoCollection listMedics(HcpFilter voHcpFilter) 
	{
		HcpAdmin hcpAdmin = (HcpAdmin)getDomainImpl(HcpAdminImpl.class);
		if (voHcpFilter != null && voHcpFilter.getQueryNameIsNotNull())
			return hcpAdmin.listHcpLiteByName(voHcpFilter.getQueryName().getSurname());
		return null;
	}

	public MUSTVoCollection listMUSTByCareContext(CareContextRefVo voCareContextRef) 
	{
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer(" ");
		String query = "from MUST m ";
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();
		String andStr = " ";
		
		if (voCareContextRef != null)
		{
			hql.append(andStr + " m.careContext.id = :id_CareContext");
			markers.add("id_CareContext");
			values.add(voCareContextRef.getID_CareContext());			
			andStr = " and ";
		}
		
		if (markers.size() > 0) query += " where ";
		query += hql.toString();
		
		List must = factory.find(query, markers, values);	
		MUSTVoCollection collMUSTVo = MUSTVoAssembler.createMUSTVoCollectionFromMUST(must);
		if(collMUSTVo != null)
			return collMUSTVo.sort();
		
		return null;
	}
	
	public MUSTVo getLastComplementedMustRecord(CareContextRefVo voCareContextRef)
	{
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer("from MUST m ");
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();
		String andStr = " ";

		if (voCareContextRef != null)
		{
			hql.append(" where ");
			hql.append(andStr + " m.careContext.id = :id_CareContext");
			markers.add("id_CareContext");
			values.add(voCareContextRef.getID_CareContext());
			andStr = " and ";
		}

		hql.append(" order by m.authoringInformation.authoringDateTime desc ");

		List list = factory.find(hql.toString(), markers, values);
		
		//Get only the first record (to optimise the work of the assembler)
		if(list != null && list.size() > 1)
		{
			Object firstItem = list.get(0);
			list.removeAll(list);
			list.add(firstItem);
		}
		
		MUSTVoCollection collMUSTVo = MUSTVoAssembler.createMUSTVoCollectionFromMUST(list);
		if (collMUSTVo != null && collMUSTVo.size() > 0)
			return collMUSTVo.get(0);

		return null;
	}

	public MUSTVo saveMUST(MUSTVo voMUST) throws StaleObjectException 
	{
		if(!voMUST.isValidated())
			throw new DomainRuntimeException("This MUST has not been validated");
		
		DomainFactory factory = getDomainFactory();
		MUST doMUST = MUSTVoAssembler.extractMUST(factory, voMUST);
		factory.save(doMUST);
		return MUSTVoAssembler.create(doMUST);
	}

	public VSMetrics getLastMetrics(CareContextRefVo voCareContext) 
	{
		VitalSignsMetrics metrics = (VitalSignsMetrics) getDomainImpl(VitalSignsImpl.class);
		VitalSignsVoCollection voVsColl = metrics.listMetrics(null, null, voCareContext);
		if (voVsColl != null)
		{
			if (voVsColl.size() > 0 && voVsColl.get(0).getMetricsIsNotNull())
				return voVsColl.get(0).getMetrics();
		}		
		return null; 		
	}

	public VSMetrics getLastMUSTMetrics(CareContextRefVo voCareContext)
	{
		if (voCareContext == null)
			return null;
		
		MUSTVo voMust = getLastComplementedMustRecord(voCareContext);
		if (voMust != null)
			return voMust.getMetrics();
		else
			return null;
	}


}
