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

package ims.nursing.domain.impl;

import java.util.ArrayList;
import java.util.List;
import ims.core.admin.domain.objects.CareContext;
import ims.core.admin.vo.CareContextRefVo;
import ims.core.vo.CareContextShortVoCollection;
import ims.core.vo.ClinicalContactShortVo;
import ims.core.vo.HcpFilter;
import ims.core.vo.HcpLiteVo;
import ims.core.vo.domain.CareContextShortVoAssembler;
import ims.core.vo.domain.HcpLiteVoAssembler;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.impl.DomainImpl;
import ims.framework.enumerations.SortOrder;
import ims.nursing.assessmenttools.domain.objects.MRSAAssessment;
import ims.nursing.assessmenttools.domain.objects.MRSASitesResults;
import ims.nursing.vo.MRSAAssessmentVo;
import ims.nursing.vo.MRSAAssessmentVoCollection;
import ims.nursing.vo.MRSASitesResultsVo;
import ims.nursing.vo.domain.MRSAAssessmentVoAssembler;
import ims.nursing.vo.domain.MRSASitesResultsVoAssembler;
import ims.nursing.vo.lookups.MRSAResult;

public class MRSAAssessmentImpl extends DomainImpl implements ims.nursing.domain.MRSAAssessment, ims.nursing.domain.MRSAHistory, ims.domain.impl.Transactional
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public MRSAAssessmentVo saveMRSA(MRSAAssessmentVo assessmentMRSA, ClinicalContactShortVo voClinicalContactShort) throws StaleObjectException
	{
		if(!assessmentMRSA.isValidated())
			throw new DomainRuntimeException("This Pain Assessment has not been validated");
		
		DomainFactory factory = getDomainFactory();
		
		MRSAAssessment doAssessMRSA = MRSAAssessmentVoAssembler.extractMRSAAssessment(factory, assessmentMRSA);
	
		factory.save(doAssessMRSA);
		
		return MRSAAssessmentVoAssembler.create(doAssessMRSA);
	}

	public MRSAAssessmentVoCollection listMRSA(CareContextRefVo careContext)
	{
		if (careContext == null || careContext.getID_CareContext() == null)
			return null;
		
		ArrayList<String> paramNames = new ArrayList<String>();
		ArrayList<Object> paramValues = new ArrayList<Object>();
		
		String query = "from MRSAAssessment mrsa where mrsa.class = MRSAAssessment and mrsa.careContext.id = :RCC";

		paramNames.add("RCC");
		paramValues.add(careContext.getID_CareContext());

		return MRSAAssessmentVoAssembler.createMRSAAssessmentVoCollectionFromMRSAAssessment(getDomainFactory().find(query, paramNames, paramValues)).sort(SortOrder.DESCENDING);
	}

	public CareContextShortVoCollection listMRSACareContexts(Integer idPatient)
	{
		if (idPatient != null)
		{
			DomainFactory factory = getDomainFactory();
			List MRSAaList  = factory.find(" select distinct mrsa.careContext from MRSAAssessment mrsa where mrsa.careContext.episodeOfCare.careSpell.patient.id = :patId",
					new String[]{"patId"},
					new Object[]{idPatient});
	
			CareContextShortVoCollection voCareContextList = new CareContextShortVoCollection();
			for (int x = 0; x < MRSAaList.size(); x++)
			{
				CareContext domMrsa = (CareContext) MRSAaList.get(x);
				voCareContextList.add(CareContextShortVoAssembler.create(domMrsa));
			}
	
			return voCareContextList.sort();
		}
		
		return null;
	}

	/**
	 * WDEV-13677
	 * Function used to retrieve the latest MRSA positive result record (Non RIE)
	 */
	public MRSASitesResultsVo getLastMrsaPosResult(Integer idPatient)
	{
		// If the patient ID is null then return null
		if (idPatient == null)
			return null;
		
		// Parameters array
		ArrayList<String> paramNames = new ArrayList<String>();
		ArrayList<Object> paramValues = new ArrayList<Object>();
		
		// Query syntax
		String query = "select siteRes from MRSAAssessment mrsa left join mrsa.sitesAndResults as siteRes where mrsa.careContext.episodeOfCare.careSpell.patient.id = :PATID and mrsa.isRIE is null and siteRes.result.id = :RES order by siteRes.dateResult desc"; //wdev-14307
		
		// Parameters values
		paramNames.add("PATID");		paramValues.add(idPatient);
		paramNames.add("RES");			paramValues.add(MRSAResult.POSITIVE.getID());  //wdev-14307
		
		// Execute query and return the results
	
		return MRSASitesResultsVoAssembler.create((MRSASitesResults) getDomainFactory().findFirst(query, paramNames, paramValues));
	}
	
	public HcpLiteVo getHcp(HcpFilter filter) 
	{
		return HcpLiteVoAssembler.create((ims.core.resource.people.domain.objects.Hcp) getDomainFactory().getDomainObject(filter));
	}
}
