/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/domain/impl/CarePlanStep2Impl.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 Barbara Worwood using IMS Development Environment (version 1.18 build 40628.1730)
// Copyright (C) 1995-2004 IMS MAXIMS plc. All rights reserved.

package ims.nursing.domain.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import ims.nursing.vo.domain.CarePlanAssembler;
import ims.nursing.vo.lookups.AssessmentStatus;
import ims.admin.domain.HcpAdmin;
import ims.admin.domain.impl.HcpAdminImpl;
import ims.core.resource.people.domain.objects.Nurse;
import ims.core.vo.Hcp;
import ims.core.vo.HcpCollection;
import ims.core.vo.HcpFilter;
import ims.core.vo.domain.HcpAssembler;
import ims.core.vo.domain.NurseVoAssembler;
import ims.core.vo.lookups.HcpDisType;
import ims.domain.exceptions.DomainException;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.impl.DomainImpl;
import ims.nursing.assessment.domain.objects.AdmissionAssessment;
import ims.nursing.careplans.domain.objects.CarePlan;
import ims.nursing.careplans.domain.objects.CarePlanTemplate;
import ims.nursing.vo.AssessmentHeaderInfo;
import ims.nursing.vo.CarePlanTemplateTitle;
import ims.nursing.vo.domain.AssessmentHeaderInfoAssembler;
import ims.nursing.vo.domain.CarePlanTemplateAssembler;
import ims.nursing.assessment.domain.objects.AssessmentComponent;

public class CarePlanStep2Impl extends DomainImpl implements ims.nursing.domain.CarePlanStep2, ims.domain.impl.Transactional
{
	/**
	 * Saves the CarePlan
	 */
	public ims.nursing.vo.CarePlan saveCarePlan(ims.nursing.vo.CarePlan carePlan, ims.nursing.vo.CarePlanTemplateTitle carePlanTempl, ims.nursing.vo.AssessmentComponent component) throws StaleObjectException
	{
		// Ensure that the careplan has been validated
		if (!carePlan.isValidated())
			throw new DomainRuntimeException("CarePlan has not been validated");

		DomainFactory factory = getDomainFactory();

		// Convert to domain object
		CarePlan domainCP = CarePlanAssembler.extractCarePlan(factory, carePlan);

		try
		{
			// Link the Careplan to the Template
			CarePlanTemplate domainCPT = null;
			if (carePlanTempl != null)
			{
				domainCPT = (CarePlanTemplate) factory.getDomainObject(CarePlanTemplate.class, carePlanTempl.getID_CarePlanTemplate());
				domainCP.setTemplate(domainCPT);

				// Update the Assessment component if passed in and a
				// template is also associated with the careplan
				if (component != null)
				{
					AssessmentComponent domComp = (AssessmentComponent) factory.getDomainObject(AssessmentComponent.class, component.getID_AssessmentComponent());
					domainCP.setAssessmentComponent(domComp);

					// We also need to set the careplan template for the
					// component
					Set temps = domComp.getCarePlanTemplate();
					temps.add(domainCPT);
					domComp.setCarePlanTemplate(temps);
				}
			}
			factory.save(domainCP);

			return (CarePlanAssembler.create(domainCP));
		}
		catch (DomainException e)
		{
			throw new DomainRuntimeException("Domain Exception occurred.\r\n" + e.getMessage(), e);
		}
	}

	public ims.nursing.vo.CarePlanTemplate getCarePlanTemplate(CarePlanTemplateTitle carePlanTemplate)
	{
		DomainFactory factory = getDomainFactory();

		CarePlanTemplate tempDom = (CarePlanTemplate) factory.getDomainObject(CarePlanTemplate.class, carePlanTemplate.getID_CarePlanTemplate());
		return (CarePlanTemplateAssembler.create(tempDom));
	}

	public void completeComponent(ims.nursing.vo.AssessmentComponent component, AssessmentHeaderInfo assessment) throws StaleObjectException
	{
		DomainFactory factory = getDomainFactory();

		// Update the VO with the completed information
		AssessmentComponent comp = (AssessmentComponent) factory.getDomainObject(AssessmentComponent.class, component.getID_AssessmentComponent());
		comp.setIsComplete(new Boolean(true));
		try
		{
			factory.save(comp);
			// We need to know whether to complete the actual assessment itself
			// too at this stage, call the
			// method in AssessmentComponentImpl to check whether it is required
			// We need to specify one component explicitly as
			// AssessmentComponent is not a class in it's own right
			AdmissionAssessment assmnt = AssessmentHeaderInfoAssembler.extractAdmissionAssessment(factory, assessment);
			if (assmnt.isAssessmentCompleted())
			{
				assmnt.setDateTimeComplete(new java.util.Date());
				assmnt.setHCPComplete(HcpAssembler.extractHcp(factory, (ims.core.vo.Hcp) getHcpUser()));
				assmnt.setAssessmentStatus(factory.getLookupInstance(AssessmentStatus.TYPE_ID, AssessmentStatus.COMPLETE.getID()));
				factory.save(assmnt);
			}
		}
		catch (DomainException e)
		{
			throw new DomainRuntimeException("DomainException occurred completing assessment component.\r\n" + e.getMessage(), e);
		}
	}

	public ims.nursing.vo.AssessmentComponent getComponent(ims.nursing.vo.AssessmentComponent component)
	{
		return null;
	}

	public HcpCollection listMedics(HcpFilter filter)
	{
		DomainFactory factory = getDomainFactory();
		ArrayList markerNur = new ArrayList();
		ArrayList valueNur = new ArrayList();

		if (filter == null) filter = new HcpFilter();
		
		// get all Nurses with required criteria
		StringBuffer condStr2 = new StringBuffer();
		String and = " and ";
		String hqlNur = " from Nurse n ";
		if (filter.getQueryName() != null)
		{
			if (filter.getQueryName().getSurname() != null && filter.getQueryName().getSurname().length() > 0)
			{
				condStr2.append(" n.mos.name.upperSurname like :hcpSname");
				markerNur.add("hcpSname");
				valueNur.add(filter.getQueryName().getSurname().toUpperCase() + "%");
				and = " and ";
			}
		}
		
		condStr2.append(and + "n.nurseType != :nurse");
		markerNur.add("nurse");
		valueNur.add(getDomLookup(HcpDisType.STUDENTNURSE));
		
		if (and.equals(" and "))
			hqlNur += " where ";
		
		hqlNur += condStr2.toString();
		List nurses = factory.find(hqlNur, markerNur, valueNur);

		ArrayList hcpList = new ArrayList();
		if(nurses != null)
		{			
			for(int i=0;i<nurses.size();i++)
			{
				if (Nurse.class.isAssignableFrom(nurses.get(i).getClass()))
					hcpList.add( NurseVoAssembler.create((Nurse)nurses.get(i)));
				else
					hcpList.add(HcpAssembler.create((ims.core.resource.people.domain.objects.Hcp) nurses.get(i)));				
			}
		}
		
		HcpCollection voCollHcps = new HcpCollection();
		for(int i=0; i < hcpList.size(); i++)
		{
			voCollHcps.add((ims.core.vo.Hcp)hcpList.get(i));
		}
		return voCollHcps;		
	}

	public Hcp getHcp(HcpFilter filter)
	{
		HcpAdmin impl = (HcpAdmin) getDomainImpl(HcpAdminImpl.class);
		return impl.getHcp(filter);
	}
}
