//#############################################################################
//#                                                                           #
//#  Copyright (C) <2014>  <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/>.    #
//#                                                                           #
//#############################################################################
//#EOH
// This code was generated by Peter Martin using IMS Development Environment (version 1.43 build 2232.25589)
// Copyright (C) 1995-2006 IMS MAXIMS plc. All rights reserved.

package ims.therapies.domain.impl;

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

import ims.admin.domain.HcpAdmin;
import ims.admin.domain.impl.HcpAdminImpl;
import ims.core.admin.vo.CareContextRefVo;
import ims.core.vo.ClinicalContactShortVoCollection;
import ims.core.vo.Hcp;
import ims.core.vo.HcpCollection;
import ims.core.vo.domain.ClinicalContactShortVoAssembler;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.exceptions.UniqueKeyViolationException;
import ims.therapies.domain.base.impl.BaseWheelchairAssessmentImpl;
import ims.therapies.mobilitytransfers.domain.objects.WheelchairAssessment;
import ims.therapies.vo.WheelchairAssessmentVo;
import ims.therapies.vo.WheelchairAssessmentVoCollection;
import ims.therapies.vo.domain.WheelchairAssessmentVoAssembler;

public class WheelchairAssessmentImpl extends BaseWheelchairAssessmentImpl
{
	private static final long serialVersionUID = 1L;

	public WheelchairAssessmentVo saveWheelchairAssessment(WheelchairAssessmentVo wheelchairAssessment) throws StaleObjectException, UniqueKeyViolationException 
	{
		DomainFactory factory = getDomainFactory();	
		
		if (!wheelchairAssessment.isValidated())
		{
			throw new DomainRuntimeException("This WheelchairAssessmentVo has not been validated");
		}
		
		if (wheelchairAssessment.getID_WheelchairAssessment() == null)//Inserting a record
		{
			WheelchairAssessmentVoCollection voWA = getWheelchairAssessment(wheelchairAssessment.getClinicalContact().getID_ClinicalContact());	//wdev-13410
			WheelchairAssessmentVo tempVo = null;  //wdev-13410
			if(voWA != null && voWA.size() > 0)		//wdev-13410
			{
				tempVo = voWA.get(0);
			}
			else
			{
				tempVo = null;
			}
			
			if(tempVo != null )
				throw new UniqueKeyViolationException("A record exists for this clinical contact, the screen will be refreshed");
		}
		WheelchairAssessment domWheelchairAssessment = WheelchairAssessmentVoAssembler.extractWheelchairAssessment(factory, wheelchairAssessment);
		
		factory.save(domWheelchairAssessment);
		
		return WheelchairAssessmentVoAssembler.create(domWheelchairAssessment);
	}
    //wdev-13410
	public WheelchairAssessmentVoCollection getWheelchairAssessment(Integer contactId) {
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer(" ");
		String query = "from WheelchairAssessment w ";
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();
		String andStr = " ";
		
		if (contactId != null)
		{
			hql.append(andStr + " w.clinicalContact.id = :id_ClinicalContact order by w.id desc");		//wdev-13410
			markers.add("id_ClinicalContact");
			values.add(contactId);			
			andStr = " and ";
		}
		
		if (markers.size() > 0) query += " where ";
		query += hql.toString();
		

		List assessments = factory.find(query, markers, values);	
		WheelchairAssessmentVoCollection collHomeVisitVo = WheelchairAssessmentVoAssembler.createWheelchairAssessmentVoCollectionFromWheelchairAssessment(assessments);
		//if(collHomeVisitVo!=null && collHomeVisitVo.size()>0)
			return collHomeVisitVo;		//wdev-13410
			//return collHomeVisitVo.get(0);
		
		//return null;
	}

	public HcpCollection listHcps(Hcp filter) {
		HcpAdmin hcpAdmin = (HcpAdmin) getDomainImpl(HcpAdminImpl.class);
		return hcpAdmin.listHCPs(filter);
	}

	public ClinicalContactShortVoCollection listClinicalContactsByCareContext(CareContextRefVo refCareContext) {
		
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer(" ");
		String query = "from ClinicalContact cc ";
		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();
		String andStr = " ";
		
		if (refCareContext != null)
		{
			hql.append(andStr + " cc.careContext.id = :id_CareContext");
			markers.add("id_CareContext");
			values.add(refCareContext.getID_CareContext());			
			andStr = " and ";
		}
		
		if (markers.size() > 0) query += " where ";
		query += hql.toString();
		

		List contacts = factory.find(query, markers, values);	
		return ClinicalContactShortVoAssembler.createClinicalContactShortVoCollectionFromClinicalContact(contacts);
	
	}

}
