//#############################################################################
//#                                                                           #
//#  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 Marius Mihalec using IMS Development Environment (version 1.65 build 3225.30788)
// Copyright (C) 1995-2008 IMS MAXIMS plc. All rights reserved.

package ims.admin.forms.dmdsearch;

import ims.admin.forms.dmdsearch.GenForm.SearchTypeEnumeration;
import ims.clinical.vo.AMPVo;
import ims.clinical.vo.AMPVoCollection;
import ims.clinical.vo.VMPVo;
import ims.clinical.vo.VMPVoCollection;
import ims.clinical.vo.VTMVo;
import ims.clinical.vo.VTMVoCollection;
import ims.clinical.vo.enums.DMDType;
import ims.core.vo.lookups.Specialty;
import ims.domain.exceptions.DomainInterfaceException;
import ims.framework.MessageButtons;
import ims.framework.MessageIcon;
import ims.framework.exceptions.PresentationLogicException;

public class Logic extends BaseLogic
{
	private static final long serialVersionUID = 1L;

	@Override
	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		initialize();
	}
	@Override
	protected void onRadioButtonSearchTypeValueChanged() throws PresentationLogicException 
	{
		updateSearchCriteria();
	}
	@Override
	protected void onCcDMDValueChanged() throws PresentationLogicException 
	{
		form.fireCustomControlValueChanged();
	}
	@Override
	protected void onBtnSearchClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if(form.SearchType().getValue().equals(SearchTypeEnumeration.rdoVTM))
		{
			searchVTM();
		}
		else if(form.SearchType().getValue().equals(SearchTypeEnumeration.rdoVMP))
		{
			searchVMP();
		}
		else if(form.SearchType().getValue().equals(SearchTypeEnumeration.rdoAMP))
		{
			searchAMP();
		}
		
		form.fireCustomControlValueChanged();
	}
	public ims.vo.interfaces.IDMDValue getValue()
	{
		return form.ccDMD().getValue();
	}
	private void searchVTM() 
	{
		try 
		{
			VTMVoCollection result = domain.searchVTMByName(form.txtName().getValue(), form.chkHotlistOnly().getValue() ? form.getLocalContext().getSpecialty() : null);			
			form.ccDMD().setValues(result == null ? null : result.toIDMDValueArray(), DMDType.VTM);
		} 
		catch (DomainInterfaceException e) 
		{
			engine.showMessage(e.getMessage(), "Error", MessageButtons.OK, MessageIcon.ERROR);
		}
	}
	private void searchVMP() 
	{
		try 
		{
			VMPVoCollection result = domain.searchVMPByName(form.txtName().getValue());
			form.ccDMD().setValues(result == null ? null : result.toIDMDValueArray(), DMDType.VMP);
		} 
		catch (DomainInterfaceException e) 
		{
			engine.showMessage(e.getMessage(), "Error", MessageButtons.OK, MessageIcon.ERROR);
		}
	}
	private void searchAMP() 
	{
		try 
		{
			AMPVoCollection result = domain.searchAMPByName(form.txtName().getValue());
			form.ccDMD().setValues(result == null ? null : result.toIDMDValueArray(), DMDType.AMP);
		} 
		catch (DomainInterfaceException e) 
		{
			engine.showMessage(e.getMessage(), "Error", MessageButtons.OK, MessageIcon.ERROR);
		}
	}
	private void initialize()
	{
		form.SearchType().setValue(SearchTypeEnumeration.rdoVTM);
		
		updateSearchCriteria();
	}
	public AMPVo getAMP() 
	{
		return form.ccDMD().getAMP();
	}
	public VMPVo getVMP() 
	{
		return form.ccDMD().getVMP();
	}
	public VTMVo getVTM() 
	{
		return form.ccDMD().getVTM();
	}	
	private void updateSearchCriteria() 
	{
		form.chkHotlistOnly().setVisible(form.SearchType().getValue().equals(SearchTypeEnumeration.rdoVTM) && form.getLocalContext().getSpecialty() != null);
	}
	public void initialize(Specialty specialty, Boolean allowVTMSelection, Boolean hotlistSearchOnly) 
	{
		form.getLocalContext().setSpecialty(specialty);
		form.getLocalContext().setallowVTMSelection(allowVTMSelection);
		form.ccDMD().initialize(specialty, allowVTMSelection);
		if(hotlistSearchOnly != null && hotlistSearchOnly)
		{
			form.chkHotlistOnly().setEnabled(false);
			form.chkHotlistOnly().setValue(true);
		}
		updateSearchCriteria();
	}
}
