/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/forms/propertychecklist/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.45 build 2421.18554)
// Copyright (C) 1995-2006 IMS MAXIMS plc. All rights reserved.

package ims.nursing.forms.propertychecklist;

import java.util.ArrayList;

import ims.configuration.gen.ConfigFlag;
import ims.core.vo.AuthoringInformationVo;
import ims.core.vo.HcpLiteVo;
import ims.core.vo.LocShortMappingsVo;
import ims.core.vo.LocShortMappingsVoCollection;
import ims.core.vo.LocShortVo;
import ims.core.vo.enums.AuthoringLabelType;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.Control;
import ims.framework.enumerations.FormMode;
import ims.framework.exceptions.PresentationLogicException;
import ims.nursing.forms.propertychecklist.GenForm.ctnDetailsContainer.grdItemsRow;
import ims.nursing.forms.propertychecklist.GenForm.ctnDetailsContainer.grdValuablesRow;
import ims.nursing.forms.propertychecklist.GenForm.grdListRow;
import ims.nursing.vo.PropertyChecklistShortVo;
import ims.nursing.vo.PropertyChecklistShortVoCollection;
import ims.nursing.vo.PropertyChecklistVo;
import ims.nursing.vo.PropertyItemVo;
import ims.nursing.vo.PropertyItemVoCollection;
import ims.nursing.vo.ValuableVo;
import ims.nursing.vo.ValuableVoCollection;
import ims.nursing.vo.lookups.LookupHelper;
import ims.nursing.vo.lookups.MovementType;
import ims.nursing.vo.lookups.MovementTypeCollection;
import ims.nursing.vo.lookups.PropertyItemCollection;
import ims.nursing.vo.lookups.PropertyItemStatus;
import ims.nursing.vo.lookups.PropertyItemStatusCollection;
import ims.nursing.vo.lookups.ValuableCollection;

public class Logic extends BaseLogic
{
	private static final long serialVersionUID = 1L;
		
	protected void onFormOpen() throws ims.framework.exceptions.PresentationLogicException
	{
		initialize();
		open();
	}
	
	private void initialize() throws PresentationLogicException 
	{	
		prepopuateGrdItems();
		prepopuateGrdValuables();
		
		form.ctnDetails().customConfirmingHCP().setLabels(AuthoringLabelType.CONFIRMED);
		setContextMenuState();
		updateControlState();
	
		form.ctnDetails().customAuthoringHCP().setIsRequiredPropertyToControls(true);
	
	
	}

	private void open() 
	{
		form.setMode(FormMode.VIEW);		
		form.grdList().getRows().clear();
		
		PropertyChecklistShortVoCollection list = domain.list(form.getGlobalContext().Core.getCurrentCareContext());
		
		if(list != null)
		{
			for(int i = 0; i < list.size(); i++)
			{
				addRowRecord(list.get(i));
			}
		}		
		//set selection back
		if (form.getLocalContext().getEditedRecordIsNotNull())
		{
			//need to get timestamp from record retrieved from List call.
			GenForm.grdListRow row = form.grdList().getRowByValue(form.getLocalContext().getEditedRecord());
			if (row != null && row.getValue() != null)
			{			
				getAndDisplayRecord();
			}
			else
				clear();
			updateControlState();			
		}
		else		
			form.ctnDetails().setCollapsed(true);

		updateControlState();			
	}
	
	private void addRowRecord(PropertyChecklistShortVo record)
	{	
		if(record == null)
			return;
		
		GenForm.grdListRow row = form.grdList().getRows().newRow(record.equals
				(form.getLocalContext().getEditedRecord()));
		
		row.setColType(record.getMovementType().getText()) ;
		LocShortVo recordWard = record.getWardIsNotNull() ? record.getWard() : null;
		row.setColWard(recordWard.getName());
		row.setColDate(record.getAuthoringDateTime().toString());
		HcpLiteVo recordHCP = record.getAuthoringHCPIsNotNull() ? record.getAuthoringHCP() : null;
		row.setColAuthoring(recordHCP.getMos().getName().toString());

		row.setValue(record);
	}
	
	private void setContextMenuState() 
	{
		form.getContextMenus().getGenericGridAddItem().setVisible(true);
		form.getContextMenus().getGenericGridAddItem().setText("New Entry");
		form.getContextMenus().getGenericGridUpdateItem().setVisible(form.grdList().getSelectedRowIndex() >= 0);
		form.getContextMenus().getGenericGridUpdateItem().setText("Edit Entry");
	}
	
	private void updateControlState()
	{
		if(form.getMode().equals(FormMode.VIEW))
		{
			form.btnUpdate().setVisible(true);
			form.btnUpdate().setEnabled(form.grdList().getSelectedRowIndex() >= 0);
		}
		form.ctnDetails().customConfirmingHCP().setIsRequiredPropertyToControls(true);
		
		
		updateContextMenuState();
	}
	
	private void updateContextMenuState()
	{
		if (form.getMode().equals(FormMode.VIEW)) 
		{
			form.getContextMenus().getGenericGridAddItem().setVisible(true);
			form.getContextMenus().getGenericGridUpdateItem().setVisible(form.grdList().getSelectedRowIndex() >= 0);
		}
		if (form.getMode().equals(FormMode.EDIT)) 
		{
			form.getContextMenus().getGenericGridAddItem().setVisible(false);
			form.getContextMenus().getGenericGridUpdateItem().setVisible(false);
		}
	}
	
	protected void onContextMenuItemClick(int menuItemID, Control sender) throws PresentationLogicException 
	{	
		switch (menuItemID) 
		{
			case GenForm.ContextMenus.GenericGrid.Add:
				newEntry();
				break;
			case GenForm.ContextMenus.GenericGrid.Update:
				updateEntry();
				break;
		}
	}

	protected void onFormModeChanged() 
	{
		getAndDisplayRecord(form.getLocalContext().getEditedRecord());
		updateControlState();
	}

	protected void onQmbDestWardTextSubmited(String value) throws PresentationLogicException 
	{
		form.ctnDetails().qmbDestWard().clear();
		LocShortMappingsVoCollection wards = domain.listWards(value);
		
		if (wards != null)
		{
			for (int i = 0; i < wards.size(); i++) 
			{
				form.ctnDetails().qmbDestWard().newRow(wards.get(i), wards.get(i).getName());
			}

			if (form.ctnDetails().qmbDestWard().getValues().size() == 1) 
			{
				form.ctnDetails().qmbDestWard().setValue( (LocShortMappingsVo) form.ctnDetails().qmbWard().getValues().get(0));
			}

			else if (form.ctnDetails().qmbDestWard().getValues().size() > 1) 
			{
				form.ctnDetails().qmbDestWard().showOpened();
			}
		}
	}

	protected void onQmbWardTextSubmited(String value) throws PresentationLogicException 
	{
		form.ctnDetails().qmbWard().clear();
		LocShortMappingsVoCollection wards = domain.listWards(value);
		
		if(wards != null)
		{
			for (int i = 0; i < wards.size(); i++)
			{
				form.ctnDetails().qmbWard().newRow(wards.get(i), wards.get(i).getName());
			}
		
			if(form.ctnDetails().qmbWard().getValues().size() == 1)
			{
				form.ctnDetails().qmbWard().setValue( (LocShortMappingsVo) form.ctnDetails().qmbWard().getValues().get(0));
			}
		
			else if(form.ctnDetails().qmbWard().getValues().size() > 1)
			{
				form.ctnDetails().qmbWard().showOpened();
			}
		}
	}

	protected void onCmbMoveTypeValueChanged() throws PresentationLogicException 
	{
		if (form.ctnDetails().cmbMoveType().getValue() != null)
		{
			if (form.ctnDetails().cmbMoveType().getValue().equals(MovementType.ADMISSION) || 
					form.ctnDetails().cmbMoveType().getValue().equals(MovementType.DISCHARGE))
			{
				showOnlyWard();
			}
			if (form.ctnDetails().cmbMoveType().getValue().equals(MovementType.TRANSFER))
			{
				showWardDest();
			}
		}
		else
		{
			engine.showMessage("Select Admission, Transfer or Discharge");
			form.ctnDetails().cmbMoveType().setValue(MovementType.TRANSFER);
			showWardDest();	
		}
	}

	private void showOnlyWard() 
	{
		form.ctnDetails().qmbDestWard().setEnabled(false);
		form.ctnDetails().setcustomConfirmingHCPEnabled(false);
		form.ctnDetails().qmbDestWard().setValue(null);
		form.ctnDetails().chkConfirm().setEnabled(false);
		form.ctnDetails().customConfirmingHCP().setValue(null);	
		form.ctnDetails().customConfirmingHCP().setIsRequiredPropertyToControls(Boolean.FALSE);
	}
	
	private void showWardDest() 
	{
		form.ctnDetails().qmbDestWard().setEnabled(true);
		form.ctnDetails().chkConfirm().setEnabled(true);
		
	}

	private void clear() 
	{
		form.ctnDetails().cmbMoveType().setValue(null);
		form.ctnDetails().qmbWard().setValue(null);
		form.ctnDetails().customAuthoringHCP().setValue(null);
		form.ctnDetails().qmbDestWard().setValue(null);
		form.ctnDetails().customConfirmingHCP().setValue(null);
		form.ctnDetails().txtSafe().setValue("");
		form.ctnDetails().chkConfirm().setValue(false);		
		prepopuateGrdItems();
		prepopuateGrdValuables();
	}

	protected void onBtnSaveClick() throws PresentationLogicException 
	{
		if (save())
			open();
	}

	protected void onBtnCancelClick() throws PresentationLogicException 
	{
		form.setMode(FormMode.VIEW);
		getAndDisplayRecord();
		updateControlState();
	}

	protected void onBtnUpdateClick() throws PresentationLogicException 
	{
		updateEntry();
	}
	
	protected void onBtnNewClick() throws PresentationLogicException 
	{
		if(allowNew())
			newEntry();	
	}	

	private boolean allowNew()
	{
		StringBuffer exceptions = new StringBuffer();	
		if (ims.nursing.vo.lookups.LookupHelper.getPropertyItem(domain.getLookupService()).size() == 0)
		{
			exceptions.append("Please configure PropertyItem lookup");
			exceptions.append("\n");
		}
		if (ims.nursing.vo.lookups.LookupHelper.getPropertyItemStatus(domain.getLookupService()).size() == 0)
		{
			exceptions.append("Please configure PropertyItemStatus lookup");
			exceptions.append("\n");
		}
		
		if (ims.nursing.vo.lookups.LookupHelper.getValuable(domain.getLookupService()).size() == 0)
		{
			exceptions.append("Please configure Valuable lookup");
			exceptions.append("\n");
		}
		
		if(exceptions.length() > 0)
		{
			engine.showMessage(exceptions.toString());
			return false;
		}
		return true;
	}

	private void newEntry() 
	{
		form.setMode(FormMode.EDIT);
		form.ctnDetails().cmbMoveType().setEnabled(true);
		populateMoveTypeCombo();
		form.getLocalContext().setEditedRecord(null);
		clear();
		form.ctnDetails().customAuthoringHCP().initializeComponent();
		form.ctnDetails().customAuthoringHCP().setEnabledAuthoringHCP(Boolean.TRUE);
		form.ctnDetails().customAuthoringHCP().setEnabledDateTime(Boolean.TRUE);
		form.ctnDetails().customConfirmingHCP().setValue(null);	
		updateControlState();
		form.ctnDetails().setCollapsed(false);
	}

	private void populateMoveTypeCombo()
	{
		form.ctnDetails().cmbMoveType().clear();
		MovementTypeCollection lookupCollection = LookupHelper.getMovementType(domain.getLookupService());
		
		if (lookupCollection != null)
		{
			for(int x = 0; x < lookupCollection.size(); x++)
			{	 
				if(!isMoveTypeConfigured(lookupCollection.get(x))) 
				{
					form.ctnDetails().cmbMoveType().newRow(lookupCollection.get(x), lookupCollection.get(x).getText(), 
						lookupCollection.get(x).getImage(), lookupCollection.get(x).getTextColor());
				}
			}
		}
	}
	
	private boolean isMoveTypeConfigured(MovementType type) 
	{		
		for(int x = 0; x < form.grdList().getRows().size(); x++)
		{
			PropertyChecklistShortVo value = form.grdList().getRows().get(x).getValue();
			
			if(value != null && 
					value.getMovementType() != null && 
						value.getMovementType().equals(type) &&
							!value.getMovementType().equals(MovementType.TRANSFER))
			{
				return true;
			}
		}
		return false;
	}

	private void updateEntry() 
	{
		form.setMode(FormMode.EDIT);
		form.ctnDetails().customAuthoringHCP().setEnabledAuthoringHCP(Boolean.FALSE);
		form.ctnDetails().customAuthoringHCP().setEnabledDateTime(Boolean.FALSE);
		form.ctnDetails().cmbMoveType().setEnabled(false);
		if (form.grdList().getSelectedRowIndex() >=0)
			if (!(form.ctnDetails().cmbMoveType().getValue().equals(MovementType.ADMISSION) || 
					form.ctnDetails().cmbMoveType().getValue().equals(MovementType.DISCHARGE)))
			{
				showWardDest();
			}
		
		int size = form.ctnDetails().grdItems().getRows().size();
		if(size > 0)
		{
			form.ctnDetails().grdItems().setValue(form.ctnDetails().grdItems().getRows().get(size-1).getValue());
		}
		
		updateControlState();
	}
	
	private void prepopuateGrdItems()
	{
		form.ctnDetails().grdItems().getRows().clear();
		
		PropertyItemCollection coll = LookupHelper.getPropertyItem(domain.getLookupService());
		
		if (coll != null)
		{
			for (int i = 0; i < coll.size(); i++) 
			{
				grdItemsRow row = form.ctnDetails().grdItems().getRows().newRow();
				row.setColItem(coll.get(i));
				bindColSelect(row);
			}
		}
	}
	
	private void prepopuateGrdValuables()
	{
		form.ctnDetails().grdValuables().getRows().clear();
		
		ValuableCollection coll = LookupHelper.getValuable(domain.getLookupService());
		
		if (coll != null)
		{
			for (int i = 0; i < coll.size(); i++) 
			{
				grdValuablesRow row = form.ctnDetails().grdValuables().getRows().newRow();
				row.setcolValuable(coll.get(i));
			}
		}
	}
	
	private boolean save() 
	{
		PropertyChecklistVo record = populateDataFromScreen(form.getLocalContext().getEditedRecord());
		
	
		
		
		
		String[] uiErrors = getUiErrors();
		String[] errors = record.validate(uiErrors);
		if(errors != null && errors.length > 0)
		{
			engine.showErrors(errors);
			
			return false;
		}
		try
		{
			record = domain.save(record);
		}
		catch(StaleObjectException e)
		{
			engine.showMessage(ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
			PropertyChecklistVo updatedRecord = getAndDisplayRecord(record);
			updateRowRecord(updatedRecord);
			
			return false;
		}
		form.getLocalContext().setEditedRecord(record);
		
		return true;
	}
	
	private boolean ifMovTypeIsTransfer() 
	{
		if (form.ctnDetails().cmbMoveType().getValue() != null)
		{
			if (form.ctnDetails().cmbMoveType().getValue().equals(MovementType.TRANSFER))
			{		
				//form.ctnDetails().qmbDestWard().setRequired(true);
				
				return true;
			}
		}
		return false;
	}
	
	private String[] getUiErrors() 
	{
		ArrayList errors = new ArrayList();
		if (ifMovTypeIsTransfer()) 
		{
			// WDEV-2276 
			if (form.ctnDetails().chkConfirm().getValue() == true)
			{	
		
				if(form.ctnDetails().customConfirmingHCP().getValue() == null)
				{
					
					errors.add("The confirmation HCP, Date/Time data are mandatory");
					
					
				}
				
				
				if (form.ctnDetails().customConfirmingHCP().getValue() != null) 
				{
					if (form.ctnDetails().customConfirmingHCP().getValue().getAuthoringHcp() == null) 
					{
						errors.add("Confirmed Hcp is mandatory");
					}
					if (form.ctnDetails().customConfirmingHCP().getValue().getAuthoringDateTime() == null) 
					{
						errors.add("Confirmed Date/Time is mandatory");
					}
				}
			}
			if (form.ctnDetails().qmbDestWard().getValue() == null) 
			{
				errors.add("Ward (Destination) is mandatory");
			}
			
			
			
			
			
		}
		if (errors.size() > 0) 
		{
			String[] searchErrors = new String[errors.size()];
			errors.toArray(searchErrors);
			engine.showErrors("Invalid Property Checklist Record", searchErrors);
			return searchErrors;
		}
		return null;
	}
	
	private void updateRowRecord(PropertyChecklistVo record) 
	{
		if (record == null)
			return;

		for (int j = 0; j < form.grdList().getRows().size(); j++) 
		{
			PropertyChecklistShortVo existingRecord = form.grdList().getRows().get(j).getValue();
			if (existingRecord != null && existingRecord.equals(record)) 
			{
				updateRowRecord(form.grdList().getRows().get(j), record);
				
				break;
			}
		}

		if (form.ctnDetails().cmbMoveType().getValue().equals(MovementType.TRANSFER))
		{
			showWardDest();
		}
		else
		{
			showOnlyWard();
		}
	}
	
	private void updateRowRecord(grdListRow row, PropertyChecklistVo record) 
	{
		if (row == null || record == null)
			return;

		row.setColType(record.getMovementType().toString());
		row.setColWard(record.getWard().toString());
		row.setColDate(record.getAuthoringDateTime().toString());
		row.setColAuthoring(record.getAuthoringHCP().toString());
		row.setValue(record);	
	}
	
	private PropertyChecklistVo getAndDisplayRecord()
	{
		return getAndDisplayRecord(form.grdList().getValue());
	}
	
	private PropertyChecklistVo getAndDisplayRecord(PropertyChecklistShortVo refRecord) 
	{
		clear();
		if(refRecord == null)
			return null;
		
		PropertyChecklistVo record = domain.get(refRecord);
		form.getLocalContext().setEditedRecord(record);
		
		populateScreenFromData(record);	
			
		return record;	
	}
	
	protected void populateScreenFromData(PropertyChecklistVo record)
	{
		if(record == null)
			return;

		form.ctnDetails().cmbMoveType().setValue(record.getMovementTypeIsNotNull() ? record.getMovementType() : null);
		
		LocShortMappingsVoCollection wards = domain.listWards(null);
		
		if(record.getWardIsNotNull())
		{
			for (int i = 0; i < wards.size(); i++)
			{
				LocShortMappingsVo item = wards.get(i);
			
				if(item.getID_Location().equals(record.getWard().getID_Location()))
				{
					form.ctnDetails().qmbWard().newRow(item, item.getName());
					form.ctnDetails().qmbWard().setValue(item);

					break;
				}
			}
		}
		
		AuthoringInformationVo recordAuthoring = new AuthoringInformationVo();
		recordAuthoring.setAuthoringDateTime(record.getAuthoringDateTime());
		recordAuthoring.setAuthoringHcp(record.getAuthoringHCP());
		form.ctnDetails().customAuthoringHCP().setValue(recordAuthoring);
		
		if(record.getDestinationWardIsNotNull())
		{
			for (int i = 0; i < wards.size(); i++)
			{
				LocShortMappingsVo itemDest = wards.get(i);
			
				if(itemDest.getID_Location().equals(record.getDestinationWard().getID_Location()))
				{
					form.ctnDetails().qmbDestWard().newRow(itemDest, itemDest.getName());
					form.ctnDetails().qmbDestWard().setValue(itemDest);
					break;
				}
			}
		}
		
		AuthoringInformationVo recordConfirmed = new AuthoringInformationVo();
		recordConfirmed.setAuthoringDateTime(record.getConfirmingDateTime());
		recordConfirmed.setAuthoringHcp(record.getConfirmingHCP());
		form.ctnDetails().customConfirmingHCP().setValue(recordConfirmed);
		
		if (record.getConfirmingDateTimeIsNotNull() && record.getConfirmingHCPIsNotNull())
		{
			form.ctnDetails().chkConfirm().setValue(true);
		}
		else
		{
			form.ctnDetails().chkConfirm().setValue(false);
		}
		
		PropertyItemVoCollection voCollPropertyItem = record.getItemsRetainedIsNotNull() ? record.getItemsRetained() : null;
		if(voCollPropertyItem != null)
		{	
			for(PropertyItemVo item : voCollPropertyItem)
			{
				//find entry in grid - if its not there lookup was made inactive so we add a row
				if(item != null)
				{
					boolean bItemFound = false;
					for(int i=0;i<form.ctnDetails().grdItems().getRows().size();i++)
					{
						grdItemsRow row = form.ctnDetails().grdItems().getRows().get(i);
						if(row.getColItem().equals(item.getItem()))
						{
							row.setColItem(item.getItem());
							bindColSelect(row);
							row.getColSelect().setValue(item.getStatus());
							if(item.getStatusIsNotNull() && row.getColSelect().getValue() == null)
							{
								row.getColSelect().newRow(item.getStatus(), item.getStatus().getText());
								row.getColSelect().setValue(item.getStatus());
							}
	
							row.setValue(item);
							bItemFound = true;
						}
					}
					if(bItemFound == false)
					{
						GenForm.ctnDetailsContainer.grdItemsRow rowGrdItems = form.ctnDetails().grdItems().getRows().newRow();
						rowGrdItems.setColItem(item.getItem());
						bindColSelect(rowGrdItems);
						rowGrdItems.getColSelect().setValue(item.getStatus());
						if(item.getStatusIsNotNull() && rowGrdItems.getColSelect().getValue() == null)
						{
							rowGrdItems.getColSelect().newRow(item.getStatus(), item.getStatus().getText());
							rowGrdItems.getColSelect().setValue(item.getStatus());
						}
						rowGrdItems.setValue(item);
					}
				}
			}
		}

		ValuableVoCollection voCollValuables = record.getValuablesIsNotNull() ? record.getValuables() : null;
		if(voCollValuables != null)
		{	
			for(ValuableVo item : voCollValuables)
			{
				//find entry in grid - if its not there lookup was made inactive so we add a row
				if(item != null)
				{
					boolean bItemFound = false;
					for(int i=0;i<form.ctnDetails().grdValuables().getRows().size();i++)
					{
						grdValuablesRow row = form.ctnDetails().grdValuables().getRows().get(i);
						if(row.getcolValuable().equals(item.getValuable()))
						{
							row.setcolValuable(item.getValuable());
							row.setcolHasValuable(item.getHasValuable());
							row.setValue(item);
							bItemFound = true;
						}
					}
					if(bItemFound == false)
					{
						grdValuablesRow row = form.ctnDetails().grdValuables().getRows().newRow();
						row.setcolValuable(item.getValuable());
						row.setcolHasValuable(item.getHasValuable());
						row.setValue(item);
					}
				}
			}
		}
		
		form.ctnDetails().txtSafe().setValue(record.getOtherValuablesIsNotNull() ? record.getOtherValuables() : null);	
	}
	
	private void bindColSelect(grdItemsRow row)
	{
		row.getColSelect().clear();
		
		PropertyItemStatusCollection collStatus = LookupHelper.getPropertyItemStatus(domain.getLookupService());
		if (collStatus != null)
		{
			for (int i = 0; i < collStatus.size(); i++) 
			{
				row.getColSelect().newRow(collStatus.get(i), collStatus.get(i).getText());
			}
		}
	}

	protected PropertyChecklistVo populateDataFromScreen(PropertyChecklistVo record)
	{
		if(record == null)
			record = new PropertyChecklistVo();
		
		record.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
		
		record.setMovementType(form.ctnDetails().cmbMoveType().getValue());
		
		record.setWard(form.ctnDetails().qmbWard().getValue());
		
		if (form.ctnDetails().customAuthoringHCP().getValue() !=null)
		{
			record.setAuthoringDateTime(form.ctnDetails().customAuthoringHCP().getValue().getAuthoringDateTime());
			record.setAuthoringHCP(form.ctnDetails().customAuthoringHCP().getValue().getAuthoringHcp());
		}
		
		record.setDestinationWard(form.ctnDetails().qmbDestWard().getValue());
		
		if (form.ctnDetails().customConfirmingHCP().getValue() !=null)
		{
			record.setConfirmingDateTime(form.ctnDetails().customConfirmingHCP().getValue().getAuthoringDateTime());
			record.setConfirmingHCP(form.ctnDetails().customConfirmingHCP().getValue().getAuthoringHcp());
		}
		
		record.setItemsRetained(new PropertyItemVoCollection());
		for(int x = 0; x < form.ctnDetails().grdItems().getRows().size(); x++)
		{
			PropertyItemVo rowValue = form.ctnDetails().grdItems().getRows().get(x).getValue();
			if(rowValue == null)
				rowValue = new PropertyItemVo();

			rowValue.setItem(this.form.ctnDetails().grdItems().getRows().get(x).getColItem());
			rowValue.setStatus((PropertyItemStatus) this.form.ctnDetails().grdItems().getRows().get(x).getColSelect().getValue());
			
			record.getItemsRetained().add(rowValue);
		}
		
		record.setValuables(new ValuableVoCollection());
		for(int x = 0; x < form.ctnDetails().grdValuables().getRows().size(); x++)
		{
			ValuableVo rowValue = form.ctnDetails().grdValuables().getRows().get(x).getValue();
			if(rowValue == null)
				rowValue = new ValuableVo();

			rowValue.setValuable(form.ctnDetails().grdValuables().getRows().get(x).getcolValuable());
			rowValue.setHasValuable(form.ctnDetails().grdValuables().getRows().get(x).getcolHasValuable());
			
			record.getValuables().add(rowValue);
		}
		
		record.setOtherValuables(form.ctnDetails().txtSafe().getValue());
		
		return record;
	}
	
	protected PropertyChecklistVo populateDataFromScreen()
	{
		return populateDataFromScreen(form.getLocalContext().getEditedRecord());
	}

	protected void onGrdListSelectionChanged() throws PresentationLogicException 
	{
		getAndDisplayRecord();
		form.ctnDetails().setCollapsed(false);
		updateControlState();
	}
	//WDEV-2276 
	protected void onChkConfirmValueChanged() throws PresentationLogicException
	{
		if (form.ctnDetails().chkConfirm().getValue() == true)
		{
			form.ctnDetails().setcustomConfirmingHCPEnabled(true);
			form.ctnDetails().customConfirmingHCP().setLabels(AuthoringLabelType.CONFIRMED);
			form.ctnDetails().customConfirmingHCP().initializeComponent();
			form.ctnDetails().customConfirmingHCP().setIsRequiredPropertyToControls(true);
	
		}
		if (form.ctnDetails().chkConfirm().getValue() == false)
		{
			form.ctnDetails().customConfirmingHCP().setValue(null);
			form.ctnDetails().setcustomConfirmingHCPEnabled(false);
			form.ctnDetails().customConfirmingHCP().setIsRequiredPropertyToControls(false);
		}
	}
}
