/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/forms/planofcare/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 Neil McAnaspie using IMS Development Environment (version 1.45 build 2435.27832)
// Copyright (C) 1995-2006 IMS MAXIMS plc. All rights reserved.

package ims.nursing.forms.planofcare;

import ims.configuration.AppRight;
import ims.configuration.gen.ConfigFlag;
import ims.core.forms.authoringinfo.IComponent;
import ims.core.vo.AuthoringInformationVo;
import ims.core.vo.enums.AuthoringLabelType;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.Control;
import ims.framework.controls.DynamicGridCell;
import ims.framework.controls.DynamicGridRow;
import ims.framework.enumerations.DynamicCellType;
import ims.framework.enumerations.FormMode;
import ims.framework.exceptions.CodingRuntimeException;
import ims.framework.exceptions.PresentationLogicException;
import ims.framework.utils.Color;
import ims.nursing.helper.NursingSummaryHelper;
import ims.nursing.vo.PlanOfCareActionsVo;
import ims.nursing.vo.PlanOfCareActionsVoCollection;
import ims.nursing.vo.PlanOfCareHistoryVo;
import ims.nursing.vo.PlanOfCareHistoryVoCollection;
import ims.nursing.vo.PlanOfCareListVo;
import ims.nursing.vo.PlanOfCareListVoCollection;
import ims.nursing.vo.PlanOfCareLiteVo;
import ims.nursing.vo.PlanOfCareSearchCriteriaVo;
import ims.nursing.vo.PlanOfCareVo;
import ims.nursing.vo.lookups.PlanOfCareStatus;

import java.util.ArrayList;

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

	private static final Integer	COL_PLAN			= new Integer(0);
	private static final Integer	COL_STARTDATE		= new Integer(1);
	private static final Integer	COL_RESOLVEDDATE	= new Integer(2);
	private static final Integer	COL_URL				= new Integer(3);

	protected void onFormOpen() throws ims.framework.exceptions.PresentationLogicException
	{
		//WDEV-16234 check if user is HCP 
		form.getLocalContext().setLoggedInUserIsHCP(domain.getHcpLiteUser() != null);
		if (form.getGlobalContext().Nursing.getFormActionIsNotNull())
		{
			if (form.getGlobalContext().Nursing.getFormAction().equals("NEW"))
			{
				newPlanOfCare(GenForm.ContextMenus.PlanOfCare.NEWPOC);	
			}
			if (form.getGlobalContext().Nursing.getFormAction().equals("VIEW"))
			{
				viewPlanOfCare(GenForm.ContextMenus.PlanOfCare.VIEWPOC);
			}
		}
		else
		{
			initialize();
			
			//WDEV-19389 
			if (!(form.getGlobalContext().Core.getCurrentCareContextIsNotNull() && form.getGlobalContext().Nursing.getPlanOfCareSearchCriteriaIsNotNull() && form.getGlobalContext().Core.getCurrentCareContext().equals(form.getGlobalContext().Nursing.getPlanOfCareSearchCriteria().getCareContext())))
			{
				form.getGlobalContext().Nursing.setPlanOfCareSearchCriteria(null);
			}
			
			if(form.getGlobalContext().Nursing.getPlanOfCareSearchCriteriaIsNotNull())
			{
				setSearchCriteria(form.getGlobalContext().Nursing.getPlanOfCareSearchCriteria());
				try
				{
					onCmbStatusValueChanged();
				}
				catch (PresentationLogicException e)
				{
					e.printStackTrace();
				}
			}
			else
				open();
			//WDEV-19389 - end
		}
	}
	
	private PlanOfCareSearchCriteriaVo getSearchCriteria()
	{
		PlanOfCareSearchCriteriaVo searchCriteria = new PlanOfCareSearchCriteriaVo();
		
		searchCriteria.setFilter(form.lyrPOC().tabPocList().cmbStatus().getValue());
		searchCriteria.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
		
		return searchCriteria;
	}
	
	
	private void setSearchCriteria(PlanOfCareSearchCriteriaVo planOfCareSearchCriteriaVo)
	{
		form.lyrPOC().tabPocList().cmbStatus().setValue(planOfCareSearchCriteriaVo.getFilter());	
	}

	private void open()
	{
		form.setMode(FormMode.VIEW);
		listActivePlansOfCare();
	}

	private void listActivePlansOfCare()
	{
		form.lyrPOC().tabPocList().cmbStatus().setValue(PlanOfCareStatus.CURRENT);
		listPlansOfCareByStatus(form.lyrPOC().tabPocList().cmbStatus().getValue());	
	}

	private void initialize()
	{
		displayPocListTabOnly();
		form.getContextMenus().getPlanOfCareNEWPOCItem().setVisible(form.getLocalContext().getLoggedInUserIsHCP());

	}

	private void populateColorCombo() 
	{
		Color[] colors = Color.getColors();
		form.lyrPOC().tabPocDetails().cmbColour().clear();
		for (int i = 0; i < colors.length; i++)
		{
			//add only color associated with a POC- Red,Yellow and Green
			if (colors[i].getName().equals("Red") ||
					colors[i].getName().equals("Yellow") ||
					colors[i].getName().equals("Green") )
			{
				form.lyrPOC().tabPocDetails().cmbColour().newRow(colors[i], colors[i].getName(), colors[i].getImage());
			}
		}
	}

	private boolean returnFlagStatus()
	{
		return engine.hasRight(AppRight.CAN_CONFIRM_NURSING_DOCUMENTATION);
	}

	private void displayPocListTabOnly()
	{
		form.lyrPOC().tabPocDetails().setHeaderVisible(false);
		form.lyrPOC().showtabPocList();
		updateContextMenuState();
	}

	protected void onFormModeChanged()
	{
		updateControlsState();
	}

	protected void onChkConfirmValueChanged() throws ims.framework.exceptions.PresentationLogicException
	{
		if (form.lyrPOC().tabPocDetails().chkConfirm().getValue() == true)
		{
			showHideCustomComponent(form.lyrPOC().tabPocDetails().customConfirming(), Boolean.TRUE, "Confirm", Boolean.TRUE);
			form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.ACTIVE);
			onCmbStatusPOCValueChanged();
		}

		if (form.lyrPOC().tabPocDetails().chkConfirm().getValue() == false)
		{
			showHideCustomComponent(form.lyrPOC().tabPocDetails().customConfirming(), Boolean.FALSE, "Confirm", Boolean.FALSE);
			form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.REQUIRES_CONFIRMATION);
			onCmbStatusPOCValueChanged();
		}
	}

	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (save())
		{
			displayPocListTabOnly();
			open();
		}
	}

	private boolean save() 
	{
		PlanOfCareVo record = null;

		if (form.getLocalContext().getSelectedRecordIsNotNull())//is Update or View
		{ 
			record = populateDataFromScreen(form.getLocalContext().getSelectedRecord());		
		}
		else
		{
			record = populateDataFromScreen(new PlanOfCareVo());//is New
		}

		String[] uiErrors = getUiErrors();	
		String[] errors = record.validate(uiErrors);
		if(errors != null && errors.length > 0)
		{
			engine.showErrors(errors);
			return false;
		}
		try
		{
			record = domain.savePlanOfCare(record);
		}

		catch(StaleObjectException e)
		{
			engine.showMessage(ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
			updatePlanOfCare(GenForm.ContextMenus.PlanOfCare.UPDATEPOC);
			return false;
		}

		form.getLocalContext().setSelectedRecord(record);
		return true;
	}

	protected PlanOfCareVo populateDataFromScreen(PlanOfCareVo record)
	{
		if(record == null)
			record = new PlanOfCareVo();

		record.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());

		if (form.lyrPOC().tabPocDetails().customStart().getValue() !=null)
		{		
			record.setAuthoringInformation(form.lyrPOC().tabPocDetails().customStart().getValue());
		}

		if (form.lyrPOC().tabPocDetails().customStart().getValue() !=null)
		{
			record.setStartDateTime(form.lyrPOC().tabPocDetails().customStart().getValue().getAuthoringDateTime());
			record.setStartHCP(form.lyrPOC().tabPocDetails().customStart().getValue().getAuthoringHcp());
		}

		record.setLevelOfIntervention(form.lyrPOC().tabPocDetails().cmbColour().getValue());

		record.setGroup(form.lyrPOC().tabPocDetails().cmbGroup().getValue());	

		record.setTitle(form.lyrPOC().tabPocDetails().txtPlanOfCare().getValue());
		record.setStatus(form.lyrPOC().tabPocDetails().cmbStatusPOC().getValue());

		if (form.lyrPOC().tabPocDetails().cmbStatusPOC().getValue() != null &&
				form.lyrPOC().tabPocDetails().cmbStatusPOC().getValue().equals(PlanOfCareStatus.RESOLVED));
		{
			if (form.lyrPOC().tabPocDetails().customResolved().getValue() !=null)
			{
				record.setResolvedDateTime(form.lyrPOC().tabPocDetails().customResolved().getValue().getAuthoringDateTime());
				record.setResolvingHCP(form.lyrPOC().tabPocDetails().customResolved().getValue().getAuthoringHcp());
			}
		}

		if (form.lyrPOC().tabPocDetails().chkConfirm().getValue() != false)
		{
			if (form.lyrPOC().tabPocDetails().customConfirming().getValue() !=null)
			{
				record.setConfirmingDateTime(form.lyrPOC().tabPocDetails().customConfirming().getValue().getAuthoringDateTime());
				record.setConfirmingHCP(form.lyrPOC().tabPocDetails().customConfirming().getValue().getAuthoringHcp());
			}	
		}

		return record;
	}

	protected PlanOfCareVo populateDataFromScreen()
	{
		return populateDataFromScreen(new PlanOfCareVo());
	}

	private String[] getUiErrors() 
	{
		ArrayList<String> errors = new ArrayList<String>();

		if (form.lyrPOC().tabPocDetails().customStart().getValue() != null) 
		{
			if (form.lyrPOC().tabPocDetails().customStart().getValue().getAuthoringHcp() == null) 
			{
				errors.add("Start Hcp is mandatory");
			}
			if (form.lyrPOC().tabPocDetails().customStart().getValue().getAuthoringDateTime() == null) 
			{
				errors.add("Start Date/Time is mandatory");
			}
		}

		if (form.lyrPOC().tabPocDetails().cmbColour().getValue() == null) 
		{
			errors.add("Level of Intervention is mandatory");
		}

		if (form.lyrPOC().tabPocDetails().cmbStatusPOC().getValue() == null) 
		{
			errors.add("Status is mandatory");
		}

		if (form.lyrPOC().tabPocDetails().txtPlanOfCare().getValue() == null) 
		{
			errors.add("Plan of Care is mandatory");
		}

		if (form.lyrPOC().tabPocDetails().cmbStatusPOC().getValue() != null)
		{
			if (form.lyrPOC().tabPocDetails().cmbStatusPOC().getValue().equals(PlanOfCareStatus.RESOLVED))
			{
				if (form.lyrPOC().tabPocDetails().cmbColour().getValue() != null)
				{
					//verify if POC is yellow or red; only this can be resolved
					if (!(form.lyrPOC().tabPocDetails().cmbColour().getValue().getText().equals(Color.Green.getText()) ||
							form.lyrPOC().tabPocDetails().cmbColour().getValue().getText().equals(Color.Red.getText()) ||
							form.lyrPOC().tabPocDetails().cmbColour().getValue().getText().equals(Color.Yellow.getText())))
					{
						errors.add("Only Plan Of Care with red/yellow/green colors can be resolved!!");

						form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.ACTIVE);	
						showHideCustomComponent(form.lyrPOC().tabPocDetails().customResolved(), Boolean.FALSE, null, Boolean.FALSE);
					}
				}
			}		
		}	

		if (form.lyrPOC().tabPocDetails().chkConfirm().getValue() == true)
		{
			if (form.lyrPOC().tabPocDetails().customConfirming().getValue() == null) 
			{ 
				errors.add(form.lyrPOC().tabPocDetails().customConfirming().getErrors());//WDEV-15090
			}	
		}

		if (PlanOfCareStatus.RESOLVED.equals(form.lyrPOC().tabPocDetails().cmbStatusPOC().getValue()))//WDEV-15094
		{
			/*if (form.lyrPOC().tabPocDetails().customResolved().getErrors().getAuthoringHcp() == null) 
			{
					errors.add("Resolved Hcp is mandatory");
			}
			if (form.lyrPOC().tabPocDetails().customResolved().getValue().getAuthoringDateTime() == null) 
			{
				errors.add("Resolved Date/Time is mandatory");
			}
			 */
			String resolveErrors = form.lyrPOC().tabPocDetails().customResolved().getErrors();//WDEV-15094
			if(resolveErrors != null && resolveErrors.length() > 0)//WDEV-15094
			{	
				errors.add(resolveErrors);//WDEV-15094
			}
		}

		if (errors.size() > 0) 
		{
			String[] searchErrors = new String[errors.size()];
			errors.toArray(searchErrors);
			engine.showErrors("Invalid Property Checklist Record", searchErrors);
			return searchErrors;
		}
		return null;
	}

	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{
		form.setMode(FormMode.VIEW);
		initialize();
		open();
	}

	protected void onCmbStatusValueChanged() throws PresentationLogicException
	{
		if (form.lyrPOC().tabPocList().cmbStatus().getValue() !=  null)
		{
			listPlansOfCareByStatus(form.lyrPOC().tabPocList().cmbStatus().getValue());	
		}
		else
		{
			form.lyrPOC().tabPocList().cmbStatus().setValue(PlanOfCareStatus.CURRENT);
			listPlansOfCareByStatus(PlanOfCareStatus.CURRENT);
		}	
		
		form.getGlobalContext().Nursing.setPlanOfCareSearchCriteria(getSearchCriteria());//WDEV-19389 
	}

	private void listPlansOfCareByStatus(PlanOfCareStatus status)
	{
		if (status != null)
		{
			PlanOfCareListVoCollection coll = domain.listPlansofCareByStatus(form.getGlobalContext().Core.getCurrentCareContext(), status);
			NursingSummaryHelper helper = new NursingSummaryHelper(form.lyrPOC().tabPocList().dyngrdPOC(), form.getImages().Core.RedSqaure, form.getImages().Core.GreenSquare, form.getImages().Core.YellowSquare, form.getImages().Core.Information, coll);
			helper.displayActionsGroupByPlan(coll, false);
			updateContextMenuState();
		}
	}

	private void clearGrdDPPList()
	{
		form.lyrPOC().tabPocDetails().grdDPP().getRows().clear();
		form.lyrPOC().tabPocDetails().grdDPP().setValue(null);
	}

	private void clearGrdPocActions()
	{
		form.lyrPOC().tabPocDetails().grdPocActions().getRows().clear();
		form.lyrPOC().tabPocDetails().grdPocActions().setValue(null);
	}

	protected void onContextMenuItemClick(int menuItemID, Control sender) throws PresentationLogicException
	{
		if (sender == null)
			throw new CodingRuntimeException("sender is null : onContextMenuItemClick");
		if (sender.equals(form.lyrPOC().tabPocList().dyngrdPOC()))
		{
			switch (menuItemID)
			{
			case GenForm.ContextMenus.PlanOfCare.NEWPOC :
				newPlanOfCare(menuItemID);
				break;
			case GenForm.ContextMenus.PlanOfCare.UPDATEPOC :
				updatePlanOfCare(menuItemID);
				break;
			case GenForm.ContextMenus.PlanOfCare.VIEWPOC :
				viewPlanOfCare(menuItemID);
				break;
			}
		}
	}

	private void viewPlanOfCare(int menuItemID) 
	{
		clearPocDetailsInstanceControls();
		form.setMode(FormMode.EDIT);
		displayPocDetailsTabOnly(menuItemID, null);
	}

	private void updateControlsState()
	{
		updateContextMenuState();
	}

	private void updatePlanOfCare(int menuItemID) 
	{	
		clearPocDetailsInstanceControls();
		form.setMode(FormMode.EDIT);

		PlanOfCareStatus pocStatus = null;

		if (form.getLocalContext().getSelectedRecordIsNotNull())
		{
			pocStatus = domain.getPlanOfCare(form.getLocalContext().getSelectedRecord()).getStatus();

		}

		populateStatusPocTypeCombo(menuItemID, pocStatus);
		displayPocDetailsTabOnly(menuItemID, pocStatus);
	}

	private void updateContextMenuState()
	{

		//WDEV-16234 check if user is HCP 
		form.getContextMenus().getPlanOfCareNEWPOCItem().setVisible(form.getLocalContext().getLoggedInUserIsHCP());
		form.getContextMenus().getPlanOfCareUPDATEPOCItem().setVisible(false);
		form.getContextMenus().getPlanOfCareVIEWPOCItem().setVisible(false);

		if (form.lyrPOC().tabPocList().dyngrdPOC().getSelectedRow() != null)
		{
			Object obj = form.lyrPOC().tabPocList().dyngrdPOC().getSelectedRow().getValue();

			if(obj instanceof PlanOfCareListVo ||
					obj instanceof PlanOfCareActionsVo)
			{
				if (form.getMode().equals(FormMode.VIEW)) 
				{
					if(obj instanceof PlanOfCareListVo)
					{
						PlanOfCareListVo  rowSel = (PlanOfCareListVo) 
								form.lyrPOC().tabPocList().dyngrdPOC().getSelectedRow().getValue();
						//WDEV-16234 
						form.getContextMenus().getPlanOfCareUPDATEPOCItem().setVisible(!rowSel.getResolvedDateTimeIsNotNull() && form.getLocalContext().getLoggedInUserIsHCP());
					}

					if(obj instanceof PlanOfCareActionsVo)
					{
						PlanOfCareActionsVo  rowSel = (PlanOfCareActionsVo) 
								form.lyrPOC().tabPocList().dyngrdPOC().getSelectedRow().getValue();
						//WDEV-16234 
						form.getContextMenus().getPlanOfCareUPDATEPOCItem().setVisible(!rowSel.getPlanOfCare().getResolvedDateTimeIsNotNull() && form.getLocalContext().getLoggedInUserIsHCP());
					}
					form.getContextMenus().getPlanOfCareVIEWPOCItem().setVisible(form.lyrPOC().tabPocList().dyngrdPOC().getSelectedRow() != null);
				}
			}
			if (form.getMode().equals(FormMode.EDIT)) 
			{
				form.getContextMenus().getPlanOfCareNEWPOCItem().setVisible(false);
				form.getContextMenus().getPlanOfCareUPDATEPOCItem().setVisible(false);
				form.getContextMenus().getPlanOfCareVIEWPOCItem().setVisible(false);
			}
		}
	}

	private void newPlanOfCare(int menuItemID)
	{
		clearPocDetailsInstanceControls();
		form.setMode(FormMode.EDIT);
		form.getLocalContext().setSelectedRecord(null);
		displayPocDetailsTabOnly(menuItemID, null);
		populateStatusPocTypeCombo(menuItemID, null);
	}

	private void listDailyPatientProgressHistory(Boolean haveDPP, PlanOfCareVo record)
	{
		clearGrdDPPList();
		if (haveDPP != null && record != null)
		{
			if (haveDPP.equals(Boolean.TRUE))
			{
				populateGrdDPP(domain.listDailyPatientProgressHistory(form.getGlobalContext().Core.getCurrentCareContext(), record));
			}
		}
	}

	private void listPocActions(PlanOfCareVo record)
	{
		clearGrdPocActions();
		if (record != null)
		{
			populateGrdPocActions(domain.listPocActions(form.getGlobalContext().Core.getCurrentCareContext(), record));
		}
		else
		{
			populateGrdPocActions(domain.listPocActions(form.getGlobalContext().Core.getCurrentCareContext(), null));
		}
	}

	private void populateGrdDPP(PlanOfCareHistoryVoCollection list) 
	{
		if (list != null)
		{
			NursingSummaryHelper helper = new NursingSummaryHelper(null, null, null, null,null, list);
			helper.sortDesc();			

			ims.nursing.forms.planofcare.GenForm.lyrPOCLayer.tabPocDetailsContainer.grdDPPRow row = null;

			for(int i=0; i< list.size(); i++)
			{
				PlanOfCareHistoryVo voHistoryItem = list.get(i);
				row = form.lyrPOC().tabPocDetails().grdDPP().getRows().newRow();

				if (voHistoryItem.getAuthoringInformation().getAuthoringDateTimeIsNotNull())
					row.setColDateTime(voHistoryItem.getAuthoringInformation().getAuthoringDateTime().toString());
				if (voHistoryItem.getAuthoringInformation().getAuthoringHcpIsNotNull())
					row.setColHcps(voHistoryItem.getAuthoringInformation().getAuthoringHcp().toString());
				if (voHistoryItem.getAnswerTextIsNotNull())
					row.setColDPP(voHistoryItem.getAnswerText());

				row.setBackColor(voHistoryItem.getLevelOfIntervention());
				row.setValue(voHistoryItem);
			}
		}	
	}

	private void populateGrdPocActions(PlanOfCareActionsVoCollection list) 
	{
		if (list != null)
		{
			NursingSummaryHelper helper = new NursingSummaryHelper(null, null, null, null,null, list);
			helper.sortDesc();

			ims.nursing.forms.planofcare.GenForm.lyrPOCLayer.tabPocDetailsContainer.grdPocActionsRow row = null;

			for(int i=0; i< list.size(); i++)
			{
				PlanOfCareActionsVo voActionsItem = list.get(i);
				row = form.lyrPOC().tabPocDetails().grdPocActions().getRows().newRow();

				if (voActionsItem.getAuthoringInformationIsNotNull())
					row.setColAuthoringInfo(voActionsItem.getAuthoringInformation().toString());
				if (voActionsItem.getActionIsNotNull())
					row.setColAction(voActionsItem.getAction().toString());
				if (voActionsItem.getReviewIsNotNull())
				{
					row.setColReview(voActionsItem.getReview());
				}

				row.setValue(voActionsItem);
			}
		}

	}

	private void clearPocDetailsInstanceControls()
	{
		form.lyrPOC().tabPocDetails().chkConfirm().setValue(false);
		form.lyrPOC().tabPocDetails().cmbColour().setValue(null);
		form.lyrPOC().tabPocDetails().cmbGroup().setValue(null);
		form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(null);
		form.lyrPOC().tabPocDetails().customConfirming().setValue(null);
		form.lyrPOC().tabPocDetails().customResolved().setValue(null);
		form.lyrPOC().tabPocDetails().customStart().setValue(null);
		form.lyrPOC().tabPocDetails().grdDPP().getRows().clear();
		form.lyrPOC().tabPocDetails().grdPocActions().getRows().clear();
		form.lyrPOC().tabPocDetails().txtPlanOfCare().setValue(null);
	}

	private void displayPocDetailsTabOnly(int menuItemID, PlanOfCareStatus pocStatus)
	{
		form.lyrPOC().showtabPocDetails();
		form.lyrPOC().tabPocDetails().setHeaderVisible(true);
		form.lyrPOC().tabPocList().setHeaderVisible(false);
		//WDEV-16234
		boolean isPOCConfirmed = (form.getLocalContext().getSelectedRecordIsNotNull() && form.getLocalContext().getSelectedRecord().getConfirmingHCPIsNotNull());
		initializePocDetailsTab(true,isPOCConfirmed);
		form.btnSave().setVisible(true);

		PlanOfCareVo record = new PlanOfCareVo();
		form.getLocalContext().setSelectedRecord(null);

		if (menuItemID == GenForm.ContextMenus.PlanOfCare.UPDATEPOC ||
				menuItemID == GenForm.ContextMenus.PlanOfCare.VIEWPOC)
		{
			if (form.getGlobalContext().Nursing.getFormActionIsNotNull() &&
					form.getGlobalContext().Nursing.getFormAction().equals("VIEW"))
			{
				PlanOfCareVo recordID = new PlanOfCareVo();
				recordID.setID_PlanOfCare(form.getGlobalContext().Nursing.getPlanOfCareId());
				record = domain.getPlanOfCare(recordID);
				form.getLocalContext().setSelectedRecord(record);
			}
			else
			{
				Object obj = form.lyrPOC().tabPocList().dyngrdPOC().getSelectedRow().getValue();

				if(obj instanceof PlanOfCareListVo)		
				{
					PlanOfCareLiteVo voRow = (PlanOfCareLiteVo) form.lyrPOC().tabPocList().dyngrdPOC().getSelectedRow().getValue();
					record = domain.getPlanOfCare(voRow);
					form.getLocalContext().setSelectedRecord(record);
				}
				if(obj instanceof PlanOfCareActionsVo || obj instanceof PlanOfCareVo)
				{
					PlanOfCareActionsVo voRow = (PlanOfCareActionsVo) form.lyrPOC().tabPocList().dyngrdPOC().getSelectedRow().getValue();
					record = domain.getPlanOfCare(voRow.getPlanOfCare());
					form.getLocalContext().setSelectedRecord(record);
				}
			}
		}

		if (menuItemID == GenForm.ContextMenus.PlanOfCare.NEWPOC)
		{
			//set default to ACTIVE
			form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.ACTIVE);
			//listPocActions(null);
			listDailyPatientProgressHistory(null, null);
			form.btnCancel().setText("Cancel");

			if (!returnFlagStatus())
			{
				form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.REQUIRES_CONFIRMATION);
				form.lyrPOC().tabPocDetails().chkConfirm().setEnabled(false);
				form.lyrPOC().tabPocDetails().customConfirming().setEnabledAuthoringHCP(Boolean.FALSE);
				form.lyrPOC().tabPocDetails().customConfirming().setEnabledDateTime(Boolean.FALSE);

			}
			else 
			{
				form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.ACTIVE);
				form.lyrPOC().tabPocDetails().chkConfirm().setEnabled(true);
			}
		}

		if (menuItemID == GenForm.ContextMenus.PlanOfCare.UPDATEPOC)
		{
			initializePocDetailsTab(true,isPOCConfirmed); //WDEV-16234
			form.btnCancel().setText("Cancel");

			if (record.getStartDateTimeIsNotNull() &&
					record.getStartHCPIsNotNull())
			{
				form.lyrPOC().tabPocDetails().customStart().setValue(null);	
				setCustomCompomentValue(form.lyrPOC().tabPocDetails().customStart(), record);
			}
			//WDEV-16234 
			if (record.getConfirmingDateTimeIsNotNull() && record.getConfirmingHCPIsNotNull())
			{
				form.lyrPOC().tabPocDetails().cmbStatusPOC().removeRow(PlanOfCareStatus.REQUIRES_CONFIRMATION);
			}
			if (record.getGroup() != null)
			{
				form.lyrPOC().tabPocDetails().cmbGroup().setValue(record.getGroup());
				form.lyrPOC().tabPocDetails().cmbGroup().setEnabled(false);
			}

			form.lyrPOC().tabPocDetails().txtPlanOfCare().setValue(record.getTitle());

			if (record.getAssessmentQuestionIsNotNull()) // is POC from DPP
			{
				form.lyrPOC().tabPocDetails().txtPlanOfCare().setEnabled(false);
				listDailyPatientProgressHistory(Boolean.TRUE, record);
			}
			else
			{
				form.lyrPOC().tabPocDetails().txtPlanOfCare().setEnabled(true);
				listDailyPatientProgressHistory(null, null);
			}

			form.lyrPOC().tabPocDetails().cmbColour().setValue(record.getLevelOfIntervention());

			if (!returnFlagStatus())
			{
				if (pocStatus != null)
				{
					if (pocStatus.getText().equals(PlanOfCareStatus.ACTIVE.getText()))
					{
						form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.ACTIVE);
					}
					if (pocStatus.getText().equals(PlanOfCareStatus.REQUIRES_CONFIRMATION.getText()) && !isPOCConfirmed) //WDEV-16234
					{
						form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.REQUIRES_CONFIRMATION);
					}
					form.lyrPOC().tabPocDetails().chkConfirm().setEnabled(!isPOCConfirmed);//WDEV-16234
					form.lyrPOC().tabPocDetails().customConfirming().setEnabledAuthoringHCP(Boolean.FALSE);
					form.lyrPOC().tabPocDetails().customConfirming().setEnabledDateTime(Boolean.FALSE);
				}

			}
			else 
			{
				form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.ACTIVE);
				//form.lyrPOC().tabPocDetails().chkConfirm().setEnabled(true);
			}


			form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(record.getStatus());

			if (record.getStatusIsNotNull() &&
					record.getStatus().equals(PlanOfCareStatus.RESOLVED))
			{
				form.lyrPOC().tabPocDetails().customResolved().setValue(null);
				form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.RESOLVED);

				if (record.getResolvedDateTimeIsNotNull() &&
						record.getResolvingHCPIsNotNull())
				{
					setCustomCompomentValue(form.lyrPOC().tabPocDetails().customResolved(), record);
				}
			}

			if (record.getConfirmingDateTimeIsNotNull() &&
					record.getConfirmingHCPIsNotNull())
			{
				form.lyrPOC().tabPocDetails().chkConfirm().setValue(true);
				form.lyrPOC().tabPocDetails().customConfirming().setValue(null);

				setCustomCompomentValue(form.lyrPOC().tabPocDetails().customConfirming(), record);
			}

			listPocActions(record);

			if (record.getAssessmentQuestionIsNotNull())
			{
				listDailyPatientProgressHistory(Boolean.TRUE, record);	
			}
			updateControlsState();
		}

		if (menuItemID == GenForm.ContextMenus.PlanOfCare.VIEWPOC)
		{
			initializePocDetailsTab(false,true);//WDEV-16234
			form.btnCancel().setText("Close");

			if (record.getStartDateTimeIsNotNull() &&
					record.getStartHCPIsNotNull())
			{
				form.lyrPOC().tabPocDetails().setcustomStartEnabled(false);
				form.lyrPOC().tabPocDetails().customStart().setValue(null);

				setCustomCompomentValue(form.lyrPOC().tabPocDetails().customStart(), record);
			}

			form.lyrPOC().tabPocDetails().txtPlanOfCare().setValue(record.getTitle());
			form.lyrPOC().tabPocDetails().txtPlanOfCare().setEnabled(false);
			form.lyrPOC().tabPocDetails().cmbColour().setValue(record.getLevelOfIntervention());
			form.lyrPOC().tabPocDetails().cmbColour().setEnabled(false);
			form.lyrPOC().tabPocDetails().cmbGroup().setValue(record.getGroup());
			form.lyrPOC().tabPocDetails().cmbGroup().setEnabled(false);
			form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(record.getStatus());
			form.lyrPOC().tabPocDetails().cmbStatusPOC().setEnabled(false);

			if (record.getStatusIsNotNull() &&
					record.getStatus().equals(PlanOfCareStatus.RESOLVED))
			{
				form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.RESOLVED);

				form.lyrPOC().tabPocDetails().setcustomResolvedEnabled(false);
				form.lyrPOC().tabPocDetails().customResolved().setValue(null);

				if (record.getResolvedDateTimeIsNotNull() &&
						record.getResolvingHCPIsNotNull())
				{
					setCustomCompomentValue(form.lyrPOC().tabPocDetails().customResolved(), record);
				}
			}

			if (record.getConfirmingDateTimeIsNotNull() &&
					record.getConfirmingHCPIsNotNull())
			{
				form.lyrPOC().tabPocDetails().chkConfirm().setValue(true);
				form.lyrPOC().tabPocDetails().chkConfirm().setEnabled(false);

				form.lyrPOC().tabPocDetails().setcustomConfirmingEnabled(false);
				form.lyrPOC().tabPocDetails().customConfirming().setValue(null);

				setCustomCompomentValue(form.lyrPOC().tabPocDetails().customConfirming(), record);
			}

			listPocActions(record);

			if (record.getAssessmentQuestionIsNotNull())
			{
				listDailyPatientProgressHistory(Boolean.TRUE, record);	
			}

			form.btnSave().setVisible(false);
		}
	}

	private void setCustomCompomentValue(IComponent customControl, PlanOfCareVo record) 
	{
		AuthoringInformationVo voAuthoringStart = new AuthoringInformationVo();
		if (customControl.equals(form.lyrPOC().tabPocDetails().customStart()))
		{
			voAuthoringStart.setAuthoringDateTime(record.getStartDateTime());
			voAuthoringStart.setAuthoringHcp(record.getStartHCP());
		}
		if (customControl.equals(form.lyrPOC().tabPocDetails().customResolved()))
		{
			voAuthoringStart.setAuthoringDateTime(record.getResolvedDateTime());
			voAuthoringStart.setAuthoringHcp(record.getResolvingHCP());
		}
		if (customControl.equals(form.lyrPOC().tabPocDetails().customConfirming()))
		{
			voAuthoringStart.setAuthoringDateTime(record.getConfirmingDateTime());
			voAuthoringStart.setAuthoringHcp(record.getConfirmingHCP());
		}

		customControl.setValue(voAuthoringStart);
	}

	private void populateStatusPocTypeCombo(int id, PlanOfCareStatus pocStatus)
	{
		form.lyrPOC().tabPocDetails().cmbStatusPOC().clear();

		ims.nursing.vo.lookups.PlanOfCareStatusCollection 
		lookupCollection = ims.nursing.vo.lookups.LookupHelper.getPlanOfCareStatus
		(this.domain.getLookupService());
		//WDEV-16234
		boolean isPOCConfirmed = form.getLocalContext().getSelectedRecordIsNotNull() && form.getLocalContext().getSelectedRecord().getConfirmingHCPIsNotNull() && form.getLocalContext().getSelectedRecord().getConfirmingDateTimeIsNotNull();

		if (lookupCollection != null)
		{
			for(int x = 0; x < lookupCollection.size(); x++)
			{	 
				if (!returnFlagStatus())
				{
					if (id == GenForm.ContextMenus.PlanOfCare.NEWPOC)
					{
						if (lookupCollection.get(x).getText().equals(PlanOfCareStatus.REQUIRES_CONFIRMATION.getText())) //||
							//lookupCollection.get(x).getText().equals(PlanOfCareStatus.ACTIVE.getText()))
						{
							this.form.lyrPOC().tabPocDetails().cmbStatusPOC().newRow
							(lookupCollection.get(x), lookupCollection.get(x).getText(), 
									lookupCollection.get(x).getImage(), lookupCollection.get(x).getTextColor());
							form.lyrPOC().tabPocDetails().chkConfirm().setEnabled(false);
						}
					}
					if (id == GenForm.ContextMenus.PlanOfCare.UPDATEPOC)
					{
						if (pocStatus != null)
						{
							if (pocStatus.getText().equals(PlanOfCareStatus.ACTIVE.getText()))
							{
								if (lookupCollection.get(x).getText().equals(PlanOfCareStatus.ACTIVE.getText()))
								{
									this.form.lyrPOC().tabPocDetails().cmbStatusPOC().newRow
									(lookupCollection.get(x), lookupCollection.get(x).getText(), 
											lookupCollection.get(x).getImage(), lookupCollection.get(x).getTextColor());
									form.lyrPOC().tabPocDetails().chkConfirm().setEnabled(false);
									form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.ACTIVE);
								}
							}

							if (pocStatus.getText().equals(PlanOfCareStatus.REQUIRES_CONFIRMATION.getText()))
							{
								if (lookupCollection.get(x).getText().equals(PlanOfCareStatus.REQUIRES_CONFIRMATION.getText()))
								{
									this.form.lyrPOC().tabPocDetails().cmbStatusPOC().newRow
									(lookupCollection.get(x), lookupCollection.get(x).getText(), 
											lookupCollection.get(x).getImage(), lookupCollection.get(x).getTextColor());
									form.lyrPOC().tabPocDetails().chkConfirm().setEnabled(!isPOCConfirmed);//WDEV-16234

									form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.REQUIRES_CONFIRMATION);
								}
							}
						}

					}
				}

				if (returnFlagStatus())
				{
					if (!lookupCollection.get(x).getText().equals(PlanOfCareStatus.CURRENT.getText()))
					{
						this.form.lyrPOC().tabPocDetails().cmbStatusPOC().newRow
						(lookupCollection.get(x), lookupCollection.get(x).getText(), 
								lookupCollection.get(x).getImage(), lookupCollection.get(x).getTextColor());
						form.lyrPOC().tabPocDetails().chkConfirm().setEnabled(!isPOCConfirmed); //WDEV-16234
						form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.ACTIVE);
					}
				}

			}
			/*if (!returnFlagStatus())
			{
				form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.REQUIRES_CONFIRMATION);
			}
			else
			{
				form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.ACTIVE);
			}*/
		}
	}

	private void initializePocDetailsTab(boolean value, boolean isPOCConfirmed) //WDEV-16234 added second parameter
	{	
		initializeCustomComponents();

		form.lyrPOC().tabPocDetails().setcustomStartEnabled(false);
		form.lyrPOC().tabPocDetails().cmbColour().setEnabled(value);
		populateColorCombo();
		form.lyrPOC().tabPocDetails().cmbGroup().setEnabled(value);
		form.lyrPOC().tabPocDetails().cmbStatusPOC().setEnabled(value);
		form.lyrPOC().tabPocDetails().txtPlanOfCare().setEnabled(value);
		form.lyrPOC().tabPocDetails().setcustomResolvedEnabled(value);
		form.lyrPOC().tabPocDetails().chkConfirm().setEnabled(!isPOCConfirmed);//WDEV-16234
		form.lyrPOC().tabPocDetails().setcustomConfirmingEnabled(value);
	}

	private void initializeCustomComponents()
	{
		showHideCustomComponent(form.lyrPOC().tabPocDetails().customStart(), Boolean.TRUE, "Start", null);
		showHideCustomComponent(form.lyrPOC().tabPocDetails().customResolved(), Boolean.FALSE, "Resolved",null);
		showHideCustomComponent(form.lyrPOC().tabPocDetails().customConfirming(), Boolean.FALSE, "Confirm",null);
	}

	protected void onCmbStatusPOCValueChanged() throws PresentationLogicException 
	{
		if (form.lyrPOC().tabPocDetails().cmbStatusPOC().getValue() != null)
		{
			if (form.lyrPOC().tabPocDetails().cmbStatusPOC().getValue().equals(PlanOfCareStatus.RESOLVED))
			{
				showHideCustomComponent(form.lyrPOC().tabPocDetails().customResolved(), Boolean.TRUE, null, Boolean.TRUE);
			}
			else
			{
				showHideCustomComponent(form.lyrPOC().tabPocDetails().customResolved(), Boolean.FALSE, null, Boolean.FALSE);
			}
		}
		else
		{
			/*if (!returnFlagStatus()) //WDEV-15094
			{
				form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue((PlanOfCareStatus)form.lyrPOC().tabPocDetails().cmbStatusPOC().getValues().get(0));
			}
			if (returnFlagStatus())
			{
				engine.showMessage("Select Active, Resolved or Requires Confirmation");
				form.lyrPOC().tabPocDetails().cmbStatusPOC().setValue(PlanOfCareStatus.ACTIVE);	
			}*/

			showHideCustomComponent(form.lyrPOC().tabPocDetails().customResolved(), Boolean.FALSE, null, Boolean.FALSE);
		}
	}

	protected void showHideCustomComponent(IComponent customControl, Boolean isEnabled, String setLabel, Boolean isRequire)
	{	
		if (customControl != null)
		{
			customControl.setEnabledAuthoringHCP(isEnabled);
			customControl.setEnabledDateTime(isEnabled);
		}
		if (isEnabled != null &&
				customControl != null)
		{
			if (isEnabled.equals(Boolean.TRUE))
			{
				customControl.initializeComponent();
			}
			if (isEnabled.equals(Boolean.FALSE))
			{
				customControl.setValue(null);
			}
		}
		if (setLabel != null &&
				customControl != null)
		{
			if (setLabel.equals("Start") ||
					setLabel.equals("Confirm") || 
					setLabel.equals("Resolved"))
			{
				if (setLabel.equals("Start"))
					customControl.setLabels(AuthoringLabelType.START);
				if (setLabel.equals("Confirm"))
					customControl.setLabels(AuthoringLabelType.CONFIRMED);
				if (setLabel.equals("Resolved"))
					customControl.setLabels(AuthoringLabelType.RESOLVING);
			}
		}
		if (isRequire != null &&
				customControl != null)
		{
			customControl.setIsRequiredPropertyToControls(isRequire);
		}
	}

	protected void onDyngrdPOCRowSelectionChanged(DynamicGridRow row) 
	{
		updateContextMenuState();
		PlanOfCareVo record = new PlanOfCareVo();
		Object obj = form.lyrPOC().tabPocList().dyngrdPOC().getSelectedRow().getValue();

		if(obj instanceof PlanOfCareListVo)		
		{
			PlanOfCareLiteVo voRow = (PlanOfCareLiteVo) form.lyrPOC().tabPocList().dyngrdPOC().getSelectedRow().getValue();
			record = domain.getPlanOfCare(voRow);
			form.getLocalContext().setSelectedRecord(record);
		}
		if(obj instanceof PlanOfCareActionsVo)
		{
			PlanOfCareActionsVo voRow = (PlanOfCareActionsVo) form.lyrPOC().tabPocList().dyngrdPOC().getSelectedRow().getValue();
			record = domain.getPlanOfCare(voRow.getPlanOfCare());
			form.getLocalContext().setSelectedRecord(record);
		}
	}

	protected void onDyngrdPOCCellButtonClicked(DynamicGridCell cell) 
	{
		if (cell != null && cell.getType().equals(DynamicCellType.IMAGEBUTTON))
		{
			if(cell.getRow().getValue() instanceof PlanOfCareListVo)
			{
				PlanOfCareVo record = new PlanOfCareVo();
				PlanOfCareLiteVo voRow = (PlanOfCareLiteVo) cell.getRow().getValue();
				record = domain.getPlanOfCare(voRow);
				form.getLocalContext().setSelectedRecord(record);

				String url = getUrlText(record);
				if(url != null && url.length() > 0)
				{
					engine.openUrl(url);
				}		
			}
		}		
	}

	private String getUrlText(PlanOfCareListVo voPocList)
	{
		return voPocList.getAssessmentQuestionIsNotNull()?voPocList.getAssessmentQuestion().getURL():"";
	}
}
