/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Pathways/src/ims/pathways/domain/impl/PatientJourneyCommentImpl.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 Gabriel Maxim using IMS Development Environment (version 1.80 build 5256.13787)
// Copyright (C) 1995-2014 IMS MAXIMS. All rights reserved.

package ims.pathways.domain.impl;

import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainInterfaceException;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.exceptions.CodingRuntimeException;
import ims.framework.utils.Date;
import ims.pathways.configuration.domain.objects.Pathway;
import ims.pathways.configuration.vo.PathwayRefVo;
import ims.pathways.domain.base.impl.BasePatientJourneyCommentImpl;
import ims.pathways.domain.objects.PatientJourneyComment;
import ims.pathways.domain.objects.PatientPathwayJourney;
import ims.pathways.vo.PathwayLiteVo;
import ims.pathways.vo.PatientJourneyCommentRefVo;
import ims.pathways.vo.PatientJourneyCommentVo;
import ims.pathways.vo.PatientJourneyCommentVoCollection;
import ims.pathways.vo.PatientJourneyInterfaceVo;
import ims.pathways.vo.PatientJourneyVo;
import ims.pathways.vo.PatientPathwayJourneyRefVo;
import ims.pathways.vo.domain.PathwayLiteVoAssembler;
import ims.pathways.vo.domain.PatientJourneyCommentVoAssembler;
import ims.pathways.vo.domain.PatientJourneyVoAssembler;

import java.util.List;

public class PatientJourneyCommentImpl extends BasePatientJourneyCommentImpl
{

	private static final long serialVersionUID = 1L;
	
	public PatientJourneyVo save(PatientJourneyInterfaceVo recordVo, PathwayLiteVo pathway, Date nextValidationDate,PatientJourneyCommentVo commentVo, Boolean validationMode, Date previousNextValidationDate) throws DomainInterfaceException,	StaleObjectException
	{
		if (recordVo == null)
			throw new CodingRuntimeException("The PatientJourneyInterfaceVo argument is null.Cannot update a null patient journey");
		if (pathway == null)
			throw new CodingRuntimeException("The PathwayLiteVo argument is null.Cannot save a null pathway argument for the patient journey.");
		if (commentVo == null)
			throw new CodingRuntimeException("The PatientJourneyCommentVo argument is null.Cannot commit.");
		if (!commentVo.isValidated())
			throw new CodingRuntimeException("The PatientJourneyCommentVo argument is not validated.Cannot commit.");
		
		PatientJourneyVo journeyVo = getJourney(recordVo);
		
		if (journeyVo.getLinkedComments() == null)
			journeyVo.setLinkedComments(new PatientJourneyCommentVoCollection());
		
		journeyVo.getLinkedComments().add(commentVo);
		journeyVo.setNextValidationDate(nextValidationDate);
		
		if (Boolean.TRUE.equals(validationMode) && previousNextValidationDate != null) //WDEV-19604 
		{
			journeyVo.setLastValidationDate(previousNextValidationDate);
		}
	
		ims.pathways.domain.HL7PathwayIf impl = (ims.pathways.domain.HL7PathwayIf ) getDomainImpl(HL7PathwayIfImpl.class);
		
		return impl.updatePatientJourney(journeyVo, pathway);
	}
	public PatientJourneyVo getJourney(PatientPathwayJourneyRefVo refVo)
	{
		if (refVo == null)
			throw new CodingRuntimeException("Cannot retrieve argument for a null PatientPathwayJourneyRefVo argument.");
	
		return PatientJourneyVoAssembler.create((PatientPathwayJourney) getDomainFactory().getDomainObject(PatientPathwayJourney.class, refVo.getID_PatientPathwayJourney()));
	}
	

	public PathwayLiteVo getPathway(PathwayRefVo pathwayRef)
	{
		if (pathwayRef == null)
			throw new CodingRuntimeException("The PathwayRefVo argument is null");
		
	return 	PathwayLiteVoAssembler.create((Pathway) getDomainFactory().getDomainObject(Pathway.class, pathwayRef.getID_Pathway()));
	}
	
	public PatientJourneyInterfaceVo getJourneyAndEvents(PatientPathwayJourneyRefVo journeyRef, PathwayRefVo pathRef)
	{
		if (journeyRef == null)
			throw new CodingRuntimeException("Cannot retrieve argument for a null PatientPathwayJourneyRefVo argument.");

		ims.pathways.domain.PatientJourney implPatientJourney = (ims.pathways.domain.PatientJourney) getDomainImpl(PatientJourneyImpl.class);

		return implPatientJourney.getPatientJourney(journeyRef, pathRef);
	}
	public String getClinicName(String clinicCode)
	{
		ims.pathways.domain.PatientJourney implPatientJourney = (ims.pathways.domain.PatientJourney) getDomainImpl(PatientJourneyImpl.class);
		
		return implPatientJourney.getClinicName(clinicCode);
	}
	
	public PatientJourneyCommentVo getLastComment(PatientPathwayJourneyRefVo patwayRef)
	{
		if (patwayRef == null)
			throw new CodingRuntimeException("The PathwayRefVo argument is null");
		
		String hql = "select comments from PatientPathwayJourney as pj left join pj.linkedComments as comments where pj.id = :JOURNEY_ID order by comments.recordingInfo.recordingDateTime desc";
		List<?> result = getDomainFactory().find(hql, "JOURNEY_ID", patwayRef.getID_PatientPathwayJourney());
		
		if (result == null || result.isEmpty())
			return null;
		
		return PatientJourneyCommentVoAssembler.create((PatientJourneyComment)result.get(0));
	}
	
	public Date getRTTBreachDate(PatientPathwayJourneyRefVo journey)
	{
		if(journey == null || journey.getID_PatientPathwayJourney() == null)
			return null;
		
		DomainFactory factory = getDomainFactory();
		
		String query = "select rd.end18WW from CatsReferral as cr left join cr.referralDetails as rd left join cr.journey as j where j.id = :JourneyId";
		List<?> dates = factory.find(query, new String[] {"JourneyId"}, new Object[] {journey.getID_PatientPathwayJourney()});
		
		if(dates != null && dates.size() > 0)
		{
			if(dates.get(0) instanceof java.util.Date)
				return new Date((java.util.Date) dates.get(0));
		}
		
		return null;
	}
	
	public PatientJourneyCommentVo getCommentDetails(PatientJourneyCommentRefVo comment)
	{
		if (comment == null)
			return null;
		
		String hql = "select comments from PatientPathwayJourney as pj left join pj.linkedComments as comments where comments.id = :COMMENT_ID";
		List<?> result = getDomainFactory().find(hql, "COMMENT_ID", comment.getID_PatientJourneyComment());
		
		if (result == null || result.isEmpty())
			return null;
		
		return PatientJourneyCommentVoAssembler.create((PatientJourneyComment)result.get(0));
	}
	
	public Boolean isStale(PatientJourneyCommentRefVo commentRef)
	{
		if(commentRef == null)
			return false;
		
		DomainFactory factory = getDomainFactory();
		List<?> appts = factory.find("select pjc.id from PatientJourneyComment as pjc where pjc.id = :noteID and pjc.version > :noteVersion", new String[] {"noteID", "noteVersion"}, new Object[] {commentRef.getID_PatientJourneyComment(), commentRef.getVersion_PatientJourneyComment()});
		
		if(appts != null && appts.size() > 0)
			return true;
		
		return false;
	}
}
