/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/domain/impl/AssessmentCarePlansImpl.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 Daniel Laffan using IMS Development Environment (version 1.20 build 40716.900)
// Copyright (C) 1995-2004 IMS MAXIMS plc. All rights reserved.

package ims.nursing.domain.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import ims.core.admin.vo.CareContextRefVo;
import ims.domain.DomainFactory;
import ims.domain.impl.DomainImpl;
import ims.framework.enumerations.SortOrder;
import ims.nursing.assessment.domain.objects.AdmissionAssessment;
import ims.nursing.vo.AdmissionAssessmentVo;
import ims.nursing.vo.domain.AdmissionAssessmentVoAssembler;
import ims.nursing.vo.domain.AssessmentComponentCarePlansAssembler;

public class AssessmentCarePlansImpl extends DomainImpl implements ims.nursing.domain.AssessmentCarePlans, ims.domain.impl.Transactional
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** 
	 * get admission assessment, which will include assessment component care plans
	 */
	
	public AdmissionAssessmentVo getAdmissionAssessmentByCareContext(CareContextRefVo voCareContextRef)
	{
		DomainFactory factory = getDomainFactory();

		// get admission assessment
		String hql = " from AdmissionAssessment admAss";
		hql += (" where admAss.careContext.id = " + voCareContextRef.getID_CareContext() + " and admAss.isRIE is null ");
		
		AdmissionAssessmentVo admissionAssessmentVo = null;
		List admAss = factory.find(hql);
		for (Iterator iter = admAss.iterator(); iter.hasNext();)
		{
			AdmissionAssessment domAdmissionAssess = (AdmissionAssessment) iter.next();
			admissionAssessmentVo = AdmissionAssessmentVoAssembler.create(domAdmissionAssess);
			break;
		}
	
		if (admissionAssessmentVo == null)
			return null;
		
		// now get associated care plans 
		//String hqlQuery = " from CarePlan cp ";
  
		String hqlQuery = "select c1_1 from CarePlan as c1_1 left join c1_1.assessmentComponent as a1_1 left join a1_1.assessment as a2_1 ";
		
		StringBuffer condStr = new StringBuffer();
		ArrayList<String> markerNames = new ArrayList<String>();
		ArrayList<Integer> markerValues = new ArrayList<Integer>();
				
		if(voCareContextRef != null)
		{
			condStr.append(" c1_1.careContext.id = :care and c1_1.assessmentComponent is not null ");
			markerNames.add("care");
			markerValues.add(voCareContextRef.getID_CareContext());
		}
		
		
		condStr.append(" and a2_1.isRIE is null ");
		if (markerNames.size() > 0) hqlQuery += " where ";
		hqlQuery += condStr.toString();
		List careplans = factory.find(hqlQuery,markerNames,markerValues);		
	
		// now that we have care plans, create assessment component care plans, set in vo, and return
		admissionAssessmentVo.setComponentCarePlans(AssessmentComponentCarePlansAssembler.createAssessmentComponentCarePlansCollectionFromCarePlan(careplans).sort(SortOrder.ASCENDING));
		
		return admissionAssessmentVo;
	}
}
