//#############################################################################
//#                                                                           #
//#  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 4342.23748)
// Copyright (C) 1995-2012 IMS MAXIMS. All rights reserved.

package ims.clinical.domain.impl;

import ims.clinical.domain.base.impl.BaseAllergiesCcImpl;
import ims.core.clinical.domain.objects.PatientNoAllergyInfo;
import ims.core.patient.vo.PatientRefVo;
import ims.core.vo.PatientNoAllergyInfoForTriageVo;
import ims.core.vo.domain.PatientAllergyForTriageVoAssembler;
import ims.core.vo.domain.PatientNoAllergyInfoForTriageVoAssembler;
import ims.domain.DomainFactory;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.exceptions.UniqueKeyViolationException;
import ims.domain.exceptions.UnqViolationUncheckedException;
import ims.framework.exceptions.CodingRuntimeException;
import ims.configuration.gen.ConfigFlag;

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

public class AllergiesCcImpl extends BaseAllergiesCcImpl
{
	private static final long serialVersionUID = 1L;

	public ims.core.vo.PatientAllergyForTriageVoCollection listAllergies(ims.core.patient.vo.PatientRefVo patient)
	{
		if(patient == null)
			throw new CodingRuntimeException("Cannot list Patient Allergies on null Patient Id.");
		
		ArrayList<String> names = new ArrayList<String>();
		ArrayList<Object> values = new ArrayList<Object>();
		
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer("from PatientAllergy patAlrg ");
		hql.append(" where patAlrg.patient.id = :patient");
		names.add("patient");
		values.add(patient.getID_Patient());
		
		hql.append (" and patAlrg.isCurrentlyActiveAllergy = :isActive");
		names.add("isActive");
		values.add(Boolean.TRUE);
		
		hql.append(" order by patAlrg.systemInformation.creationDateTime desc");
		
		List<?> list = factory.find(hql.toString(), names,values);
		return(PatientAllergyForTriageVoAssembler.createPatientAllergyForTriageVoCollectionFromPatientAllergy(list));
	}

	public PatientNoAllergyInfoForTriageVo getPatientNoAllergyInfo(PatientRefVo patient) 
	{
		if(patient == null || !patient.getID_PatientIsNotNull())
			throw new CodingRuntimeException("Cannot get PatientNoAllergyInfo on null Patient Id.");
		
		DomainFactory factory = getDomainFactory();
		
		PatientNoAllergyInfo patientNoAllergyInfo = PatientNoAllergyInfo.getPatientNoAllergyInfoFromPatient(factory, patient.getID_Patient());
		return PatientNoAllergyInfoForTriageVoAssembler.create(patientNoAllergyInfo);
	}

	public PatientNoAllergyInfoForTriageVo savePatientNoAllergyInfo(PatientNoAllergyInfoForTriageVo noAllergy) throws StaleObjectException, UniqueKeyViolationException 
	{
		if(noAllergy == null)
			throw new CodingRuntimeException("Cannot save a null PatientNoAllergyInfo.");
		
		if(!noAllergy.isValidated())
			throw new CodingRuntimeException("PatientNoAllergyInfo is not validated");
		
		DomainFactory factory = getDomainFactory();
		
		PatientNoAllergyInfo doPatientNoAllergyInfo = PatientNoAllergyInfoForTriageVoAssembler.extractPatientNoAllergyInfo(factory, noAllergy);
		
		try
		{
			factory.save(doPatientNoAllergyInfo);
		}
		catch(UnqViolationUncheckedException e)
		{
			throw new UniqueKeyViolationException(ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
		}
		
		return PatientNoAllergyInfoForTriageVoAssembler.create(doPatientNoAllergyInfo);
	}
}
