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

package ims.nursing.domain.impl;

import ims.core.admin.vo.CareContextRefVo;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainInterfaceException;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.impl.DomainImpl;
import ims.framework.utils.Date;
import ims.nursing.domain.WaterlowDialog;
import ims.nursing.vo.AssessmentComponent;
import ims.nursing.vo.AssessmentHeaderInfo;
import ims.nursing.vo.WaterlowAssessmentVo;
import ims.nursing.vo.WaterlowAssessmentVoCollection;
import ims.nursing.vo.domain.WaterlowAssessmentVoAssembler;

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

public class WaterlowAssessmentImpl extends DomainImpl implements ims.nursing.domain.WaterlowView, ims.domain.impl.Transactional
{
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	public Boolean isPartOfSkinAssessment(WaterlowAssessmentVo waterRecord) throws DomainInterfaceException
	{
		WaterlowDialog impl = (WaterlowDialog)getDomainImpl(WaterlowDialogImpl.class);
		
		return impl.isPartOfSkinAssessment(waterRecord);
	}
	
	/**
	* list waterlow assessments
	*/
	public WaterlowAssessmentVoCollection listAssessments(Date dateFrom, Date dateTo, CareContextRefVo voCurrentCareContext, Integer idPatient) throws DomainInterfaceException
	{
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer("");
		String query ="from Waterlow wa ";
		ArrayList<String> markers= new ArrayList<String>();
		ArrayList<Object> values = new ArrayList<Object>();
		String andStr = " ";	
		
		hql.append(" wa.careContext.episodeOfCare.careSpell.patient.id = :patId");
		markers.add("patId");
		values.add(idPatient);
		
		if (dateFrom != null && dateTo != null) 
		{
			hql.append(" and ");
			hql.append(" wa.dateTimeInitiated > :fromDate and wa.dateTimeInitiated < :toDate ");
			markers.add("fromDate");
			markers.add("toDate");
			values.add(dateFrom.addDay(-1).getDate());
			values.add(dateTo.addDay(1).getDate());
		} 
		else if (dateFrom != null) 
		{
			hql.append(" and ");			
			hql.append(" wa.dateTimeInitiated > :fromDate ");
			markers.add("fromDate");
			values.add(dateFrom.getDate());			
		}
		
		if(voCurrentCareContext != null && voCurrentCareContext.getID_CareContextIsNotNull())
		{
			hql.append(" and ");			
			hql.append(andStr + " wa.careContext.id = :rcc");
			markers.add("rcc");
			values.add(voCurrentCareContext.getID_CareContext());
		}
		
		if (markers.size() > 0)
			query += " where ";
		
		query += hql.toString();
		query += " order by wa.dateTimeInitiated";
		
		return WaterlowAssessmentVoAssembler.createWaterlowAssessmentVoCollectionFromWaterlow(factory.find(query,markers,values));
	}


	public Boolean hasWaterlowForCurrentAdmission(CareContextRefVo careContextRef)
	{
		// check at episode of care level that owns the current care context
		
		String hql = "select count (w1_1.id) from Waterlow as w1_1 join w1_1.careContext as cc WHERE cc.id = :CCID and w1_1.isRIE is null";
		
		DomainFactory factory = getDomainFactory();
		
		@SuppressWarnings("rawtypes")
		List list = factory.find(hql, new String[] {"CCID"}, new Object[] {careContextRef.getID_CareContext()});
		
		return list.get(0).toString().equals("0") ? Boolean.FALSE : Boolean.TRUE;
	}

	public AssessmentHeaderInfo saveRecord(WaterlowAssessmentVo waterRecord, AssessmentHeaderInfo assessment, AssessmentComponent component) throws StaleObjectException
	{
		WaterlowDialog impl = (WaterlowDialog) getDomainImpl(WaterlowDialogImpl.class);
		
		return impl.saveRecord(waterRecord, assessment, component);
	}


}
