/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/domain/impl/PainBodyChartImpl.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 Alexie Ursache using IMS Development Environment (version 1.20 build 40812.900)
// Copyright (C) 1995-2004 IMS MAXIMS plc. All rights reserved.

package ims.nursing.domain.impl;

import ims.admin.domain.DrawingConfiguration;
import ims.admin.domain.impl.DrawingConfigurationImpl;
import ims.core.admin.vo.CareContextRefVo;
import ims.core.admin.vo.ClinicalContactRefVo;
import ims.core.vo.DrawingImageVo;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainException;
import ims.domain.exceptions.DomainInterfaceException;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.exceptions.UniqueKeyViolationException;
import ims.domain.impl.DomainImpl;
import ims.framework.enumerations.SortOrder;
import ims.framework.exceptions.CodingRuntimeException;
import ims.nursing.vo.AssessmentComponent;
import ims.nursing.vo.AssessmentHeaderInfo;
import ims.nursing.vo.PainAssessmentCollection;
import ims.nursing.vo.domain.PainAssessmentAssembler;

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

public class PainBodyChartImpl extends DomainImpl implements ims.nursing.domain.PainBodyChart, ims.domain.impl.Transactional
{
	public DrawingImageVo getDrawingImage(DrawingImageVo drawingImage) 
	{
		DrawingConfiguration drawingConfigImpl = (DrawingConfiguration)getDomainImpl(DrawingConfigurationImpl.class);
		return drawingConfigImpl.getDrawingImage(drawingImage);
	}
	
	/**
	 * save the PainAssessment Record
	 * @throws UniqueKeyViolationException 
	 * @throws DomainInterfaceException 
	 * **/
	public AssessmentHeaderInfo saveRecord(ims.nursing.vo.PainAssessment painAssessment, AssessmentHeaderInfo assessment, AssessmentComponent component) throws StaleObjectException, UniqueKeyViolationException, DomainInterfaceException 
	{
		if(painAssessment != null && !painAssessment.isValidated())
			throw new DomainRuntimeException("The PainAssessment has not been validated");
						
		if (assessment != null && component != null)
		{
			if(!assessment.isValidated())
				throw new DomainRuntimeException("The AssessmentHeaderInfo has not been validated");
		
			if(!component.isValidated())
				throw new DomainRuntimeException("The AssessmentComponent has not been validated");
		
			return (savePainAssessmentComponent(assessment, component, painAssessment.getClinicalContact()));
		}
		else
		{
			DomainFactory factory = getDomainFactory();

			ims.nursing.assessmenttools.domain.objects.PainAssessment domPainAssess = PainAssessmentAssembler.extractPainAssessment(factory, painAssessment);
			try
			{				
				factory.save(domPainAssess);
				ims.nursing.vo.PainAssessment voPain = PainAssessmentAssembler.create(domPainAssess);
				return null;
			}
			catch (DomainException e) 
			{
				throw new DomainRuntimeException("DomainException occurred saving Skin Assessment.\r\n" + e.getMessage(), e);
			}
		}		
	}
	
	/**
	 * This method is called when the Pain body chart form is called from the
	 * Assessment Pain Component form.  It will save the pain component by calling
	 * the implementation from AssessmentComponentImpl.java, with the pain body chart details.
	 * @throws DomainInterfaceException 
	 */
//	public ims.nursing.vo.AssessmentHeaderInfo savePainAssessmentComponent(ims.nursing.vo.AssessmentHeaderInfo assessment, ims.nursing.vo.AssessmentComponent component, ClinicalContactRefVo clinContactShortVo)
	public ims.nursing.vo.AssessmentHeaderInfo savePainAssessmentComponent(ims.nursing.vo.AssessmentHeaderInfo assessment, ims.nursing.vo.AssessmentComponent component, ClinicalContactRefVo clinContactShortVo) throws StaleObjectException, UniqueKeyViolationException, DomainInterfaceException
	{
		return null;
	}
	
	/**
	* lists the PainAssessment records
	*/
	public PainAssessmentCollection listPain(CareContextRefVo voCurrentCareContext)
	{
		if(voCurrentCareContext == null && voCurrentCareContext.getID_CareContextIsNotNull())
			throw new CodingRuntimeException("Current Care Context not available.");
		
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer(" ");
		String query = "from PainAssessment p ";
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();
		String andStr = " ";

		hql.append(andStr + " p.careContext.id = :rcc");
		markers.add("rcc");
		values.add(voCurrentCareContext.getID_CareContext());
		andStr = " and ";

		if (markers.size() > 0) query += " where ";
		query += hql.toString();
		List procs = factory.find(query, markers, values);							
		return PainAssessmentAssembler.createPainAssessmentCollectionFromPainAssessment(procs);
	}

	public ims.nursing.vo.PainAssessment geLastPain(CareContextRefVo voCurrentCareContext)
	{
		if(voCurrentCareContext == null && voCurrentCareContext.getID_CareContextIsNotNull())
			throw new CodingRuntimeException("Current Care Context not available.");
		
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer(" ");
		String query = "from PainAssessment pa ";
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();
		String andStr = " ";

		hql.append(andStr + " pa.careContext.id = :rcc");
		markers.add("rcc");
		values.add(voCurrentCareContext.getID_CareContext());
		andStr = " and ";

		if (markers.size() > 0) query += " where ";
		query += hql.toString();
		List painAssessmentList = factory.find(query, markers, values);		
		
		if (painAssessmentList != null && painAssessmentList.size() > 0) 
		{
			ims.nursing.vo.PainAssessmentCollection voPainAssmntColl = PainAssessmentAssembler.createPainAssessmentCollectionFromPainAssessment(painAssessmentList);
			voPainAssmntColl.sort(SortOrder.DESCENDING);
			return voPainAssmntColl.get(0);
		}
		else
		{
			return null;
		}
	}

	public ims.nursing.vo.PainAssessment getLastPainAssessmentByPatient(Integer idPatient)
	{
		if(idPatient == null)
			throw new CodingRuntimeException("Patient Context not available.");
		
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer(" ");
		String query = "from PainAssessment pa ";
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();
		String andStr = " ";

		hql.append(andStr + " pa.careContext.episodeOfCare.careSpell.patient.id = :idPat");
		markers.add("idPat");
		values.add(idPatient);
		andStr = " and ";

		if (markers.size() > 0) query += " where ";
		query += hql.toString();
		List painAssessmentList = factory.find(query, markers, values);		
		
		if (painAssessmentList != null && painAssessmentList.size() > 0) 
		{
			ims.nursing.vo.PainAssessmentCollection voPainAssmntColl = PainAssessmentAssembler.createPainAssessmentCollectionFromPainAssessment(painAssessmentList).sort(SortOrder.DESCENDING);
			if(voPainAssmntColl!=null&&voPainAssmntColl.size()>0)
				return voPainAssmntColl.get(0);
		}

		return null;

	}
}
