gt: 		if (voBoneJointShortColl != null)
context: //#############################################################################
  //#                                                                           #
  //#  Copyright (C) <2015>  <IMS MAXIMS>                                       #
  //#                                                                           #
  //#  This program is free software: you can redistribute it and/or modify     #
  //#  it under the terms of the GNU Affero General Public License as           #
  //#  published by the Free Software Foundation, either version 3 of the       #
  //#  License, or (at your option) any later version.                          # 
  //#                                                                           #
  //#  This program is distributed in the hope that it will be useful,          #
  //#  but WITHOUT ANY WARRANTY; without even the implied warranty of           #
  //#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
  //#  GNU Affero General Public License for more details.                      #
  //#                                                                           #
  //#  You should have received a copy of the GNU Affero General Public License #
  //#  along with this program.  If not, see <http://www.gnu.org/licenses/>.    #
  //#                                                                           #
  //#  IMS MAXIMS provides absolutely NO GUARANTEE OF THE CLINICAL SAFTEY of    #
  //#  this program.  Users of this software do so entirely at their own risk.  #
  //#  IMS MAXIMS only ensures the Clinical Safety of unaltered run-time        #
  //#  software that it builds, deploys and maintains.                          #
  //#                                                                           #
  //#############################################################################
  //#EOH
  // This code was generated by Alexie Ursache using IMS Development Environment (version 1.22 build 50314.1400)
  // Copyright (C) 1995-2005 IMS MAXIMS plc. All rights reserved.
  
  package ims.spinalinjuries.forms.medmskjointsbones;
  
  import ims.core.admin.vo.CareContextRefVo;
  import ims.core.clinical.vo.MskGroupsRefVo;
  import ims.core.clinical.vo.MskJointsRefVo;
  import ims.core.vo.AuthoringInformationVo;
  import ims.core.vo.CareContextLiteVo;
  import ims.core.vo.CareContextShortVo;
  import ims.core.vo.ClinicalContactShortVo;
  import ims.core.vo.MskGroupVo;
  import ims.core.vo.MskGroupVoCollection;
  import ims.core.vo.MskJointMovementVo;
  import ims.core.vo.MskJointMovementVoCollection;
  import ims.core.vo.MskJointVo;
  import ims.core.vo.lookups.ContactType;
  import ims.domain.exceptions.StaleObjectException;
  import ims.framework.Control;
  import ims.framework.cn.data.TreeNode;
  import ims.framework.enumerations.DialogResult;
  import ims.framework.enumerations.FormMode;
  import ims.framework.enumerations.SortOrder;
  import ims.framework.exceptions.PresentationLogicException;
  import ims.framework.utils.Color;
  import ims.generalmedical.vo.MedicalProbOnAdmisVo;
  import ims.generalmedical.vo.MskBoneJointShortVo;
  import ims.generalmedical.vo.MskBoneJointShortVoCollection;
  import ims.generalmedical.vo.MskBoneJointVo;
  import ims.generalmedical.vo.MskExamJointBonesDetailVo;
  import ims.generalmedical.vo.MskExamJointBonesDetailVoCollection;
  import ims.spinalinjuries.forms.medmskjointsbones.GenForm.grdIndexRow;
  import ims.spinalinjuries.vo.lookups.GeneralFindingsBone;
  import ims.spinalinjuries.vo.lookups.GeneralFindingsBoneCollection;
  import ims.spinalinjuries.vo.lookups.GeneralFindingsJoint;
  import ims.spinalinjuries.vo.lookups.GeneralFindingsJointCollection;
  import ims.spinalinjuries.vo.lookups.LookupHelper;
  import ims.spinalinjuries.vo.lookups.MSKGroupType;
  import ims.spinalinjuries.vo.lookups.MskBones;
  import ims.vo.ValueObject;
  
  import java.util.ArrayList;
  
  public class Logic extends BaseLogic
  {
  	protected void onFormOpen() throws ims.framework.exceptions.FormOpenException
  	{
  		initialize();
  		open();
  	}
  
  	private void initialize()
  	{
  		prepopulateControls();
  
  		ClinicalContactShortVo currentClinicalContact = form.getGlobalContext().Core.getCurrentClinicalContact();
  
  		Boolean bMedicalAdmissionContactSelected = new Boolean(currentClinicalContact != null && currentClinicalContact.getContactType().equals(ContactType.SPINALMEDICALADMISSION));
  		Boolean bInpatientOutpatientForm = new Boolean(engine.getFormName().equals(form.getForms().SpinalInjuries.MedMskJointsBonesInpatient) || engine.getFormName().equals(form.getForms().SpinalInjuries.MedMskJointsBonesDialog));
  
  		form.getLocalContext().setbMedicalAdmissionContactSelected(bMedicalAdmissionContactSelected);
  		form.getLocalContext().setIsMedicalInpatientForm(bInpatientOutpatientForm);
  		form.btnOk().setEnabled(false);
  		displayRecordBrowser(bInpatientOutpatientForm.booleanValue());
  		form.chkProblem().setVisible(!form.getLocalContext().getIsMedicalInpatientForm().booleanValue());
  
  		if (writeConditionsApply(bInpatientOutpatientForm, bMedicalAdmissionContactSelected))
  			form.grdIndex().setReadOnly(false);
  		else
  			form.grdIndex().setReadOnly(true);
  		
  		form.ccAuthoring().setIsRequiredPropertyToControls(true); //WDEV-15172
  	}
  
  	/**
  	 * inpatient form writable when Spinal Medical Admission contact NOT selected admission form writable when Spinal Medical Admission contact IS selected
  	 * 
  	 * @param bMedicalInpatientForm,
  	 *            currentClinicalContact
  	 * @return boolean
  	 */
  	private boolean writeConditionsApply(Boolean bMedicalInpatientForm, Boolean bMedicalAdmissionContactSelected)
  	{
  
  		if (bMedicalInpatientForm.booleanValue())
  		{
  
  			if (bMedicalAdmissionContactSelected.booleanValue())
  				return false;
  			else
  				return true;
  		}
  		else
  		{
  			if (bMedicalAdmissionContactSelected.booleanValue())
  				return true;
  			else
  				return false;
  		}
  	}
  
  	private void open()
  	{
  		clear();
  		form.setMode(FormMode.VIEW);
  		form.chkProblem().setEnabled(false);
  
  		if (!engine.getFormName().equals(form.getForms().SpinalInjuries.MedMskJointsBones))
  		{
  			fillRecordBrowser();
  		}
  		else
  		{
  			ClinicalContactShortVo voClinicalContact = form.getGlobalContext().Core.getCurrentClinicalContact();
  
  			if (voClinicalContact != null && voClinicalContact.getContactType().equals(ContactType.SPINALMEDICALADMISSION))
  				form.getLocalContext().setSelectedRecord(domain.getBoneJointsRecords(voClinicalContact, null));
  			else
  				form.getLocalContext().setSelectedRecord(domain.getMedicalAdmissionJointByCareContext(form.getGlobalContext().Core.getCurrentCareContext()));
  
  			populateGrid();
  		}
  
  		reselectUpdatedNode();
  		enableDetailsButtons();
  	}
  
  	private void reselectUpdatedNode()
  	{
  		// Pattern demands that the last updated node is displayed
  		if (form.getLocalContext().getUpdatedChildInstanceIsNotNull())
  		{
  			form.grdIndex().setValue(form.getLocalContext().getUpdatedChildInstance());
  			getSelectedInstance();
  			form.getLocalContext().setUpdatedChildInstance(null);
  			form.getLocalContext().setSelectedChildInstance(null);
  		}
  	}
  
  	private void clear()
  	{
  		clearParentControls();
  		clearChildInstanceControls();
  	}
  
  	private void displayRecordBrowser(boolean visible)
  	{
  		form.recbrJointBone().setVisible(visible);
  		form.lblBrowseStatus().setVisible(visible);
  		form.pnlJoints().setVisible(!visible);
  	}
  
  	protected void onFormModeChanged()
  	{
  		updateControlsState();
  
  		if (form.getMode().equals(FormMode.EDIT))
  		{
  			// Disable Problem checkbox if we saved the problem
  			if (form.grdIndex().getValue() != null)
  				form.chkProblem().setEnabled(form.grdIndex().getValue().getIsProblemIsNotNull() && form.grdIndex().getValue().getIsProblem().booleanValue() == false);
  
  			if (engine.isDialog())
  				form.btnClose().setVisible(false);
  		}
  		else if (form.getMode().equals(FormMode.VIEW))
  		{
  			form.treGeneralFindings().setEnabled(false);
  			form.grdMovements().setReadOnly(true);
  			if (engine.isDialog())
  				form.btnClose().setVisible(true);
  			else
  				form.btnClose().setVisible(false);
  		}
  	}
  
  	private void newInstance()
  	{
  		clearParentControls();
  		newParentInstance();
  		form.grdIndex().setReadOnly(false);
  		addMovement();
  		enableAuthoringControls(true);
  	}
  
  	private void addMovement()
  	{
  		clearChildInstanceControls();
  		form.setMode(FormMode.EDIT);
  		form.chkProblem().setEnabled(true);
  		form.grdIndex().setValue(null);
  
  		newChildInstance();
  		setContextControlState();
  		enableDetailsButtons();
  	}
  
  	private void newParentInstance()
  	{
  		form.grdIndex().getRows().clear();
  		//WDEV-18846
  		setDefaultAuthoringInfo();
  
  		MskBoneJointVo voMskBoneJoint = new MskBoneJointVo();
  		if (form.ccAuthoring().getValue() != null)
  		{	
  			voMskBoneJoint.setAuthoringDateTime(form.ccAuthoring().getValue().getAuthoringDateTime());
  			voMskBoneJoint.setAuthoringCP(form.ccAuthoring().getValue().getAuthoringHcp());
  		}
  		if (engine.getFormName().equals(form.getForms().SpinalInjuries.MedMskJointsBonesDialog))
  		{
  			// all records are going to be saved under the careContext in one record for all outpatient Clinical contacts,
  			// therefore if it is of type outpatient it must be set to null, if a record is being created from the dialog then it is
  			// of type Outpatient
  			voMskBoneJoint.setClinicalContact(null);
  		}
  		else
  			voMskBoneJoint.setClinicalContact(form.getGlobalContext().Core.getCurrentClinicalContact());
  
  		voMskBoneJoint.setJointBoneDetail(new MskExamJointBonesDetailVoCollection());
  		populateParentInstanceControls(voMskBoneJoint);
  		form.getLocalContext().setSelectedChildInstance(null);
  		form.getLocalContext().setSelectedRecord(voMskBoneJoint);
  
  	}
  	
  	//WDEV-18846 - start
  	private void setDefaultAuthoringInfo()
  	{		
  		boolean isLoggedOnUserHCP = domain.getHcpLiteUser() != null;
  		if (isLoggedOnUserHCP)
  		{
  			form.ccAuthoring().initializeComponent(true,null);
  		}
  		else
  		{
  			if (form.getGlobalContext().Core.getCurrentClinicalContact() != null)
  			{
  				form.ccAuthoring().initializeComponent(false,true); //WDEV-15172 
  			}
  		}
  	}	
  	//---------- end WDEV-18846
  	
  	private void populateParentInstanceControls(MskBoneJointVo voMskBoneJoint)
  	{
  		if (voMskBoneJoint != null)
  		{
  			//WDEV-15172
  			AuthoringInformationVo authoringInfo = new AuthoringInformationVo(); 
  			authoringInfo.setAuthoringHcp(voMskBoneJoint.getAuthoringCP());
  			authoringInfo.setAuthoringDateTime(voMskBoneJoint.getAuthoringDateTime());
  			form.ccAuthoring().setValue(authoringInfo);
  		}
  	}
  
  	private void setUpdatedRecord()
  	{
  		if (form.recbrJointBone().getValue() != null)
  		{
  			form.getLocalContext().setUpdatedInstance(form.recbrJointBone().getValue());
  		}
  	}
  
  	protected void onCmbGroupValueChanged() throws ims.framework.exceptions.PresentationLogicException
  	{
  		if(form.cmbGroup().getValue()== null)
  		{
  			form.btnOk().setEnabled(false);
  		}
  		
  		loadJBCombo();
  	}
  
  	private void loadJBCombo()
  	{
  		MskGroupVo mskGroupVo = form.cmbGroup().getValue();
  		form.cmbJB().clear();
  		form.cmbLaterality().setValue(null);
  		form.txtInterpret().setValue(null);
  		form.txtLigSpecTests().setValue(null);
  		form.grdMovements().getRows().clear();
  		form.treGeneralFindings().clear();
  
  		if (mskGroupVo != null && mskGroupVo.getIsJointIsNotNull())
  		{
  			if (mskGroupVo.getIsJoint().booleanValue())
  			{
  				// Joint
  				for (int i = 0; i < mskGroupVo.getJoints().size(); i++)
  					form.cmbJB().newRow(mskGroupVo.getJoints().get(i), mskGroupVo.getJoints().get(i).getName());
  
  				// Findings
  				populateJointFindingTree();
  			}
  			else
  			{
  				// Bone
  				for (int i = 0; i < mskGroupVo.getBones().size(); i++)
  					form.cmbJB().newRow(mskGroupVo.getBones().get(i), mskGroupVo.getBones().get(i).getText());
  
  				// Findings
  				populateBoneFindingsTree();
  				form.grdMovements().setVisible(false);
  				form.grdMovements().setReadOnly(form.getMode().equals(FormMode.VIEW));
  			}
  		}
  	}
  
  	protected void onGrdIndexSelectionChanged() throws ims.framework.exceptions.PresentationLogicException
  	{
  		getSelectedInstance();
  	}
  
  	private void getSelectedInstance()
  	{
  		// In update mode we can select nodes without displaying anything but the context
  		// menus can change based on what's selected so we need to update them
  		if (form.getMode().equals(FormMode.EDIT) && !form.grdIndex().getReadOnly())
  		{
  			updateContextMenusState();
  			return;
  		}
  		form.getLocalContext().setSelectedChildInstance(form.grdIndex().getValue());
  		populateChildInstanceControls(form.grdIndex().getValue());
  		updateControlsState();
  	}
  
  	private void populateChildInstanceControls(MskExamJointBonesDetailVo voExamJointBones)
  	{
  		if (voExamJointBones == null)
  			return;
  
  		if (voExamJointBones.getGroupIsNotNull() && groupExists(voExamJointBones.getGroup()) == false)
  			form.cmbGroup().newRow(voExamJointBones.getGroup(), voExamJointBones.getGroup().getName());
  		form.cmbGroup().setValue(voExamJointBones.getGroup());
  
  		if (form.cmbGroup().getValue() != null)
  		{
  			loadJBCombo();
  			if (form.cmbGroup().getValue().getIsJoint().booleanValue())
  			{
  
  				if (voExamJointBones.getJointIsNotNull())
  				{
  					// Joint
  					if (jointExists(voExamJointBones.getJoint()) == false)
  						form.cmbJB().newRow(voExamJointBones.getJoint(), voExamJointBones.getJoint().getName());
  
  					form.cmbJB().setValue(voExamJointBones.getJoint());
  
  					// Findings
  					setJointFindings(voExamJointBones.getGeneralFindingsJoint());
  
  					// Movements
  					loadMovementGrd();
  					populateMovementGrd(voExamJointBones.getSelectedMovements());
  				}
  			}
  			else
  			{
  				if (voExamJointBones.getBoneIsNotNull())
  				{
  					// Bone
  					if (boneExists(voExamJointBones.getBone()) == false)
  						form.cmbJB().newRow(voExamJointBones.getBone(), voExamJointBones.getBone().getText());
  					form.cmbJB().setValue(voExamJointBones.getBone());
  
  					loadMovementGrd();
  					// Findings
  					setBoneFindings(voExamJointBones.getGeneralFindingsBone());
  				}
  			}
  		}
  		form.chkProblem().setValue(voExamJointBones.getIsProblemIsNotNull() ? voExamJointBones.getIsProblem().booleanValue() : false);
  		form.cmbLaterality().setValue(voExamJointBones.getLaterality());
  		form.txtLigSpecTests().setValue(voExamJointBones.getLigamentsTests());
  		form.txtInterpret().setValue(voExamJointBones.getInterpretation());
  	}
  
  	private void populateMovementGrd(MskJointMovementVoCollection voJointMovementVoColl)
  	{
  		if (voJointMovementVoColl != null)
  		{
  			GenForm.grdMovementsRow row;
  			for (int y = 0; y < voJointMovementVoColl.size(); y++)
  			{
  				MskJointMovementVo voJointMovement = voJointMovementVoColl.get(y);
  				int a = 0;
  				for (a = 0; a < form.grdMovements().getRows().size(); a++)
  				{
  					row = form.grdMovements().getRows().get(a);
  					if (row.getMovements() != null)
  					{
  						if (row.getMovements().equals(voJointMovement.getMoveType().getText()))
  						{
  							row.setcolSor(voJointMovement.getMovSOR());
  							row.setcolEor(voJointMovement.getMovEOR());
  							row.setcolFinding(voJointMovement.getMoveFinding());
  							break;
  						}
  					}
  				}
  				if (a == form.grdMovements().getRows().size())
  				{
  					row = form.grdMovements().getRows().newRow();
  					row.setValue(voJointMovement);
  					row.setcolSor(voJointMovement.getMovSOR());
  					row.setcolEor(voJointMovement.getMovEOR());
  					row.setcolFinding(voJointMovement.getMoveFinding());
  				}
  			}
  		}
  	}
  
  	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
  	{
  		if (save())
  			open();
  	}
  
  	private boolean save()
  	{
  		if (checkForUnsavedChild())
  			return false;
  
  		MskBoneJointVo voMskBoneJoint = populateInstanceData(form.getLocalContext().getSelectedRecord());
  
  		if (voMskBoneJoint.getJointBoneDetail().size() == 0)
  		{
  			engine.showMessage("At least one data field must be entered");
  			return false;
  		}
  
  		if (voMskBoneJoint.getCareContext() == null)
  			voMskBoneJoint.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
  		
  		if ( ! validateUiErrors()) //WDEV-15172
  			 return false;
  
  		String[] arrErrors = voMskBoneJoint.validate();
  
  		if (arrErrors != null)
  		{
  			engine.showErrors(arrErrors);
  			return false;
  		}
  
  		try
  		{
  			form.getLocalContext().setSelectedRecord(domain.saveBoneJointsRecord(voMskBoneJoint));
  		}
  		catch (StaleObjectException e)
  		{
  			engine.showMessage(ims.configuration.gen.ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
  			open();
  			return false;
  		}
  
  		return true;
  
  	}
  
  	private boolean validateUiErrors()
  	{
  		ArrayList<String> listOfErrors = new ArrayList<String>();
  
  		if(form.ccAuthoring().getValue() == null)
  		{
  			listOfErrors.add(form.ccAuthoring().getErrors());
  		}
  
  		int errorCount = listOfErrors.size();
  		String[] result = new String[errorCount];
  
  		for (int x = 0; x < errorCount; x++)
  			result[x] = (String) listOfErrors.get(x);
  
  		if (result != null && result.length > 0)
  		{
  			engine.showErrors(result);
  			return false;
  		}
  
  		return true;
  	}
  
  	private MskBoneJointVo populateInstanceData(MskBoneJointVo voMskBoneJoint)
  	{
  		if (voMskBoneJoint == null)
  			voMskBoneJoint = new MskBoneJointVo();
  
  		populateParentInstanceData(voMskBoneJoint);
  
  		if (form.getLocalContext().getSelectedChildInstance() == null)
  		{
  			populateChildCollectionFromGrid(voMskBoneJoint);
  		}
  		else
  		// Update child instance.
  		{
  			MskExamJointBonesDetailVo voJointBonesDetail = form.getLocalContext().getSelectedChildInstance();
  			populateChildInstanceData(voJointBonesDetail);
  			form.getLocalContext().setUpdatedChildInstance(voJointBonesDetail);
  			form.getLocalContext().setSelectedChildInstance(voJointBonesDetail);
  
  			for (int i = 0; i < voMskBoneJoint.getJointBoneDetail().size(); i++)
  			{
  				if (voMskBoneJoint.getJointBoneDetail().get(i).getID_MSkExamJointBonesDetail() == form.getLocalContext().getSelectedChildInstance().getID_MSkExamJointBonesDetail())
  				{
  					voMskBoneJoint.getJointBoneDetail().set(i, form.getLocalContext().getSelectedChildInstance());
  				}
  			}
  		}
  		return voMskBoneJoint;
  	}
  
  	private void populateChildCollectionFromGrid(MskBoneJointVo voMskBoneJoint)
  	{
  		if (voMskBoneJoint.getJointBoneDetail() == null)
  			voMskBoneJoint.setJointBoneDetail(new MskExamJointBonesDetailVoCollection());
  
  		if (form.grdIndex().getRows().size() > 0)
  			voMskBoneJoint.getJointBoneDetail().clear();
  
  		for (int i = 0; i < form.grdIndex().getRows().size(); i++)
  		{
  			voMskBoneJoint.getJointBoneDetail().add(form.grdIndex().getRows().get(i).getValue());
  		}
  	}
  
  	private void populateParentInstanceData(MskBoneJointVo voMskBoneJoint)
  	{
  		//WDEV-15172
  		if (form.ccAuthoring().getValue() != null)
  		{
  			voMskBoneJoint.setAuthoringDateTime(form.ccAuthoring().getValue().getAuthoringDateTime());  
  			voMskBoneJoint.setAuthoringCP(form.ccAuthoring().getValue().getAuthoringHcp());
  		}
  	}
  
  	/**
  	 * Invoked as part of the save routine. The user may have entered data in the details and not bothered clicking ok. This method counts the vo fields to decide if it should promote the unsaved data entry to the hierarchy grid before saving.
  	 */
  	private boolean checkForUnsavedChild()
  	{
  		if (addOrUpdateChild())
  			return true;
  
  		return false;
  	}
  
  	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
  	{
  		form.btnOk().setEnabled(false);
  
  		form.getLocalContext().setSelectedRecord(null);
  		form.getLocalContext().setUpdatedInstance(null);
  		open();
  	}
  
  	protected void onCmbFilterByValueChanged() throws PresentationLogicException
  	{
  
  	}
  
  	protected void onCmbJBValueChanged() throws PresentationLogicException
  	{
  
  		form.btnOk().setEnabled(false);
  		if(form.cmbJB().getValue()!= null)
  		{
  		form.btnOk().setEnabled(true);
  		}
  		
  			
  			
  		loadMovementGrd();
  	}
  
  	private void loadMovementGrd()
  	{
  		Object jbValue = form.cmbJB().getValue();
  		form.txtLigSpecTests().setValue(null);
  		form.txtInterpret().setValue(null);
  		form.grdMovements().getRows().clear();
  		if (jbValue != null)
  		{
  			if (jbValue instanceof MskJointVo)
  			{
  				GenForm.grdMovementsRow row;
  				MskJointVo mskJointVo = (MskJointVo) jbValue;
  				for (int i = 0; mskJointVo.getMovementsIsNotNull() && i < mskJointVo.getMovements().size(); i++)
  				{
  					row = form.grdMovements().getRows().newRow();
  					row.setMovements(mskJointVo.getMovements().get(i).getText());
  					row.setcolMovementId(new Integer(mskJointVo.getMovements().get(i).getId()));
  				}
  				form.grdMovements().setVisible(true);
  				if (form.getMode().equals(FormMode.EDIT))
  					form.grdMovements().setReadOnly(false);
  			}
  			else
  				form.grdMovements().setVisible(false);
  		}
  
  	}
  
  	protected void onContextMenuItemClick(int menuItemID, Control sender) throws PresentationLogicException
  	{
  		switch (menuItemID)
  		{
  			case GenForm.ContextMenus.JointsBones.New :
  				form.btnOk().setEnabled(false);
  				newInstance();
  			break;
  			case GenForm.ContextMenus.JointsBones.ADD :
  				addMovement();
  			break;
  			case GenForm.ContextMenus.JointsBones.UPDATE :
  				if(form.btnOk().isVisible())
  				form.btnOk().setEnabled(true);
  			
  				
  				updateInstance();
  			break;
  		}
  	}
  
  	private void updateInstance()
  	{
  		if (form.grdIndex().getValue() != null)
  		{
  			MskExamJointBonesDetailVo voCachedChild = form.grdIndex().getValue();
  			form.getLocalContext().setUpdatedChildInstance(voCachedChild);
  			form.getLocalContext().setSelectedChildInstance(voCachedChild);
  			if (voCachedChild.getID_MSkExamJointBonesDetail() == null)
  			{
  				populateChildInstanceControls(voCachedChild);
  			}
  		}
  		if (form.cmbGroup().getValue() != null)
  		{
  			if (form.cmbGroup().getValue().getIsJoint().booleanValue())
  				form.grdMovements().setReadOnly(false);
  		}
  		setUpdatedRecord();
  		form.setMode(FormMode.EDIT);
  		setContextControlState();
  	}
  
  	private void fillRecordBrowser()
  	{
  		form.recbrJointBone().clear();
  		MskBoneJointShortVoCollection voBoneJointShortColl = null;
  
  		if (engine.getFormName().equals(form.getForms().SpinalInjuries.MedMskJointsBonesDialog))
  		{
  			// load from episode of care
  			voBoneJointShortColl = domain.listByEpisodeOfCare(form.getGlobalContext().Core.getEpisodeofCareShort());
  			loadCareContextRecords(isCareContextPresent(voBoneJointShortColl));
  		}
  		else
  		{
  			// load by care context
  			voBoneJointShortColl = domain.listByCareContext(form.getGlobalContext().Core.getCurrentCareContext());
  			loadCareContextRecords(isCareContextPresent(voBoneJointShortColl));
  			checkForSelectedClinicalContact();
  		}
  
  		setRecordBrowser();
  		browseRecord();
  	}
  
  	private MskBoneJointShortVoCollection isCareContextPresent(MskBoneJointShortVoCollection voBoneJointShortColl)
  	{
  		boolean currentCareContextFound = false;
  		if (voBoneJointShortColl != null)
  		{
  			for (int i = 0; i < voBoneJointShortColl.size(); i++)
  			{
  				CareContextShortVo voCareContext = voBoneJointShortColl.get(i).getCareContext();
  				if (voCareContext.getID_CareContext().equals(form.getGlobalContext().Core.getCurrentCareContext().getID_CareContext()))
  					currentCareContextFound = true;
  			}
  		}
  
  		if (!currentCareContextFound)
  		{
  			MskBoneJointShortVo voMskBoneJoint = new MskBoneJointShortVo();
  			voMskBoneJoint.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
  			if (voBoneJointShortColl == null)
  				voBoneJointShortColl = new MskBoneJointShortVoCollection();
  			voBoneJointShortColl.add(voMskBoneJoint);
  			voBoneJointShortColl.sort(SortOrder.DESCENDING);
  		}
  		return voBoneJointShortColl;
  	}
  
  	private void checkForSelectedClinicalContact()
  	{
  		// the selected Clinical Contact that has been selected from Patient Summary may not have a record and therefore
  		// must be added to the record browser
  		boolean clinicalContactFound = false;
  		ClinicalContactShortVo voSelectedClinicalContact = form.getGlobalContext().Core.getCurrentClinicalContact();
  
  		if (voSelectedClinicalContact != null)
  		{
  			for (int i = 0; i < form.recbrJointBone().size(); i++)
  			{
  				ValueObject vo = (ValueObject) form.recbrJointBone().getValues().get(i);
  				if (vo instanceof ClinicalContactShortVo)
  				{
  					ClinicalContactShortVo voClinicalContact = (ClinicalContactShortVo) form.recbrJointBone().getValues().get(i);
  					if (voClinicalContact.equals(voSelectedClinicalContact))
  						clinicalContactFound = true;
  				}
  			}
  
  			if (!clinicalContactFound)
  			{
  				form.recbrJointBone().newRow(voSelectedClinicalContact, "\t\t\t\tClinical Contact: " + voSelectedClinicalContact.getStartDateTime() + " - " + voSelectedClinicalContact.getSeenBy(), Color.Green);
  				form.recbrJointBone().setValue(voSelectedClinicalContact);
  			}
  		}
  	}
  
  	private void sortBoneJointColl(CareContextShortVo voCareContext, MskBoneJointShortVoCollection voBoneJointShortColl)
  	{
  		if (voBoneJointShortColl != null)
  		{
  			int x = 0;
  			for (x = 0; x < voBoneJointShortColl.size(); x++)
  			{
  				MskBoneJointShortVoCollection voBoneJointCollForClinicalContact = new MskBoneJointShortVoCollection();
  				if (voBoneJointShortColl.get(x).getClinicalContact() != null && voCareContext.getID_CareContext().equals(voBoneJointShortColl.get(x).getCareContext().getID_CareContext()))
  				{
  					ClinicalContactShortVo voClinicalContact = voBoneJointShortColl.get(x).getClinicalContact();
  
  					for (int i = 0; i < voBoneJointShortColl.size(); i++)
  					{
  						if (voBoneJointShortColl.get(i).getClinicalContact() != null)
  						{
  							if (voBoneJointShortColl.get(i).getClinicalContact().getID_ClinicalContact().equals(voClinicalContact.getID_ClinicalContact()))
  							{
  								voBoneJointCollForClinicalContact.add(voBoneJointShortColl.get(i));
  								x = i;
  							}
  						}
  					}
  					loadClinicalContactBoneJointRecords(voClinicalContact);
  				}
  			}
  		}
  	}
  
  	private void loadClinicalContactBoneJointRecords(ClinicalContactShortVo voClinicalContact)
  	{
  		boolean isClinicalContactSelected = checkCurrentClinicalContact(voClinicalContact);
  
  		Color textColour = null;
  		if (isClinicalContactSelected)
  			textColour = Color.Green;
  
  		form.recbrJointBone().newRow(voClinicalContact, "\t\t\t\tClinical Contact: " + voClinicalContact.getStartDateTime() + " - " + voClinicalContact.getSeenBy(), textColour);
  
  		if (isClinicalContactSelected)
  			form.recbrJointBone().setValue(voClinicalContact);
  	}
  
  	private boolean checkCurrentClinicalContact(ClinicalContactShortVo voClinicalContact)
  	{
  		// checks if this Clinical Contact is the selected Clinical Contact from Patient Summary
  		ClinicalContactShortVo voSelectedClinicalContact = form.getGlobalContext().Core.getCurrentClinicalContact();
  		if (engine.getFormName().equals(form.getForms().SpinalInjuries.MedMskJointsBonesDialog))
  		{
  			CareContextRefVo voCareContextRef = voClinicalContact.getCareContext();
  			if (form.getGlobalContext().Core.getCurrentCareContext().getID_CareContext().equals(voCareContextRef.getID_CareContext()))
  				return true;
  			else
  				return false;
  		}
  		if (voSelectedClinicalContact != null)
  		{
  			if (voSelectedClinicalContact.equals(voClinicalContact))
  				return true;
  			else
  				return false;
  		}
  
  		return false;
  	}
  
  	private void loadCareContextRecords(MskBoneJointShortVoCollection voBoneJointShortColl)
  	{
  		CareContextShortVo voSelectedCareContext = form.getGlobalContext().Core.getCurrentCareContext();
  		ClinicalContactShortVo voSelectedClinicalContact = form.getGlobalContext().Core.getCurrentClinicalContact();
  
  		for (int i = 0; i < voBoneJointShortColl.size(); i++)
  		{
  			MskBoneJointShortVo voMskBoneJoint = voBoneJointShortColl.get(i);
  
  			if (voMskBoneJoint.getClinicalContact() == null)
  			{
  				CareContextShortVo voCareContext = voMskBoneJoint.getCareContext();
  				if (voSelectedClinicalContact == null && voCareContext.getID_CareContext().equals(voSelectedCareContext.getID_CareContext()))
  				{
  					loadCareContext(voCareContext, Color.Green, voBoneJointShortColl);
  					form.recbrJointBone().setValue(voCareContext);
  				}
  				else
  				{
  					if (engine.getFormName().equals(form.getForms().SpinalInjuries.MedMskJointsBonesDialog) && voCareContext.getID_CareContext().equals(voSelectedCareContext.getID_CareContext()))
  					{
  						loadCareContext(voCareContext, Color.Green, voBoneJointShortColl);
  						form.recbrJointBone().setValue(voCareContext);
  					}
  					else
  						loadCareContext(voCareContext, null, voBoneJointShortColl);
  				}
  			}
  			else
  			{
  				CareContextShortVo voCareContext = voMskBoneJoint.getCareContext();
  				loadCareContext(voCareContext, null, voBoneJointShortColl);
  			}
  
  		}
  	}
  
  	private void loadCareContext(CareContextShortVo voCareContext, Color current, MskBoneJointShortVoCollection voBoneJointShortColl)
  	{
  		loadActiveCareContext(voCareContext, current);
  		loadCareContextJointRecords(voCareContext, voBoneJointShortColl, current);
  	}
  
  	private void loadActiveCareContext(CareContextShortVo voSelectedCareContext, Color active)
  	{
  		// A ClinicalContact may be selected and its CareContext may not have a record, therefor display it.
  		// A CareContext may be selected and have no record, display it
  		form.recbrJointBone().newRow(voSelectedCareContext, voSelectedCareContext.getRecordBrowserText(), active);
  	}
  
  	private void loadCareContextJointRecords(CareContextShortVo voCareContext, MskBoneJointShortVoCollection voBoneJointShortColl, Color activeColor)
  	{
  		// loads the CareContext Joint Bones records
  