/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/domain/impl/NursingSummaryImpl.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.51 build 2469.26857)
// Copyright (C) 1995-2006 IMS MAXIMS plc. All rights reserved.

package ims.nursing.domain.impl;

import ims.assessment.domain.DynamicAssessments;
import ims.assessment.domain.impl.DynamicAssessmentsImpl;
import ims.assessment.vo.PatientAssessmentListVo;
import ims.assessment.vo.PatientAssessmentListVoCollection;
import ims.assessment.vo.PatientAssessmentVo;
import ims.assessment.vo.PatientAssessmentVoCollection;
import ims.assessment.vo.domain.PatientAssessmentListVoAssembler;
import ims.assessment.vo.domain.PatientAssessmentVoAssembler;
import ims.core.admin.vo.CareContextRefVo;
import ims.core.vo.lookups.PatientAssessmentStatusReason;
import ims.core.vo.lookups.UserDefinedAssessmentType;
import ims.domain.DomainFactory;
import ims.framework.exceptions.CodingRuntimeException;
import ims.nursing.domain.DailyPatientProgress;
import ims.nursing.domain.base.impl.BaseNursingSummaryImpl;
import ims.nursing.vo.BradenScale;
import ims.nursing.vo.MUSTVo;
import ims.nursing.vo.PatientInvasiveDeviceShortVoCollection;
import ims.nursing.vo.PlanOfCareListVoCollection;
import ims.nursing.vo.RepositioningPlanVo;
import ims.nursing.vo.domain.PlanOfCareListVoAssembler;
import ims.nursing.vo.lookups.PlanOfCareStatus;
import java.util.ArrayList;
import java.util.List;

public class NursingSummaryImpl extends BaseNursingSummaryImpl
{
	private static final long serialVersionUID = 1L;

	public PlanOfCareListVoCollection listPlansofCare(CareContextRefVo voCareContextRef, PlanOfCareStatus status, ims.framework.utils.DateTime date1, ims.framework.utils.DateTime date2) 
	{
		if(voCareContextRef == null)
			throw new CodingRuntimeException("The 'careContext' parameter cannot be null !");
		
		DomainFactory factory = getDomainFactory();
		
		String hql = new String();

		StringBuffer condStr = new StringBuffer();
		String andStr = " ";
	
		ArrayList<String> markers = new ArrayList<String>();
		ArrayList<Comparable> values = new ArrayList<Comparable>();
		
		hql = "select p1_1 from PlanOfCare as p1_1 left join p1_1.actions as p2_1 left join p1_1.group as l1_1";
		
		if(status != null)
		{
			if (status.equals(PlanOfCareStatus.CURRENT))
			{
				condStr.append(andStr + " (p1_1.status.id ='-1089'  or p1_1.status.id='-1091')  ");
				andStr = " and ";	
			}
			else
			{
				condStr.append(andStr + " p1_1.status.id = :STATUS ");
				markers.add("STATUS");
				values.add(new Integer(status.getId()));
			
				andStr = " and ";
			}
		}
		
		if(date1 != null && date2 != null)
		{
			condStr.append(andStr + " p1_1.authoringInformation.authoringDateTime between :DATE1 and :DATE2 ");
			markers.add("DATE1");
			values.add(date1.getJavaDate());
			markers.add("DATE2");
			values.add(date2.getJavaDate());
			
			andStr = " and ";
		}
		
		condStr.append(andStr + " p1_1.careContext.id = :CONTEXT ");
		markers.add("CONTEXT");
		values.add(voCareContextRef.getID_CareContext());
		
		hql += " where ";

		hql += condStr.toString();
		hql += " order by l1_1.text asc, p1_1.title asc, p2_1.authoringInformation.authoringDateTime desc";
		
		return (PlanOfCareListVoAssembler.createPlanOfCareListVoCollectionFromPlanOfCare(factory.find(hql,markers,values)));
	}
	

	public MUSTVo getLastComplementedMustRecord(CareContextRefVo careContextRefVo)
	{
		DailyPatientProgress impl = (DailyPatientProgress) getDomainImpl(DailyPatientProgressImpl.class);
		return impl.getLastComplementedMustRecord(careContextRefVo);
	}

	public PatientAssessmentListVo getLastCompletedAssessment(UserDefinedAssessmentType type, CareContextRefVo careContext, Boolean isCurrentForType)
	{
		DailyPatientProgress impl = (DailyPatientProgress) getDomainImpl(DailyPatientProgressImpl.class);
		return impl.getLastCompletedAssessment(type, careContext, isCurrentForType);
	}

	public BradenScale getLastCompletedBradenScale(CareContextRefVo careContextRefVo)
	{
		DailyPatientProgress impl = (DailyPatientProgress) getDomainImpl(DailyPatientProgressImpl.class);
		return impl.getLastCompletedBradenScale(careContextRefVo);
	}

	public RepositioningPlanVo getLastCompletedRepositioningPlan(CareContextRefVo voCareContextRef)
	{
		DailyPatientProgress impl = (DailyPatientProgress) getDomainImpl(DailyPatientProgressImpl.class);
		return impl.getLastCompletedRepositioningPlan(voCareContextRef);
	}

	public PatientInvasiveDeviceShortVoCollection listCurrentPatientInvasiveDevice(CareContextRefVo careContext)
	{
		DailyPatientProgress impl = (DailyPatientProgress) getDomainImpl(DailyPatientProgressImpl.class);
		return impl.listCurrentPatientInvasiveDevice(careContext);
	}

	public PatientAssessmentVo getLastDPPAssessment(UserDefinedAssessmentType type, CareContextRefVo careContext)
	{
		if(type == null)
			throw new CodingRuntimeException("The 'type' parameter cannot be null !");
		if(careContext == null)
			throw new CodingRuntimeException("The 'careContext' parameter cannot be null !");
		
		DomainFactory factory = getDomainFactory();
		
		String hql = new String();
		hql = "select p1_1 from PatientAssessment as p1_1 left join p1_1.assessmentData as p2_1 left join p2_1.userAssessment as u1_1 left join u1_1.assessmentType as l1_1 left join p1_1.careContext as c1_1 left join p1_1.status as l2_1\r\n" + 
				"where\r\n" + 
				"(l2_1 = :STATUS and l1_1.id = :ASSTYPE and c1_1.id = :CONTEXT)\r\n" + 
				"order by p1_1.systemInformation.creationDateTime desc ";

		PatientAssessmentVoCollection coll = PatientAssessmentVoAssembler.createPatientAssessmentVoCollectionFromPatientAssessment(
				factory.find(hql, new String[] {"STATUS","ASSTYPE","CONTEXT"}, 
						new Object[] {getDomLookup(PatientAssessmentStatusReason.COMPLETED), new Integer(type.getId()), careContext.getID_CareContext()}));
		
		return coll.size() > 0 ? coll.get(0) : null;
	}

	//WDEV-12215
	public PatientAssessmentListVo getLastStructuredAssessment(UserDefinedAssessmentType type, CareContextRefVo careContext)
	{
		List list = getDomainFactory().find("from PatientAssessment as p1_1 where p1_1.assessmentData.userAssessment.assessmentType.id = :TYPE and p1_1.careContext.id = :CARE_CONTEXT order by p1_1.authoringInformation.authoringDateTime desc", new String[] {"TYPE", "CARE_CONTEXT"}, new Object[] {new Integer(type.getID()), careContext.getID_CareContext()});
		
		//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);
		}
		
		PatientAssessmentListVoCollection voColl = PatientAssessmentListVoAssembler.createPatientAssessmentListVoCollectionFromPatientAssessment(list);
		if(voColl != null && voColl.size() > 0)
			return voColl.get(0);
			
		return null;
	}
}
