/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Pathways/src/ims/pathways/forms/eventsearchandselectdialog/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 Daniel Laffan using IMS Development Environment (version 1.62 build 3079.31028)
// Copyright (C) 1995-2008 IMS MAXIMS plc. All rights reserved.

package ims.pathways.forms.eventsearchandselectdialog;

import ims.core.vo.lookups.PreActiveActiveInactiveStatus;
import ims.framework.enumerations.DialogResult;
import ims.framework.exceptions.PresentationLogicException;
import ims.pathways.forms.eventsearchandselectdialog.GenForm.grdEventsRow;
import ims.pathways.vo.EventLiteVo;
import ims.pathways.vo.EventLiteVoCollection;

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

	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		initialize();
		search();
		showSelectedEvent();
	}

	private void showSelectedEvent()
	{
		form.grdEvents().setValue(form.getGlobalContext().Pathway.getSelectedEventLiteVo());
	}

	protected void onImbClearClick() throws ims.framework.exceptions.PresentationLogicException
	{
		clearSearch();
		form.btnOK().setEnabled(false);
		form.grdEvents().getRows().clear();
		form.grdEvents().setFooterValue("Total records : " + form.grdEvents().getRows().size());
	}

	protected void onImbSearchClick() throws ims.framework.exceptions.PresentationLogicException
	{
		search();

		if (form.grdEvents().getSelectedRow() == null || form.txtEventNameSearch().getValue() == null)
		{
			form.btnOK().setEnabled(false);
		}

	}

	private void initialize()
	{
		form.cmbStatusSearch().setValue(PreActiveActiveInactiveStatus.ACTIVE);
		form.btnOK().setEnabled(false);
	}

	private void search()
	{
		EventLiteVo filter = new EventLiteVo();
		if (form.txtEventNameSearch().getValue() != null)
			filter.setName(form.txtEventNameSearch().getValue());

		if (form.cmbStatusSearch().getValue() != null)
			filter.setStatus(form.cmbStatusSearch().getValue());

		populateGridFromData(domain.listEvents(filter));

		form.grdEvents().setFooterValue("Total records : " + form.grdEvents().getRows().size());
	}

	private void populateGridFromData(EventLiteVoCollection voCollEvents)
	{
		form.grdEvents().getRows().clear();
		if (voCollEvents == null || voCollEvents.size() == 0)
		{
			engine.showMessage("No matching records found");
			return;
		}

		for (int i = 0; i < voCollEvents.size(); i++)
		{
			grdEventsRow row = form.grdEvents().getRows().newRow();
			EventLiteVo voEvent = voCollEvents.get(i);
			if (voEvent != null)
			{
				row.setColName(voEvent.getNameIsNotNull() ? voEvent.getName() : "");
				row.setColDescr(voEvent.getDescriptionIsNotNull() ? voEvent.getDescription() : "");
				row.setValue(voEvent);
			}
		}
	}

	private void clearSearch()
	{
		form.txtEventNameSearch().setValue(null);
		form.cmbStatusSearch().setValue(null);
	}

	protected void onGrdEventsSelectionChanged() throws PresentationLogicException
	{
		form.btnOK().setEnabled(true);
	}

	protected void onBtnCancelClick() throws PresentationLogicException
	{
		engine.close(DialogResult.CANCEL);
	}

	protected void onBtnOKClick() throws PresentationLogicException
	{
		form.getGlobalContext().Pathway.setSelectedEventLiteVo(form.grdEvents().getValue());
		engine.close(DialogResult.OK);
	}
}
