/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/domain/impl/SkinBodyChartImpl.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 Callum Wells using IMS Development Environment (version 1.20 build 40805.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.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.SkinAssessment;
import ims.nursing.vo.SkinAssessmentCollection;
import ims.nursing.vo.domain.SkinAssessmentAssembler;

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

public class SkinBodyChartImpl extends DomainImpl implements ims.nursing.domain.SkinBodyChart, ims.domain.impl.Transactional
{
	protected AssessmentHeaderInfo saveAssessmentComponent(AssessmentHeaderInfo assessment, AssessmentComponent component) throws StaleObjectException,DomainInterfaceException,UniqueKeyViolationException 
	{
		return null;
	}

	public DrawingImageVo getDrawingImage(DrawingImageVo drawingImage) 
	{
		DrawingConfiguration drawingConfigImpl = (DrawingConfiguration)getDomainImpl(DrawingConfigurationImpl.class);
		return drawingConfigImpl.getDrawingImage(drawingImage);
	}

	/** 
	 *  save record
	 */
	public AssessmentHeaderInfo saveRecord(SkinAssessment skinAssessment, AssessmentHeaderInfo assessment, AssessmentComponent component) throws StaleObjectException 
	{
		if(!skinAssessment.isValidated())
			throw new DomainRuntimeException("This Skin Assessment has not been validated");
		
		DomainFactory factory = getDomainFactory();

		ims.nursing.assessmenttools.domain.objects.SkinAssessment domSkinAssess = SkinAssessmentAssembler.extractSkinAssessment(factory, skinAssessment);
		try
		{
			if(domSkinAssess == null)
				throw new DomainException("No Skin Assessment");
			
			if (assessment != null)
			{
				String[] arrErrors = component.validate();
				if(arrErrors != null)
					throw new DomainRuntimeException("Validation Errors");

				return (saveAssessmentComponent(assessment, component));
			}
			else
			{
				factory.save(domSkinAssess);
				SkinAssessment voSkin = SkinAssessmentAssembler.create(domSkinAssess);
				return null;
			}
		}
		catch (DomainException e) 
		{
			throw new DomainRuntimeException("DomainException occurred saving Skin Assessment.\r\n" + e.getMessage(), e);
		}
	}

	/** 
	 *  lists Skin Assessments
	 */
	public SkinAssessmentCollection listSkinAssessment(CareContextRefVo voCurrentCareContext)
	{			
		if ((voCurrentCareContext == null) || (voCurrentCareContext.getID_CareContext() == null))
			return null;
		
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer(" ");
		String query = "from SkinAssessment sa ";
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();
		String andStr = " ";

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

		if (markers.size() > 0) query += " where ";
		query += hql.toString();
		List skinAssList = factory.find(query, markers, values);							
		return SkinAssessmentAssembler.createSkinAssessmentCollectionFromSkinAssessment(skinAssList).sort();
	}

	/** 
	 *  gets last Skin Assessment
	 */	
	public SkinAssessment getLastSkinAssessmentByCareContext(CareContextRefVo voCurrentCareContext)
	{
		if(voCurrentCareContext == null && voCurrentCareContext.getID_CareContextIsNotNull())
			throw new CodingRuntimeException("Current Care Context not available.");
		
		DomainFactory factory = getDomainFactory();
		
		String filter = " from SkinAssessment sa ";			
		StringBuffer filterString = new StringBuffer();

		ArrayList markerNames = new ArrayList();
		ArrayList markerValues = new ArrayList();

		filterString.append(" where sa.careContext.id = :rcc");
		markerNames.add("rcc");
		markerValues.add(voCurrentCareContext.getID_CareContext());
		filter += filterString.toString();		

		filter += " order by sa.dateTimeInitiated desc";

		java.util.List skinAssess = factory.find(filter, markerNames, markerValues);		
		SkinAssessmentCollection voSkinAssessColl = SkinAssessmentAssembler.createSkinAssessmentCollectionFromSkinAssessment(skinAssess).sort(SortOrder.DESCENDING);
		
		if(voSkinAssessColl.size() > 0)
			return voSkinAssessColl.get(0);
		
		return null;
		
	}

	public SkinAssessment getLastSkinAssessmentByPatient(Integer idPatient)
	{
		if(idPatient == null)
			throw new CodingRuntimeException("Patient context not available.");
		
		DomainFactory factory = getDomainFactory();
		
		String filter = " from SkinAssessment sa ";			
		StringBuffer filterString = new StringBuffer();

		ArrayList markerNames = new ArrayList();
		ArrayList markerValues = new ArrayList();

		filterString.append(" where sa.careContext.episodeOfCare.careSpell.patient.id = :idPat");
		markerNames.add("idPat");
		markerValues.add(idPatient);
		filter += filterString.toString();		

		java.util.List skinAssess = factory.find(filter, markerNames, markerValues);		
		if (skinAssess != null && skinAssess.size() > 0) 
		{
			SkinAssessmentCollection voSkinAssessColl = SkinAssessmentAssembler.createSkinAssessmentCollectionFromSkinAssessment(skinAssess).sort(SortOrder.DESCENDING);
			if(voSkinAssessColl!=null && voSkinAssessColl.size()>0)
				return voSkinAssessColl.get(0);
		}
		
		return null;				
	}
}
