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

package ims.nursing.domain.impl;

import java.util.ArrayList;
import java.util.List;
import ims.admin.domain.HcpAdmin;
import ims.admin.domain.impl.HcpAdminImpl;
import ims.core.admin.vo.CareContextRefVo;
import ims.core.vo.HcpLiteVoCollection;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.StaleObjectException;
import ims.nursing.domain.base.impl.BaseRepositioningPlanImpl;
import ims.nursing.domain.objects.RepositioningPlan;
import ims.nursing.vo.RepositioningPlanVo;
import ims.nursing.vo.RepositioningPlanVoCollection;
import ims.nursing.vo.domain.RepositioningPlanVoAssembler;
import ims.framework.enumerations.SortOrder;

public class RepositioningPlanImpl extends BaseRepositioningPlanImpl
{
	private static final long serialVersionUID = 1L;

	public RepositioningPlanVoCollection listRepositioningPlanByCareContext(CareContextRefVo voCareContextRef) 
	{
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer(" ");
		String query = "from RepositioningPlan rp";
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();
		String andStr = " ";
		
		if (voCareContextRef != null)
		{
			hql.append(andStr + " rp.careContext.id = :id_CareContext");
			markers.add("id_CareContext");
			values.add(voCareContextRef.getID_CareContext());			
			andStr = " and ";
		}
		
		hql.append(" order by rp.authoringInformation.authoringDateTime desc");
		
		if (markers.size() > 0) query += " where ";
		query += hql.toString();
				
		List plan = factory.find(query, markers, values);	
		RepositioningPlanVoCollection collRepositioningPlanVo = RepositioningPlanVoAssembler.createRepositioningPlanVoCollectionFromRepositioningPlan(plan);
		if(collRepositioningPlanVo != null)
			return collRepositioningPlanVo.sort(SortOrder.DESCENDING);
		
		return null;
	}
	
	public RepositioningPlanVo getLastCompletedRepositioningPlan(CareContextRefVo voCareContextRef) 
	{
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer("from RepositioningPlan rp ");
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();
		String andStr = " ";
		
		if (voCareContextRef != null)
		{
			hql.append(" where ");
			hql.append(andStr + " rp.careContext.id = :id_CareContext");
			markers.add("id_CareContext");
			values.add(voCareContextRef.getID_CareContext());			
		}
		
		hql.append(" order by rp.authoringInformation.authoringDateTime desc ");
		
		List plan = factory.find(hql.toString(), markers, values);	
		if(plan != null && plan.size() > 1)
		{
			Object firstItem = plan.get(0);
			plan.removeAll(plan);
			plan.add(firstItem);
		}
		
		RepositioningPlanVoCollection collRepositioningPlanVo = RepositioningPlanVoAssembler.createRepositioningPlanVoCollectionFromRepositioningPlan(plan);
		if(collRepositioningPlanVo != null && collRepositioningPlanVo.size() > 0)
			return collRepositioningPlanVo.get(0);
		
		return null;
	}

	public RepositioningPlanVo saveRepositioningPlan(RepositioningPlanVo voRepositioningPlan) throws StaleObjectException 
	{
		if(!voRepositioningPlan.isValidated())
			throw new DomainRuntimeException("This Repositioning Plan has not been validated");
		
		DomainFactory factory = getDomainFactory();
		RepositioningPlan doRepositioning = RepositioningPlanVoAssembler.extractRepositioningPlan(factory, voRepositioningPlan);
		factory.save(doRepositioning);
		return RepositioningPlanVoAssembler.create(doRepositioning);
	}
	public HcpLiteVoCollection listHCPs(String hcpName)
	{
		HcpAdmin hcpAdmin = (HcpAdmin)getDomainImpl(HcpAdminImpl.class);
		return hcpAdmin.listHcpLiteByName(hcpName);
	}
}
