/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/domain/impl/CarePlanWorklistImpl.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 Florin Blindu using IMS Development Environment (version 1.80 build 4471.18200)
// Copyright (C) 1995-2012 IMS MAXIMS. All rights reserved.

package ims.nursing.domain.impl;

import ims.core.patient.domain.objects.Patient;
import ims.core.vo.LocationLiteVoCollection;
import ims.core.vo.domain.LocationLiteVoAssembler;
import ims.core.vo.lookups.LocationType;
import ims.domain.DomainFactory;
import ims.framework.utils.Date;
import ims.framework.utils.DateTime;
import ims.framework.utils.Time;
import ims.nursing.careplans.domain.objects.CarePlan;
import ims.nursing.domain.base.impl.BaseCarePlanWorklistImpl;
import ims.nursing.vo.CarePlanForCarePlanWorklistVo;
import ims.nursing.vo.CarePlanWorklistSearchVo;
import ims.nursing.vo.CarePlanWorklistVo;
import ims.nursing.vo.CarePlanWorklistVoCollection;
import ims.nursing.vo.PatientForCarePlanWorklistVo;
import ims.nursing.vo.domain.CarePlanForCarePlanWorklistVoAssembler;
import ims.nursing.vo.domain.PatientForCarePlanWorklistVoAssembler;

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

public class CarePlanWorklistImpl extends BaseCarePlanWorklistImpl
{

	private static final long serialVersionUID = 1L;

	
	public LocationLiteVoCollection getWards() 
	{
		String hql = "from Location loc where loc.type = " + LocationType.WARD.getId() + " and loc.isActive = 1 and loc.isVirtual = 0 order by loc.upperName asc"; //WDEV-19532		WDEV-20219
		return LocationLiteVoAssembler.createLocationLiteVoCollectionFromLocation(getDomainFactory().find(hql));
	}


	
	public CarePlanWorklistVoCollection listCarePlans(CarePlanWorklistSearchVo searchCriteria) 
	{
		if (searchCriteria.getDateFrom()==null && searchCriteria.getDateTo()==null)
		{
			searchCriteria.setDateTo((new Date()).addDay(ims.configuration.gen.ConfigFlag.DOM.CAREPLAN_WORKLIST_TO_BE_REVIEWED_RANGE.getValue()));
		}
			
		String hql="select carePlan,patient from CarePlan as carePlan left join carePlan.confirmedBy as confirmedBy left join carePlan.careContext as careContext left join careContext.episodeOfCare as episodeOfCare left join episodeOfCare.careSpell as careSpell left join careSpell.patient as patient ";
		//StringBuilder hqlConditions = new StringBuilder(" where ( ");
		
		StringBuffer hqlConditions = new StringBuffer();
		
		ArrayList <String> markers = new ArrayList<String>();
		ArrayList <Object> values = new ArrayList<Object>();
				
		String andStr = "";
		
		//WDEV-15222
		hqlConditions.append(andStr);
		hqlConditions.append(" carePlan.isRIE is null  ");
		andStr = " and ";
		
		if (searchCriteria.getDateFromIsNotNull())
		{
			DateTime dateTimeFrom=new DateTime(searchCriteria.getDateFrom(), new Time(0, 0, 0));
			hqlConditions.append(andStr);
			hqlConditions.append(" carePlan.nextEvaluationDate >= :dateFrom ");
			markers.add("dateFrom");
			values.add(dateTimeFrom.getJavaDate());
			andStr = " and ";
		}
		
		if (searchCriteria.getDateToIsNotNull())
		{
			DateTime dateTimeTo=new DateTime(searchCriteria.getDateTo(), new Time(23, 59, 59));
			hqlConditions.append(andStr);
			hqlConditions.append(" carePlan.nextEvaluationDate <= :dateTo ");
			markers.add("dateTo");
			values.add(dateTimeTo.getJavaDate());
			andStr = " and ";
		}
		
		if (searchCriteria.getHCPIsNotNull())
		{
			hqlConditions.append(andStr);
			hqlConditions.append(" carePlan.confirmedBy.id = :HCP ");
			markers.add("HCP");
			values.add(searchCriteria.getHCP().getID_Hcp());
			andStr = " and ";
		}
		
		if (searchCriteria.getWardIsNotNull())
		{
			hqlConditions.append(andStr);
			hqlConditions.append(" patient.ward.id = :Ward ");
			markers.add("Ward");
			values.add(searchCriteria.getWard().getID_Location());
			andStr = " and ";
		}
		
		if (searchCriteria.getActiveIsNotNull() && searchCriteria.getActive() )
		{
			hqlConditions.append(andStr);
			hqlConditions.append("carePlan.currentCarePlanStatus.status.id= :Status ");
			markers.add("Status");
			values.add(ims.nursing.vo.lookups.CarePlanStatus.ACTIVE.getID());
			andStr = " and ";
		}
		
		if (hqlConditions.length()>0)
		{
			hqlConditions.insert(0, " where (");
			hqlConditions.append(" and  careContext.pasEvent.id in (select i1_1.pasEvent.id from InpatientEpisode as i1_1 left join i1_1.pasEvent as p1_1 left join p1_1.patient as p2_1 where(p2_1.id = patient.id))  "); //wdev-15805
			hqlConditions.append(" ) ");
		}
		else
			hqlConditions.append(" where (careContext.pasEvent.id in (select i1_1.pasEvent.id from InpatientEpisode as i1_1 left join i1_1.pasEvent as p1_1 left join p1_1.patient as p2_1 where(p2_1.id = patient.id)) ) "); //wdev-15805
		
		DomainFactory factory = getDomainFactory();
		
		List <?> list = factory.find((hql+hqlConditions.toString()+" order by carePlan.nextEvaluationDate desc").toString(), markers, values,2000);//WDEV-15222
		
		if(list == null || list.size() == 0)
			return null;

		CarePlanWorklistVoCollection carePlanWorklistColl = new CarePlanWorklistVoCollection();
		
		for(int i = 0;i < list.size();i++)
		{
			CarePlanWorklistVo tempCarePlanWorklist = new CarePlanWorklistVo();
			
			Object[] temp = (Object[]) list.get(i);
			CarePlanForCarePlanWorklistVo tempCarePlan = CarePlanForCarePlanWorklistVoAssembler.create((CarePlan)temp[0]);
			PatientForCarePlanWorklistVo tempPatient = PatientForCarePlanWorklistVoAssembler.create((Patient)temp[1]);		//patient
			
			
			if(tempPatient != null)
			{
				 tempCarePlanWorklist.setPatient(tempPatient);
			}
			
			if(tempCarePlan != null)
			{
				 tempCarePlanWorklist.setCarePlan(tempCarePlan);
			}
			
			carePlanWorklistColl.add(tempCarePlanWorklist);
			
				
		}
		
		return carePlanWorklistColl;
	}

}
