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

package ims.pathways.forms.eventconfiguration;

import java.util.ArrayList;
import ims.admin.vo.AppImageVo;
import ims.admin.vo.AppImageVoCollection;
import ims.admin.vo.AppRoleShortVo;
import ims.admin.vo.AppRoleShortVoCollection;
import ims.configuration.gen.ConfigFlag;
import ims.core.admin.vo.ProviderSystemRefVo;
import ims.core.vo.lookups.PreActiveActiveInactiveStatus;
import ims.core.vo.lookups.PreActiveActiveInactiveStatusCollection;
import ims.domain.exceptions.DomainInterfaceException;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.ForeignKeyViolationException;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.exceptions.UniqueKeyViolationException;
import ims.domain.exceptions.UnqViolationUncheckedException;
import ims.framework.Control;
import ims.framework.FormName;
import ims.framework.LayerBridge;
import ims.framework.MessageButtons;
import ims.framework.MessageIcon;
import ims.framework.controls.DynamicGrid;
import ims.framework.controls.DynamicGridCell;
import ims.framework.controls.DynamicGridColumn;
import ims.framework.controls.DynamicGridRow;
import ims.framework.enumerations.Alignment;
import ims.framework.enumerations.DialogResult;
import ims.framework.enumerations.DynamicCellType;
import ims.framework.enumerations.FormMode;
import ims.framework.exceptions.PresentationLogicException;
import ims.framework.utils.Color;
import ims.ocrr.vo.ProviderSystemVo;
import ims.ocrr.vo.ProviderSystemVoCollection;
import ims.pathways.configuration.vo.TargetRefVo;
import ims.pathways.configuration.vo.TargetRefVoCollection;
import ims.pathways.forms.eventconfiguration.GenForm.lyrTargetsLayer.tabActioningRolesContainer.grdRolesRow;
import ims.pathways.forms.eventconfiguration.GenForm.lyrTargetsLayer.tabExternalMappingsContainer.grdExternalEventMappingsRow;
import ims.pathways.forms.eventconfiguration.GenForm.lyrTargetsLayer.tabRTTContainer.grdRTTRow;
import ims.pathways.vo.EventExternalEventMappingVo;
import ims.pathways.vo.EventExternalEventMappingVoCollection;
import ims.pathways.vo.EventLiteVo;
import ims.pathways.vo.EventRTTEventVo;
import ims.pathways.vo.EventRTTEventVoCollection;
import ims.pathways.vo.EventTargetVo;
import ims.pathways.vo.EventTargetVoCollection;
import ims.pathways.vo.EventVo;
import ims.pathways.vo.ExternalEventMappingVo;
import ims.pathways.vo.ExternalEventMappingVoCollection;
import ims.pathways.vo.RTTEventVo;
import ims.pathways.vo.RTTEventVoCollection;
import ims.pathways.vo.TargetConfigurationShortVo;
import ims.pathways.vo.TargetConfigurationShortVoCollection;
import ims.pathways.vo.TargetShortVo;
import ims.pathways.vo.lookups.EventCreationType;
import ims.pathways.vo.lookups.PatientTargetStatus;
import ims.pathways.vo.lookups.PatientTargetStatusCollection;
import ims.scheduling.vo.lookups.Status_Reason;

public class Logic extends BaseLogic
{
	private static final long serialVersionUID 		= 1L;
	public static final String	COLNAME				= "-1";
	public static final String	COLJOURNEYSTATUS	= "-2";
	public static final String	COLSETTOINSCOPE		= "-3";
	public static final String	COLJOURNEYTARGET	= "-4";
	public static final String	COLSTATUS			= "-5";
	public static final Integer	PARENT				= -5;
	public static final Integer	CHILD				= -6;

	@Override
	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		form.customControlMappings().initialize();
		FormMode mode = null;
		if(args != null && args.length == 1)
		{
			if(args[0] instanceof FormMode)
				mode = (FormMode) args[0];
		}
		
		initialize(mode);
	}
	
	private void initialize(FormMode mode) 
	{
		bindCancellationType();
		
		if (mode != null)
		{
			if(mode.equals(FormMode.EDIT))
			{
				form.setMode(FormMode.EDIT);
				//Edit Record
				if (form.getGlobalContext().Pathway.getSelectedEventLiteVoIsNotNull())
				{
					if (form.getGlobalContext().Pathway.getSelectedEventLiteVo().getStatusIsNotNull())
					{
						PreActiveActiveInactiveStatus status = form.getGlobalContext().Pathway.getSelectedEventLiteVo().getStatus();
						if (status.equals(PreActiveActiveInactiveStatus.ACTIVE))
						{
							form.cmbStatus().removeRow(PreActiveActiveInactiveStatus.PREACTIVE);
						}
					}
					populateScreenFromGlobalContext();
					updateChkScheduleControlState();
				}
				//New Record
				else
				{
					initialiseEventTargetManagementDynamicGrid();
					populateRolesGrid(null);
					form.cmbStatus().removeRow(PreActiveActiveInactiveStatus.INACTIVE);
					form.chkScheduleEvent().setVisible(false);
				}
			}
			else
			{
				
				PreActiveActiveInactiveStatus status = form.getGlobalContext().Pathway.getSelectedEventLiteVo().getStatus();
				if (status.equals(PreActiveActiveInactiveStatus.ACTIVE))
				{
					form.cmbStatus().removeRow(PreActiveActiveInactiveStatus.PREACTIVE);
				}
				
				populateScreenFromGlobalContext();
				updateChkScheduleControlState();
				form.setMode(FormMode.VIEW);
			}
		}
		else
		{
			form.setMode(FormMode.VIEW);
			form.btnEdit().setVisible(false);
			form.cmbStatus().removeRow(PreActiveActiveInactiveStatus.INACTIVE);
			form.chkScheduleEvent().setVisible(false);
		}
		
		updateContextMenuState();
	}
	
	private void bindCancellationType()
	{
		form.cmbType().clear();
		
		form.cmbType().newRow(Status_Reason.PATIENTCANCELLED, Status_Reason.PATIENTCANCELLED.getText());
		form.cmbType().newRow(Status_Reason.HOSPITALCANCELLED, Status_Reason.HOSPITALCANCELLED.getText());
	}

	private void updateChkScheduleControlState()
	{
		if(form.cmbEventType().getValue() != null)
		{
			if(form.cmbEventType().getValue().equals(EventCreationType.INTERNAL) || form.cmbEventType().getValue().equals(EventCreationType.BOTH))
			{
				form.chkScheduleEvent().setVisible(true);
				form.chkScheduleEvent().setEnabled(form.getMode().equals(FormMode.EDIT));
			}
			else
			{
				form.chkScheduleEvent().setVisible(false);
				form.chkScheduleEvent().setValue(false);
			}
		}
		else
		{
			form.chkScheduleEvent().setVisible(false);
			form.chkScheduleEvent().setValue(false);
		}
	}

	private void populateScreenFromGlobalContext() 
	{
		EventLiteVo editedRecord = form.getGlobalContext().Pathway.getSelectedEventLiteVo();
		form.getLocalContext().setEditedRecord(domain.getEvent(editedRecord));
		populateScreenFromData(form.getLocalContext().getEditedRecord());
	}

	private void populateScreenFromData(EventVo editedRecord) 
	{
		if (editedRecord != null)
		{
			form.txtName().setValue(editedRecord.getName());
			form.txtDesc().setValue(editedRecord.getDescription());
		
			if (editedRecord.getIcon() != null)
			{
				form.qmbImage().newRow(editedRecord.getIcon(), editedRecord.getIcon().getImagePath(), engine.getRegisteredImage(editedRecord.getIcon().getImageId()));	
				form.qmbImage().setValue(editedRecord.getIcon());
			}
		
			form.cmbStatus().newRow(editedRecord.getStatus(), editedRecord.getStatus().getText());
			form.cmbStatus().setValue(editedRecord.getStatus());
			
			form.cmbEventType().setValue(editedRecord.getEventType());
		
			form.customControlMappings().setValue(editedRecord.getTaxonomyMaps());
			form.chkDisplayInPatientDiary().setValue(editedRecord.getPatientDiaryImpactIsNotNull() ? editedRecord.getPatientDiaryImpact() : false);
			form.chkStartsClock().setValue(editedRecord.getStartsClockIsNotNull() ? editedRecord.getStartsClock() : false);
			form.chkStopClocks().setValue(editedRecord.getStopsClockIsNotNull() ? editedRecord.getStopsClock() : false);
			form.chkEndsPathway().setValue(editedRecord.getEndsPathwayIsNotNull() ? editedRecord.getEndsPathway() : false);		
			form.cmbType().setValue(editedRecord.getCancellationType());
			form.cmbReason().setValue(editedRecord.getCancellationReason());
			form.chkScheduleEvent().setValue(editedRecord.getIsManualScheduledEventIsNotNull() ? editedRecord.getIsManualScheduledEvent() : false);	
		}

		updateChkScheduleControlState();
		populateExternalEventMappingsGrid(editedRecord);
		populateRTTEventGrid(editedRecord);
		populateEventTargetDinamicGrid(editedRecord);
		populateRolesGrid(editedRecord);
	}

	private void updateContextMenuState() 
	{
		boolean isEditMode = form.getMode().equals(FormMode.EDIT);
		boolean isEventActive = (form.cmbStatus().getValue() != null && form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.ACTIVE) &&
					form.getLocalContext().getEditedRecord()!= null) ? true : false;		
		boolean isEventInactive = form.cmbStatus().getValue() != null && form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.INACTIVE);		
		
		EventCreationType eventType = form.cmbEventType().getValue();

		if (isEditMode)
		{
			//Event External Mappings
			form.getContextMenus().Pathways.getEventConfigExternalSystemEventAddItem().setVisible(eventType != null && !eventType.equals(EventCreationType.INTERNAL ) && !isEventActive && !isEventInactive);
			form.getContextMenus().Pathways.getEventConfigExternalSystemEventRemoveItem().setVisible(form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getSelectedRow() != null && !isEventActive && !isEventInactive);
			
			//RTT Event
			form.getContextMenus().Pathways.getEventConfigExternalSystemRTTAddItem().setVisible(eventType != null && !eventType.equals(EventCreationType.INTERNAL) && !isEventActive && !isEventInactive);
			form.getContextMenus().Pathways.getEventConfigExternalSystemRTTEditItem().setVisible(form.lyrTargets().tabRTT().grdRTT().getSelectedRow() != null && !isEventActive && !isEventInactive);
			form.getContextMenus().Pathways.getEventConfigExternalSystemRTTRemoveItem().setVisible(form.lyrTargets().tabRTT().grdRTT().getSelectedRow() != null && !isEventActive && !isEventInactive);
			
			//Event Target
			form.getContextMenus().Pathways.getEventTargetManagementAddTargetItem().setVisible(!isEventActive && !isEventInactive);
			form.getContextMenus().Pathways.getEventTargetManagementAddActivateTargetItem().setVisible(form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getSelectedRow() != null && !isEventActive && !isEventInactive);
			form.getContextMenus().Pathways.getEventTargetManagementEditItem().setVisible(form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getSelectedRow() != null && !isEventInactive && !isEventActive);
			form.getContextMenus().Pathways.getEventTargetManagementRemoveItem().setVisible(form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getSelectedRow() != null && !isEventActive && !isEventInactive);
		}
		else
		{
			form.getContextMenus().Pathways.hideAllEventConfigExternalSystemEventMenuItems();
			form.getContextMenus().Pathways.hideAllEventConfigExternalSystemRTTMenuItems();
			form.getContextMenus().Pathways.hideAllEventTargetManagementMenuItems();
		}		
	}
	
	private void open() 
	{
		populateScreenFromData(form.getLocalContext().getEditedRecord());
		form.setMode(FormMode.VIEW);
	}
	
	protected void onBtnNewClick() throws ims.framework.exceptions.PresentationLogicException
	{
		newInstance();
	}
	
	public void newInstance() throws ims.framework.exceptions.PresentationLogicException
	{
		form.setMode(FormMode.EDIT);
		clearAll();
		form.lyrTargets().showtabExternalMappings();
		initialiseEventTargetManagementDynamicGrid();
		
		form.getLocalContext().setEditedRecord(null);
		form.getLocalContext().setEventExternalEventMappingOriginalCollection(null);
		form.getLocalContext().setEventExternalEventMappingCollection(null);
		form.getLocalContext().setEventRTTEventOriginalCollection(null);
		
		form.cmbStatus().clear();
		super.bindcmbStatusLookup();
		form.cmbStatus().removeRow(PreActiveActiveInactiveStatus.INACTIVE);
		populateRolesGrid(null);
		updateContextMenuState();
	}

	private void clearAll() 
	{
		form.customControlMappings().setValue(null);
		form.txtName().setValue(null);
		form.txtDesc().setValue(null);
		//WDEV-9965
		form.qmbImage().clear();
		form.qmbImage().setValue(null);
		
		form.cmbStatus().setValue(null);
		form.cmbEventType().setValue(null);
		form.chkDisplayInPatientDiary().setValue(false);
		form.chkStartsClock().setValue(false);
		form.chkStopClocks().setValue(false);
		form.chkEndsPathway().setValue(false);
		form.cmbType().setValue(null);
		form.cmbReason().setValue(null);
		form.chkScheduleEvent().setValue(false);
		form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().clear();
		form.lyrTargets().tabRTT().grdRTT().getRows().clear();
		form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getRows().clear();
		form.lyrTargets().tabActioningRoles().grdRoles().getRows().clear();
	}
	
	protected void onBtnEditClick() throws ims.framework.exceptions.PresentationLogicException
	{
		form.setMode(FormMode.EDIT);
	}

	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{
		try 
		{	
			if (save())
				open();
		} 
		catch (ForeignKeyViolationException e) 
		{
			e.printStackTrace();
		}
	}

	private boolean save() throws ForeignKeyViolationException, DomainRuntimeException 
	{
		EventVo event = populateDataFromScreen(form.getLocalContext().getEditedRecord());
		EventTargetVoCollection eventTargets = populateDataFromEventTargetScreen(event);
		
		//Validate TargetEvent
		String[] uiErrors = getUiErrors();
		String[] errors = event.validate(uiErrors);
		for (int i = 0; i < eventTargets.size(); i++) 
		{
			errors = eventTargets.get(i).validate(uiErrors);
		}
		if(errors != null && errors.length > 0)
		{
			engine.showErrors(errors);	
			return false;
		}
		//Validate Event
		errors = event.validate(uiErrors);
		
		if(errors != null && errors.length > 0)
		{
			engine.showErrors(errors);	
			return false;
		}
		
		if(	checkExternalMappingsChilds() ||
				checkRTTChilds() ||
					checkEventTargetChilds())
						return false;
		
		try
		{
			event = domain.save(event, 
					eventTargets, 
						form.getLocalContext().getEventExternalEventMappingCollection(),
							form.getLocalContext().getEventExternalEventMappingOriginalCollection(),
								form.getLocalContext().getEventRTTEventCollection(),
									form.getLocalContext().getEventRTTEventOriginalCollection());
		}
		catch(StaleObjectException e)
		{
			engine.showMessage(ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
			open();
			return false;
		}
		catch (UniqueKeyViolationException e)
		{	
			engine.showErrors(new String[]{e.getMessage().toString()});
			return false;
		}
		catch(UnqViolationUncheckedException e)
		{
			engine.showErrors("Taxonomy Map error", new String[]{"TaxonomyMap must be unique!"});
			return false;
		}
		catch (DomainInterfaceException e)
		{
			engine.showMessage(e.getMessage());
			return false;
		}
		
		form.getLocalContext().setEditedRecord(event);
		
		return true;
	}
	
	private EventExternalEventMappingVo populateEventExternalEventMapping(EventVo event, ExternalEventMappingVo mapping) 
	{
		EventExternalEventMappingVo vo = new EventExternalEventMappingVo();
		vo.setEvent(event);
		vo.setExternalEventMapping(mapping);	
		vo.setStatus(form.cmbStatus().getValue());
		return vo;
	}

	private EventRTTEventVo populateEventRTTEvent(EventVo event, RTTEventVo rtt) 
	{
		EventRTTEventVo vo = new EventRTTEventVo();
		vo.setEvent(event);
		vo.setRTTEvent(rtt);
		vo.setStatus(form.cmbStatus().getValue());
		return vo;
	}
	
	private EventTargetVoCollection populateDataFromEventTargetScreen(EventVo event) 
	{
		EventTargetVoCollection voEventTargetColl = new EventTargetVoCollection();
		DynamicGrid dynGrid = form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement();
		DynamicGridRow currentRow = null;
		int size = dynGrid.getRows().size();
		
		for (int i = 0; i < size; i++) 
		{
			currentRow = dynGrid.getRows().get(i);
			
			if (currentRow.getIdentifier().equals(PARENT))
			{
				EventTargetVo voEventTarget = new EventTargetVo();
				if(currentRow.getValue() != null && currentRow.getValue() instanceof EventTargetVo)
					voEventTarget = (EventTargetVo) currentRow.getValue();
				
				Object targetName = (TargetConfigurationShortVo) currentRow.getCells().get(getColumn(COLNAME)).getValue();
				PatientTargetStatus journeyTarget = (PatientTargetStatus) currentRow.getCells().get(getColumn(COLJOURNEYTARGET)).getValue();
				Object status = (PreActiveActiveInactiveStatus) currentRow.getCells().get(getColumn(COLSTATUS)).getValue();
				Boolean setToInScope = (Boolean) currentRow.getCells().get(getColumn(COLSETTOINSCOPE)).getValue();
				
				TargetShortVo target = new TargetShortVo();
				if (targetName != null)
				{
					target.setID_Target(((TargetConfigurationShortVo) targetName).getID_Target());
					target.setName(((TargetConfigurationShortVo) targetName).getName());
					target.setStatus(((TargetConfigurationShortVo) targetName).getStatus());
					target.setEventLinkType(((TargetConfigurationShortVo) targetName).getEventLinkType());
					voEventTarget.setTarget(target);
				}
				voEventTarget.setEvent(event);
				voEventTarget.setStatus((PreActiveActiveInactiveStatus) status);
				
				//WDEV-4026
				voEventTarget.setSetToInScope(setToInScope);
				if(setToInScope != null &&
						setToInScope.booleanValue())
				{
					if(journeyTarget == null)
						journeyTarget = PatientTargetStatus.INSCOPE;
				}
				
				voEventTarget.setSetJourneyTargetStatusTo((PatientTargetStatus) journeyTarget);
				
				if (currentRow.getRows().size() > 0)
				{
					TargetRefVoCollection activateTargets = new TargetRefVoCollection();
					for (int j = 0; j < currentRow.getRows().size(); j++) 
					{
						if (currentRow.getRows().get(j).getCells().get(getColumn(COLNAME)).getValue() == null)
							continue;
						
						TargetRefVo currentActivateTargets = new TargetRefVo();
						Object childTargetName = (TargetConfigurationShortVo) currentRow.getRows().get(j).getCells().get(getColumn(COLNAME)).getValue();
						currentActivateTargets.setID_Target(((TargetConfigurationShortVo)childTargetName).getID_Target());
						activateTargets.add(currentActivateTargets);
					}
					
					voEventTarget.setActivateTargets(activateTargets);
				}
				
				voEventTargetColl.add(voEventTarget);
			}
		}
		
		return voEventTargetColl;
	}

	private String[] getUiErrors() 
	{
		ArrayList<String> errors = new ArrayList<String>();
		
		/*if (form.txtName().getValue() == null  ||
				(form.txtName().getValue() != null  &&
						form.txtName().getValue().trim().length() == 0))
		{
			errors.add("Name is mandatory!");
		}
		if (form.cmbStatus().getValue() == null)
		{
			errors.add("Status is mandatory!");
		}*/
		
		if(form.chkEndsPathway().getValue())
		{
			if(form.cmbType().getValue() == null)
			{
				errors.add("Cancellation Type is mandatory");
			}
			if(form.cmbReason().getValue() == null)
			{
				errors.add("Cancellation Reason is mandatory");
			}
		}
		
		//GridExternal - check all rows to have an External System, Name, PAS Code and National Code
		int sizeExternal = form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().size();
		if (sizeExternal > 0)
		{
			for (int i = 0; i < sizeExternal; i++) 
			{
				grdExternalEventMappingsRow row = form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i);
				if (row.getColStatus() == null)
				{
					errors.add("Status from External Event tab is mandatory");
				}
			}
		}
		
		//GridRTT - check all rows to have an External System, Name, PAS Code and National Code
		int sizeRtt = form.lyrTargets().tabRTT().grdRTT().getRows().size();
		if (sizeRtt > 0)
		{
			for (int i = 0; i < sizeRtt; i++) 
			{
				grdRTTRow row = form.lyrTargets().tabRTT().grdRTT().getRows().get(i);
				if (row.getColProviderSystem().getValue() == null)
				{
					errors.add("External System from External System RTT tab is mandatory");
				}
				if (row.getColName().getValue() == null)
				{
					errors.add("Name from External System RTT tab is mandatory");
				}
				if (row.getColNatCode() == null)
				{
					errors.add("National Code from External System RTT tab is mandatory");
				}
				if (row.getColExtCode() == null)
				{
					errors.add("External System Code from External System RTT tab is mandatory");
				}
				if (row.getColStatus() == null)
				{
					errors.add("Status from External System RTT tab is mandatory");
				}
			}
		}
		
		//DynGridTargetEvent - check all rows to have Target and Status
		int sizeDynGrid = form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getRows().size();
		if (sizeDynGrid > 0)
		{
			for (int i = 0; i < sizeDynGrid; i++) 
			{
				DynamicGridRow dynRow = form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getRows().get(i);
				if (dynRow.getCells().get(getColumn(COLNAME)) == null)
				{
					errors.add("Target Name from Event Target Management tab is mandatory");
				}
				if (dynRow.getCells().get(getColumn(COLSTATUS)) == null)
				{
					errors.add("Status from Event Target Management tab is mandatory");
				}
			}
		}
		
		if(errors.size() > 0)
		{
			String[] searchErrors = new String[errors.size()];
			errors.toArray(searchErrors);
			engine.showErrors("Invalid Event Configuration", searchErrors);
			
			return searchErrors;
		}
		return null;
	}
	
	private EventVo populateDataFromScreen(EventVo record) 
	{
		if(record == null)
			record = new EventVo();
		
		record.setName(form.txtName().getValue());
		record.setDescription(form.txtDesc().getValue());
		
		if (form.qmbImage().getValue() != null)
		{
			record.setIcon(form.qmbImage().getValue());
		}
		
		record.setStatus(form.cmbStatus().getValue());
		record.setEventType(form.cmbEventType().getValue());
		record.setActioningRoles(populateRolesFromRolesGrid());
		record.setPatientDiaryImpact(form.chkDisplayInPatientDiary().getValue());
		record.setStartsClock(form.chkStartsClock().getValue());
		record.setStopsClock(form.chkStopClocks().getValue());
		record.setEndsPathway(form.chkEndsPathway().getValue());
		record.setIsManualScheduledEvent(form.chkScheduleEvent().getValue());
		form.getLocalContext().setEventExternalEventMappingCollection(populateExternalEventMappingsFromExternalMappingGrid());
		record.setTaxonomyMaps(form.customControlMappings().getValue());
		form.getLocalContext().setEventRTTEventCollection(populateRTTEventFromRTTGrid());
		record.setActioningRoles(populateRolesFromRolesGrid());
		
		if(form.chkEndsPathway().getValue())
		{
			record.setCancellationType(form.cmbType().getValue());
			record.setCancellationReason(form.cmbReason().getValue());
		}
		
		return record;
	}

	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if((form.getLocalContext().getEditedRecordIsNotNull() &&
				form.getGlobalContext().Pathway.getSelectedEventLiteVoIsNotNull()) ||
					(form.getGlobalContext().Pathway.getSelectedEventLiteVoIsNotNull()))
		{
			EventVo event = domain.getEvent(form.getGlobalContext().Pathway.getSelectedEventLiteVo());
			form.getLocalContext().setEditedRecord(event);
			populateScreenFromData(event);
			updateContextMenuState();
		}
		else
		{
			clearAll();
			initialiseEventTargetManagementDynamicGrid();
		}
		
		form.setMode(FormMode.VIEW);
	}

	protected void onContextMenuItemClick(int menuItemID, Control sender)
			throws PresentationLogicException 
	{
		switch (menuItemID)
		{
			case GenForm.ContextMenus.PathwaysNamespace.EventConfigExternalSystemEvent.Add:
				EventConfigExternalSystemAdd();
				break;
			case GenForm.ContextMenus.PathwaysNamespace.EventConfigExternalSystemEvent.Remove:
				EventConfigExternalSystemRemove();
				updateContextMenuState();
				break;
		
			case GenForm.ContextMenus.PathwaysNamespace.EventConfigExternalSystemRTT.Add:
				EventConfigExternalSystemRTTAdd();
				break;
			case GenForm.ContextMenus.PathwaysNamespace.EventConfigExternalSystemRTT.Remove:
				EventConfigExternalSystemRTTRemove();
				updateContextMenuState();
				break;	
			case GenForm.ContextMenus.PathwaysNamespace.EventConfigExternalSystemRTT.Edit:
				EventConfigExternalSystemRTTEdit();
				break;	
			case GenForm.ContextMenus.PathwaysNamespace.EventTargetManagement.AddTarget:
				EventTargetManagementAddTarget();
				updateContextMenuState();
				break;
			case GenForm.ContextMenus.PathwaysNamespace.EventTargetManagement.AddActivateTarget:
				EventTargetManagementAddActivateTarget();
				break;
			case GenForm.ContextMenus.PathwaysNamespace.EventTargetManagement.Remove:
				EventTargetManagementRemove();
				updateContextMenuState();
				break;	
			case GenForm.ContextMenus.PathwaysNamespace.EventTargetManagement.Edit:
				EventTargetManagementEdit();
				break;	
		}	
	}

	private void EventConfigExternalSystemRTTAdd() 
	{
		grdRTTRow row = form.lyrTargets().tabRTT().grdRTT().getRows().newRow();
		form.lyrTargets().tabRTT().grdRTT().setEnabled(true);
		form.lyrTargets().tabRTT().grdRTT().setReadOnly(false);
		
		EventRTTEventVo rowValue = new EventRTTEventVo();
		RTTEventVo rtt = new RTTEventVo();
		rowValue.setRTTEvent(rtt);
		ProviderSystemVoCollection voCollProviderSystems = domain.listProviderSystems();
		row.getColProviderSystem().clear();
		row.setColImage(form.getImages().ICP.Stage);
		
		for (int i = 0; i < voCollProviderSystems.size(); i++) 
		{
			ProviderSystemVo voProviderSystems = voCollProviderSystems.get(i);
			row.getColProviderSystem().newRow(voProviderSystems, voProviderSystems.getSystemName());
		}		
		row.setColStatus(PreActiveActiveInactiveStatus.PREACTIVE);
		
		row.setValue(rowValue);
		row.setReadOnly(false);
	}

	private void EventConfigExternalSystemRTTRemove() 
	{
		if (form.lyrTargets().tabRTT().grdRTT().getSelectedRowIndex() >= 0)
			form.lyrTargets().tabRTT().grdRTT().getRows().remove
				(form.lyrTargets().tabRTT().grdRTT().getSelectedRowIndex());
	}

	private void EventConfigExternalSystemRTTEdit() 
	{
		form.lyrTargets().tabRTT().grdRTT().setEnabled(true);
		form.lyrTargets().tabRTT().grdRTT().setReadOnly(false);	
		form.lyrTargets().tabRTT().grdRTT().getSelectedRow().setReadOnly(false);
	}

	private void EventConfigExternalSystemAdd() 
	{
		ExternalEventMappingVoCollection voMappings = new ExternalEventMappingVoCollection();
		for (int i = 0; i < form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().size(); i++)
		{
				voMappings.add(form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).getValue().getExternalEventMapping());
		}
		form.getGlobalContext().Pathway.setSelectedExternalEvents(voMappings);
		engine.open(form.getForms().Pathways.ExternalEventsSearchAndSelect);
	}

	private void EventConfigExternalSystemRemove() 
	{
		if (form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getSelectedRowIndex() >= 0)
			form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().remove
				(form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getSelectedRowIndex());		
	}

	private DynamicGridColumn getColumn(String identifier)
	{
		return form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getColumns().getByIdentifier(identifier);
	}
	
	private void EventTargetManagementAddTarget() 
	{
		form.getLocalContext().setTargetConfigurationVoCollection(null);
		
		DynamicGrid dynGrid = form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement();
		dynGrid.setEnabled(true);
		dynGrid.setReadOnly(false);
		DynamicGridRow dynParentRow = dynGrid.getRows().newRow();
		dynParentRow.setIdentifier(PARENT);
		dynParentRow.setReadOnly(false);

		DynamicGridCell cellTargetName = dynParentRow.getCells().newCell(getColumn(COLNAME), DynamicCellType.ENUMERATION);
		populateParentTargetNameCell(cellTargetName);

		DynamicGridCell cellSetToInScope = dynParentRow.getCells().newCell(getColumn(COLSETTOINSCOPE), DynamicCellType.BOOL);
		cellSetToInScope.setValue(Boolean.FALSE);
		cellSetToInScope.setAutoPostBack(true);
		
		DynamicGridCell cellJourneyTarget = dynParentRow.getCells().newCell(getColumn(COLJOURNEYTARGET), DynamicCellType.ENUMERATION);
		populateJourneyTargetCell(cellJourneyTarget);
		
		DynamicGridCell cellStatus = dynParentRow.getCells().newCell(getColumn(COLSTATUS), DynamicCellType.ENUMERATION);
		populateStatusCell(cellStatus, PreActiveActiveInactiveStatus.INACTIVE);
		
		dynGrid.setSelectedRow(dynParentRow);
	}

	private void populateParentTargetNameCell(DynamicGridCell cellTargetName)
	{
		cellTargetName.getItems().clear();
		cellTargetName.setAutoPostBack(true);
		TargetConfigurationShortVoCollection voColl = form.getLocalContext().getTargetConfigurationVoCollection();
		if (voColl == null)
		{
			voColl = domain.listTargetByCriteria(null);
		}
		
		for(int i = 0; voColl != null && i < voColl.size(); i++)
		{
			if (!voColl.get(i).getStatus().equals(PreActiveActiveInactiveStatus.INACTIVE))
			{
				cellTargetName.getItems().newItem(voColl.get(i), form.getImages().Blackpool.Target);
			}
		}
	}
	
	private void populateChildTargetNameCell(DynamicGridCell cellTargetName)
	{
		cellTargetName.getItems().clear();
		cellTargetName.setAutoPostBack(true);
		TargetConfigurationShortVoCollection voColl = form.getLocalContext().getTargetConfigurationVoCollection();
		if (voColl == null)
		{
			voColl = domain.listTargetByCriteria(null);
		}
		
		for(int i = 0; voColl != null && i < voColl.size(); i++)
		{
			cellTargetName.getItems().newItem(voColl.get(i), form.getImages().Blackpool.Target);
		}
	}
	
	private void populateJourneyTargetCell(DynamicGridCell cellJourneyTarget)
	{
		cellJourneyTarget.getItems().clear();
		cellJourneyTarget.setAutoPostBack(true);
		PatientTargetStatusCollection voColl = ims.pathways.vo.lookups.LookupHelper.getPatientTargetStatus(domain.getLookupService());
		for(int i = 0; voColl != null && i < voColl.size(); i++)
		{
			if(voColl.get(i).getParent() == null)
				cellJourneyTarget.getItems().newItem(voColl.get(i));
		}
	}
	
	private void populateStatusCell(DynamicGridCell cellStatus, PreActiveActiveInactiveStatus status)
	{
		cellStatus.getItems().clear();
		cellStatus.setAutoPostBack(true);
		PreActiveActiveInactiveStatusCollection voColl = ims.core.vo.lookups.LookupHelper.getPreActiveActiveInactiveStatus(domain.getLookupService());
		
		for(int i = 0; voColl != null && i < voColl.size(); i++)
		{
			if(voColl.get(i).getParent() == null)
			{
				if (status != null)
				{
					if (!voColl.get(i).equals(status))
						cellStatus.getItems().newItem(voColl.get(i));
				}
				else
				{
					cellStatus.getItems().newItem(voColl.get(i));
				}
			}
		}
	}
	
	private void EventTargetManagementAddActivateTarget() 
	{
		DynamicGrid dynGrid = form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement();
		DynamicGridRow dynChildRow = null;
		if(dynGrid.getSelectedRow() == null)
			return;
		if (dynGrid.getSelectedRow().getIdentifier().equals(PARENT))
		{
			dynChildRow = dynGrid.getSelectedRow().getRows().newRow();
		}
		else if ((dynGrid.getSelectedRow().getIdentifier().equals(CHILD)))
		{
			dynChildRow = dynGrid.getSelectedRow().getParent().getRows().newRow();
		}
		
		dynChildRow.setIdentifier(CHILD);
		dynChildRow.setReadOnly(false);
		dynGrid.getSelectedRow().setExpanded(true);
		
		DynamicGridCell cellTargetName = dynChildRow.getCells().newCell(getColumn(COLNAME), DynamicCellType.ENUMERATION);
		populateChildTargetNameCell(cellTargetName);
	}

	private void EventTargetManagementRemove() 
	{
		if (form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getSelectedRow() != null)
			form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getRows().remove(
				form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getSelectedRow());
	}

	private void EventTargetManagementEdit() 
	{
		form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getSelectedRow().setReadOnly(false);
		
		if (form.cmbStatus().getValue() != null &&
				form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.ACTIVE))
		{
			form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getSelectedRow().setReadOnly(true);
			DynamicGridCell dynamicGridCell = form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getSelectedRow().getCells().get(getColumn(COLSTATUS));
			if(dynamicGridCell!=null)
				dynamicGridCell.setReadOnly(false);
		}
		else
		{
			form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getSelectedRow().setReadOnly(false);
			DynamicGridCell dynamicGridCell = form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getSelectedRow().getCells().get(getColumn(COLSTATUS));
			if(dynamicGridCell!=null)
				dynamicGridCell.setReadOnly(false);
		}
	}

	@Override
	protected void onlyrTargetsTabChanged(LayerBridge tab) 
	{
		
	}
	
	private void populateEventTargetDinamicGrid(EventVo event) 
	{
		initialiseEventTargetManagementDynamicGrid();
		DynamicGrid dynGrid = form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement();

		EventTargetVoCollection voColl = domain.getEventTargetsByEvent(event);
		
		for (int i = 0; i < voColl.size(); i++) 
		{
			EventTargetVo vo = voColl.get(i);
			DynamicGridRow dynParentRow = dynGrid.getRows().newRow();
			dynParentRow.setIdentifier(PARENT);
			dynParentRow.setReadOnly(true);
			
			DynamicGridCell cellTargetName = dynParentRow.getCells().newCell(getColumn(COLNAME), DynamicCellType.ENUMERATION);
			populateParentTargetNameCell(cellTargetName);
			cellTargetName.setValue(vo.getTargetIsNotNull() ? vo.getTarget() : null);
			cellTargetName.setTooltip(vo.getTargetIsNotNull() ? vo.getTarget().getName() : null);

			DynamicGridCell cellSetToInScope = dynParentRow.getCells().newCell(getColumn(COLSETTOINSCOPE), DynamicCellType.BOOL);
			cellSetToInScope.setValue(vo.getSetToInScope());
			cellSetToInScope.setTooltip("Set To In Scope");
			cellSetToInScope.setAutoPostBack(true);

			DynamicGridCell cellJourneyTarget = dynParentRow.getCells().newCell(getColumn(COLJOURNEYTARGET), DynamicCellType.ENUMERATION);
			populateJourneyTargetCell(cellJourneyTarget);
			cellJourneyTarget.setValue(vo.getSetJourneyTargetStatusToIsNotNull() ? vo.getSetJourneyTargetStatusTo() : null);
			cellJourneyTarget.setTooltip(vo.getSetJourneyTargetStatusToIsNotNull() ? vo.getSetJourneyTargetStatusTo().getText() : null);
				
			DynamicGridCell cellStatus = dynParentRow.getCells().newCell(getColumn(COLSTATUS), DynamicCellType.ENUMERATION);
			if (vo.getStatus().equals(PreActiveActiveInactiveStatus.ACTIVE))
				populateStatusCell(cellStatus, PreActiveActiveInactiveStatus.PREACTIVE);
			else
				populateStatusCell(cellStatus, null);
			
			
			cellStatus.setValue(vo.getStatusIsNotNull() ? vo.getStatus() : null);
			cellStatus.setTooltip(vo.getStatusIsNotNull() ? vo.getStatus().getText() : null);
				
			dynParentRow.setValue(vo);
			dynGrid.setSelectedRow(dynParentRow);
			
			if (voColl.get(i).getActivateTargetsIsNotNull())
			{
				TargetRefVoCollection activateTargets = voColl.get(i).getActivateTargets();
				
				for (int j = 0; j < activateTargets.size(); j++) 
				{
					TargetRefVo activateTarget = activateTargets.get(j);
					DynamicGridRow dynChildRow = dynGrid.getSelectedRow().getRows().newRow();
					dynChildRow.setIdentifier(CHILD);
					dynChildRow.setReadOnly(true);
					dynGrid.getSelectedRow().setExpanded(true);
					
					DynamicGridCell cellChildTargetName = dynChildRow.getCells().newCell(getColumn(COLNAME), DynamicCellType.ENUMERATION);
					populateChildTargetNameCell(cellChildTargetName);
					cellChildTargetName.setValue(activateTarget);
				}		
			}
		}		
	}

	private void initialiseEventTargetManagementDynamicGrid()
	{
		DynamicGrid dynGrid = form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement();
		form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().clear();
		
		DynamicGridColumn colTargetName = null;
		DynamicGridColumn colJourneyTarget = null;
		DynamicGridColumn colSetToInscope = null;
		DynamicGridColumn colStatus = null;
		
		colTargetName = dynGrid.getColumns().newColumn("Target Name", COLNAME);
		colSetToInscope = dynGrid.getColumns().newColumn("Set To In Scope", COLSETTOINSCOPE);
		colJourneyTarget = dynGrid.getColumns().newColumn("Set Journey Target Status", COLJOURNEYTARGET);
		colStatus = dynGrid.getColumns().newColumn("Status", COLSTATUS);
		
		colTargetName.setWidth(300);
		colSetToInscope.setWidth(100);
		colSetToInscope.setAlignment(Alignment.CENTER);
		colJourneyTarget.setWidth(170);
		colStatus.setWidth(-1);
	}
	
	private void populateRolesGrid(EventVo event) 
	{
		form.lyrTargets().tabActioningRoles().grdRoles().getRows().clear();
		
		AppRoleShortVoCollection voColl = domain.listRoles();
		if (voColl == null)
			return;
		
		form.lyrTargets().tabActioningRoles().grdRoles().getRows().clear();
		for (int i = 0; i < voColl.size(); i++)
		{
			AppRoleShortVo voAppRole = voColl.get(i);
			if(voAppRole.getIsActiveIsNotNull() && voAppRole.getIsActive().booleanValue())
			{
				grdRolesRow row = form.lyrTargets().tabActioningRoles().grdRoles().getRows().newRow();
				row.setColImage(form.getImages().OCRR.Role);
				row.setColRole(voAppRole.getName());
				if (event != null &&
						event.getActioningRolesIsNotNull())
				{
					for (int j = 0; j < event.getActioningRoles().size(); j++) 
					{
						if (voAppRole.getName().equals(event.getActioningRoles().get(j).getName()))
						{
							row.setColSelect(true);
						}
					}
				}
				
				row.setValue(voAppRole);
			}
		}		
	}
	
	private void populateExternalEventMappingsGrid(EventVo event)
	{
		EventExternalEventMappingVoCollection editedRecord = domain.getEventExternalEventMapping(event, event.getStatus());
		form.getLocalContext().setEventExternalEventMappingOriginalCollection(editedRecord);
		form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().clear();
		
		for (int i = 0; i < editedRecord.size(); i++) 
		{
			grdExternalEventMappingsRow row = form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().newRow();
			row.setColImage(form.getImages().OCRR.Sent);
			row.setColProviderSystem(editedRecord.get(i).getExternalEventMapping().getProviderSystem().getSystemName());
			row.setTooltipForColProviderSystem(editedRecord.get(i).getExternalEventMapping().getProviderSystem().getSystemName());
			row.setColGroup(editedRecord.get(i).getExternalEventMapping().getGroupIsNotNull() ? editedRecord.get(i).getExternalEventMapping().getGroup().toString() : null);
			row.setTooltipForColGroup(editedRecord.get(i).getExternalEventMapping().getGroupIsNotNull() ? editedRecord.get(i).getExternalEventMapping().getGroup().toString() : null);
			row.setColDetail(editedRecord.get(i).getExternalEventMapping().getDetailIsNotNull() ? editedRecord.get(i).getExternalEventMapping().getDetail().toString() : null);
			row.setTooltipForColDetail(editedRecord.get(i).getExternalEventMapping().getDetailIsNotNull() ? editedRecord.get(i).getExternalEventMapping().getDetail().toString() : null);
			row.setColSpecialty(editedRecord.get(i).getExternalEventMapping().getSpecialtyIsNotNull() ? editedRecord.get(i).getExternalEventMapping().getSpecialty().toString() : null);
			row.setTooltipForColSpecialty(editedRecord.get(i).getExternalEventMapping().getSpecialtyIsNotNull() ? editedRecord.get(i).getExternalEventMapping().getSpecialty().toString() : null);
			row.setColConsultant(editedRecord.get(i).getExternalEventMapping().getConsultantIsNotNull() ? editedRecord.get(i).getExternalEventMapping().getConsultant().toString() : null);
			row.setTooltipForColConsultant(editedRecord.get(i).getExternalEventMapping().getConsultantIsNotNull() ? editedRecord.get(i).getExternalEventMapping().getConsultant().toString() : null);
			row.setColClinicCode(editedRecord.get(i).getExternalEventMapping().getClinicCodeIsNotNull() ? editedRecord.get(i).getExternalEventMapping().getClinicCode() : null);
			row.setTooltipForColClinicCode(editedRecord.get(i).getExternalEventMapping().getClinicCodeIsNotNull() ? editedRecord.get(i).getExternalEventMapping().getClinicCode() : null);
			row.setColDescription(editedRecord.get(i).getExternalEventMapping().getDescriptionIsNotNull() ? editedRecord.get(i).getExternalEventMapping().getDescription() : null);
			row.setTooltipForColDescription(editedRecord.get(i).getExternalEventMapping().getDescriptionIsNotNull() ? editedRecord.get(i).getExternalEventMapping().getDescription() : null);
			row.setColStatus(editedRecord.get(i).getStatus());
			
			row.setValue(editedRecord.get(i));	
		}
	}

	private void populateRTTEventGrid(EventVo event)
	{
		//RTTEventVoCollection editedRecord = domain.getRTTByEventAndStatus(event, null);
		EventRTTEventVoCollection editedRecord = domain.getEventRTTEvent(event, event.getStatus());
		form.getLocalContext().setEventRTTEventOriginalCollection(editedRecord);
		ProviderSystemVoCollection voCollProviderSystems = domain.listProviderSystems();
		
		form.lyrTargets().tabRTT().grdRTT().getRows().clear();		
		
		for (int i = 0; i < editedRecord.size(); i++) 
		{
			if(editedRecord.get(i) == null)
				return;
			
			grdRTTRow row = form.lyrTargets().tabRTT().grdRTT().getRows().newRow();
			row.setColImage(form.getImages().ICP.Stage);
			row.getColProviderSystem().clear();
			
			for (int j = 0; j < voCollProviderSystems.size(); j++) 
			{
				ProviderSystemVo voProviderSystems = voCollProviderSystems.get(j);
				row.getColProviderSystem().newRow(voProviderSystems, voProviderSystems.getSystemName());
			}		
			row.getColProviderSystem().setValue(editedRecord.get(i).getRTTEvent().getProviderSystemIsNotNull() ? editedRecord.get(i).getRTTEvent().getProviderSystem() : null);
			row.setTooltipForColProviderSystem(editedRecord.get(i).getRTTEvent().getProviderSystemIsNotNull() ? editedRecord.get(i).getRTTEvent().getProviderSystem().getSystemName() : null);
			
			row.getColName().clear();
			RTTEventVoCollection voCollRTTEvent = domain.getNameByProviderSystems((ProviderSystemRefVo)editedRecord.get(i).getRTTEvent().getProviderSystem() );
			for (int j = 0; j < voCollRTTEvent.size(); j++) 
			{
				RTTEventVo vo = voCollRTTEvent.get(j);
				row.getColName().newRow(vo, vo.getName());
			}
			row.getColName().setValue(editedRecord.get(i).getRTTEvent().getName() != null ? editedRecord.get(i).getRTTEvent(): null);
			row.setTooltipForColName(editedRecord.get(i).getRTTEvent().getName() != null ? editedRecord.get(i).getRTTEvent().getName() : null);
			
			row.setColNatCode(editedRecord.get(i).getRTTEvent().getNationalCodeIsNotNull() ? editedRecord.get(i).getRTTEvent().getNationalCode() : null);
			row.setTooltipForColNatCode(editedRecord.get(i).getRTTEvent().getNationalCodeIsNotNull() ? editedRecord.get(i).getRTTEvent().getNationalCode() : null);
			row.setColExtCode(editedRecord.get(i).getRTTEvent().getPASCodeIsNotNull() ? editedRecord.get(i).getRTTEvent().getPASCode() : null);
			row.setTooltipForColExtCode(editedRecord.get(i).getRTTEvent().getPASCodeIsNotNull() ? editedRecord.get(i).getRTTEvent().getPASCode() : null);
			row.setColAction(editedRecord.get(i).getRTTEvent().getActionIsNotNull() ? editedRecord.get(i).getRTTEvent().getAction() : null);
			row.setTooltipForColAction(editedRecord.get(i).getRTTEvent().getActionIsNotNull() ? editedRecord.get(i).getRTTEvent().getAction().getText() : null);
			row.setColStatus(editedRecord.get(i).getStatus());
		
			row.setValue(editedRecord.get(i));
			row.setReadOnly(true);
		}	
	}
	
	private AppRoleShortVoCollection populateRolesFromRolesGrid()
	{
		AppRoleShortVoCollection voRoles = new AppRoleShortVoCollection();
		for (int i = 0; i < form.lyrTargets().tabActioningRoles().grdRoles().getRows().size(); i++)
		{
			if (form.lyrTargets().tabActioningRoles().grdRoles().getRows().get(i).getColSelect())
				voRoles.add(form.lyrTargets().tabActioningRoles().grdRoles().getRows().get(i).getValue());
		}
		
		return voRoles;
	}
	
	private EventExternalEventMappingVoCollection populateExternalEventMappingsFromExternalMappingGrid()
	{
		EventExternalEventMappingVoCollection voColl = new EventExternalEventMappingVoCollection();
		for (int i = 0; i < form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().size(); i++)
		{
			ExternalEventMappingVo mapping = form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).getValue().getExternalEventMapping();
			EventVo event = form.getLocalContext().getEditedRecordIsNotNull() ? form.getLocalContext().getEditedRecord() : new EventVo();
			
			EventExternalEventMappingVo vo = populateEventExternalEventMapping(event, mapping);
			vo.setStatus(form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).getColStatus());
			
			if (form.getLocalContext().getEventExternalEventMappingOriginalCollectionIsNotNull())
			{
				for (int j = 0; j < form.getLocalContext().getEventExternalEventMappingOriginalCollection().size(); j++) 
				{
					if (form.getLocalContext().getEventExternalEventMappingOriginalCollection().get(j).getEvent().equals(
							vo.getEvent()) &&
							form.getLocalContext().getEventExternalEventMappingOriginalCollection().get(j).getExternalEventMapping().equals(
									vo.getExternalEventMapping()))
									{
										if (form.getLocalContext().getEventExternalEventMappingOriginalCollection().get(j).getActivatedDateTime() != null)
										{
											vo.setActivatedDateTime(form.getLocalContext().getEventExternalEventMappingOriginalCollection().get(j).getActivatedDateTime());
											vo.setActivatedBy(form.getLocalContext().getEventExternalEventMappingOriginalCollection().get(j).getActivatedBy());
										}
										else if (form.getLocalContext().getEventExternalEventMappingOriginalCollection().get(j).getInactivatedBy() != null)
										{
											vo.setInactivatedDateTime(form.getLocalContext().getEventExternalEventMappingOriginalCollection().get(j).getInactivatedDateTime());
											vo.setInactivatedBy(form.getLocalContext().getEventExternalEventMappingOriginalCollection().get(j).getInactivatedBy());
										} 
									}
				}
			}
			
			voColl.add(vo);
		}
		
		return voColl;
	}

	private EventRTTEventVoCollection populateRTTEventFromRTTGrid()
	{
		EventRTTEventVoCollection voColl = new EventRTTEventVoCollection();
		for (int i = 0; i < form.lyrTargets().tabRTT().grdRTT().getRows().size(); i++)
		{
			RTTEventVo rtt = form.lyrTargets().tabRTT().grdRTT().getRows().get(i).getValue().getRTTEvent();
			EventVo event = form.getLocalContext().getEditedRecordIsNotNull() ? form.getLocalContext().getEditedRecord() : new EventVo();
			
			EventRTTEventVo vo = populateEventRTTEvent(event, rtt);
			vo.setStatus(form.lyrTargets().tabRTT().grdRTT().getRows().get(i).getColStatus());
			
			if (form.getLocalContext().getEventRTTEventOriginalCollectionIsNotNull())
			{
				for (int j = 0; j < form.getLocalContext().getEventRTTEventOriginalCollection().size(); j++) 
				{
					if (form.getLocalContext().getEventRTTEventOriginalCollection().get(j).getEvent().equals(
							vo.getEvent()) &&
							form.getLocalContext().getEventRTTEventOriginalCollection().get(j).getRTTEvent().equals(
									vo.getRTTEvent()))
									{
										if (form.getLocalContext().getEventRTTEventOriginalCollection().get(j).getActivatedDateTime() != null)
										{
											vo.setActivatedDateTime(form.getLocalContext().getEventRTTEventOriginalCollection().get(j).getActivatedDateTime());
											vo.setActivatedBy(form.getLocalContext().getEventRTTEventOriginalCollection().get(j).getActivatedBy());
										}
										else if (form.getLocalContext().getEventRTTEventOriginalCollection().get(j).getInactivatedBy() != null)
										{
											vo.setInactivatedDateTime(form.getLocalContext().getEventRTTEventOriginalCollection().get(j).getInactivatedDateTime());
											vo.setInactivatedBy(form.getLocalContext().getEventRTTEventOriginalCollection().get(j).getInactivatedBy());
										} 
									}
				}
			}
			
			voColl.add(vo);	
		}
		
		return voColl;
	}
	
	protected void onQmbImageTextSubmited(String value)
			throws PresentationLogicException 
	{
		form.qmbImage().clear();
		AppImageVoCollection images = domain.list16x16Icons(value);
		AppImageVo img = null;
	
		for (int i = 0; i < images.size(); i++)
		{
			img = images.get(i);
			form.qmbImage().newRow(img, img.getImagePath(), img);
		}
		int size = form.qmbImage().getValues().size();
		
		if (size == 1)
		{
			form.qmbImage().setValue((AppImageVo) form.qmbImage().getValues().get(0));
		}
		else if (size > 1)
		{
			form.qmbImage().showOpened();
		}
	}

	protected void onFormModeChanged() 
	{
		boolean isEditedRecord = (form.getLocalContext().getEditedRecordIsNotNull() || form.getGlobalContext().Pathway.getSelectedEventLiteVoIsNotNull()) ? true : false;
		
		if (form.getMode().equals(FormMode.EDIT))
		{
			form.customControlMappings().initialize();
			
			// WDEV-18547 
			if (isEditedRecord && form.getLocalContext().getEditedRecordIsNotNull())
				form.customControlMappings().setValue(form.getLocalContext().getEditedRecord().getTaxonomyMaps());
			
			form.customControlMappings().setComponentMode(FormMode.EDIT);
			
			if (form.getGlobalContext().Pathway.getSelectedEventLiteVoIsNotNull() &&
					form.getGlobalContext().Pathway.getSelectedEventLiteVo().getStatusIsNotNull())
			{
				PreActiveActiveInactiveStatus status = form.getGlobalContext().Pathway.getSelectedEventLiteVo().getStatus();
				if (status.equals(PreActiveActiveInactiveStatus.ACTIVE))
				{
					super.bindcmbStatusLookup();
					form.cmbStatus().removeRow(PreActiveActiveInactiveStatus.PREACTIVE);
					form.cmbStatus().setValue(status);
				}
			}
			if (form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows() != null)
			{
				for (int i = 0; i < form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().size(); i++) 
				{
					if (form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).getColStatus() != null &&
							form.cmbStatus().getValue() != null &&
								!form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.INACTIVE))
					{
						form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).setColStatusReadOnly(false);
					}
					else
					{
						form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).setColStatusReadOnly(true);
					}
				}
			}
			
			if (form.lyrTargets().tabRTT().grdRTT().getRows() != null)
			{
				for (int i = 0; i < form.lyrTargets().tabRTT().grdRTT().getRows().size(); i++) 
				{
					if (form.lyrTargets().tabRTT().grdRTT().getRows().get(i).getColStatus() != null &&
							form.cmbStatus().getValue() != null &&
								!form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.INACTIVE))
					{
						form.lyrTargets().tabRTT().grdRTT().getRows().get(i).setColStatusReadOnly(false);
					}
					else
					{
						form.lyrTargets().tabRTT().grdRTT().getRows().get(i).setColStatusReadOnly(true);
					}
				}
			}	
		}
		else
		{
			if (form.cmbStatus().getValue() != null &&
					form.getGlobalContext().Pathway.getSelectedEventLiteVoIsNotNull() &&
						form.getGlobalContext().Pathway.getSelectedEventLiteVo().getStatusIsNotNull())
			{
				PreActiveActiveInactiveStatus status = form.cmbStatus().getValue();
				if (status.equals(PreActiveActiveInactiveStatus.ACTIVE))
				{
					super.bindcmbStatusLookup();
					form.cmbStatus().removeRow(PreActiveActiveInactiveStatus.PREACTIVE);
					form.cmbStatus().setValue(status);
					form.btnEdit().setVisible(isEditedRecord);
				}
				if (status.equals(PreActiveActiveInactiveStatus.INACTIVE))
				{
					form.btnEdit().setVisible(false);	
				}
			}
			if (form.getLocalContext().getEditedRecordIsNotNull() &&
					form.getGlobalContext().Pathway.getSelectedEventLiteVo() == null)
			{
				form.getGlobalContext().Pathway.setSelectedEventLiteVo(form.getLocalContext().getEditedRecord());
				form.btnEdit().setVisible(true);
			}
			
			form.customControlMappings().setComponentMode(FormMode.VIEW);
			
			form.lyrTargets().showtabExternalMappings();
		}
		
		form.chkScheduleEvent().setEnabled(form.getMode().equals(FormMode.EDIT));
		updateContextMenuState();
		
		updateCancellationControls();
	}

	protected void onGrdExternalEventMappingsSelectionChanged()
			throws PresentationLogicException 
	{
		updateContextMenuState();
	}

	protected void onGrdRTTSelectionChanged() throws PresentationLogicException 
	{
		updateContextMenuState();
	}

	protected void onDyngrdEventTargetManagementRowSelectionChanged(DynamicGridRow row) 
	{
		updateContextMenuState();
	}

	protected void onFormDialogClosed(FormName formName, DialogResult result) throws PresentationLogicException 
	{
		if ((formName.equals(form.getForms().Pathways.ExternalEventsSearchAndSelect)) && (result.equals(DialogResult.OK)) )
		{
			ExternalEventMappingVoCollection editedRecord = form.getGlobalContext().Pathway.getSelectedExternalEvents();
			boolean found = false;
			for (int i = 0; i < editedRecord.size(); i++) 
			{
				for (int j = 0; j < form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().size(); j++) 
				{
					found = false;
					if (form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(j).
							getValue().getExternalEventMapping().getID_ExternalEventMapping().equals(
									editedRecord.get(i).getID_ExternalEventMapping()))
					{
										found = true;
										break;
					}
				}
				
				if (found)
					continue;
				
				grdExternalEventMappingsRow row = form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().newRow();
				row.setColImage(form.getImages().OCRR.Sent);
				row.setColProviderSystem(editedRecord.get(i).getProviderSystem().getSystemName());
				row.setTooltipForColProviderSystem(editedRecord.get(i).getProviderSystem().getSystemName());
				row.setColGroup(editedRecord.get(i).getGroupIsNotNull() ? editedRecord.get(i).getGroup().toString() : null);
				row.setTooltipForColGroup(editedRecord.get(i).getGroupIsNotNull() ? editedRecord.get(i).getGroup().toString() : null);
				row.setColDetail(editedRecord.get(i).getDetailIsNotNull() ? editedRecord.get(i).getDetail().toString() : null);
				row.setTooltipForColDetail(editedRecord.get(i).getDetailIsNotNull() ? editedRecord.get(i).getDetail().toString() : null);
				row.setColSpecialty(editedRecord.get(i).getSpecialtyIsNotNull() ? editedRecord.get(i).getSpecialty().toString() : null);
				row.setTooltipForColSpecialty(editedRecord.get(i).getSpecialtyIsNotNull() ? editedRecord.get(i).getSpecialty().toString() : null);
				row.setColConsultant(editedRecord.get(i).getConsultantIsNotNull() ? editedRecord.get(i).getConsultant().toString() : null);
				row.setTooltipForColConsultant(editedRecord.get(i).getConsultantIsNotNull() ? editedRecord.get(i).getConsultant().toString() : null);
				row.setColClinicCode(editedRecord.get(i).getClinicCodeIsNotNull() ? editedRecord.get(i).getClinicCode() : null);
				row.setTooltipForColClinicCode(editedRecord.get(i).getClinicCodeIsNotNull() ? editedRecord.get(i).getClinicCode() : null);
				row.setColDescription(editedRecord.get(i).getDescriptionIsNotNull() ? editedRecord.get(i).getDescription() : null);
				row.setTooltipForColDescription(editedRecord.get(i).getDescriptionIsNotNull() ? editedRecord.get(i).getDescription() : null);
				if (row.getColStatus() == null)
				{
					row.setColStatus(PreActiveActiveInactiveStatus.PREACTIVE);
				}
				
				EventExternalEventMappingVo rowValue = new EventExternalEventMappingVo();
				rowValue.setExternalEventMapping(editedRecord.get(i));
				row.setValue(rowValue);	
			}
		}
		
	}

	protected void onGrdRTTMutableComboBoxSelected(int column, grdRTTRow row,
			Object value) throws PresentationLogicException 
	{
		if (value == null)
			return;
		
		//ProviderSystem
		if (column == 1)
		{
			RTTEventVoCollection voCollRTTEvent = domain.getNameByProviderSystems((ProviderSystemRefVo) value);
			row.getColName().clear();
			for (int i = 0; i < voCollRTTEvent.size(); i++) 
			{
				RTTEventVo voRTTEvent = voCollRTTEvent.get(i);
				row.getColName().newRow(voRTTEvent, voRTTEvent.getName());
			}
			EventRTTEventVo rowValue = row.getValue();
			RTTEventVo rtt = rowValue.getRTTEvent();
			
			rtt.setProviderSystem((ProviderSystemVo) row.getColProviderSystem().getValue());
			rtt.setName(null);
			rtt.setNationalCode(null);
			rtt.setPASCode(null);
			rtt.setAction(null);
			
			row.getColName().setValue(null);
			row.setColNatCode(null);
			row.setColExtCode(null);
			row.setColAction(null);
			rowValue.setRTTEvent(rtt);
			row.setValue(rowValue);
		}
		//Name
		if (column == 2)
		{
			row.setColNatCode(((RTTEventVo)row.getColName().getValue()).getNationalCode());
			row.setColExtCode(((RTTEventVo)row.getColName().getValue()).getPASCode());
			row.setColAction(((RTTEventVo)row.getColName().getValue()).getAction());
			
			EventRTTEventVo rowValue = row.getValue();
			RTTEventVo rtt = rowValue.getRTTEvent();
			rtt.setName(((RTTEventVo) row.getColName().getValue()).getName());
			rtt.setNationalCode((String) row.getColNatCode());
			rtt.setPASCode((String) row.getColExtCode());
			rtt.setAction(row.getColAction());
			RTTEventVo eventByPASCode = domain.getRTTEventByPASCode(rtt.getPASCode());
			rtt.setID_RTTEvent(eventByPASCode.getID_RTTEvent());
			rowValue.setRTTEvent(rtt);
			row.setValue(rowValue);
		}
		
	}

	protected void onLnkReturnClick() throws PresentationLogicException 
	{
		form.getGlobalContext().Pathway.setSelectedEventLiteVo(null);
		engine.open(form.getForms().Pathways.EventSearchAndSelect);
	}

	protected void onCmbEventTypeValueChanged() throws PresentationLogicException
	{
		updateContextMenuState();
		if(form.cmbEventType().getValue() != null)
		{
			if(form.cmbEventType().getValue().equals(EventCreationType.INTERNAL))
			{
				form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().clear();
				form.lyrTargets().tabRTT().grdRTT().getRows().clear();
			}
		}
		
		updateChkScheduleControlState();
	}

	@Override
	protected void onCmbStatusValueChanged() throws PresentationLogicException 
	{
		//updateContextMenuState();
		/*if (form.lyrTargets().tabExternalMappings().isVisible())
		{  /* WDEV-7027 - doesn't matter what is visible, should prompt anyway! */
			if (form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().size() > 0)
			{
				if (form.cmbStatus().getValue() != null && form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.INACTIVE))
				{
					engine.showMessage("If you wish to inactivate the Event, All Events Mappingss must be inactivated also\nSet all Events Mappings  to 'Inactive' now","Confirmation", MessageButtons.YESNO, MessageIcon.QUESTION);
				}
			}
			
		/*}
		if (form.lyrTargets().tabRTT().isVisible())
		{*/
			if (form.lyrTargets().tabRTT().grdRTT().getRows().size() > 0)
			{
				if (form.cmbStatus().getValue() != null && form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.INACTIVE))
				{
					engine.showMessage("If you wish to inactivate the Event, All RTT Events must be inactivated also\nSet all RTT Events  to 'Inactive' now","Confirmation", MessageButtons.YESNO, MessageIcon.QUESTION);
				}
			}
		/*}*/
	}

	private boolean checkRTTChilds() 
	{
		if (form.cmbStatus().getValue() != null)
		{
				if (form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.INACTIVE))
				{
					boolean found = false;
					for (int i = 0; i < form.lyrTargets().tabRTT().grdRTT().getRows().size(); i++) 
					{
						if (form.lyrTargets().tabRTT().grdRTT().getRows().get(i).getColStatus() != null &&
								(form.lyrTargets().tabRTT().grdRTT().getRows().get(i).getColStatus().equals(PreActiveActiveInactiveStatus.PREACTIVE) ||
										form.lyrTargets().tabRTT().grdRTT().getRows().get(i).getColStatus().equals(PreActiveActiveInactiveStatus.ACTIVE)))		
						{
							found = true;
							form.lyrTargets().tabRTT().grdRTT().getRows().get(i).setBackColor(Color.Red);
							form.lyrTargets().tabRTT().grdRTT().getRows().get(i).setColStatusReadOnly(false);
						}
					}
					if(found)
					{
						form.lyrTargets().showtabRTT();
						engine.showMessage("All RTT Events should be Inactive when inactivating the Event");
						return true;
					}
				}
			
				if (form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.ACTIVE))
				{
					boolean found = false;
					for (int i = 0; i < form.lyrTargets().tabRTT().grdRTT().getRows().size(); i++) 
					{
						if (form.lyrTargets().tabRTT().grdRTT().getRows().get(i).getColStatus() != null &&
								form.lyrTargets().tabRTT().grdRTT().getRows().get(i).getColStatus().equals(PreActiveActiveInactiveStatus.PREACTIVE))
								
						{
							found = true;
							form.lyrTargets().tabRTT().grdRTT().getRows().get(i).setBackColor(Color.Red);
							form.lyrTargets().tabRTT().grdRTT().getRows().get(i).setColStatusReadOnly(false);
						}
					}
					if(found)
					{
						form.lyrTargets().showtabRTT();
						engine.showMessage("All RTT /Events should be Active or Inactive when activating the Event");
						return true;
					}
				}
				if (form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.PREACTIVE))
				{
					for (int i = 0; i <form.lyrTargets().tabRTT().grdRTT().getRows().size(); i++) 
					{
						form.lyrTargets().tabRTT().grdRTT().getRows().get(i).setBackColor(null);
						form.lyrTargets().tabRTT().grdRTT().getRows().get(i).setColStatusReadOnly(false);
					}
				}
		}
		
		return false;
	}

	private boolean checkExternalMappingsChilds()
	{
		if (form.cmbStatus().getValue() != null)
		{		
				if (form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows() != null)
				{
					if (form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.INACTIVE))
					{
						boolean found = false;
						for (int i = 0; i < form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().size(); i++) 
						{
							if (form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).getColStatus() != null &&
									(form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).getColStatus().equals(PreActiveActiveInactiveStatus.PREACTIVE) ||
									form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).getColStatus().equals(PreActiveActiveInactiveStatus.ACTIVE)))
									
							{
								found = true;
								form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).setBackColor(Color.Red);
								form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).setColStatusReadOnly(false);
							}
						}
						if(found)
						{
							form.lyrTargets().showtabExternalMappings();
							engine.showMessage("All Events Mappings should be Inactive when inactivating the Event");
							return true;
						}
					}
					
					if (form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.ACTIVE))
					{
						boolean found = false;
						for (int i = 0; i < form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().size(); i++) 
						{
							if (form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).getColStatus() != null &&
									form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).getColStatus().equals(PreActiveActiveInactiveStatus.PREACTIVE))
									
							{
								found = true;
								form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).setBackColor(Color.Red);
								form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).setColStatusReadOnly(false);
							}
						}
						if(found)
						{
							form.lyrTargets().showtabExternalMappings();
							engine.showMessage("All Events Mappings should be Active or Inactive when activating the Event");
							return true;
						}
					}
					if (form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.PREACTIVE))
					{
						for (int i = 0; i < form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().size(); i++) 
						{
							form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).setBackColor(null);
							form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).setColStatusReadOnly(false);
						}
					}
				}
		}	
		
		return false;
	}
	
	private boolean checkEventTargetChilds()
	{
		if (form.cmbStatus().getValue() != null)
		{		
				if (form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getRows() != null)
				{
					if (form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.ACTIVE))
					{
						boolean found = false;
						for (int i = 0; i < form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getRows().size(); i++) 
						{
							if (form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getRows().get(i).getCells().get(getColumn(COLSTATUS)) != null &&
									(form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getRows().get(i).getCells().get(getColumn(COLSTATUS)).getValue().equals(PreActiveActiveInactiveStatus.PREACTIVE)))
									
							{
								found = true;
								form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getRows().get(i).setBackColor(Color.Red);
								form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getRows().get(i).getCells().get(getColumn(COLSTATUS)).setReadOnly(false);
							}
						}
						if(found)
						{
							form.lyrTargets().showtabEventTargets();
							engine.showMessage(form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getSelectedRow().getCells().get(getColumn(COLNAME)).getValue() +
									" is currently pre-active. Please change the Event Target status or activate the Target via Target Configuration");
							return true;
						}
					}
					
				}	
		}	
		
		return false;
	}
	
	@Override
	protected void onGrdExternalEventMappingsGridComboBoxSelectionChanged(
			int column, grdExternalEventMappingsRow row, Object value)
				throws PresentationLogicException 
	{
		if (form.cmbStatus().getValue() != null &&
				form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.ACTIVE))
		{
			if (value != null)
			{
				if (value.equals(PreActiveActiveInactiveStatus.ACTIVE) ||
						value.equals(PreActiveActiveInactiveStatus.INACTIVE))
				{
					row.setBackColor(null);
				}
				else if (value.equals(PreActiveActiveInactiveStatus.PREACTIVE))
				{
					checkExternalMappingsChilds();
					checkRTTChilds();
				}
			}
			else
			{
				engine.showMessage("Status is mandatory!");
				row.setColStatus(PreActiveActiveInactiveStatus.ACTIVE);
			}
		}
	}

	@Override
	protected void onMessageBoxClosed(int messageBoxId, DialogResult result)
			throws PresentationLogicException 
	{
		if (result.equals(DialogResult.YES))
		{
			for (int i = 0; i < form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().size(); i++) 
			{
				if (form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).getColStatus().equals(PreActiveActiveInactiveStatus.PREACTIVE) ||
						form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).getColStatus().equals(PreActiveActiveInactiveStatus.ACTIVE))
				{	
					form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).setBackColor(null);
					form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).setColStatus(PreActiveActiveInactiveStatus.INACTIVE);
					form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().get(i).setColStatusReadOnly(true);
				}
			}
			for (int i = 0; i < form.lyrTargets().tabRTT().grdRTT().getRows().size(); i++) 
			{
				if (form.lyrTargets().tabRTT().grdRTT().getRows().get(i).getColStatus().equals(PreActiveActiveInactiveStatus.PREACTIVE) ||
						form.lyrTargets().tabRTT().grdRTT().getRows().get(i).getColStatus().equals(PreActiveActiveInactiveStatus.ACTIVE))
				{	
					form.lyrTargets().tabRTT().grdRTT().getRows().get(i).setBackColor(null);
					form.lyrTargets().tabRTT().grdRTT().getRows().get(i).setColStatus(PreActiveActiveInactiveStatus.INACTIVE);
					form.lyrTargets().tabRTT().grdRTT().getRows().get(i).setColStatusReadOnly(true);
				}
			}
			
			// wdev-7027 switch to the tab that has the items i.e. if external mappings visible, but rtt has been updated
			// now show rtt tab
			if (form.lyrTargets().tabExternalMappings().grdExternalEventMappings().getRows().size() > 0 && !form.lyrTargets().tabEventTargets().isVisible())
			{
				form.lyrTargets().tabEventTargets().setVisible(true);
			}
			else if (form.lyrTargets().tabRTT().grdRTT().getRows().size() > 0 && !form.lyrTargets().tabRTT().isVisible())
			{
				form.lyrTargets().tabRTT().setVisible(true);
			}
		}
	}

	@Override
	protected void onGrdRTTGridComboBoxSelectionChanged(int column,
			grdRTTRow row, Object value) throws PresentationLogicException 
	{
		if (form.cmbStatus().getValue() != null &&
				form.cmbStatus().getValue().equals(PreActiveActiveInactiveStatus.ACTIVE))
		{
			if (value != null)
			{
				if (value.equals(PreActiveActiveInactiveStatus.ACTIVE) ||
						value.equals(PreActiveActiveInactiveStatus.INACTIVE))
				{
					row.setBackColor(null);
				}
				else if (value.equals(PreActiveActiveInactiveStatus.PREACTIVE))
				{
					checkExternalMappingsChilds();
					checkRTTChilds();
				}
			}
			else
			{
				engine.showMessage("Status is mandatory!");
				row.setColStatus(PreActiveActiveInactiveStatus.ACTIVE);
			}
		}
	}

	@Override
	protected void onChkStartsClockValueChanged() throws PresentationLogicException 
	{
		if (form.chkStartsClock().getValue())
		{
			form.chkStopClocks().setValue(false);
		}
	}

	@Override
	protected void onChkStopClocksValueChanged() throws PresentationLogicException 
	{
		if (form.chkStopClocks().getValue())
		{
			form.chkStartsClock().setValue(false);
		}	
	}

	@Override protected void onDyngrdEventTargetManagementCellValueChanged(DynamicGridCell cell)
	{
		DynamicGridColumn colSetToInscope = cell.getColumn().equals(form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getColumns().getByIdentifier(COLSETTOINSCOPE)) ? cell.getColumn() : null;
		if(colSetToInscope != null)
		{
			if(cell.getValue().equals(Boolean.TRUE))
			{
				//get the journeytarget cell and check its value
				//if it is null set it to inscope
				DynamicGridCell cellJourneyTarg = cell.getRow().getCells().get(form.lyrTargets().tabEventTargets().dyngrdEventTargetManagement().getColumns().getByIdentifier(COLJOURNEYTARGET));
				if(cellJourneyTarg.getValue() == null)
					cellJourneyTarg.setValue(PatientTargetStatus.INSCOPE);	
			}
		}
	}

	@Override
	protected void onChkEndsPathwayValueChanged() throws PresentationLogicException
	{
		if(!form.chkEndsPathway().getValue())
		{
			form.cmbType().setValue(null);
			form.cmbReason().setValue(null);
		}
		
		updateCancellationControls();
	}

	private void updateCancellationControls()
	{
		form.cmbType().setVisible(form.chkEndsPathway().getValue());
		form.cmbType().setEnabled(FormMode.EDIT.equals(form.getMode()));
		form.cmbReason().setVisible(form.chkEndsPathway().getValue());
		form.cmbReason().setEnabled(FormMode.EDIT.equals(form.getMode()));
		form.lblCancellationType().setVisible(form.chkEndsPathway().getValue());
		form.lblCancellationReason().setVisible(form.chkEndsPathway().getValue());
	}

	@Override
	protected void onCmbTypeValueChanged() throws PresentationLogicException
	{
		form.cmbReason().setValue(null);
	}
}
