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

package ims.nursing.domain.impl;

import java.util.List;

import ims.core.admin.vo.CareContextRefVo;
import ims.core.vo.ClinicalContactShortVo;
import ims.domain.exceptions.DomainException;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainInterfaceException;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.impl.DomainImpl;
import ims.nursing.assessmenttools.domain.objects.Waterlow;
import ims.nursing.vo.AssessmentComponent;
import ims.nursing.vo.AssessmentHeaderInfo;
import ims.nursing.vo.WaterlowAssessmentVo;
import ims.nursing.vo.domain.WaterlowAssessmentVoAssembler;
import ims.spinalinjuries.vo.NurAssessmentSkinVo;

public class WaterlowDialogImpl extends DomainImpl implements ims.nursing.domain.WaterlowDialog, ims.domain.impl.Transactional
{
	/**
	* getRecord ==> Nursing.WaterlowAssessmentVo
	*/
	public ims.nursing.vo.WaterlowAssessmentVo getRecord(Integer recordId) throws ims.domain.exceptions.DomainInterfaceException
	{
		DomainFactory factory = getDomainFactory();
		
		Waterlow domAssess = (Waterlow)factory.getDomainObject(Waterlow.class, recordId);
		return(WaterlowAssessmentVoAssembler.create(domAssess));
	}

	/**
	* save waterlow Record
	*/
	public AssessmentHeaderInfo saveRecord(WaterlowAssessmentVo waterRecord, AssessmentHeaderInfo assessment, AssessmentComponent component) throws StaleObjectException 
	{
		// Ensure the Value Object was validated
		if (!waterRecord.isValidated())
			throw new DomainRuntimeException("This Waterlow Assessment has not been validated");
			
		DomainFactory factory = getDomainFactory();			
		Waterlow domAssessment = WaterlowAssessmentVoAssembler.extractWaterlow(factory, waterRecord);
		
		try
		{
			factory.save(domAssessment);
				
			if (assessment != null)
			{
				if(component != null && component instanceof NurAssessmentSkinVo)
				{
					ims.nursing.vo.WaterlowAssessmentVo water = WaterlowAssessmentVoAssembler.create(domAssessment);
					water.validate();
	
					((NurAssessmentSkinVo)component).setWaterlow(water);
					component.validate();	
				}

				assessment.validate();

				return saveSkinComponent(assessment, component, null);
			}
			return null;  // It otherwise expects assessment header
		}
		catch (StaleObjectException e)
		{
			throw new StaleObjectException(domAssessment,"StaleObjectException occurred.\r\n" + e.getMessage(), e);
		}
		catch (DomainException e)
		{
			throw new DomainRuntimeException("DomainException occurred.\r\n" + e.getMessage(), e);
		} 
	}

	/**
	* true if it is Part Of SkinAssessment
	*/
	public Boolean isPartOfSkinAssessment(ims.nursing.vo.WaterlowAssessmentVo waterRecord) throws ims.domain.exceptions.DomainInterfaceException
	{
		DomainFactory factory = getDomainFactory();
		
		Waterlow domWaterlow = WaterlowAssessmentVoAssembler.extractWaterlow(factory, waterRecord);
		java.util.List water = factory.find("from SkinComponent s where s.waterlow = :water", "water", domWaterlow);
		
		if(water == null || water.size() == 0)
			return Boolean.FALSE;
		
		return Boolean.TRUE;
	}

	public AssessmentHeaderInfo saveSkinComponent(AssessmentHeaderInfo assessment, AssessmentComponent component, ClinicalContactShortVo voClinicalContactShort) throws DomainInterfaceException, StaleObjectException {
		// TODO Auto-generated method stub
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public WaterlowAssessmentVo getLastWaterlowForCurrentAdmission(CareContextRefVo careContextRef)
	{
		// check at episode of care level that owns the current care context
		String hql = "select w1_1 from Waterlow as w1_1 join w1_1.careContext as cc WHERE cc.id = :CCID and w1_1.isRIE is null" +
				" ORDER BY w1_1.dateTimeInitiated desc";
		
		DomainFactory factory = getDomainFactory();
		
		List list = factory.find(hql, new String[] {"CCID"}, new Object[] {careContextRef.getID_CareContext()});
		
		if(list.size() == 0)
			return null;
		
		return WaterlowAssessmentVoAssembler.create((Waterlow) list.get(0));
	}


}
