gt: 			if (row.getValue().getCareContextIsNotNull() &&
context: //#############################################################################
  //#                                                                           #
  //#  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.62 build 2929.28798)
  // Copyright (C) 1995-2008 IMS MAXIMS plc. All rights reserved.
  
  package ims.clinical.forms.demoreferrallist;
  
  import java.util.ArrayList;
  import ims.clinical.forms.demoreferrallist.GenForm.grdDetailsRow;
  import ims.clinical.vo.ReferralsRecordingListVo;
  import ims.clinical.vo.ReferralsRecordingListVoCollection;
  import ims.core.vo.MemberOfStaffLiteVoCollection;
  import ims.core.vo.PatientShort;
  import ims.framework.enumerations.FormMode;
  import ims.framework.exceptions.PresentationLogicException;
  import ims.framework.utils.Date;
  
  public class Logic extends BaseLogic
  {
  	private static final long serialVersionUID = 1L;
  
  	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
  	{
  		initialize();
  		updateContextMenu();
  	}
  	
  	private String[] getUiErrors(String error) 
  	{
  		ArrayList<String> errors = new ArrayList<String>();
  		
  		if (error != null)
  		{
  			errors.add(error);
  		}
  		
  		if (form.cmbSpecialty().getValue() == null)
  		{
  			errors.add("Specialty is mandatory");
  		}
  		
  		if (form.dteFrom().getValue() == null ||
  				form.dteTo().getValue() == null)
  		{
  			errors.add("DateFrom and DateTo are mandatory");
  		}
  		
  		if (form.dteFrom().getValue() != null &&
  				form.dteTo().getValue() != null &&
  				(form.dteFrom().getValue().isGreaterThan(form.dteTo().getValue())))
  		{
  			errors.add("DateTo must be greater than DateFrom");
  		}
  			
  		if(errors.size() > 0)
  		{
  			String[] searchErrors = new String[errors.size()];
  			errors.toArray(searchErrors);
  		
  			return searchErrors;
  		}
  		
  		return null;
  	}
  	
  	private void initialize()
  	{
  		form.dteFrom().setValue(new Date());
  		form.dteTo().setValue(new Date());
  	}
  	
  	protected void onImbClearClick() throws ims.framework.exceptions.PresentationLogicException
  	{
  		form.grdDetails().getRows().clear();
  		clear();
  		
  		//Clear the selected patient information in the Engine
  		form.getGlobalContext().Core.setPatientShort(null);		
  		engine.clearAlerts();
  		
  		form.grdDetails().setFooterValue(form.grdDetails().getRows() != null ? "Total : " + form.grdDetails().getRows().size() : "Total : 0" );	
  	}
  	
  	protected void onImbSearchClick() throws ims.framework.exceptions.PresentationLogicException
  	{
  		String[] errors = getUiErrors(null);
  		if (errors != null)
  		{
  			engine.showErrors(errors);
  			return;
  		}
  		
  		ReferralsRecordingListVoCollection voColl = search(createFilter(new ReferralsRecordingListVo()));
  		if (voColl == null ||
  				(voColl != null &&
  						voColl.size() == 0))
  		{
  			String[] err = getUiErrors("No record(s) found!");
  			engine.showErrors(err);
  			return;
  		}
  		else
  		{
  			populateGridDetails(voColl);
  		}
  		form.grdDetails().setFooterValue(form.grdDetails().getRows() != null ? "Total : " + form.grdDetails().getRows().size() : "Total : 0" );
  	}
  	
  	private void populateGridDetails(ReferralsRecordingListVoCollection voColl)
  	{
  		form.grdDetails().getRows().clear();
  		for (int i = 0; i < voColl.size(); i++)
  		{
  			grdDetailsRow row = form.grdDetails().getRows().newRow();
  			if (voColl.get(i).getCareContextIsNotNull() &&
  					voColl.get(i).getCareContext().getEpisodeOfCareIsNotNull() &&
  						voColl.get(i).getCareContext().getEpisodeOfCare().getCareSpellIsNotNull() &&
  							voColl.get(i).getCareContext().getEpisodeOfCare().getCareSpell().getPatientIsNotNull())
  			{
  				PatientShort patient = voColl.get(i).getCareContext().getEpisodeOfCare().getCareSpell().getPatient();
  				if (patient.getNameIsNotNull())
  				{
  					row.setColSurname(patient.getName().getSurname());
  					row.setTooltipForColSurname(patient.getName().getSurname());
  					row.setColForename(patient.getName().getForename());
  					row.setTooltipForColForename(patient.getName().getForename());
  				}
  				row.setColSex(patient.getSex() != null? patient.getSex().getText() : "Unknown");
  				row.setTooltipForColSex(patient.getSex() != null ? patient.getSex().getText() : "Unknown");
  				row.setColDOB(patient.getDob());
  				row.setTooltipForColDOB(patient.getDobIsNotNull() ?	patient.getDob().toString() : null);
  				row.setColHospNo(patient.getHospnum() != null ?	patient.getHospnum().getValue() : null);
  				row.setTooltipForColHospNo(patient.getHospnum() != null ? patient.getHospnum().getValue() : null);
  			}
  			
  			row.setColDiscipline(voColl.get(i).getDisciplineIsNotNull() ? voColl.get(i).getDiscipline().toString() : null);
  			row.setTooltipForColDiscipline(voColl.get(i).getDisciplineIsNotNull() ? voColl.get(i).getDiscipline().toString() : null);
  			row.setColStatus(voColl.get(i).getReferralStatusIsNotNull() ?	voColl.get(i).getReferralStatus().toString() : null);
  			row.setTooltipForColStatus(voColl.get(i).getReferralStatusIsNotNull() ?	voColl.get(i).getReferralStatus().toString() : null);			
  			row.setColSourceOfReferral(voColl.get(i).getReferralSourceIsNotNull() ? voColl.get(i).getReferralSource().toString() : null);
  			row.setTooltipForColSourceOfReferral(voColl.get(i).getReferralSourceIsNotNull() ? voColl.get(i).getReferralSource().toString() : null);			
  			row.setColReferredTo(voColl.get(i).getReferralHCPIsNotNull() ? (voColl.get(i).getReferralHCP().getNameIsNotNull() ? voColl.get(i).getReferralHCP().getName().toString() : null) : null);
  			row.setTooltipForColSourceOfReferral(voColl.get(i).getReferralHCPIsNotNull() ? (voColl.get(i).getReferralHCP().getNameIsNotNull() ? voColl.get(i).getReferralHCP().getName().toString() : null) : null);
  			row.setColDateSent(voColl.get(i).getDateSent());
  			row.setTooltipForColDateSent(voColl.get(i).getDateSentIsNotNull() ?	voColl.get(i).getDateSent().toString() : null);
  			row.setValue(voColl.get(i));
  		}
  	}
  
  	private ReferralsRecordingListVoCollection search(ReferralsRecordingListVo filter)
  	{
  		Date dateFrom = form.dteFrom().getValue();
  		Date dateTo = form.dteTo().getValue();
  		return domain.listByCriteria(filter, dateFrom, dateTo);
  	}
  
  	private ReferralsRecordingListVo createFilter(ReferralsRecordingListVo filter)
  	{
  		filter.setSpecialtyFunctionCode(form.cmbSpecialty().getValue());
  		filter.setDiscipline(form.cmbDiscipline().getValue());
  		filter.setReferralStatus(form.cmbStatus().getValue());
  		filter.setReferralSource(form.cmbReferal().getValue());
  		filter.setReferralHCP(form.qmbReferredTo().getValue());
  		return filter;
  	}
  
  	protected void onQmbReferredToTextSubmited(String value) throws ims.framework.exceptions.PresentationLogicException
  	{
  		form.qmbReferredTo().clear();
  		
  		MemberOfStaffLiteVoCollection voCollMOSs = listMOSs(value);
  		if(voCollMOSs == null || voCollMOSs.size() == 0)
  		{
  			engine.showMessage("No MOS found matching the search criteria");
  			return;
  		}
  		
  		for(int i=0;i<voCollMOSs.size();i++)
  		{
  			form.qmbReferredTo().newRow(voCollMOSs.get(i), voCollMOSs.get(i).toString());
  		}
  		
  		if(voCollMOSs.size() == 1)
  		{
  			form.qmbReferredTo().setValue(voCollMOSs.get(0));
  		}
  		else
  		{
  			form.qmbReferredTo().showOpened();
  		}
  	}
  	
  	private void clear()
  	{
  		form.cmbSpecialty().setValue(null);
  		form.cmbDiscipline().setValue(null);
  		form.cmbStatus().setValue(null);
  		form.cmbReferal().setValue(null);
  		form.qmbReferredTo().setValue(null);
  		form.dteFrom().setValue(new Date());
  		form.dteTo().setValue(new Date());
  	}
  	
  	private MemberOfStaffLiteVoCollection listMOSs(String name) 
  	{
  		return domain.listActiveMosByName(name);
  	}
  	
  	
  	protected void onContextMenuItemClick(int menuItemID, ims.framework.Control sender) throws ims.framework.exceptions.PresentationLogicException
  	{
  		switch (menuItemID)
  		{
  			case GenForm.ContextMenus.ClinicalNamespace.ReferralList.Edit :
  				editRecord();;
  				break;
  			
  			case GenForm.ContextMenus.ClinicalNamespace.ReferralList.View :
  				viewRecord();;
  				break;
  						
  			default :
  		}
  	}
  
  	private void viewRecord()
  	{
  		engine.open(form.getForms().Clinical.DemoreferralDetails, new Object[] {FormMode.VIEW});
  	}
  
  	private void editRecord()
  	{
  		engine.open(form.getForms().Clinical.DemoreferralDetails, new Object[] {FormMode.EDIT});
  	}
  
  	private void updateContextMenu()
  	{
  		form.getContextMenus().Clinical.getReferralListEditItem().setVisible(form.grdDetails().getSelectedRowIndex() > -1);
  		form.getContextMenus().Clinical.getReferralListViewItem().setVisible(form.grdDetails().getSelectedRowIndex() > -1);
  	}
  	
  	@Override
  	protected void onGrdDetailsSelectionChanged() throws PresentationLogicException
  	{
  		grdDetailsRow row = form.grdDetails().getSelectedRow();
  
  		if(row != null &&
  				row.getValue() != null)
  		{			
  