/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/forms/careplanstep2/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 Barbara Worwood using IMS Development Environment (version 1.18 build 40628.1730)
// Copyright (C) 1995-2004 IMS MAXIMS plc. All rights reserved.

package ims.nursing.forms.careplanstep2;

import ims.coe.vo.AssessmentEatingAndDrinking;
import ims.coe.vo.AssessmentMobility;
import ims.coe.vo.AssessmentPain;
import ims.coe.vo.AssessmentSkin;
import ims.coe.vo.InfectionControlComponentVo;
import ims.configuration.AppRight;
import ims.configuration.gen.ConfigFlag;
import ims.core.vo.Hcp;
import ims.core.vo.HcpCollection;
import ims.core.vo.HcpFilter;
import ims.core.vo.HcpLiteVo;
import ims.core.vo.MemberOfStaffLiteVo;
import ims.core.vo.MemberOfStaffShortVo;
import ims.core.vo.NurseVo;
import ims.core.vo.PersonName;
import ims.core.vo.RecordingUserInformationVo;
import ims.core.vo.lookups.HcpDisType;
import ims.domain.exceptions.DomainInterfaceException;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.Control;
import ims.framework.FormName;
import ims.framework.enumerations.DialogResult;
import ims.framework.exceptions.PresentationLogicException;
import ims.framework.interfaces.IAppRight;
import ims.framework.interfaces.IAppRole;
import ims.framework.utils.Date;
import ims.framework.utils.DateTime;
import ims.framework.utils.Time;
import ims.nursing.forms.careplanstep2.GenForm.gridCarePlanRow;
import ims.nursing.vo.AssessmentComponent;
import ims.nursing.vo.AssessmentHeaderInfo;
import ims.nursing.vo.CarePlan;
import ims.nursing.vo.CarePlanInterventions;
import ims.nursing.vo.CarePlanInterventionsCollection;
import ims.nursing.vo.CarePlanStatus;
import ims.nursing.vo.CarePlanStatusCollection;
import ims.nursing.vo.CarePlanTemplate;
import ims.nursing.vo.CarePlanTemplateIntervention;
import ims.nursing.vo.CarePlanTemplateInterventionCollection;
import ims.nursing.vo.CarePlanTemplateTitle;
import ims.nursing.vo.CarePlanTemplateTitleCollection;
import ims.nursing.vo.lookups.CarePlanInterventionManOpt;
import ims.spinalinjuries.vo.NurAssessmentEatingVo;
import ims.spinalinjuries.vo.NurAssessmentSkinVo;
public class Logic extends BaseLogic
{
	protected void onFormOpen() throws ims.framework.exceptions.FormOpenException
	{
		form.textAddAction().setFocus();
		// Populate the actions grid if this is from a template
		CarePlanTemplateTitle templateTitle = form.getGlobalContext().COE.getCarePlanTemplateTitle();
		// If the templateId is null, check the arraylist. If this is not null,
		// we
		// will use this as the templateId and remove it from the array list
		if (templateTitle == null)
		{
			CarePlanTemplateTitleCollection templateList = form.getGlobalContext().COE.CarePlan.getTemplateList();
			if (templateList != null && templateList.size() > 0)
				templateTitle = templateList.get(0);

		}

		form.dateNextEvaluation().setValue(null);
		form.textAddAction().setValue(null);
		form.gridCarePlan().getRows().clear();
		
		//hide Select All if care plan is not created from a template
		form.chkSelectAll().setVisible(templateTitle != null);
		
		Object mos = domain.getMosUser();
		if(mos != null){
			form.cmbRecordingHCP().newRow((MemberOfStaffLiteVo)mos, mos.toString());
			form.cmbRecordingHCP().setValue((MemberOfStaffLiteVo)mos);
		}
		form.dtimRecordingDateTime().setValue(new DateTime());
		
		if (templateTitle != null)
		{
			CarePlanTemplate template = domain.getCarePlanTemplate(templateTitle);
			if (template != null)
			{
				form.textObjective().setValue(template.getObjectives().get(0).getObjective());
				form.textObjective().setTooltip(returnMultilineString(template.getObjectives().get(0).getObjective()));
				form.textTitle().setValue(template.getTitle());
				GenForm.gridCarePlanRow row;
				CarePlanTemplateInterventionCollection coll = template.getInterventions().sort();
				for (int i = 0; i < coll.size(); i++)
				{
					CarePlanTemplateIntervention action = coll.get(i);

					if (action.getInterventionType() != null && action.getInterventionType().equals(CarePlanInterventionManOpt.OPTIONAL))
					{
						// 27/10/2004 - Sinead Request - Display only the active
						// records
						if (action.getActiveIsNotNull() && action.getActive().booleanValue() == true)
						{
							row = form.gridCarePlan().getRows().newRow(); // if
							// optional,
							// must
							// be
							// shown
							// if
							// active
							// or
							// not
							// active.
							row.setValue(action);
							row.setcolActions(action.getIntervention());

							// 27/10/2004 - Sinead Request - Don't precheck the
							// active records
							// if(action.getActive().booleanValue()== true)
							// {
							// row.setColSelected(true);
							// }
						}
					}
					if (action.getInterventionType() != null && action.getInterventionType().equals(CarePlanInterventionManOpt.MANDATORY))
					{
						if (action.getActive().booleanValue() == true)
						{
							row = form.gridCarePlan().getRows().newRow(); // only
							// if
							// Mandatory
							// is
							// true
							// should
							// it
							// be
							// shown
							// on
							// the
							// graph
							row.setValue(action); // if true then it cannot be
							// deselected
							row.setcolActions(action.getIntervention());
							row.setColSelected(true);
							row.setReadOnly(true);
						}
					}
				}
			}
		}
		else
		{
			form.textObjective().setValue(form.getGlobalContext().COE.getCarePlanObjective());
			form.textObjective().setTooltip(returnMultilineString(form.getGlobalContext().COE.getCarePlanObjective()));
			form.textTitle().setValue(form.getGlobalContext().COE.getCarePlanTitle());
		}

		// Default ConfirmForStudentNurse local context value
		form.getLocalContext().setConfirmForStudentNurse(Boolean.FALSE);

		// all specific to Middlesborough
		if (ims.configuration.gen.ConfigFlag.UI.CONFIRM_FOR_STUDENT_NURSE.getValue())
		{
			boolean bShow = false;

			if (domain.getMosUser() != null)
			{
				MemberOfStaffShortVo mosUserVo = (MemberOfStaffShortVo) domain.getMosUser();

				if (mosUserVo.getHcp() != null)
				{
					ims.core.vo.Hcp nurse;

					HcpFilter filter = new HcpFilter();
					filter.setID_Hcp(mosUserVo.getHcp().getID_Hcp());
					nurse = domain.getHcp(filter);

					if (nurse instanceof NurseVo)
					{
						if (((NurseVo) nurse).getNurseType() != null)
						{
							if (((NurseVo) nurse).getNurseType().equals(HcpDisType.STUDENTNURSE))
							{
								bShow = true;
								form.getLocalContext().setConfirmForStudentNurse(Boolean.TRUE);
							}
						}
					}
				}
			}
			else
			{
				bShow = false;
				form.getLocalContext().setConfirmForStudentNurse(Boolean.FALSE);
			}

			form.qmbConfirmedBy().setVisible(bShow);
			form.dteConfirmed().setVisible(bShow);
			form.timConfirmed().setVisible(bShow);
			form.labelConfirmedBy().setVisible(bShow);
			form.lblConfirnedDate().setVisible(bShow);

			/*
			 * if (bShow) { form.dteConfirmed().setValue(new Date()); form.timConfirmed().setValue(new Time()); }
			 */
		}
		if (ConfigFlag.UI.CAREPLAN_CONFIRMED_BY_DETAILS.getValue())
		{
			form.labelConfirmedBy().setVisible(true);
			form.dteConfirmed().setVisible(true);
			form.lblConfirnedDate().setVisible(true);
			form.timConfirmed().setVisible(true);
			form.qmbConfirmedBy().setVisible(true);
		}
		else
		{
			form.labelConfirmedBy().setVisible(false);
			form.dteConfirmed().setVisible(false);
			form.lblConfirnedDate().setVisible(false);
			form.timConfirmed().setVisible(false);
			form.qmbConfirmedBy().setVisible(false);
		}
		
		checkForNurse();
		updateControlsState();
	}

	private void checkForNurse() 
	{
		Object hcp = domain.getHcpUser();
		
		if(hcp instanceof NurseVo && engine.hasRight(AppRight.CAN_CONFIRM_NURSING_DOCUMENTATION))
		{
			form.qmbConfirmedBy().newRow((Hcp) hcp, ((Hcp) hcp).toString());
			form.qmbConfirmedBy().setValue((Hcp) hcp);
			form.dteConfirmed().setValue(new Date());
			form.timConfirmed().setValue(new Time());
		}
	}

	protected void onBAddClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (form.textAddAction().getValue() == null)
		{
			engine.showMessage("Cannot add an empty action");
			return;
		}
		Integer x = new Integer(form.textAddAction().getValue().length());
		Integer config = new Integer(ConfigFlag.UI.CAREPLAN_ACTION_TEXT_LENGTH.getValue());
		if (x.intValue() > config.intValue())
		{
			engine.showMessage("Care Plan Action can be no more than " + ConfigFlag.UI.CAREPLAN_ACTION_TEXT_LENGTH.getValue() + " characters");
			return;
		}

		GenForm.gridCarePlanRow row = form.gridCarePlan().getRows().newRow();
		row.setValue(new CarePlanTemplateIntervention());
		row.setcolActions(form.textAddAction().getValue());
		row.setColSelected(true);
		form.textAddAction().setValue(null);
		form.textAddAction().setFocus();

	}

	protected void onBCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (engine.getPreviousNonDialogFormName() != null && !engine.getPreviousNonDialogFormName().equals(form.getForms().Nursing.CarePlanStep1))
			engine.open(form.getForms().Nursing.AdmissionAssessOverview);
		else
			engine.open(form.getForms().Nursing.CarePlanOverview);
	}

	protected void onBPreviousClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (engine.getPreviousNonDialogFormName() != null && !engine.getPreviousNonDialogFormName().equals(form.getForms().Nursing.CarePlanStep1))
			engine.open(engine.getPreviousNonDialogFormName());
		else
			engine.open(form.getForms().Nursing.CarePlanStep1);
	}

	protected void onBSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (checkLoggedOnUser())
			return;

		boolean noneSelected = true;
		for (int i = 0; i < form.gridCarePlan().getRows().size(); i++)
		{
			GenForm.gridCarePlanRow row = form.gridCarePlan().getRows().get(i);
			if (row.getColSelected() == true)
				noneSelected = false;
		}
		if (noneSelected == true || (form.gridCarePlan().getRows().size() == 0))
		{
			engine.showMessage("At least one action must be selected.");
			return;
		}

		// Save the details - title and objective from step1 and actions etc..
		// from this form
		CarePlan cplan = new CarePlan();
		cplan.setObjective(form.textObjective().getValue());
		cplan.setTitle(form.textTitle().getValue());

		if (form.dateNextEvaluation().getValue() == null)
		{
			engine.showMessage("Please enter the Next Evaluation Date.");
			return;
		}
		//wdev-14915
		if(	ims.configuration.gen.ConfigFlag.DOM.CAREPLAN_NEXT_EVALUTION_DATE_VALIDATION.getValue() != 0 )
		{
			Date dateT = new Date();
			dateT.addDay(ims.configuration.gen.ConfigFlag.DOM.CAREPLAN_NEXT_EVALUTION_DATE_VALIDATION.getValue());
			if( form.dateNextEvaluation().getValue() != null && form.dateNextEvaluation().getValue().isGreaterThan(dateT))
			{
				engine.showMessage("Next Evaluation Date exceeds " +ims.configuration.gen.ConfigFlag.DOM.CAREPLAN_NEXT_EVALUTION_DATE_VALIDATION.getValue()+" day policy!" ); //WDEV-15275
				return;
			}
		}
		//---------------
		if (form.dateNextEvaluation().getValue() != null)
		{
			Date today = new Date();
			if (form.dateNextEvaluation().getValue().isLessThan(today))
			{
				engine.showMessage("The Next Evaluation Date cannot be in the past.");
				return;
			}
		}

		if (form.gridCarePlan().getRows().size() <= 0)
		{
			engine.showMessage("Please enter an action");
			return;
		}

		cplan.setNextEvaluationDate(form.dateNextEvaluation().getValue());

		if (form.getLocalContext().getConfirmForStudentNurse().booleanValue())
		{
			cplan.setConfirmedBy(form.qmbConfirmedBy().getValue());
			cplan.setConfirmedDateTime(new DateTime(form.dteConfirmed().getValue(), form.timConfirmed().getValue()));
		}
		
		//WDEV-12901 set Recording Information
		if(cplan.getRecordingInformation()==null){
			RecordingUserInformationVo voRecordingUserInformation = new RecordingUserInformationVo();
			voRecordingUserInformation.setRecordingUser(form.cmbRecordingHCP().getValue());
			voRecordingUserInformation.setRecordingDateTime(new DateTime());
			cplan.setRecordingInformation(voRecordingUserInformation);
		}

		// Update the actions
		CarePlanInterventionsCollection coll = new CarePlanInterventionsCollection();
		GenForm.gridCarePlanRow row;
		Object Object = domain.getHcpLiteUser();
		HcpLiteVo hcpLiteVo = Object != null ? (HcpLiteVo) Object : null;

		for (int i = 0; i < form.gridCarePlan().getRows().size(); i++)
		{
			row = form.gridCarePlan().getRows().get(i);
			if (row.getColSelected() == true)
			{
				CarePlanInterventions interv = new CarePlanInterventions();
				interv.setActive(new Boolean(true));
				interv.setDescription(row.getcolActions());
				interv.setStartDate(new ims.framework.utils.Date());
				interv.setStartBy(hcpLiteVo);

				interv.setOrder(new Integer(i));
				interv.setInterventionType(row.getValue().getInterventionType());
				interv.setFrequency(row.getValue().getFrequency());
				coll.add(interv);
			}
		}
		cplan.setInterventions(coll);

		// Care Plan Status
		CarePlanStatus status = new CarePlanStatus();
		status.setDateTime(new DateTime());

		status.setHCP(hcpLiteVo);

		status.setStatus(ims.nursing.vo.lookups.CarePlanStatus.ACTIVE);

		cplan.setCurrentCarePlanStatus(status);

		CarePlanStatusCollection voColl = new CarePlanStatusCollection();
		voColl.add(status);
		cplan.setCarePlanStatus(voColl);

		try
		{
			AssessmentComponent component = null;
			CarePlanTemplateTitle carePlanTemplTitle = null;
			if (form.getGlobalContext().Nursing.getAssessmentHeaderInfo() != null)
			{
				component = form.getGlobalContext().Nursing.getAssessmentHeaderInfo().getCurrentComponent();
				carePlanTemplTitle = form.getGlobalContext().COE.getCarePlanTemplateTitle();
				if (carePlanTemplTitle == null)
				{
					// Get the first value in the array list if not null
					carePlanTemplTitle = form.getGlobalContext().COE.CarePlan.getTemplateList().get(0);
				}
			}
			else
				carePlanTemplTitle = form.getGlobalContext().COE.getCarePlanTemplateTitle();

			if (cplan.getClinicalContact() == null)
				cplan.setClinicalContact(form.getGlobalContext().Core.getCurrentClinicalContact());

			if (cplan.getCareContext() == null)
				cplan.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());

			String[] errors = cplan.validate();
			if (errors != null)
			{
				engine.showErrors("Validation Errors", errors);
				return;
			}

			domain.saveCarePlan(cplan, carePlanTemplTitle, component);
		}
		catch (StaleObjectException e)
		{
			engine.showMessage(ims.configuration.gen.ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
			onFormOpen();
			return;
		}

		// If the array list has more than one entry, we want to
		// call onformOpen again with the new array list (minus the first value)
		CarePlanTemplateTitleCollection templList = form.getGlobalContext().COE.CarePlan.getTemplateList();
		if (templList != null && templList.size() > 0)
		{
			templList.remove(0);
			if (templList.size() > 0)
			{
				form.getGlobalContext().COE.CarePlan.setTemplateList(templList);
				onFormOpen();
			}
			else
			{
				if (checkIfComponentCanBeCompleted())// RF 6/10/04
				{
					// Ask whether to complete the component
					// Dialog to ask whether to complete the component
					form.getGlobalContext().Core.setYesNoDialogMessage("Complete this Assessment Component?");
					engine.open(form.getForms().Core.YesNoDialog);
				}
				else
					// Return to the AdmissionAssessOverview form
					engine.open(form.getForms().Nursing.AdmissionAssessOverview);
			}
		}
		else
		{
			// If the template title and objective in the global context are
			// null
			// we know we have come from the assessment component and so we
			// should go back to the assessment overview screen rather
			// than the careplan overview
			if (form.getGlobalContext().COE.getCarePlanTitle() == null)
				engine.open(form.getForms().Nursing.AdmissionAssessOverview);
			else
				engine.open(form.getForms().Nursing.CarePlanOverview);
		}

	}

	private boolean checkLoggedOnUser()
	{
		if (form.qmbConfirmedBy().getVisible())
		{
			if (form.qmbConfirmedBy().getValue() == null)
			{
				Hcp voHcp = (Hcp) domain.getHcpUser();
				if (voHcp != null)
				{
					if (HcpDisType.NURSING.equals(voHcp.getHcpType()))
					{
						NurseVo voNurse = (NurseVo) voHcp;
						//wdev-4938
						if (voNurse != null && voNurse.getNurseTypeIsNotNull() && voNurse.getNurseType().equals(HcpDisType.STUDENTNURSE))
						{
							engine.showMessage("'Confirmed by' is a mandatory field as you are logged in as a student nurse");
							return true;
						}
					}
				}
			}
		}
		return false;
	}

	private boolean checkIfComponentCanBeCompleted()
	{
		IAppRole voRole = engine.getLoggedInRole();
		if (voRole != null)
		{
			ims.framework.interfaces.IAppRight[] rights = voRole.getRoleRights();
			boolean canConFirmNursingDocumentation = false;
			for (int i = 0; i < rights.length; i++)
			{
				IAppRight right = rights[i];
				if (right.getName().equals("CAN_CONFIRM_NURSING_DOCUMENTATION"))
				{
					canConFirmNursingDocumentation = true;
					break;
				}
			}

			if (!canConFirmNursingDocumentation)
				return false;
		}

		if (form.getGlobalContext().Nursing.getAssessmentComponent() != null)
		{
			AssessmentHeaderInfo assmnt = form.getGlobalContext().Nursing.getAssessmentHeaderInfo();
			if (assmnt != null && assmnt.getCurrentComponent() != null)
			{
				//Infection
				if(form.getGlobalContext().Nursing.getAssessmentComponent()instanceof InfectionControlComponentVo)
				{
					InfectionControlComponentVo assessinfection = (InfectionControlComponentVo) domain.getComponent(assmnt.getCurrentComponent());
					if(assessinfection == null)
						return false;
					if(assessinfection.getInfectionControlAssessmentIsNotNull())
						return true;
					else
						return false;
				}
				
				
				// Skin
				if (form.getGlobalContext().Nursing.getAssessmentComponent() instanceof AssessmentSkin)
				{
					// AssessmentSkin assessSkin = (AssessmentSkin) domain.getComponent(assmnt.getCurrentComponent(), AssessmentComponentType.SKIN);
					AssessmentSkin assessSkin = (AssessmentSkin) domain.getComponent(assmnt.getCurrentComponent());
					if (assessSkin == null)
						return false;

					if ((assessSkin.getSkinAssessment() != null) && (assessSkin.getBradenScale() != null))
						return true;
					else
						return false;
				}
				// Eating/Drinking
				if (form.getGlobalContext().Nursing.getAssessmentComponent() instanceof AssessmentEatingAndDrinking)
				{
					// AssessmentEatingAndDrinking assessEat = (AssessmentEatingAndDrinking) domain.getComponent(assmnt.getCurrentComponent(), AssessmentComponentType.EATING);
					AssessmentEatingAndDrinking assessEat = (AssessmentEatingAndDrinking) domain.getComponent(assmnt.getCurrentComponent());
					if (assessEat == null)
						return false;

					if (assessEat.getMiniNutritionalAssessment() != null)
						return true;
					else
						return false;
				}
				// Pain
				if (form.getGlobalContext().Nursing.getAssessmentComponent() instanceof AssessmentPain)
				{
					// AssessmentPain assessPain = (AssessmentPain) domain.getComponent(assmnt.getCurrentComponent(), AssessmentComponentType.PAIN);
					AssessmentPain assessPain = (AssessmentPain) domain.getComponent(assmnt.getCurrentComponent());
					if (assessPain == null)
						return false;

					if (assessPain.getOptionalPainAssessment() != null)
						return true;
					else
						return false;
				}
				// Mobility
				if (form.getGlobalContext().Nursing.getAssessmentComponent() instanceof AssessmentMobility)
				{
					AssessmentMobility assessMob = (AssessmentMobility) domain.getComponent(assmnt.getCurrentComponent());
					// AssessmentMobility assessMob = (AssessmentMobility) domain.getComponent(assmnt.getCurrentComponent(), AssessmentComponentType.MOBILITY);
					if (assessMob == null)
						return false;

					// Cannot complete the assessment until the patient movement
					// record + Fall assessment record exists
					if ((assessMob.getPatientMovement() != null) && (assessMob.getPatientHandling() != null) 
							&& (assessMob.getOptionalFallAssessment() != null) && (assessMob.getEnvironmentChecklistIsNotNull()))
						return true;
					else
						return false;
				}
				// SpinalInjuries - Eating and Drinking
				if (form.getGlobalContext().Nursing.getAssessmentComponent() instanceof NurAssessmentEatingVo)
				{
					// NurAssessmentEatingVo voEating = (NurAssessmentEatingVo) domain.getComponent(assmnt.getCurrentComponent(), AssessmentComponentType.SPINAL_EATING);
					NurAssessmentEatingVo voEating = (NurAssessmentEatingVo) domain.getComponent(assmnt.getCurrentComponent());
					if (voEating == null)
						return false;
					// Cannot complete the assessment until the Nutritional Risk
					// record
					if (voEating.getNutritionalRiskAssessment() != null)
						return true;
					else
						return false;
				}
				// SpinalInjuries - Skin
				if (form.getGlobalContext().Nursing.getAssessmentComponent() instanceof NurAssessmentSkinVo)
				{
					// NurAssessmentSkinVo voSkin = (NurAssessmentSkinVo) domain.getComponent(assmnt.getCurrentComponent(), AssessmentComponentType.SPINAL_SKIN);
					NurAssessmentSkinVo voSkin = (NurAssessmentSkinVo) domain.getComponent(assmnt.getCurrentComponent());
					if (voSkin == null)
						return false;
					// Cannot complete the assessment until the Waterlow and
					// Skin assessment record exists
					if ((voSkin.getWaterlow() != null) && (voSkin.getSkinAssessment() != null))
						return true;
					else
						return false;
				}
			}
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ims.coe.forms.careplanstep2.Handlers#onFormDialogClosed(ims.framework.FormName, ims.framework.enumerations.DialogResult)
	 */
	protected void onFormDialogClosed(FormName formName, DialogResult result) throws PresentationLogicException
	{
		// Check the result, and if YES, Call the Domain Interface method to
		// complete the component
		// then go to the admission overview form
		if(formName.equals(form.getForms().Nursing.CarePlanEditAction))
		{
			if(DialogResult.OK.equals(result) && form.gridCarePlan().getSelectedRow() != null)
			{
				form.gridCarePlan().getSelectedRow().setcolActions(form.getGlobalContext().Nursing.getCarePlanAction());
			}
			
			return;
		}
		if (formName.equals(form.getForms().Core.YesNoDialog) && result.equals(DialogResult.YES))
		{
			try
			{
				domain.completeComponent(form.getGlobalContext().Nursing.getAssessmentHeaderInfo().getCurrentComponent(), form.getGlobalContext().Nursing.getAssessmentHeaderInfo());
			}
			catch (StaleObjectException e)
			{
				// As Assessment Component is not handled directly by this form,
				// we should not call OnFormOpen
				engine.showMessage("The data has been chaged by another user, cannot proceed with this update");
				return;
			}
			catch (DomainInterfaceException e)
			{
				engine.showMessage("Error occurred completing assessment component " + e.getMessage());
				return;
			}
		}
		engine.open(form.getForms().Nursing.AdmissionAssessOverview);

	}

	protected void onQmbConfirmedByTextSubmited(String value) throws PresentationLogicException
	{
		if (value == null)
			return;

		form.qmbConfirmedBy().clear();
		HcpFilter filter = new HcpFilter();
		PersonName name = new PersonName();
		name.setSurname(value);
		filter.setQueryName(name);

		HcpCollection coll = domain.listMedics(filter);
		for (int i = 0; i < coll.size(); i++)
		{
			Hcp med = coll.get(i);
			form.qmbConfirmedBy().newRow(med, med.toString());
		}
		if (coll.size() == 1)
		{
			form.qmbConfirmedBy().setValue(coll.get(0));
			populateConfirmedDetails();
		}
		else if (coll.size() > 1)
		{
			form.qmbConfirmedBy().showOpened();
		}
	}

	private void populateConfirmedDetails()
	{
		form.dteConfirmed().setValue(new Date());
		form.timConfirmed().setValue(new Time());
	}

	protected void onQmbConfirmedByValueChanged() throws PresentationLogicException
	{
		if (form.qmbConfirmedBy().getValue() != null)
			populateConfirmedDetails();
	}

	@Override
	protected void onChkSelectAllValueChanged()	throws PresentationLogicException 
	{
		selectAllFromCarePlanActionGrid();
	}

	private void selectAllFromCarePlanActionGrid() 
	{
		for(int i=0; i<form.gridCarePlan().getRows().size(); i++)
		{
			gridCarePlanRow row = form.gridCarePlan().getRows().get(i);
			
			if(row.getValue() instanceof CarePlanTemplateIntervention && ((row.getValue().getInterventionType() != null && row.getValue().getInterventionType().equals(CarePlanInterventionManOpt.OPTIONAL)) || row.getValue().getInterventionType() == null))
				row.setColSelected(form.chkSelectAll().getValue());
		}
	}

	@Override
	protected void onContextMenuItemClick(int menuItemID, Control sender) throws PresentationLogicException 
	{
		switch(menuItemID)
		{
			case GenForm.ContextMenus.NursingNamespace.CarePlanAction.EDIT:
				editCarePlanAction();
			break;
		}
	}

	private void editCarePlanAction()
	{
		if(form.gridCarePlan().getSelectedRow() != null)
		{
			form.getGlobalContext().Nursing.setCarePlanAction(form.gridCarePlan().getSelectedRow().getcolActions());
			engine.open(form.getForms().Nursing.CarePlanEditAction);
		}
	}	
	
	private void updateControlsState()
	{
		form.getContextMenus().Nursing.getCarePlanActionEDITItem().setVisible(form.gridCarePlan().getValue() instanceof CarePlanTemplateIntervention);
	}

	@Override
	protected void onGridCarePlanSelectionChanged()	throws PresentationLogicException 
	{
		updateControlsState();
	}
	private String returnMultilineString(String str)
	{
		String temp = new String();
		if(str == null || str.length() == 0)
			return null;
				
		for(int i = 0; i < str.length();i++)
		{
			temp = temp + str.charAt(i);
			if(i != 0 && i % 100 == 0)
				temp = temp + "<br>";
		
		}
		return temp;
	}

}
