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

package ims.nursing.domain.impl;

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

import ims.nursing.domain.BradenScaleDialog;
import ims.nursing.vo.BradenScale;
import ims.nursing.vo.BradenScaleCollection;
import ims.nursing.vo.domain.BradenScaleAssembler;
import ims.core.admin.vo.CareContextRefVo;
import ims.core.domain.Reports;
import ims.core.domain.impl.ReportsImpl;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainInterfaceException;
import ims.domain.impl.DomainImpl;
import ims.framework.utils.Date;

public class BradenScaleVIEWImpl extends DomainImpl implements ims.nursing.domain.BradenScaleVIEW, ims.domain.impl.Transactional
{
	public Boolean isPartOfSkinAssessment(BradenScale bradenRecord) throws DomainInterfaceException
	{
		BradenScaleDialog impl = (BradenScaleDialog) getDomainImpl(BradenScaleDialogImpl.class);
		return impl.isPartOfSkinAssessment(bradenRecord);
	}

	/**
	 * lists Assessments
	 */
	public BradenScaleCollection listAssessments(Date dateFrom, Date dateTo, CareContextRefVo voCareContextRef) throws DomainInterfaceException
	{
		DomainFactory factory = getDomainFactory();

		StringBuffer hql = new StringBuffer(" ");
		String query = "from BradenScale bs ";
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();
		String andStr = " ";
		boolean conditionFound = false;

		if (dateFrom != null && dateTo != null)
		{
			hql.append(" bs.dateTimeInitiated > :fromDate and bs.dateTimeInitiated < :toDate ");
			conditionFound = true;
			markers.add("fromDate");
			markers.add("toDate");
			values.add(dateFrom.addDay(-1).getDate());
			values.add(dateTo.addDay(1).getDate());
			// andStr = " and ";
		}
		else if (dateFrom != null)
		{
			hql.append(" bs.dateTimeInitiated > :fromDate ");
			conditionFound = true;
			markers.add("fromDate");

			values.add(dateFrom.addDay(-1).getDate());

			// andStr = " and ";
		}

		if (voCareContextRef != null)
		{
			if (conditionFound)
				hql.append(" and ");

			hql.append(andStr + " bs.careContext.id = :cc");
			markers.add("cc");
			values.add(voCareContextRef.getID_CareContext());
			andStr = " and ";
		}

		if (markers.size() > 0)
			query += " where ";
		query += hql.toString();
		List braden = factory.find(query, markers, values);

		return (BradenScaleAssembler.createBradenScaleCollectionFromBradenScale(braden).sort());
	}
	
	public BradenScale getLastCompletedBradenScale(CareContextRefVo voCareContextRef)
	{
		DomainFactory factory = getDomainFactory();

		StringBuffer hql = new StringBuffer("from BradenScale bs");
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();

		if (voCareContextRef != null)
		{
			hql.append(" where ");
			hql.append(" bs.careContext.id = :cc");
			markers.add("cc");
			values.add(voCareContextRef.getID_CareContext());
		}
		hql.append(" order by bs.dateTimeInitiated desc ");
		List list = factory.find(hql.toString(), markers, values);

		//Get only the first record (to optimise the work of the assembler) 
		if(list != null && list.size() > 1)
		{
			Object firstItem = list.get(0);
			list.removeAll(list);
			list.add(firstItem);
		}
		BradenScaleCollection voBradenScaleColl = BradenScaleAssembler.createBradenScaleCollectionFromBradenScale(list);
		if (voBradenScaleColl != null && voBradenScaleColl.size() > 0)
			return voBradenScaleColl.get(0);

		return null;
	}

	/**
	 * call getReportAndTemplate form Core.Reports domainImpl
	 */
	public String[] getReportAndTemplate(Integer nReportId, Integer nTemplateId) throws DomainInterfaceException
	{
		Reports impl = (Reports) getDomainImpl(ReportsImpl.class);
		return impl.getReportAndTemplate(nReportId, nTemplateId);
	}
}
