/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/domain/impl/CopyCarePlansImpl.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 Florin Blindu using IMS Development Environment (version 1.80 build 4471.18200)
// Copyright (C) 1995-2012 IMS MAXIMS. All rights reserved.

package ims.nursing.domain.impl;

import ims.core.admin.domain.objects.CareContext;
import ims.core.admin.domain.objects.CareSpell;
import ims.core.admin.vo.CareContextRefVo;
import ims.core.vo.CareContextVo;
import ims.core.vo.CareSpellVo;
import ims.core.vo.domain.CareContextVoAssembler;
import ims.core.vo.domain.CareSpellVoAssembler;
import ims.domain.DomainFactory;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.enumerations.SortOrder;
import ims.framework.exceptions.CodingRuntimeException;
import ims.nursing.careplans.domain.objects.CarePlan;
import ims.nursing.careplans.vo.CarePlanRefVo;
import ims.nursing.domain.base.impl.BaseCopyCarePlansImpl;
import ims.nursing.vo.CarePlanForCopyCarePlansVo;
import ims.nursing.vo.CarePlanForCopyCarePlansVoCollection;
import ims.nursing.vo.CarePlanStatus;
import ims.nursing.vo.CopyCarePlanVoCollection;
import ims.nursing.vo.domain.CarePlanForCopyCarePlansVoAssembler;
import ims.nursing.vo.domain.CopyCarePlanVoAssembler;

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

public class CopyCarePlansImpl extends BaseCopyCarePlansImpl
{

	private static final long serialVersionUID = 1L;

	public CopyCarePlanVoCollection listCarePlans(CareContextRefVo voCurrentCareContext, CarePlanStatus currentStatus, Boolean bCarePlansFromOtherContexts)
	{
		if (voCurrentCareContext == null || voCurrentCareContext.getID_CareContext()==null)
			throw new CodingRuntimeException("Current Care Context not available.");

		DomainFactory factory = getDomainFactory();

		String hqlQuery = " from CarePlan cp ";
		StringBuffer condStr = new StringBuffer();
		ArrayList<String> markerNames = new ArrayList<String>();
		ArrayList<Integer> markerValues = new ArrayList<Integer>();

		CopyCarePlanVoCollection voColl = null;

		condStr.append(" where ");

		if (voCurrentCareContext != null)
		{
			if (!bCarePlansFromOtherContexts)
			{
				condStr.append(" cp.careContext.id = :rcc");
				markerNames.add("rcc");
				markerValues.add(voCurrentCareContext.getID_CareContext());
			}
			else
			{
				condStr.append(" cp.careContext.episodeOfCare.careSpell.patient.id = :patientId");
				markerNames.add("patientId");
				markerValues.add(getPatientId(voCurrentCareContext));
			}

		}

		if (currentStatus != null)
		{
			if (currentStatus.getStatus() != null)
			{
				condStr.append(" and cp.currentCarePlanStatus.status.id = :status");
				markerNames.add("status");
				markerValues.add(new Integer(currentStatus.getStatus().getId()));
			}
		}

		hqlQuery += condStr.toString();

		List<?> careplans = factory.find(hqlQuery, markerNames, markerValues);

		if (careplans != null)
			voColl = (CopyCarePlanVoAssembler.createCopyCarePlanVoCollectionFromCarePlan(careplans));

		if (voColl != null)
			voColl.sort(SortOrder.ASCENDING);

		return voColl;
	}

	private Integer getPatientId(CareContextRefVo voCurrentCareContext)
	{

		DomainFactory factory = getDomainFactory();
		CareContext doCareContext = (CareContext) factory.getDomainObject(CareContext.class, voCurrentCareContext.getID_CareContext());
		CareContextVo voCareContext = CareContextVoAssembler.create(doCareContext);
		CareSpell doCareSpell = (CareSpell) factory.getDomainObject(CareSpell.class, voCareContext.getEpisodeOfCare().getCareSpell().getID_CareSpell());
		CareSpellVo voCareSpell = CareSpellVoAssembler.create(doCareSpell);
		return voCareSpell.getPatient().getID_Patient();
	}

	public void saveCarePlans(CarePlanForCopyCarePlansVoCollection collCarePlansToSave) throws StaleObjectException
	{
		if (collCarePlansToSave == null || collCarePlansToSave.size() == 0)
		{
			throw new CodingRuntimeException("Cannot copy Care Plans");
		}

		DomainFactory factory = getDomainFactory();

		for (int i = 0; i < collCarePlansToSave.size(); i++)
		{
			CarePlan domainCarePlan = CarePlanForCopyCarePlansVoAssembler.extractCarePlan(factory, collCarePlansToSave.get(i));
			factory.save(domainCarePlan);
		}
	}

	public CarePlanForCopyCarePlansVo getCarePlan(CarePlanRefVo carePlanRef)
	{
		if (carePlanRef == null || carePlanRef.getID_CarePlan() == null)
		{
			throw new CodingRuntimeException("Cannot get CarePlanVo on null Id ");
		}

		DomainFactory factory = getDomainFactory();

		CarePlan domainCarePlan = (CarePlan) factory.getDomainObject(CarePlan.class, carePlanRef.getID_CarePlan());

		return CarePlanForCopyCarePlansVoAssembler.create(domainCarePlan);
	}

}
