/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/domain/impl/PlanOfCareImpl.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 Neil McAnaspie using IMS Development Environment (version 1.45 build 2435.27832)
// Copyright (C) 1995-2006 IMS MAXIMS plc. All rights reserved.

package ims.nursing.domain.impl;

import java.util.ArrayList;
import ims.core.admin.vo.CareContextRefVo;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.exceptions.CodingRuntimeException;
import ims.nursing.domain.base.impl.BasePlanOfCareImpl;
import ims.nursing.domain.objects.PlanOfCare;
import ims.nursing.vo.PlanOfCareActionsVoCollection;
import ims.nursing.vo.PlanOfCareHistoryVoCollection;
import ims.nursing.vo.PlanOfCareListVoCollection;
import ims.nursing.vo.PlanOfCareRefVo;
import ims.nursing.vo.PlanOfCareVo;
import ims.nursing.vo.domain.PlanOfCareActionsVoAssembler;
import ims.nursing.vo.domain.PlanOfCareHistoryVoAssembler;
import ims.nursing.vo.domain.PlanOfCareListVoAssembler;
import ims.nursing.vo.domain.PlanOfCareVoAssembler;
import ims.nursing.vo.lookups.PlanOfCareStatus;

public class PlanOfCareImpl extends BasePlanOfCareImpl
{

	private static final long serialVersionUID = 1L;

	public PlanOfCareListVoCollection listPlansofCareByStatus(CareContextRefVo voCareContextRef, PlanOfCareStatus status) 
	{
		if(voCareContextRef == null)
			throw new CodingRuntimeException("The 'careContext' parameter cannot be null !");
		
		DomainFactory factory = getDomainFactory();
		
		String hql = new String();

		StringBuffer condStr = new StringBuffer();
		String andStr = " ";
	
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();
		
		hql = "select p1_1 from PlanOfCare as p1_1 left join p1_1.actions as p2_1 left join p1_1.group as l1_1";
		
		if(status != null)
		{
			if (status.equals(PlanOfCareStatus.CURRENT))
			{
				condStr.append(andStr + " (p1_1.status.id ='-1089'  or p1_1.status.id='-1091')  ");
				andStr = " and ";	
			}
			else
			{
				condStr.append(andStr + " p1_1.status.id = :STATUS ");
				markers.add("STATUS");
				values.add(new Integer(status.getId()));
			
				andStr = " and ";
			}
		}
		
		condStr.append(andStr + " p1_1.careContext.id = :CONTEXT ");
		markers.add("CONTEXT");
		values.add(voCareContextRef.getID_CareContext());
		
		hql += " where ";

		hql += condStr.toString();
		hql += " order by l1_1.text asc, p1_1.title asc, p2_1.authoringInformation.authoringDateTime desc";
		
		return (PlanOfCareListVoAssembler.createPlanOfCareListVoCollectionFromPlanOfCare(factory.find(hql,markers,values)));
	}
	
	public PlanOfCareHistoryVoCollection listDailyPatientProgressHistory(CareContextRefVo voCareContextRef, PlanOfCareRefVo record)
	{
		return PlanOfCareHistoryVoAssembler.createPlanOfCareHistoryVoCollectionFromPlanOfCareHistory(
				getDomainFactory().find("select answerHistory from PlanOfCare poc where poc.careContext.id = :careContext and poc.id = :pocID", new String[] {"careContext","pocID"}, new Object[] {voCareContextRef.getID_CareContext(), record.getID_PlanOfCare()}));	
	}
		
	public PlanOfCareActionsVoCollection listPocActions(CareContextRefVo voCareContextRef, PlanOfCareRefVo record) 
	{
		return PlanOfCareActionsVoAssembler.createPlanOfCareActionsVoCollectionFromPlanOfCareActions(
				getDomainFactory().find("select actions from PlanOfCare poc where poc.careContext.id = :careContext and poc.id = :pocID", new String[] {"careContext","pocID"}, new Object[] {voCareContextRef.getID_CareContext(), record.getID_PlanOfCare()}));
	}
	
	public PlanOfCareVo getPlanOfCare(PlanOfCareRefVo voPlanOfCareRef) 
	{
		if(voPlanOfCareRef == null || voPlanOfCareRef.getID_PlanOfCare() == null)
		{
			throw new DomainRuntimeException("Invalid record");
		}
		
		DomainFactory factory = getDomainFactory();
		PlanOfCare domainObject = (PlanOfCare)factory.getDomainObject(voPlanOfCareRef);
		
		return PlanOfCareVoAssembler.create(domainObject);
	}

	public PlanOfCareVo savePlanOfCare(PlanOfCareVo record) throws StaleObjectException 
	{
		if(record == null)
		{
			throw new DomainRuntimeException("Invalid record");
		}
		if(!record.isValidated())
		{
			throw new CodingRuntimeException("Record not validated");
		}
			
		DomainFactory factory = getDomainFactory();
		PlanOfCare domainRecord = PlanOfCareVoAssembler.extractPlanOfCare(factory, record);		
		
		factory.save(domainRecord);
			
		return PlanOfCareVoAssembler.create(domainRecord);
	}
}
