/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Pathways/src/ims/pathways/forms/externaleventssearchandselect/Logic.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 Rory Fitzpatrick using IMS Development Environment (version 1.60 build 2838.16667)
// Copyright (C) 1995-2007 IMS MAXIMS plc. All rights reserved.

package ims.pathways.forms.externaleventssearchandselect;


import ims.core.vo.MedicLiteVo;
import ims.core.vo.MedicLiteVoCollection;
import ims.framework.Control;
import ims.framework.enumerations.DialogResult;
import ims.framework.enumerations.FormMode;
import ims.framework.exceptions.PresentationLogicException;
import ims.ocrr.vo.ProviderSystemVoCollection;
import ims.pathways.forms.externaleventssearchandselect.GenForm.grdGroupSearchRow;
import ims.pathways.forms.externaleventssearchandselect.GenForm.grdGroupsRow;
import ims.pathways.vo.ExternalEventMappingVo;
import ims.pathways.vo.ExternalEventMappingVoCollection;

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

	@Override
	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		initialize();
		open();
		form.btnOk().setVisible(false);
		
		
	}
	private void initialize() 
	{
		populateProviderSystem();
	
		form.getContextMenus().hideAllGenericGridMenuItems();
		
	}
	
	@Override
	protected void onBtnAddToListClick() throws ims.framework.exceptions.PresentationLogicException
	{
		for(int i = 0 ; i < form.grdGroupSearch().getRows().size() ; i++)
		{
			if (form.grdGroupSearch().getRows().get(i).getColSelect())
			{
				//Check if already in grid, if so dont add it again.
				boolean bFound = false;
				for (int x = 0 ; x < form.grdGroups().getRows().size() ; x++)
				{
					if (form.grdGroupSearch().getRows().get(i).getValue().equals(form.grdGroups().getRows().get(x).getValue()) )
						bFound = true;		
				}
				
				if ( ! bFound)
				{
					addAGridRow(form.grdGroups().getRows().newRow(), form.grdGroupSearch().getRows().get(i).getValue());
				
					form.btnOk().setEnabled(true);
					form.btnOk().setVisible(true);
				}
			}
		}
	}

	private void addAGridRow(grdGroupsRow row, ExternalEventMappingVo vo) 
	{
		row.setColGroup(vo.getGroupIsNotNull() ? vo.getGroup().toString() : "");
		row.setColDetail(vo.getDetailIsNotNull() ? vo.getDetail().toString() : "");
		row.setColSpecialty(vo.getSpecialtyIsNotNull() ? vo.getSpecialty().toString() : "");
		row.setColProviderSys(vo.getProviderSystemIsNotNull() && vo.getProviderSystem().getSystemNameIsNotNull() ? vo.getProviderSystem().getSystemName() : "");
		row.setColClinic(vo.getClinicCodeIsNotNull() ? vo.getClinicCode().toString() : "");
		row.setColCons(vo.getConsultantIsNotNull() ? vo.getConsultant().toString() :"" );
		row.setValue(vo);	
	}
	@Override
	protected void onBtnDeSelectAllClick() throws ims.framework.exceptions.PresentationLogicException
	{
		for(int i = 0 ; i < form.grdGroupSearch().getRows().size() ; i++)
			form.grdGroupSearch().getRows().get(i).setColSelect(false);
	}
	
	@Override
	protected void onBtnSelectAllClick() throws ims.framework.exceptions.PresentationLogicException
	{
		for(int i = 0 ; i < form.grdGroupSearch().getRows().size() ; i++)
			form.grdGroupSearch().getRows().get(i).setColSelect(true);
	}

	@Override
	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{
		ExternalEventMappingVoCollection voColl = new ExternalEventMappingVoCollection();
		for(int i = 0 ; i < form.grdGroups().getRows().size() ; i++)
			voColl.add(form.grdGroups().getRows().get(i).getValue());
	
		form.getGlobalContext().Pathway.setSelectedExternalEvents(voColl);
		engine.close(DialogResult.OK);
	}
	
	@Override
	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{
		form.getGlobalContext().Pathway.setSelectedExternalEvents(null);
		engine.close(DialogResult.CANCEL);
	}
	
	private void search(boolean listAll) 
	{
		ExternalEventMappingVo filter = new ExternalEventMappingVo();
		ExternalEventMappingVoCollection collection  = new ExternalEventMappingVoCollection();
		
		if (form.cmbGroupSearch().getValue() != null)
		{
			filter.setGroup(form.cmbGroupSearch().getValue());
		}
		if (form.cmbDetailSearch().getValue() != null)
		{
			filter.setDetail(form.cmbDetailSearch().getValue());
		}
		if (form.cmbSpecSearch().getValue() != null)
		{
			filter.setSpecialty(form.cmbSpecSearch().getValue());
		}
		if (form.txtClinicCodeSearch().getValue() != null)
		{
			filter.setClinicCode(form.txtClinicCodeSearch().getValue());
		}
		if (form.qmbConsultantSearch().getValue() != null)
		{
			filter.setConsultant(form.qmbConsultantSearch().getValue());
		}
		if (form.cmbProviderSysSearch().getValue() != null)
		{
			filter.setProviderSystem(form.cmbProviderSysSearch().getValue());
		}
		
		if (listAll)
		{
			collection = domain.listByCriteria(null);
		}
		else
		{
			collection = domain.listByCriteria(filter);
		}
		
		if (collection == null)
		{
			engine.showMessage("Please provide a valid search criteria!");
			return;
		}
		
		
		if (collection.size() == 0)
		{
			engine.showMessage("No record(s) found"); 
			return;
		}
		
		for (int i = 0; i < collection.size(); i++) 
		{
			grdGroupSearchRow row = form.grdGroupSearch().getRows().newRow();
			
			addASearchGridRow(row, collection.get(i));
		}
	}
	
	private void addASearchGridRow(grdGroupSearchRow row, ExternalEventMappingVo vo) 
	{
		row.setColGroup(vo.getGroupIsNotNull() ? vo.getGroup().toString() : "");
		row.setColDetail(vo.getDetailIsNotNull() ? vo.getDetail().toString() : "");
		row.setColSpecialty(vo.getSpecialtyIsNotNull() ? vo.getSpecialty().toString() : "");
		row.setColProviderSys(vo.getProviderSystemIsNotNull() && vo.getProviderSystem().getSystemNameIsNotNull() ? vo.getProviderSystem().getSystemName() : "");
		row.setColClinic(vo.getClinicCodeIsNotNull() ? vo.getClinicCode().toString() : "");
		row.setColCons(vo.getConsultantIsNotNull() ? vo.getConsultant().toString() :"" );

		row.setValue(vo);	
	}
	
	private void populateProviderSystem() 
	{
		form.cmbProviderSysSearch().clear();
		ProviderSystemVoCollection voCollection = domain.listProviderSystems();
	
		for (int i = 0; voCollection != null && i < voCollection.size(); i++) 
			form.cmbProviderSysSearch().newRow(voCollection.get(i), voCollection.get(i).getSystemName());
	}
	
	private void open() 
	{
		form.setMode(FormMode.VIEW);
		clearSearchCriteria();
		clearListGrid();
		
		//if any data in the global context load it into the selected grid.
		if (form.getGlobalContext().Pathway.getSelectedExternalEventsIsNotNull()
			&& form.getGlobalContext().Pathway.getSelectedExternalEvents().size() > 0)
		{
			for(int i = 0 ; i < form.getGlobalContext().Pathway.getSelectedExternalEvents().size() ; i++)
			{
				//Check if already in grid, if so dont add it again.
				boolean bFound = false;
				for (int x = 0 ; x < form.grdGroups().getRows().size() ; x++)
				{
					if (form.getGlobalContext().Pathway.getSelectedExternalEvents().get(i).equals(form.grdGroups().getRows().get(x).getValue()) )
						bFound = true;		
				}
					
				if ( ! bFound)
				{
					addAGridRow(form.grdGroups().getRows().newRow(), form.getGlobalContext().Pathway.getSelectedExternalEvents().get(i));
					
					form.btnOk().setEnabled(true);
					form.btnOk().setVisible(true);
				}
			}
		}
	}
	

	private void clearSearchCriteria()
	{
		form.cmbGroupSearch().setValue(null);
		form.cmbDetailSearch().setValue(null);
		form.cmbSpecSearch().setValue(null);
		form.txtClinicCodeSearch().setValue(null);
		form.qmbConsultantSearch().setValue(null);
		form.cmbProviderSysSearch().setValue(null);
	}
	
	@Override
	protected void onQmbConsultantSearchTextSubmited(String value) throws PresentationLogicException 
	{
		form.qmbConsultantSearch().clear();
		
		MedicLiteVoCollection coll = domain.listMedics(value);
				
		for (int i = 0; i < coll.size(); i++)
		{
			MedicLiteVo med = coll.get(i);
			
			form.qmbConsultantSearch().newRow(med, med.getMos().getName().toString());
		}
		if (coll.size() == 1)
		{
			form.qmbConsultantSearch().setValue(coll.get(0));
		}
		else if (coll.size() > 1)
		{
			form.qmbConsultantSearch().showOpened();
		}
	}
	
	
	protected void onImbClearClick() throws PresentationLogicException 
	{
		clearSearchCriteria();
		
		clearListGrid();
		form.grdGroupSearch().setFooterValue(null);
		form.grdGroupSearch().setFooterValue("Total records : " + form.grdGroupSearch().getRows().size());
	}
	@Override
	protected void onImbSearchClick() throws PresentationLogicException 
	{
		clearListGrid();

		search(false);
		
		form.grdGroupSearch().setFooterValue("Total records : " + form.grdGroupSearch().getRows().size());
	}
	
	private void clearListGrid()
	{
		form.grdGroupSearch().getRows().clear();
	}
	@Override
	protected void onGrdGroupsSelectionChanged() throws PresentationLogicException 
	{
		form.getContextMenus().getGenericGridRemoveItem().setVisible(true);
		form.getContextMenus().getGenericGridRemoveItem().setEnabled(true);
	}
	
	@Override
	protected void onContextMenuItemClick(int menuItemID, Control sender) throws PresentationLogicException 
	{
		if (form.grdGroups().getSelectedRow() != null)
		{
			form.grdGroups().getRows().remove(form.grdGroups().getSelectedRowIndex());
		
			form.getContextMenus().getGenericGridRemoveItem().setVisible(true);
			form.getContextMenus().getGenericGridRemoveItem().setEnabled(false);
		}
	}
	
}
