/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/RACPC/src/ims/racpc/domain/impl/RACPCSummaryImpl.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 Cristian Belciug using IMS Development Environment (version 1.80 build 4261.20360)
// Copyright (C) 1995-2011 IMS MAXIMS. All rights reserved.

package ims.racpc.domain.impl;

import java.util.List;

import ims.clinical.domain.objects.RACPCSummary;
import ims.core.admin.vo.CareContextRefVo;
import ims.core.configuration.domain.objects.AppSite;
import ims.core.resource.people.domain.objects.Gp;
import ims.core.vo.AppSiteVo;
import ims.core.vo.domain.AppSiteVoAssembler;
import ims.core.vo.domain.GpLiteVoAssembler;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainInterfaceException;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.exceptions.UniqueKeyViolationException;
import ims.framework.exceptions.CodingRuntimeException;
import ims.racpc.domain.base.impl.BaseRACPCSummaryImpl;
import ims.racpc.vo.RACPCSummaryVo;
import ims.racpc.vo.domain.RACPCSummaryVoAssembler;

public class RACPCSummaryImpl extends BaseRACPCSummaryImpl
{

	private static final long serialVersionUID = 1L;

	public ims.racpc.vo.RACPCSummaryVo getRACPCSummary(ims.core.admin.vo.CareContextRefVo careContext)
	{
		if(careContext == null || careContext.getID_CareContext() == null)
			throw new CodingRuntimeException("Cannot get RACPCSummaryVo on null Care Context.");
		
		DomainFactory factory = getDomainFactory();
		
		List<?> list = factory.find("from RACPCSummary as r where r.careContext.id = :CareContext", new String[] {"CareContext"}, new Object[] {careContext.getID_CareContext()});
		
		if(list != null && list.size() > 0)
			return RACPCSummaryVoAssembler.create((RACPCSummary) list.get(0));
		
		return null;
	}

	public ims.core.vo.GpLiteVo getPatientGp(ims.core.patient.vo.PatientRefVo patient)
	{
		if(patient == null || !patient.getID_PatientIsNotNull())
			throw new CodingRuntimeException("Can not get Patient Gp on null Patient.");
		
		DomainFactory factory = getDomainFactory();
		
		List<?> gpList = factory.find("select gp from Patient as p left join p.gp as gp where (p.id = :patientid and gp.isRIE is null)", new String[] {"patientid"}, new Object[] {patient.getID_Patient()});
		
		if(gpList == null || gpList.size() == 0)
			return null;
		
		return GpLiteVoAssembler.create((Gp) gpList.get(0));
	}

	public String[] getSystemReportAndTemplate(Integer imsId) 
	{
		String[] result = null;		
		DomainFactory factory = getDomainFactory();
		
		List<?> lst = factory.find("select r.reportXml, t.templateXml from ReportBo as r left join r.templates as t where (r.imsId= :imsid) order by t.name", new String[] {"imsid"}, new Object[] {imsId});
		
		if(lst.iterator().hasNext())
		{
			Object[] obj = (Object[])lst.iterator().next();
			
			result = new String[] {(String)obj[0], (String)obj[1]};
		}
		
		return result;
	}

	public RACPCSummaryVo saveRACPCSummary(RACPCSummaryVo summary) throws DomainInterfaceException, StaleObjectException, UniqueKeyViolationException 
	{
		if(summary == null)
			throw new CodingRuntimeException("Cannot save a null RACPCSummaryVo");
		
		if(!summary.isValidated())
			throw new CodingRuntimeException("RACPCSummaryVo is not validated");
		
		DomainFactory factory = getDomainFactory();
		RACPCSummary doSummary = RACPCSummaryVoAssembler.extractRACPCSummary(factory, summary);
		
		factory.save(doSummary);
		
		return RACPCSummaryVoAssembler.create(doSummary);
	}

	public Boolean isRACPCSummaryComplete(CareContextRefVo careContext) 
	{
		if(careContext == null || careContext.getID_CareContext() == null)
			throw new CodingRuntimeException("Cannot get RACPCSummaryVo on null Care Context.");
		
		DomainFactory factory = getDomainFactory();
		
		List<?> list = factory.find("select r.isClinicalDetailsComplete from RACPCSummary as r where r.careContext.id = :CareContext", new String[] {"CareContext"}, new Object[] {careContext.getID_CareContext()});
		
		if(list != null && list.size() > 0)
			return (Boolean) list.get(0);
		
		return Boolean.FALSE;
	}

	//wdev-16298
	public AppSiteVo getSite() throws DomainInterfaceException 
	{
		
		DomainFactory factory = getDomainFactory();	
		
		List list = factory.find("from AppSite");

		if(list.size() > 1)
		{
			throw new ims.domain.exceptions.DomainInterfaceException("There is more than 1 record in core_appsite table !");
		}
		
		if(list.size() == 1)
			return AppSiteVoAssembler.create((AppSite) list.get(0));
		
		return null;
	}
}
