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

package ims.nursing.forms.invasivedevice;

import ims.clinical.vo.PatientInvasiveDeviceRefVo;
import ims.clinical.vo.lookups.InvasiveDeviceVIPScale;
import ims.clinicaladmin.vo.InvasiveDeviceConfigShortVo;
import ims.clinicaladmin.vo.InvasiveDeviceConfigSizeVoCollection;
import ims.clinicaladmin.vo.InvasiveDeviceConfigVo;
import ims.clinicaladmin.vo.InvasiveDeviceConfigVoCollection;
import ims.clinicaladmin.vo.InvasiveDeviceTypeVo;
import ims.clinicaladmin.vo.InvasiveDeviceTypeVoCollection;
import ims.configuration.gen.ConfigFlag;
import ims.core.vo.AuthoringInformationVo;
import ims.core.vo.HcpLiteVo;
import ims.core.vo.HcpLiteVoCollection;
import ims.core.vo.enums.AuthoringLabelType;
import ims.core.vo.lookups.TimeUnitsSecondsToMonths;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.controls.DynamicGridCell;
import ims.framework.controls.DynamicGridColumn;
import ims.framework.controls.DynamicGridRow;
import ims.framework.enumerations.DynamicCellType;
import ims.framework.enumerations.FormMode;
import ims.framework.exceptions.PresentationLogicException;
import ims.framework.utils.DateTime;
import ims.nursing.vo.InvasiveDeviceSearchCriteriaVo;
import ims.nursing.vo.PatientInvasiveDeviceShortVo;
import ims.nursing.vo.PatientInvasiveDeviceShortVoCollection;
import ims.nursing.vo.PatientInvasiveDeviceVipVo;
import ims.nursing.vo.PatientInvasiveDeviceVipVoCollection;
import ims.nursing.vo.PatientInvasiveDeviceVo;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeMap;

public class Logic extends BaseLogic
{	
	private static final String FILTER_HISTORIC 	= "-2";
	private static final String FILTER_CURRENT 		= "-1";
	public static final String	COLDEVICE			= "0";
	public static final String	COLVIP				= "1";
	public static final String	COLSITE				= "2";
	public static final String	COLINSERTEDDATE		= "3";
	public static final String	COLINSERTEDBY		= "4";
	public static final String	COLREMOVALDATE		= "5";
	public static final String	COLREMOVALBY		= "6";
	
	private static final long serialVersionUID = 1L;

	protected void onFormOpen() throws ims.framework.exceptions.PresentationLogicException
	{
		initialize();
		
		//WDEV-19389 --- start
		if (!(form.getGlobalContext().Core.getCurrentCareContextIsNotNull() && form.getGlobalContext().Nursing.getInvasiveDeviceSearchCriteriaIsNotNull() && form.getGlobalContext().Nursing.getInvasiveDeviceSearchCriteria().getCareContextIsNotNull() && form.getGlobalContext().Core.getCurrentCareContext().equals(form.getGlobalContext().Nursing.getInvasiveDeviceSearchCriteria().getCareContext())))
			form.getGlobalContext().Nursing.setInvasiveDeviceSearchCriteria(null);
		
		if(form.getGlobalContext().Nursing.getInvasiveDeviceSearchCriteriaIsNotNull())
		{
			setSearchCriteria(form.getGlobalContext().Nursing.getInvasiveDeviceSearchCriteria());
			onCmbFilterValueChanged();
		}	
		else
			open();
		//WDEV-19389 --- end
	}
	
	private InvasiveDeviceSearchCriteriaVo getSearchCriteria()
	{
		InvasiveDeviceSearchCriteriaVo searchCriteria = new InvasiveDeviceSearchCriteriaVo();
		
		searchCriteria.setFilter(form.cmbFilter().getValue());
		searchCriteria.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
		
		return searchCriteria;
	}
	
	
	private void setSearchCriteria(InvasiveDeviceSearchCriteriaVo invasiveDeviceSearchCriteriaVo)
	{
		form.cmbFilter().setValue(invasiveDeviceSearchCriteriaVo.getFilter());	
	}
	
	private void initialize() 
	{
		form.ctnDetail().lyrDetails().tabDevice().customInsertedHCP().ignoreComponentReadonlyFlag(Boolean.TRUE);
				
		populateFilterCombo(); 
		setContextMenuState();	
		updateContextMenuState();
		
		form.ctnDetail().lyrDetails().showtabDevice();
		form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(false);
		form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(true);
		
		form.ctnDetail().lyrDetails().tabDevice().customInsertedHCP().setLabels(AuthoringLabelType.INSERTED);
		form.ctnDetail().lyrDetails().tabDevice().setcustomInsertedHCPEnabled(false);
		
		form.ctnDetail().lyrDetails().tabVIP().customVipAuthoringHCP().setIsRequiredPropertyToControls(true);

		if (form.cmbFilter().getValue() != null)
		{
			if (form.cmbFilter().getValue().equals(FILTER_HISTORIC))
			{
				initialiseHistoricDynamicGrid();
			}
			if (form.cmbFilter().getValue().equals(FILTER_CURRENT))
			{
				initialiseCurrentDynamicGrid();
			}
		}
		try //WDEV-19962
		{
			form.ctnDetail().lyrDetails().tabDevice().dtimTargetRemDate().setMaxValue(new DateTime("290001010000"));
			form.ctnDetail().lyrDetails().tabDevice().dtimRemDate().setMaxValue(new DateTime("290001010000"));
		}
		catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void setContextMenuState() 
	{
		form.getContextMenus().getGenericGridAddItem().setText("New Entry");
		form.getContextMenus().getGenericGridUpdateItem().setText("Edit Entry");
		form.getContextMenus().getGenericGridMoveUpItem().setText("Add VIP Score");
		form.getContextMenus().getGenericGridMoveUpItem().setIcon(form.getImages().Core.New);
		form.getContextMenus().getGenericGridMoveDownItem().setText("Edit VIP Score");
		form.getContextMenus().getGenericGridMoveDownItem().setIcon(form.getImages().Core.Edit);
	}
	
	private void open() 
	{
		form.setMode(FormMode.VIEW);	
		form.dyngrdDisplay().getRows().clear();
		form.ctnDetail().setCollapsed(true);
		
		PatientInvasiveDeviceShortVoCollection list;
		
		if (form.cmbFilter().getValue() != null)
		{	
			list = null;	
			
			if (form.cmbFilter().getValue().equals(FILTER_HISTORIC))
			{
				list = domain.listHistoricPatientInvasiveDevice(form.getGlobalContext().Core.getCurrentCareContext());
			}
			if (form.cmbFilter().getValue().equals(FILTER_CURRENT))
			{
				list = domain.listCurrentPatientInvasiveDevice(form.getGlobalContext().Core.getCurrentCareContext());
			}
			
			form.dyngrdDisplay().getRows().clear();
		
			TreeMap map = new TreeMap();
		
			if(list != null)
			{
				countForInvasiveDevice(list, map);
				
				for (Iterator iter = map.keySet().iterator(); iter.hasNext();) 
				{
					String element = (String) iter.next();
					
					if (form.cmbFilter().getValue().equals(FILTER_HISTORIC))
					{	
						DynamicGridRow dynRow = addParentRow(map, element);		
					
						for(int i = 0; i < list.size(); i++)
						{
							if (list.get(i).getInvasiveDevice().getInvasiveDevice().equals(element))
							{
								addHistoricRowRecord(list, dynRow, i);		
							}
						}
					}
				}
			
				if (form.cmbFilter().getValue().equals(FILTER_CURRENT))
				{	
					for(int i = 0; i < list.size(); i++)
					{		
						addCurrentRowRecord(list.get(i));
					}
					
					if (form.dyngrdDisplay().getSelectedRow() == null)
					{
						clearTabDeviceInstanceControls();
					}
				}
				
				if (form.getLocalContext().getEditedVipRecordIsNotNull())
				{	
					for (int i = 0; i < form.dyngrdDisplay().getRows().size(); i++) 
					{
						DynamicGridRow parentRow = form.dyngrdDisplay().getRows().get(i);
						for (int j = 0; j < parentRow.getRows().size(); j++) 
						{
							DynamicGridRow rowChild = parentRow.getRows().get(j);
								
							if (rowChild.getValue() instanceof PatientInvasiveDeviceVipVo && form.getLocalContext().getEditedVipRecordIsNotNull())
							{
								PatientInvasiveDeviceVipVo value = (PatientInvasiveDeviceVipVo)rowChild.getValue();
									
								if(value.getID_VIPScore().equals(form.getLocalContext().getEditedVipRecord().getID_VIPScore()))
								{
									form.dyngrdDisplay().setSelectedRow(rowChild);
									parentRow.setExpanded(true);
									getAndDisplayRecordVip(form.getLocalContext().getEditedVipRecord());
									form.getLocalContext().setEditedDeviceRecord(null);//RIE
									form.ctnDetail().setCollapsed(false);
									
									break;
								}
							}
								
						}
							
					}						
				}
			}
			updateControlsState();			
		}
		updateControlsState();
		
		form.ctnDetail().setCollapsed(form.dyngrdDisplay().getSelectedRow() == null);
	}

	private DynamicGridRow addParentRow(TreeMap map, String element) 
	{	
		DynamicGridRow dynRow = form.dyngrdDisplay().getRows().newRow();
		DynamicGridCell deviceCellVirtual = dynRow.getCells().newCell(getColumn(COLDEVICE), DynamicCellType.STRING);
		deviceCellVirtual.setValue(element + " (" + map.get(element) + ")");
		dynRow.setSelectable(false);
		
		return dynRow;
	}
	
	private void countForInvasiveDevice(PatientInvasiveDeviceShortVoCollection list, TreeMap map) 
	{
		for(int i = 0; i < list.size(); i++)
		{
			
				String device = list.get(i).getInvasiveDevice().getInvasiveDevice();
				Integer count = (Integer) map.get(device);
		
				if(count == null)
				{	
					count = new Integer(1);
					map.put(device, count);
				}
				else
				{
					map.put(device, new Integer(count.intValue() + 1));
				}
		}
	}
	
	private void addHistoricRowRecord(PatientInvasiveDeviceShortVoCollection list, DynamicGridRow dynVirtualRow, int index) 
	{
		if(list == null)
			return;
		
		DynamicGridRow dynParentRow = dynVirtualRow.getRows().newRow();
		dynParentRow.setIdentifier(list.get(index));

		InvasiveDeviceConfigShortVo recordDeviceParent = list.get(index).getInvasiveDeviceIsNotNull() ?  list.get(index).getInvasiveDevice() : null;							
		DynamicGridCell deviceChildCellVirtual = dynParentRow.getCells().newCell(getColumn(COLDEVICE), DynamicCellType.STRING);
		deviceChildCellVirtual.setValue(recordDeviceParent.getInvasiveDevice());
		
		InvasiveDeviceConfigShortVo recordDevice =  list.get(index).getInvasiveDeviceIsNotNull() ?  list.get(index).getInvasiveDevice() : null;
		DynamicGridCell deviceCell = dynParentRow.getCells().newCell(getColumn(COLDEVICE), DynamicCellType.STRING);
		deviceCell.setValue(recordDevice.getInvasiveDevice());

		DynamicGridCell siteCell = dynParentRow.getCells().newCell(getColumn(COLSITE), DynamicCellType.STRING);
		siteCell.setValue( list.get(index).getSiteIsNotNull() ?  list.get(index).getSite() : null);

		DynamicGridCell insDateCell = dynParentRow.getCells().newCell(getColumn(COLINSERTEDDATE), DynamicCellType.STRING);
		insDateCell.setValue( list.get(index).getInsertedDateTime().toString());

		HcpLiteVo recordHCP =  list.get(index).getInsertedHCPIsNotNull() ?  list.get(index).getInsertedHCP() : null;
		DynamicGridCell insHcpCell = dynParentRow.getCells().newCell(getColumn(COLINSERTEDBY), DynamicCellType.STRING);
		insHcpCell.setValue(recordHCP.getMos().getName().toString());

		DynamicGridCell remDateCell = dynParentRow.getCells().newCell(getColumn(COLREMOVALDATE), DynamicCellType.STRING);
		remDateCell.setValue( list.get(index).getRemovalDateTime().toString());

		HcpLiteVo removeHCP =  list.get(index).getRemovalHCPIsNotNull() ?  list.get(index).getRemovalHCP() : null;
		DynamicGridCell removeHcpCell = dynParentRow.getCells().newCell(getColumn(COLREMOVALBY), DynamicCellType.STRING);		
		removeHcpCell.setValue(removeHCP.getMos().getName().toString());
		
		PatientInvasiveDeviceVipVoCollection recordVip = list.get(index).getVIPScoreIsNotNull() ? list.get(index).getVIPScore() : null;
		
		if (recordVip != null)
		{
			for(int x = 0; x < list.get(index).getVIPScore().size(); x++)
			{
				if(recordVip.get(x) != null)
				{
					DynamicGridRow childVipRow = dynParentRow.getRows().newRow();
					childVipRow.setIdentifier(list.get(index).getVIPScore().get(x));
					DynamicGridCell vipCell = childVipRow.getCells().newCell(getColumn(COLDEVICE), DynamicCellType.STRING);
					vipCell.setValue("VIP="+list.get(index).getVIPScore().get(x).getVIPScale().toString());
					//childVipRow.setValue(list.get(index).getVIPScore().get(x));
				}
			}
		}
		dynParentRow.setValue(list.get(index));
	}
	
	private void addCurrentRowRecord(PatientInvasiveDeviceShortVo record) 
	{	
		if(record == null)
			return;	
		
		DynamicGridRow dynRow = form.dyngrdDisplay().getRows().newRow(record.equals(form.getLocalContext().getEditedDeviceRecord()));
		dynRow.setIdentifier(record);
		PatientInvasiveDeviceVipVoCollection recordVip = record.getVIPScoreIsNotNull() ? record.getVIPScore() : null;
				
		InvasiveDeviceConfigShortVo recordDevice = record.getInvasiveDeviceIsNotNull() ? record.getInvasiveDevice() : null;
		DynamicGridCell deviceCell = dynRow.getCells().newCell(getColumn(COLDEVICE), DynamicCellType.STRING);
		deviceCell.setValue(recordDevice.getInvasiveDevice());

		DynamicGridCell siteCell = dynRow.getCells().newCell(getColumn(COLSITE), DynamicCellType.STRING);
		siteCell.setValue(record.getSiteIsNotNull() ? record.getSite() : null);
				
		DynamicGridCell insDateCell = dynRow.getCells().newCell(getColumn(COLINSERTEDDATE), DynamicCellType.STRING);
		insDateCell.setValue(record.getInsertedDateTime().toString());
				
		HcpLiteVo recordHCP = record.getInsertedHCPIsNotNull() ? record.getInsertedHCP() : null;
		DynamicGridCell insHcpCell = dynRow.getCells().newCell(getColumn(COLINSERTEDBY), DynamicCellType.STRING);
		insHcpCell.setValue(recordHCP.getMos().getName().toString());
		
		if (record.getTargetRemovalDateTime().getDate().isLessOrEqualThan(new DateTime().getDate()))
		{
			dynRow.setBackColor(ims.configuration.gen.ConfigFlag.UI.INVASIVE_DEVICE_REMOVAL_ROW_COLOR.getValue());
		}

		if (recordVip != null)
		{
			for(int x = 0; x < record.getVIPScore().size(); x++)
			{
					if(recordVip.get(x) != null)
					{
						DynamicGridRow childRow = dynRow.getRows().newRow();
						DynamicGridCell vipCell = childRow.getCells().newCell(getColumn(COLDEVICE), DynamicCellType.STRING);
						vipCell.setValue("VIP="+record.getVIPScore().get(x).getVIPScale().toString());
						childRow.setIdentifier(record.getVIPScore().get(x));
						childRow.setValue(record.getVIPScore().get(x));
					}
			}
		}
		dynRow.setValue(record);
	}

	private DynamicGridColumn getColumn(String identifier)
	{
		return form.dyngrdDisplay().getColumns().getByIdentifier(identifier);
	}
	
	public void updateControlsState()
	{
		if(form.getMode().equals(FormMode.VIEW))
		{
			form.btnNew().setVisible(true);
			form.btnUpdate().setVisible(true);
			
			if (form.cmbFilter().getValue().equals(FILTER_CURRENT))
			{
				if (form.dyngrdDisplay().getSelectedRow() != null)
				{
					form.btnUpdate().setEnabled(true);
					Object obj = form.dyngrdDisplay().getSelectedRow().getIdentifier();
					if(obj instanceof PatientInvasiveDeviceShortVo)
					{
						form.btnNew().setEnabled(true);
						form.ctnDetail().lyrDetails().showtabDevice();
						form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(true);
						form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(false);
					}
					else if(obj instanceof PatientInvasiveDeviceVipVo)
					{
						form.btnNew().setEnabled(false);
						form.ctnDetail().lyrDetails().showtabVIP();
						form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(false);
						form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(true);
					}
				}
				else
				{
					form.btnNew().setEnabled(true);
					form.btnUpdate().setEnabled(false);
				}
			}
			if (form.cmbFilter().getValue().equals(FILTER_HISTORIC))
			{
				form.btnNew().setEnabled(false);
				form.btnUpdate().setEnabled(false);
			}
		}
		updateContextMenuState();
	}
	
	private void populateFilterCombo() 
	{
		form.cmbFilter().clear();
		form.cmbFilter().newRow(FILTER_CURRENT, "Current");
		form.cmbFilter().newRow(FILTER_HISTORIC, "Historic");
		form.cmbFilter().setValue(FILTER_CURRENT);
	}
	
	protected void onQmbRemovalByTextSubmited(String value) throws ims.framework.exceptions.PresentationLogicException
	{
		form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().clear();
		
		HcpLiteVoCollection coll = domain.listHcps(value);
		for (int i = 0; i < coll.size(); i++)
		{
			HcpLiteVo med = coll.get(i);
			form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().newRow(med, med.toString());			
		}
		if (coll.size() == 1)
		{
			form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().setValue(coll.get(0));
		}
		else if (coll.size() > 1)
		{
			form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().showOpened();		
		}		
	}
	
	protected void onCmbFilterValueChanged() throws ims.framework.exceptions.PresentationLogicException
	{	
		if (form.cmbFilter().getValue() != null)
		{
			if (form.cmbFilter().getValue().equals(FILTER_CURRENT))
			{
				initialiseCurrentDynamicGrid();
			}
			if (form.cmbFilter().getValue().equals(FILTER_HISTORIC))
			{
				initialiseHistoricDynamicGrid();
			}
		}
		else
		{
			engine.showMessage("Select Current or Historic");
			form.cmbFilter().setValue(FILTER_CURRENT);
			initialiseCurrentDynamicGrid();
		}
		
		form.getLocalContext().setEditedDeviceRecord(null);
		form.getLocalContext().setEditedVipRecord(null);
		
		setContextMenuState();
		
		form.getGlobalContext().Nursing.setInvasiveDeviceSearchCriteria(getSearchCriteria());//WDEV-19389 
		
		open();
	}
	
	protected void onBtnNewClick() throws ims.framework.exceptions.PresentationLogicException
	{
		newEntry();
	}
	
	protected void onBtnUpdateClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (form.ctnDetail().lyrDetails().tabDevice().isVisible() == true)
		{
			updateEntryDevice();
		}
		else if (form.ctnDetail().lyrDetails().tabVIP().isVisible() == true)
		{
			updateEntryVip();
		}
	}
	
	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (form.ctnDetail().lyrDetails().tabDevice().isHeaderVisible() == true && 
				form.ctnDetail().lyrDetails().tabVIP().isHeaderVisible() == true)
		{
			if(	saveVip())
			{
				form.getLocalContext().setEditedVipRecord(null);
				open();
				return;
			}
		}
		
		if (form.ctnDetail().lyrDetails().tabDevice().isHeaderVisible() == true && 
				form.ctnDetail().lyrDetails().tabVIP().isHeaderVisible() == false)
		{
			if(saveDevice())
			{
				form.getLocalContext().setEditedVipRecord(null);
				open();
			}
		}
		
		if (form.ctnDetail().lyrDetails().tabVIP().isHeaderVisible() == true && 
				form.ctnDetail().lyrDetails().tabDevice().isHeaderVisible() == false)
		{	
			if(saveVip())
			{
				open();
			}
		}
	}
	
	private boolean saveVip()
	{
		PatientInvasiveDeviceVipVoCollection vipCollection = new PatientInvasiveDeviceVipVoCollection();
		PatientInvasiveDeviceVo record = new PatientInvasiveDeviceVo();
		PatientInvasiveDeviceVipVo vipRecord = new PatientInvasiveDeviceVipVo();
			
		if (form.ctnDetail().lyrDetails().tabVIP().isHeaderVisible() == true && 
				form.ctnDetail().lyrDetails().tabDevice().isHeaderVisible() == false)
		{
			Object obj = form.dyngrdDisplay().getSelectedRow().getIdentifier();
			record = returnParent();
			vipCollection = record.getVIPScore();
		
			if(vipCollection == null)
				vipCollection = new PatientInvasiveDeviceVipVoCollection();
			
			vipRecord = populateDataFromScreenForVip(getNewVipVo(vipCollection));
	    
			if(obj instanceof PatientInvasiveDeviceVipVo)
			{
				vipRecord = populateDataFromScreenForVip(form.getLocalContext().getEditedVipRecord());
			}
	
			if(obj instanceof PatientInvasiveDeviceVipVo)
			{
				vipCollection.remove(form.getLocalContext().getEditedVipRecord());
			}
		}
		
		if (form.ctnDetail().lyrDetails().tabVIP().isHeaderVisible() == true && 
				form.ctnDetail().lyrDetails().tabDevice().isHeaderVisible() == true)
		{
			if (!saveDevice())
			{
				return false;
			}
			
			record = form.getLocalContext().getEditedDeviceRecord(); 
			vipCollection = record.getVIPScore();
	
			if(vipCollection == null)
				vipCollection = new PatientInvasiveDeviceVipVoCollection();
	
			vipRecord = populateDataFromScreenForVip(getNewVipVo(vipCollection));
			
			//vipRecord = populateDataFromScreenForVip(form.getLocalContext().getEditedVipRecord());
		}
				
		if(vipRecord != null)
		{
			int vipIndex = vipCollection.indexOf(vipRecord);
			
			if(vipIndex < 0)
			{
				vipCollection.add(vipRecord);
			}
			else
			{
				vipCollection.set(vipIndex, vipRecord);
			}
		}
		record.setVIPScore(vipCollection);
		
		String[] errors = getUiErrors();
		errors = removeDuplicateErrors(errors);
		
		if(errors != null && errors.length > 0)
		{
			engine.showErrors(errors);
			return false;
		}
		
		errors = record.validate(errors);
		if(errors != null && errors.length > 0)
		{
			engine.showErrors(errors);
			
			return false;
		}
		
		try
		{
			record = domain.savePatientInvasiveDevice(record);
		}
		catch(StaleObjectException e)
		{
			engine.showMessage(ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
			PatientInvasiveDeviceVipVo updatedRecordVip = getAndDisplayRecordVip(form.getLocalContext().getEditedVipRecord());
			updateRowRecordVip(updatedRecordVip);
			form.setMode(FormMode.VIEW);
			open();
		
			return false;
		}
	
		form.ctnDetail().lyrDetails().showtabDevice();
		form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(false);
		form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(true);
		
		//form.getLocalContext().setEditedVipRecord(vipRecord);
		form.getLocalContext().setEditedVipRecord(record.getVIPScore().get(record.getVIPScore().size()-1));
		
		return true;
	}

	/**
	 * Remove duplicate errors 
	 * @param errors
	 * @return
	 */
	private String[] removeDuplicateErrors(String[] errors)
	{
		if(errors == null || errors.length == 0)
			return null;
		
		ArrayList<String> a = new ArrayList<String>();
		
		for (int i = 0; i < errors.length; i++)
		{
			if(!a.contains(errors[i]))
				a.add(errors[i]);
		}
		
		return (String[]) a.toArray(errors);
	}

	private PatientInvasiveDeviceVipVo getNewVipVo(PatientInvasiveDeviceVipVoCollection vipCollection) 
	{
		for (int i = 0; vipCollection != null && i < vipCollection.size(); i++) 
		{
			if(vipCollection.get(i).getID_VIPScore() == null)
			{
				return vipCollection.get(i);
			}
		}
		return new PatientInvasiveDeviceVipVo();
	}

	private PatientInvasiveDeviceVo returnParent() 
	{		
		Object obj = form.dyngrdDisplay().getSelectedRow().getIdentifier();
		
		if(obj instanceof PatientInvasiveDeviceShortVo)
		{
			PatientInvasiveDeviceVo record = form.getLocalContext().getEditedDeviceRecord();
			return record;
		}
		
		Object selectedRowID = form.dyngrdDisplay().getSelectedRow().getParent().getIdentifier();
		
		PatientInvasiveDeviceShortVo recordParent = (PatientInvasiveDeviceShortVo) selectedRowID;
		PatientInvasiveDeviceVo record = domain.getPatientInvasiveDevice(recordParent);
		
		return record;
	}
	
	private boolean saveDevice() 
	{	
		PatientInvasiveDeviceVo record = populateDataFromScreenForDevice(form.getLocalContext().getEditedDeviceRecord());
			
		record.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
		record.setClinicalContact(form.getGlobalContext().Core.getCurrentClinicalContact());
		
		PatientInvasiveDeviceVipVoCollection vipCollection = record.getVIPScore();
			
		if(vipCollection == null)
		{
			vipCollection = new PatientInvasiveDeviceVipVoCollection();
		}
		else
		{
			record.setVIPScore(vipCollection);
		}	
				
		String[] errors = getUiErrors();
		
		if(errors != null && errors.length > 0)
		{
			if (form.ctnDetail().lyrDetails().tabDevice().isHeaderVisible() == true && 
					form.ctnDetail().lyrDetails().tabVIP().isHeaderVisible() == true &&
						record != null )
			{
				if (record.validate() != null)
				{
					if (record.validate().length > 0)
					{
						form.ctnDetail().lyrDetails().showtabDevice();
					}
				}
				else
				{
					if ((ifRemovalDateIsPopulates() && 
							!ifRemovalMandatoryFieldsArePolulates()) ||
								(form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().getValue() != null &&
										(form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().getValue().getAuthoringHcp() == null ||
												form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().getValue().getAuthoringDateTime() == null)))							
					{
						form.ctnDetail().lyrDetails().showtabDevice();
					}			
					else
					{	
						form.ctnDetail().lyrDetails().showtabVIP();
					}
				}
			}
				
			errors = removeDuplicateErrors(errors);
			engine.showErrors(errors);
			
			return false;
		}
		
		errors = record.validate(errors);
		if(errors != null && errors.length > 0)
		{
			engine.showErrors(errors);
			
			return false;
		}
		
		try
		{
			record = domain.savePatientInvasiveDevice(record);
		}
		catch(StaleObjectException e)
		{
			engine.showMessage(ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());	
			PatientInvasiveDeviceVo updatedRecordDevice = getAndDisplayRecordDevice(record);
			updateRowRecord(updatedRecordDevice);
			form.setMode(FormMode.VIEW);
			open();
		
			return false;
		}

		form.getLocalContext().setEditedDeviceRecord(record);

		return true;
	}
	
	private void updateRowRecord(PatientInvasiveDeviceVo record) 
	{
		if(record == null)
			return;
		
		for(int j = 0; j < form.dyngrdDisplay().getRows().size(); j++)
		{
			PatientInvasiveDeviceShortVo existingRecord = (PatientInvasiveDeviceShortVo)
						(form.dyngrdDisplay().getRows().get(j).getValue());
			if(existingRecord != null && existingRecord.equals(record))
			{
				updateRowRecord(form.dyngrdDisplay().getRows().get(j), record);
				break;
			}
		}	
	}
	
	private void updateRowRecord(DynamicGridRow dynRow, PatientInvasiveDeviceVo record) 
	{
		if(dynRow == null || record == null)
			return;
		
		InvasiveDeviceConfigShortVo recordDevice = record.getInvasiveDeviceIsNotNull() ? record.getInvasiveDevice() : null;
		DynamicGridCell deviceCell = dynRow.getCells().newCell(getColumn(COLDEVICE), DynamicCellType.STRING);
		deviceCell.setValue(recordDevice.getInvasiveDevice());
				
		DynamicGridCell siteCell = dynRow.getCells().newCell(getColumn(COLSITE), DynamicCellType.STRING);
		siteCell.setValue(record.getSiteIsNotNull() ? record.getSite() : null);
				
		DynamicGridCell insDateCell = dynRow.getCells().newCell(getColumn(COLINSERTEDDATE), DynamicCellType.STRING);
		insDateCell.setValue(record.getInsertedDateTime().toString());
		
		HcpLiteVo recordHCP = record.getInsertedHCPIsNotNull() ? record.getInsertedHCP() : null;
		DynamicGridCell insHcpCell = dynRow.getCells().newCell(getColumn(COLINSERTEDBY), DynamicCellType.STRING);
		insHcpCell.setValue(recordHCP.getMos().getName().toString());
		
		dynRow.setValue(record);
	}
	
	private void updateRowRecordVip(PatientInvasiveDeviceVipVo record) 
	{
		if(record == null)
			return;

		Object obj = form.dyngrdDisplay().getSelectedRow().getValue();
		
		if(obj instanceof PatientInvasiveDeviceVipVo)
		{
			PatientInvasiveDeviceVipVo existingRecord = (PatientInvasiveDeviceVipVo)
					(form.dyngrdDisplay().getSelectedRow().getValue());
			
			if(existingRecord != null && existingRecord.equals(record))
			{
					updateRowRecordVip(form.dyngrdDisplay().getSelectedRow(), record);
			}
		}
	}
	
	private void updateRowRecordVip(DynamicGridRow childRow, PatientInvasiveDeviceVipVo record) 
	{
		if(childRow == null || record == null)
			return;
		
		InvasiveDeviceVIPScale recordVip = record.getVIPScale();	
		DynamicGridCell vipCell = childRow.getCells().newCell(getColumn(COLDEVICE), DynamicCellType.STRING);
		vipCell.setValue("VIP="+recordVip);
		
		childRow.setValue(record);
	}
	
	private String[] getUiErrors() 
	{
		ArrayList errors = new ArrayList();
		
		if (form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue() == null)
		{
			errors.add("Device is mandatory");
		}
		
		String insertHcpCCErrors = form.ctnDetail().lyrDetails().tabDevice().customInsertedHCP().getErrors();
		
		if(insertHcpCCErrors != null && insertHcpCCErrors.length() > 0)
		{
			errors.add(insertHcpCCErrors);
		}
		
		if (form.ctnDetail().lyrDetails().tabDevice().dtimTargetRemDate().getValue() == null)
		{
			errors.add("Target Removal Date is mandatory");
		}
		
		String authoringHcpCCErrors = form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().getErrors();
		
		if(authoringHcpCCErrors != null && authoringHcpCCErrors.length() > 0)
		{
			errors.add(authoringHcpCCErrors);
		}
		
		if (form.ctnDetail().lyrDetails().tabDevice().isHeaderVisible() == true && 
				form.ctnDetail().lyrDetails().tabVIP().isHeaderVisible() == true)
		{
			if (form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue().getHasSiteIsNotNull() && 
					form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue().getHasSite().equals(Boolean.TRUE) &&
					form.ctnDetail().lyrDetails().tabDevice().txtSite().getValue() == null)
			{
				errors.add("Site is mandatory");
			}
			
			if(form.ctnDetail().lyrDetails().tabVIP().cmbVipSore().getValue() == null)
			{
				errors.add("VIP Score is mandatory");
			}
			if(form.ctnDetail().lyrDetails().tabVIP().cmbAction().getValue() == null)
			{
				errors.add("Action is mandatory");
			}
			if(form.ctnDetail().lyrDetails().tabVIP().customVipAuthoringHCP().getErrors()!= null && form.ctnDetail().lyrDetails().tabVIP().customVipAuthoringHCP().getErrors().length() > 0)
			{
				errors.add(form.ctnDetail().lyrDetails().tabVIP().customVipAuthoringHCP().getErrors());
			}
			if (ifRemovalDateIsPopulates())
			{
				if(form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().getValue() == null)
				{
					errors.add("Removal HCP is mandatory");
				}
				if(form.ctnDetail().lyrDetails().tabDevice().cmbRemovalReason().getValue() == null)
				{
					errors.add("Removal Reason is mandatory");
				}
				if(form.ctnDetail().lyrDetails().tabDevice().ansCulture().getValue() == null)
				{
					errors.add("Send for culture is mandatory");
				}
			}
			
			if (form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().getValue() != null)
			{
				if (form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().getValue().getAuthoringHcp() == null)
				{
					errors.add("Device Authoring Hcp is mandatory");
				}
				if (form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().getValue().getAuthoringDateTime() == null)
				{
					errors.add("Device Authoring Date/Time is mandatory");
				}
			}	
			
		}
		
		if (form.ctnDetail().lyrDetails().tabDevice().isHeaderVisible() == true && 
				form.ctnDetail().lyrDetails().tabVIP().isHeaderVisible() == false)
		{	
			if (form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().getValue() != null)
			{
				if (form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().getValue().getAuthoringHcp() == null)
				{
					errors.add("Device Authoring Hcp is mandatory");
				}
				if (form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().getValue().getAuthoringDateTime() == null)
				{
					errors.add("Device Authoring Date/Time is mandatory");
				}
			}	
			
			if (form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue() != null &&
					form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue().getHasSiteIsNotNull() && 
						form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue().getHasSite().equals(Boolean.TRUE) &&
							form.ctnDetail().lyrDetails().tabDevice().txtSite().getValue() == null)
			{
				errors.add("Site is mandatory");
			}
			if (ifRemovalDateIsPopulates())
			{
				if(form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().getValue() == null)
				{
					errors.add("Removal HCP is mandatory");
				}
				if(form.ctnDetail().lyrDetails().tabDevice().cmbRemovalReason().getValue() == null)
				{
					errors.add("Removal Reason is mandatory");
				}
				if(form.ctnDetail().lyrDetails().tabDevice().ansCulture().getValue() == null)
				{
					errors.add("Send for culture is mandatory");
				}
			}	
		}
		
		if (form.ctnDetail().lyrDetails().tabDevice().isHeaderVisible() == false && 
				form.ctnDetail().lyrDetails().tabVIP().isHeaderVisible() == true)
		{
			if (form.ctnDetail().lyrDetails().tabVIP().customVipAuthoringHCP().getValue() != null)
			{
				if (form.ctnDetail().lyrDetails().tabVIP().customVipAuthoringHCP().getValue().getAuthoringHcp() == null)
				{
					errors.add("Authoring Hcp is mandatory");
				}
				if (form.ctnDetail().lyrDetails().tabVIP().customVipAuthoringHCP().getValue().getAuthoringDateTime() == null)
				{
					errors.add("Authoring Date/Time is mandatory");
				}
			}	
		}
		
		if(errors.size() > 0)
		{
			String[] searchErrors = new String[errors.size()];
			errors.toArray(searchErrors);
			//engine.showErrors("Invalid Device Record", searchErrors);
			
			return searchErrors;
		}
		
		return null;
	}
	
	private boolean ifRemovalDateIsPopulates() 
	{
		if (this.form.ctnDetail().lyrDetails().tabDevice().dtimRemDate().getValue() != null)
		{	
			//populateRemovalHcpCombo();
			form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().setRequired(true);
			form.ctnDetail().lyrDetails().tabDevice().cmbRemovalReason().setRequired(true);
			form.ctnDetail().lyrDetails().tabDevice().ansCulture().setRequired(true);
				
			return true;
		}
		return false;
	}
	
	private boolean ifRemovalMandatoryFieldsArePolulates()
	{
		if (ifRemovalDateIsPopulates())
		{
			if(form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().getValue() == null ||
					form.ctnDetail().lyrDetails().tabDevice().cmbRemovalReason().getValue() == null ||
						form.ctnDetail().lyrDetails().tabDevice().ansCulture().getValue() == null)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		return false;
	}
	
	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{	
		
		if (form.dyngrdDisplay().getSelectedRow() == null) //wdev-10489
		{
			form.ctnDetail().lyrDetails().showtabDevice();
			form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(false);
			form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(true);
			clearTabDeviceInstanceControls();
	
		}
		else
		{
			if (form.ctnDetail().lyrDetails().tabDevice().isVisible() == true)
			{
				form.ctnDetail().lyrDetails().showtabDevice();
				form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(false);
				form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(true);
				clearTabDeviceInstanceControls();
			}
			if (form.ctnDetail().lyrDetails().tabVIP().isVisible() == true)
			{
				form.ctnDetail().lyrDetails().showtabVIP();
				form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(false);
				form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(true);
				clearTabVipInstanceControls();
			}
		}
		form.setMode(FormMode.VIEW);
		form.ctnDetail().setCollapsed(form.dyngrdDisplay().getSelectedRow() == null);
		updateControlsState();
	}
	
	protected void onContextMenuItemClick(int menuItemID, ims.framework.Control sender) throws ims.framework.exceptions.PresentationLogicException
	{
		switch(menuItemID)
		{	
			case GenForm.ContextMenus.GenericGrid.Add:	
				newEntry();
				break;
			case GenForm.ContextMenus.GenericGrid.Update:
				updateEntryDevice();
				break;
			case GenForm.ContextMenus.GenericGrid.MoveUp:
				newVipScore();
				break;
			case GenForm.ContextMenus.GenericGrid.MoveDown:
				updateEntryVip();
				break;
		}
	}
	
	private boolean newVipScore() 
	{
		Object obj = form.dyngrdDisplay().getSelectedRow().getValue();
		
		if(obj instanceof PatientInvasiveDeviceShortVo)
			if (requireVip((PatientInvasiveDeviceShortVo) form.dyngrdDisplay().getSelectedRow().getValue()) != true)
			{
				return false;
			}
		
		form.setMode(FormMode.EDIT);
		
		form.ctnDetail().lyrDetails().showtabVIP();
		form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(false);
		form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(true);
		
		clearTabVipInstanceControls() ;
		form.ctnDetail().lyrDetails().tabVIP().customVipAuthoringHCP().initializeComponent();
		updateContextMenuState();
		
		return true;
	}
	
	private boolean requireVip(PatientInvasiveDeviceShortVo record) 
	{
		InvasiveDeviceConfigShortVo voInvasiveDeviceConfig = record.getInvasiveDevice();
	
		if (voInvasiveDeviceConfig.getRequiresVIPIsNotNull() && voInvasiveDeviceConfig.getRequiresVIP().booleanValue() == false)
			return false;
	
		return true;
	}
	
	private void updateEntryDevice() 
	{
		form.setMode(FormMode.EDIT);	
		
		form.ctnDetail().lyrDetails().showtabDevice();
		form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(false);
		form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(true);
		
		form.ctnDetail().lyrDetails().tabDevice().cmbDevice().setEnabled(false);
		form.ctnDetail().lyrDetails().tabDevice().setcustomAuthoringHCPEnabled(false);
		if (form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue()!= null)
		{
			if (form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue().getHasSiteIsNotNull() && 
					form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue().getHasSite().equals(Boolean.TRUE))
			{
				form.ctnDetail().lyrDetails().tabDevice().txtSite().setRequired(true);
			}
			else
				form.ctnDetail().lyrDetails().tabDevice().txtSite().setRequired(false);
		}
	}
	private void updateEntryVip() 
	{
		form.setMode(FormMode.EDIT);
	
		form.ctnDetail().lyrDetails().showtabVIP();
		form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(false);
		form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(true);
		
		form.ctnDetail().lyrDetails().tabVIP().setcustomVipAuthoringHCPEnabled(false);
	}
	
	private void newEntry() 
	{
		form.dyngrdDisplay().setValue(null);
		form.setMode(FormMode.EDIT);
		form.ctnDetail().lyrDetails().tabDevice().txtSite().setRequired(false);
		form.ctnDetail().lyrDetails().showtabDevice();
		form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(false);
		form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(true);
		
		form.getLocalContext().setEditedDeviceRecord(null);
		//form.getLocalContext().setEditedVipRecord(null);
		form.ctnDetail().lyrDetails().tabDevice().cmbDevice().setEnabled(true);
		
		clearTabDeviceInstanceControls() ;
		populateDeviceCombo();
		form.ctnDetail().lyrDetails().tabDevice().cmbType().clear();
		form.ctnDetail().lyrDetails().tabDevice().cmbSize().clear();
		
		form.ctnDetail().lyrDetails().tabDevice().customInsertedHCP().initializeComponent();
		form.ctnDetail().lyrDetails().tabDevice().setcustomInsertedHCPEnabled(true);
		form.ctnDetail().lyrDetails().tabDevice().customInsertedHCP().setIsRequiredPropertyToControls(Boolean.TRUE);

		hideRemoval();
		form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().initializeComponent();
		form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().setIsRequiredPropertyToControls(Boolean.TRUE);
		
		form.ctnDetail().setCollapsed(false);
		
		updateContextMenuState();
	}
	
	private void clearTabDeviceInstanceControls() 
	{
		form.ctnDetail().lyrDetails().tabDevice().cmbDevice().setValue(null);
		form.ctnDetail().lyrDetails().tabDevice().cmbType().setValue(null);
		form.ctnDetail().lyrDetails().tabDevice().cmbSize().setValue(null);
		form.ctnDetail().lyrDetails().tabDevice().txtSite().setValue("");
		form.ctnDetail().lyrDetails().tabDevice().txtDesc().setValue("");
		form.ctnDetail().lyrDetails().tabDevice().txtDesc().setValue("");
		form.ctnDetail().lyrDetails().tabDevice().customInsertedHCP().setValue(null);
		
		form.ctnDetail().lyrDetails().tabDevice().dtimRemDate().setValue(null);
		form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().setValue(null);
		form.ctnDetail().lyrDetails().tabDevice().cmbRemovalReason().setValue(null);
		form.ctnDetail().lyrDetails().tabDevice().dtimTargetRemDate().setValue(null);
		form.ctnDetail().lyrDetails().tabDevice().ansCulture().setValue(null);
		form.ctnDetail().lyrDetails().tabDevice().txtResult().setValue("");
		form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().setValue(null);
	}
	
	private void clearTabVipInstanceControls()
	{
		form.ctnDetail().lyrDetails().tabVIP().cmbVipSore().setValue(null);
		form.ctnDetail().lyrDetails().tabVIP().cmbAction().setValue(null);
		form.ctnDetail().lyrDetails().tabVIP().customVipAuthoringHCP().setValue(null);
	}
	
	private void updateContextMenuState()
	{
		form.getContextMenus().getGenericGridAddItem().setVisible(false);
		form.getContextMenus().getGenericGridUpdateItem().setVisible(false);
		form.getContextMenus().getGenericGridMoveUpItem().setVisible(false);
		form.getContextMenus().getGenericGridMoveDownItem().setVisible(false);
		
		if (form.dyngrdDisplay().getSelectedRow() != null)
		{
			if (form.cmbFilter().getValue().equals(FILTER_CURRENT))
			{
				Object obj = form.dyngrdDisplay().getSelectedRow().getIdentifier();
				if(form.getMode().equals(FormMode.VIEW))
				{
					if(obj instanceof PatientInvasiveDeviceShortVo)
					{		
						form.getContextMenus().getGenericGridAddItem().setVisible(true);
						form.getContextMenus().getGenericGridUpdateItem().setVisible(true);
						
						Object objVip = form.dyngrdDisplay().getSelectedRow().getValue();
						if(objVip instanceof PatientInvasiveDeviceShortVo)
						{
							if (requireVip((PatientInvasiveDeviceShortVo) form.dyngrdDisplay().getSelectedRow().getValue()) != true)
							{
								form.getContextMenus().getGenericGridMoveUpItem().setVisible(false);
								form.getContextMenus().getGenericGridMoveDownItem().setVisible(false);
							}
							else
							{
								form.getContextMenus().getGenericGridMoveUpItem().setVisible(true);
								form.getContextMenus().getGenericGridMoveDownItem().setVisible(false);
							}
						}
					}
					if(obj instanceof PatientInvasiveDeviceVipVo)
					{
						form.getContextMenus().getGenericGridMoveUpItem().setVisible(false);
						form.getContextMenus().getGenericGridMoveDownItem().setVisible(true);
						form.getContextMenus().getGenericGridAddItem().setVisible(false);
						form.getContextMenus().getGenericGridUpdateItem().setVisible(false);
					}
				}
				else
				{
					form.getContextMenus().getGenericGridAddItem().setVisible(false);
					form.getContextMenus().getGenericGridUpdateItem().setVisible(false);
					form.getContextMenus().getGenericGridRemoveItem().setVisible(false);
				}
			}
			if (form.cmbFilter().getValue().equals(FILTER_HISTORIC))
			{
				form.getContextMenus().getGenericGridAddItem().setVisible(false);
				form.getContextMenus().getGenericGridUpdateItem().setVisible(false);
				form.getContextMenus().getGenericGridRemoveItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveUpItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveDownItem().setVisible(false);
			}
		}
		else
		{
			if (form.getMode().equals(FormMode.VIEW))
			{
				if (form.cmbFilter().getValue().equals(FILTER_CURRENT))
				{
					form.getContextMenus().getGenericGridAddItem().setVisible(true);
				}
				if (form.cmbFilter().getValue().equals(FILTER_HISTORIC))
				{
					form.getContextMenus().getGenericGridAddItem().setVisible(false);
					form.getContextMenus().getGenericGridUpdateItem().setVisible(false);
					form.getContextMenus().getGenericGridRemoveItem().setVisible(false);
					form.getContextMenus().getGenericGridMoveUpItem().setVisible(false);
				}			
			}
			else
			{
				form.getContextMenus().getGenericGridAddItem().setVisible(false);
				form.getContextMenus().getGenericGridUpdateItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveUpItem().setVisible(false);
				form.getContextMenus().getGenericGridMoveDownItem().setVisible(false);	 
			}
		}
	}
	
	private void initialiseCurrentDynamicGrid()
	{
		form.dyngrdDisplay().clear();
		
		clearTabDeviceInstanceControls();
		clearTabVipInstanceControls();
		
		form.ctnDetail().lyrDetails().showtabDevice();
		form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(false);
		form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(true);
		
		DynamicGridColumn colDevice = null;
		DynamicGridColumn colVip = null;
		DynamicGridColumn colSite = null;
		DynamicGridColumn colInsertedDate = null;
		DynamicGridColumn colInsertedBy	= null;
		
		colDevice = form.dyngrdDisplay().getColumns().newColumn("Device", COLDEVICE);
		colVip = form.dyngrdDisplay().getColumns().newColumn("VIP Score", COLVIP);
		colSite = form.dyngrdDisplay().getColumns().newColumn("Site", COLSITE);
		colInsertedDate = form.dyngrdDisplay().getColumns().newColumn("Inserted Date", COLINSERTEDDATE);
		colInsertedBy = form.dyngrdDisplay().getColumns().newColumn("Inserted By", COLINSERTEDBY);
		
		colDevice.setWidth(200);
		colVip.setWidth(100);
		colSite.setWidth(100);
		colInsertedDate.setWidth(120);
		colInsertedBy.setWidth(170);
		
		colDevice.setCanGrow(false);
	}				
	
	private void initialiseHistoricDynamicGrid()
	{
		form.dyngrdDisplay().clear();
		
		clearTabDeviceInstanceControls();
		clearTabVipInstanceControls();	
		
		form.ctnDetail().lyrDetails().showtabDevice();
		form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(false);
		form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(true);
			
		DynamicGridColumn colDevice = null;
		DynamicGridColumn colVip = null;
		DynamicGridColumn colSite = null;
		DynamicGridColumn colInsertedDate = null;
		DynamicGridColumn colInsertedBy	= null;
		DynamicGridColumn colRemovalDate	= null;
		DynamicGridColumn colRemovalBy	= null;
		
		colDevice = form.dyngrdDisplay().getColumns().newColumn("Device", COLDEVICE);
		colVip = form.dyngrdDisplay().getColumns().newColumn("VIP Score", COLVIP);
		colSite = form.dyngrdDisplay().getColumns().newColumn("Site", COLSITE);
		colInsertedDate = form.dyngrdDisplay().getColumns().newColumn("Inserted Date", COLINSERTEDDATE);
		colInsertedBy = form.dyngrdDisplay().getColumns().newColumn("Inserted By", COLINSERTEDBY);
		colRemovalDate = form.dyngrdDisplay().getColumns().newColumn("Removal Date", COLREMOVALDATE);
		colRemovalBy = form.dyngrdDisplay().getColumns().newColumn("Removal By", COLREMOVALBY);
		
		colDevice.setWidth(200);
		colVip.setWidth(100);
		colSite.setWidth(100);
		colInsertedDate.setWidth(120);
		colInsertedBy.setWidth(170);
		colRemovalDate.setWidth(120);
		colRemovalBy.setWidth(170);
		
		colDevice.setCanGrow(false);
	}
	
	protected void onFormModeChanged()
	{
		if(form.getMode().equals(FormMode.VIEW))
		{
			form.dyngrdDisplay().setReadOnly(true);
		}
		
		if(form.getMode().equals(FormMode.EDIT))
		{
			form.ctnDetail().lyrDetails().tabDevice().customInsertedHCP().setEnabledAuthoringHCP(Boolean.TRUE);
			form.ctnDetail().lyrDetails().tabDevice().customInsertedHCP().setEnabledDateTime(Boolean.TRUE);
		}
		
		if (form.dyngrdDisplay().getSelectedRow() != null)
		{
			Object obj = form.dyngrdDisplay().getSelectedRow().getIdentifier();
		
			if(obj instanceof PatientInvasiveDeviceShortVo)
			{
				if (form.getLocalContext().getEditedDeviceRecordIsNotNull())
				{
					getAndDisplayRecordDevice(form.getLocalContext().getEditedDeviceRecord());
				}
				else
				{
					getAndDisplayRecordDevice((PatientInvasiveDeviceShortVo)form.dyngrdDisplay().getSelectedRow().getIdentifier());		
				}
			}
			if(obj instanceof PatientInvasiveDeviceVipVo)
			{
				getAndDisplayRecordVip(form.getLocalContext().getEditedVipRecord());
			}
		}
		
		updateControlsState();	
	}

	protected void onDyngrdDisplayRowSelectionChanged(DynamicGridRow row)
	{
		Object obj = form.dyngrdDisplay().getSelectedRow().getIdentifier();
		
		if(obj instanceof PatientInvasiveDeviceShortVo)
		{
			getAndDisplayRecordDevice();
			
		}
		else if(obj instanceof PatientInvasiveDeviceVipVo)
		{
			getAndDisplayRecordVip();
			form.getLocalContext().setEditedDeviceRecord(null);
			form.ctnDetail().setCollapsed(form.dyngrdDisplay().getSelectedRow() == null);
		}
		updateControlsState();
	}
	
	private PatientInvasiveDeviceVo getAndDisplayRecordDevice()
	{	
		PatientInvasiveDeviceShortVo record = (PatientInvasiveDeviceShortVo)form.dyngrdDisplay().getSelectedRow().getIdentifier();
		return getAndDisplayRecordDevice(record);
	}
	
	private PatientInvasiveDeviceVo getAndDisplayRecordDevice(PatientInvasiveDeviceRefVo refRecord) 
	{	
		if(refRecord == null)
			return null;
		
		clearTabVipInstanceControls();
		clearTabDeviceInstanceControls();
		
		form.ctnDetail().lyrDetails().showtabDevice();
		form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(false);
		form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(true);
		
		PatientInvasiveDeviceVo record = domain.getPatientInvasiveDevice(refRecord);
		form.getLocalContext().setEditedDeviceRecord(record);
			
		populateScreenFromData(record);	
		
		return record;	
	}
	
	private PatientInvasiveDeviceVipVo getAndDisplayRecordVip()
	{	
		PatientInvasiveDeviceVipVo record = (PatientInvasiveDeviceVipVo)form.dyngrdDisplay().getSelectedRow().getIdentifier();
		
		return getAndDisplayRecordVip(record);
	}
	
	private PatientInvasiveDeviceVipVo getAndDisplayRecordVip(PatientInvasiveDeviceVipVo refRecord) 
	{
		if(refRecord == null)
			return null;
		
		clearTabVipInstanceControls();
		clearTabDeviceInstanceControls();
	
		form.ctnDetail().lyrDetails().showtabVIP();
		form.ctnDetail().lyrDetails().tabDevice().setHeaderVisible(false);
		form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(true);
		
		PatientInvasiveDeviceVipVo record = domain.getPatientInvasiveDeviceVip(refRecord);

		form.getLocalContext().setEditedVipRecord(record);
			
		populateScreenFromDataVip(record);	
		
		return record;	
	}
	
	private void populateDeviceCombo()
	{
		this.form.ctnDetail().lyrDetails().tabDevice().cmbDevice().clear();
		
		InvasiveDeviceConfigVoCollection list = domain.listFull();
		
		if(list != null)
		{
			for(int i = 0; i < list.size(); i++)
			{
				form.ctnDetail().lyrDetails().tabDevice().cmbDevice().newRow(list.get(i), list.get(i).getInvasiveDevice());
			}
		}		
	}
	
	private void populateRemovalHcpCombo()
	{	
		HcpLiteVo hcp = (HcpLiteVo)domain.getHcpLiteUser();
		
		if(hcp != null)
		{
			form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().newRow(hcp, hcp.toString());
			form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().setValue(hcp);
		}	
	}
	protected void onCmbDeviceValueChanged() throws PresentationLogicException 
	{
		if (form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue() != null)
		{
			form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(false);
			
			populateTypeAndSizeCombo();
			
			if (form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue().getRequiresVIP().equals(Boolean.TRUE))
			{
				form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(true);
				clearTabVipInstanceControls();
				form.ctnDetail().lyrDetails().tabVIP().customVipAuthoringHCP().initializeComponent();
			}
				
			if (form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue().getHasSiteIsNotNull() && 
					form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue().getHasSite().equals(Boolean.TRUE))
			{
				form.ctnDetail().lyrDetails().tabDevice().txtSite().setRequired(true);
			}
			else
			{
				form.ctnDetail().lyrDetails().tabDevice().txtSite().setRequired(false);
			}
				
			if (form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue().getDurationIntIsNotNull())
				onCustomInsertedHCPValueChanged();
			else
				form.ctnDetail().lyrDetails().tabDevice().dtimTargetRemDate().setValue(null);
		}
		//WDEV-2445
		else
		{
			form.ctnDetail().lyrDetails().tabDevice().txtSite().setRequired(false);
			form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(false);
			clearTypeAndSizeCombo();
			form.ctnDetail().lyrDetails().tabDevice().dtimTargetRemDate().setValue(null);
		}
	}
	
	private void clearTypeAndSizeCombo() 
	{
		form.ctnDetail().lyrDetails().tabDevice().cmbSize().clear();
		form.ctnDetail().lyrDetails().tabDevice().cmbType().clear();	
	}

	private void populateTypeAndSizeCombo()
	{
		InvasiveDeviceConfigVo voInvasiveDeviceConfig = (InvasiveDeviceConfigVo) form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue();
		
		if (voInvasiveDeviceConfig != null)
		{
			form.ctnDetail().lyrDetails().tabDevice().cmbSize().clear();
			form.ctnDetail().lyrDetails().tabDevice().cmbType().clear();
			 
			InvasiveDeviceTypeVoCollection listType = voInvasiveDeviceConfig.getType();
			
			if (listType == null)
				listType = new InvasiveDeviceTypeVoCollection();
					
			InvasiveDeviceConfigSizeVoCollection listSize = voInvasiveDeviceConfig.getSize();
			
				if (listSize == null)
					listSize = new InvasiveDeviceConfigSizeVoCollection();
			
			for (int i = 0; i < listType.size(); i++)
			{
				if (listType.get(i).getIsActive().equals(Boolean.TRUE))
				{
					form.ctnDetail().lyrDetails().tabDevice().cmbType().newRow(listType.get(i), listType.get(i).getType());
				}
			}
				
			for (int i = 0; i < listSize.size(); i++)
			{
				form.ctnDetail().lyrDetails().tabDevice().cmbSize().newRow(listSize.get(i), listSize.get(i).getSize());
			}
		}
	}
	
	protected ims.nursing.vo.PatientInvasiveDeviceVo populateDataFromScreenForDevice(ims.nursing.vo.PatientInvasiveDeviceVo record)
	{
		if(record == null)
			record = new ims.nursing.vo.PatientInvasiveDeviceVo();
		
		record.setInvasiveDevice(this.form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue());
		record.setType(this.form.ctnDetail().lyrDetails().tabDevice().cmbType().getValue());
		record.setSize(this.form.ctnDetail().lyrDetails().tabDevice().cmbSize().getValue());
		record.setSite(this.form.ctnDetail().lyrDetails().tabDevice().txtSite().getValue());
		record.setComment(this.form.ctnDetail().lyrDetails().tabDevice().txtDesc().getValue());
		
		if (this.form.ctnDetail().lyrDetails().tabDevice().customInsertedHCP().getValue() !=null)
		{
			record.setInsertedHCP(this.form.ctnDetail().lyrDetails().tabDevice().customInsertedHCP().getValue().getAuthoringHcp());
			record.setInsertedDateTime(this.form.ctnDetail().lyrDetails().tabDevice().customInsertedHCP().getValue().getAuthoringDateTime());
		}
		
		record.setTargetRemovalDateTime(this.form.ctnDetail().lyrDetails().tabDevice().dtimTargetRemDate().getValue());
		record.setRemovalDateTime(this.form.ctnDetail().lyrDetails().tabDevice().dtimRemDate().getValue());
		record.setRemovalHCP(this.form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().getValue());
		record.setRemovalReason(this.form.ctnDetail().lyrDetails().tabDevice().cmbRemovalReason().getValue());
		record.setSentForCulture(this.form.ctnDetail().lyrDetails().tabDevice().ansCulture().getValue());	
		record.setResult(this.form.ctnDetail().lyrDetails().tabDevice().txtResult().getValue());	
		
		if (this.form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().getValue() !=null)
		{
			record.setAuthoringInformation(this.form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().getValue());
		}
	
		return record;
	}
	
	protected PatientInvasiveDeviceVipVo populateDataFromScreenForVip(PatientInvasiveDeviceVipVo record)
	{
		if(record == null)
			record = new PatientInvasiveDeviceVipVo();
		
		record.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
		record.setClinicalContact(form.getGlobalContext().Core.getCurrentClinicalContact());
		
		record.setVIPScale(form.ctnDetail().lyrDetails().tabVIP().cmbVipSore().getValue());
		record.setAction(form.ctnDetail().lyrDetails().tabVIP().cmbAction().getValue());
		record.setAuthoringInformation(form.ctnDetail().lyrDetails().tabVIP().customVipAuthoringHCP().getValue());
		
		return record;
	}
	
	protected void populateScreenFromData(ims.nursing.vo.PatientInvasiveDeviceVo record)
	{
		clearTabDeviceInstanceControls();
		if(record == null)
			return;
						
		//populate device combo
		populateDeviceCombo();
		this.form.ctnDetail().lyrDetails().tabDevice().cmbDevice().setValue(record.getInvasiveDeviceIsNotNull() ? record.getInvasiveDevice() : null);

		if (record.getInvasiveDeviceIsNotNull() && this.form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue() == null)
		{
			InvasiveDeviceConfigVo voIDC = domain.getInvasiveDeviceConfig(record.getInvasiveDevice());
			
			this.form.ctnDetail().lyrDetails().tabDevice().cmbDevice().newRow(voIDC, voIDC.getInvasiveDevice());
			this.form.ctnDetail().lyrDetails().tabDevice().cmbDevice().setValue(voIDC);
		}
		
		//populate type and size
		populateTypeAndSizeCombo();
		form.ctnDetail().lyrDetails().tabDevice().cmbType().setValue(record.getType());
		form.ctnDetail().lyrDetails().tabDevice().cmbSize().setValue(record.getSize());
		
		this.form.ctnDetail().lyrDetails().tabDevice().txtSite().setValue(record.getSiteIsNotNull() ? record.getSite(): null);
		this.form.ctnDetail().lyrDetails().tabDevice().txtDesc().setValue(record.getCommentIsNotNull() ? record.getComment(): null);
			
		if(record.getInsertedHCPIsNotNull())
		{	
			if(record.getInsertedDateTimeIsNotNull() && record.getInsertedHCPIsNotNull())
			{
				AuthoringInformationVo InsertedHcpVo = new AuthoringInformationVo();
				InsertedHcpVo.setAuthoringDateTime(record.getInsertedDateTime());
				InsertedHcpVo.setAuthoringHcp(record.getInsertedHCP());
				
				form.ctnDetail().lyrDetails().tabDevice().customInsertedHCP().setValue(InsertedHcpVo);
			}
		}

		this.form.ctnDetail().lyrDetails().tabDevice().dtimTargetRemDate().setValue(record.getTargetRemovalDateTimeIsNotNull() ? record.getTargetRemovalDateTime() : null);
		this.form.ctnDetail().lyrDetails().tabDevice().dtimRemDate().setValue(record.getRemovalDateTimeIsNotNull() ? record.getRemovalDateTime() : null);
		
		if (record.getRemovalHCPIsNotNull())
		this.form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().newRow(record.getRemovalHCP(), record.getRemovalHCP().getMos().getName().toString());
		this.form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().setValue(record.getRemovalHCPIsNotNull() ? record.getRemovalHCP() : null);
		
		this.form.ctnDetail().lyrDetails().tabDevice().cmbRemovalReason().setValue(record.getRemovalReasonIsNotNull() ? record.getRemovalReason() : null);
		this.form.ctnDetail().lyrDetails().tabDevice().txtResult().setValue(record.getResultIsNotNull() ? record.getResult() : null);
		
		this.form.ctnDetail().lyrDetails().tabDevice().ansCulture().setValue(record.getSentForCulture());
		
		if(record.getAuthoringInformationIsNotNull())
		{
			if(record.getAuthoringInformation().getAuthoringDateTimeIsNotNull() && record.getAuthoringInformation().getAuthoringHcpIsNotNull())
			{
				form.ctnDetail().lyrDetails().tabDevice().customAuthoringHCP().setValue(record.getAuthoringInformation());
			}
		}
		
		form.ctnDetail().setCollapsed(false);
	}
	
	protected void populateScreenFromDataVip(ims.nursing.vo.PatientInvasiveDeviceVipVo record)
	{
		if(record == null)
			return;
						
		form.ctnDetail().lyrDetails().tabVIP().cmbVipSore().setValue(record.getVIPScaleIsNotNull() ? record.getVIPScale() : null);
		form.ctnDetail().lyrDetails().tabVIP().cmbAction().setValue(record.getActionIsNotNull() ? record.getAction() : null);
		
		if(record.getAuthoringInformationIsNotNull())
		{	
			if(record.getAuthoringInformation().getAuthoringDateTimeIsNotNull() &&	record.getAuthoringInformation().getAuthoringHcpIsNotNull())
			{
				form.ctnDetail().lyrDetails().tabVIP().customVipAuthoringHCP().setValue(record.getAuthoringInformation());
			}
		}
	}
	
	private void showRemoval() 
	{
		form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().setEnabled(true);
		form.ctnDetail().lyrDetails().tabDevice().cmbRemovalReason().setEnabled(true);
		form.ctnDetail().lyrDetails().tabDevice().ansCulture().setEnabled(true);
		form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().setRequired(true);	//wdev-13507
		form.ctnDetail().lyrDetails().tabDevice().cmbRemovalReason().setRequired(true); //wdev-13507
		form.ctnDetail().lyrDetails().tabDevice().ansCulture().setRequired(true); //wdev-13507
		populateRemovalHcpCombo();
	}
	
	private void hideRemoval() 
	{
		form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().setValue(null);
		form.ctnDetail().lyrDetails().tabDevice().cmbRemovalReason().setValue(null);
		form.ctnDetail().lyrDetails().tabDevice().ansCulture().setValue(null);
		form.ctnDetail().lyrDetails().tabDevice().qmbRemovalBy().setEnabled(false);
		form.ctnDetail().lyrDetails().tabDevice().cmbRemovalReason().setEnabled(false);
		form.ctnDetail().lyrDetails().tabDevice().ansCulture().setEnabled(false);
		
	}

	protected void onDtimRemDateValueChanged() throws PresentationLogicException 
	{
		if (form.ctnDetail().lyrDetails().tabDevice().dtimRemDate().getValue() !=null)
		{
			showRemoval();
		}
		else
		{
			hideRemoval();	
		}
	}

	protected void onCustomInsertedHCPValueChanged() throws PresentationLogicException
	{
		//WDEV-10949
		if (form.ctnDetail().lyrDetails().tabDevice().customInsertedHCP().getValue() != null
				&& form.ctnDetail().lyrDetails().tabDevice().customInsertedHCP().getValue().getAuthoringDateTimeIsNotNull()
				&& form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue() != null)
		{
			InvasiveDeviceConfigVo voInvasiveDeviceConfig = (InvasiveDeviceConfigVo) form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue();
			DateTime currentDate = new DateTime();

			if (voInvasiveDeviceConfig.getDurationIntIsNotNull())
			{
				Integer duration = voInvasiveDeviceConfig.getDurationInt();
				TimeUnitsSecondsToMonths durationUnit = voInvasiveDeviceConfig.getDurationUnit();
				if (durationUnit.equals(TimeUnitsSecondsToMonths.DAYS))
				{
					currentDate.addDays(duration.intValue());
				}
				if (durationUnit.equals(TimeUnitsSecondsToMonths.HOURS))
				{
					currentDate.addHours(duration.intValue());
				}
				if (durationUnit.equals(TimeUnitsSecondsToMonths.MINUTES))
				{
					currentDate.addMinutes(duration.intValue());
				}
				if (durationUnit.equals(TimeUnitsSecondsToMonths.MONTHS))
				{
					currentDate.addMonth(duration.intValue());
				}
				if (durationUnit.equals(TimeUnitsSecondsToMonths.SECONDS))
				{
					currentDate.addSeconds(duration.intValue());
				}
				if (durationUnit.equals(TimeUnitsSecondsToMonths.WEEKS))
				{
					currentDate.addDays(duration.intValue()*7);
				}
				form.ctnDetail().lyrDetails().tabDevice().dtimTargetRemDate().setValue(currentDate);
			}
			//WDEV-2445
			else
			{
				InvasiveDeviceTypeVo voInvasiveDeviceConfigType = (InvasiveDeviceTypeVo) form.ctnDetail().lyrDetails().tabDevice().cmbType().getValue();
				
				if(voInvasiveDeviceConfigType != null)
				{
    				Integer duration = voInvasiveDeviceConfigType.getDurationInt();
    				TimeUnitsSecondsToMonths durationUnit = voInvasiveDeviceConfigType.getDurationUnit();
    				if (durationUnit.equals(TimeUnitsSecondsToMonths.DAYS))
    				{
    					currentDate.addDays(duration.intValue());
    				}
    				if (durationUnit.equals(TimeUnitsSecondsToMonths.HOURS))
    				{
    					currentDate.addHours(duration.intValue());
    				}
    				if (durationUnit.equals(TimeUnitsSecondsToMonths.MINUTES))
    				{
    					currentDate.addMinutes(duration.intValue());
    				}
    				if (durationUnit.equals(TimeUnitsSecondsToMonths.MONTHS))
    				{
    					currentDate.addMonth(duration.intValue());
    				}
    				if (durationUnit.equals(TimeUnitsSecondsToMonths.SECONDS))
    				{
    					currentDate.addSeconds(duration.intValue());
    				}
    				if (durationUnit.equals(TimeUnitsSecondsToMonths.WEEKS))
    				{
    					currentDate.addDays(duration.intValue()*7);
    				}
    				form.ctnDetail().lyrDetails().tabDevice().dtimTargetRemDate().setValue(currentDate);
				}
			}
		}
	}
	//WDEV-2445 
	protected void onCmbTypeValueChanged() throws PresentationLogicException
	{
		if (form.ctnDetail().lyrDetails().tabDevice().cmbDevice().getValue() != null)
		{
			if (form.ctnDetail().lyrDetails().tabDevice().cmbType().getValue() != null)
			{
				onCustomInsertedHCPValueChanged();
			}
			else
			{
				form.ctnDetail().lyrDetails().tabDevice().dtimTargetRemDate().setValue(null);
				
				if (form.ctnDetail().lyrDetails().tabDevice().cmbDevice().isEnabled() == true)
				{
					form.ctnDetail().lyrDetails().tabDevice().cmbSize().clear();
					form.ctnDetail().lyrDetails().tabVIP().setHeaderVisible(false);
					form.ctnDetail().lyrDetails().tabDevice().cmbType().clear();
					form.ctnDetail().lyrDetails().tabDevice().cmbDevice().clear();
					populateDeviceCombo();
				}
			}
		}
	}
}
