/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/forms/painbodychart/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 Alexie Ursache using IMS Development Environment (version 1.20 build 40812.900)
// Copyright (C) 1995-2004 IMS MAXIMS plc. All rights reserved.

package ims.nursing.forms.painbodychart;

import ims.admin.vo.AppImageVo;
import ims.core.vo.DrawingAreaVo;
import ims.core.vo.DrawingGroupVo;
import ims.core.vo.DrawingImageVersionVo;
import ims.core.vo.DrawingImageVo;
import ims.core.vo.HcpLiteVo;
import ims.core.vo.MemberOfStaffShortVo;
import ims.domain.exceptions.DomainInterfaceException;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.exceptions.UniqueKeyViolationException;
import ims.framework.controls.DrawingControlArea;
import ims.framework.controls.DrawingControlAreaCollection;
import ims.framework.controls.DrawingControlGroup;
import ims.framework.controls.DrawingControlGroupCollection;
import ims.framework.controls.DrawingControlShape;
import ims.framework.enumerations.DialogResult;
import ims.framework.enumerations.FormMode;
import ims.framework.exceptions.PresentationLogicException;
import ims.framework.utils.Date;
import ims.framework.utils.DateFormat;
import ims.framework.utils.DateTime;
import ims.framework.utils.Time;
import ims.framework.utils.TimeFormat;
import ims.coe.vo.AssessmentPain;
import ims.nursing.vo.AssessmentHeaderInfo;

import ims.nursing.vo.PainAssessment;
import ims.nursing.vo.PainAssessmentFindings;
import ims.nursing.vo.PainAssessmentFindingsCollection;
import ims.nursing.vo.PainAssessmentReview;
import ims.nursing.vo.PainAssessmentReviewCollection;
import ims.nursing.vo.lookups.PainHypersensitivity;
import ims.nursing.vo.lookups.PainStatus;

public class Logic extends BaseLogic
{
	protected void onFormOpen() throws ims.framework.exceptions.FormOpenException
	{
		form.btnNew().setFocus();
		clearContext();
		form.drawingBodyChart().setPrintSubTitle("Pain - Body Chart");
		if(form.getGlobalContext().Core.getPatientShortIsNotNull())
		{
			form.drawingBodyChart().setPrintTitle(form.getGlobalContext().Core.getPatientShort().getPatientInfo());
		}
		
		if(!open(true))
		{
			form.setMode(FormMode.VIEW);
			//Disable new Button if we don't have an image loaded
			form.btnNew().setEnabled(false);
		}
		else
		{
			if(form.getGlobalContext().Nursing.getAssessmentComponent() != null && form.getGlobalContext().Nursing.getAssessmentComponent() instanceof AssessmentPain) // Came from the Pain Assessment screen
			{
				AssessmentPain comp = (AssessmentPain)form.getGlobalContext().Nursing.getAssessmentComponent();
				if (comp.getOptionalPainAssessment() == null)
				{
					try 
					{
						onBtnNewClick();
					}catch (PresentationLogicException e) {displayError(e.getMessage());}
				}
			}
			else
				form.setMode(FormMode.VIEW);
		}
	}
	protected void onFormDialogClosed(ims.framework.FormName formName, ims.framework.enumerations.DialogResult result) throws ims.framework.exceptions.PresentationLogicException
	{
		if(formName.equals(form.getForms().Nursing.PainReviewDialog))
		{
			if(result.equals(DialogResult.OK))
			{
				PainAssessment painAssVO = form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO();
				if(painAssVO != null)
				{
					//we have to create a dummy SkinAssessmentReview to keep the DateTimeReview for SkinAssessmentFindings
					PainAssessmentFindings pafVO = form.getGlobalContext().COE.PainBodyChart.getAssessmentFinding();

					if(pafVO != null)
					{
						//Add the new VO to the collection
						if(painAssVO.getAssessmentDateTime() != null)
						{
							form.dteAssessment().setValue(painAssVO.getAssessmentDateTime().getDate());
							form.timAssessment().setValue(painAssVO.getAssessmentDateTime().getTime());
						}
						if(painAssVO.getDateTimeInitiated() != null)
						{
							form.dteRecording().setValue(painAssVO.getDateTimeInitiated().getDate());
							form.timRecording().setValue(painAssVO.getDateTimeInitiated().getTime());
						}

						if(pafVO.getPainAssessmentReview() == null)
							pafVO.setPainAssessmentReview(new PainAssessmentReviewCollection());

						if(pafVO.getPainAssessmentReview().size() == 0 || reviewEdited(pafVO))
						{
							PainAssessmentReview parVO = getReviewVOfromFindingVO(pafVO);
							int reviewIndex = pafVO.getPainAssessmentReview().indexOf(parVO);
							if(reviewIndex >=0)
								pafVO.getPainAssessmentReview().set(reviewIndex, parVO);
							else
								pafVO.getPainAssessmentReview().add(parVO);
							form.getGlobalContext().COE.PainBodyChart.setAssessmentFinding(pafVO);
						}
					}


					int index = painAssVO.getSiteDetails().indexOf(form.getGlobalContext().COE.PainBodyChart.getAssessmentFinding());
					if( index >= 0)
					{
						painAssVO.getSiteDetails().remove(index);
						if(painAssVO.getSiteDetails() == null)
							painAssVO.setSiteDetails(new PainAssessmentFindingsCollection());
						painAssVO.getSiteDetails().add(form.getGlobalContext().COE.PainBodyChart.getAssessmentFinding());
					}
					else
						painAssVO.getSiteDetails().add(form.getGlobalContext().COE.PainBodyChart.getAssessmentFinding());
					//set the Pain Assessment Context back
					form.getGlobalContext().COE.PainBodyChart.setPainAssessmentVO(painAssVO);
				}
			}
			else if(result.equals(DialogResult.CANCEL))
			{
				//Find if the user clicked Cancel for the first Time (was changing his mind)
				//and warn the user to clear the drawing
				PainAssessment painAssVO = form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO();
				if(painAssVO != null)
				{
					int index = painAssVO.getSiteDetails().indexOf(form.getGlobalContext().COE.PainBodyChart.getAssessmentFinding());
					if(index < 0)
					{
						displayError("Please select the previous image and remove it.");
					}
				}
			}
		}
		else if(formName.equals(form.getForms().Core.YesNoDialog))
		{
			if(result.equals(DialogResult.YES))
				//copy previous assessment
				copyLastAssessment();
			else
			{
				form.getGlobalContext().COE.PainBodyChart.setAreaList(null);
				form.getGlobalContext().COE.PainBodyChart.setAssessmentFinding(null);
				form.getGlobalContext().COE.PainBodyChart.setGroupList(null);
				form.getGlobalContext().COE.PainBodyChart.setPainAssessmentVO(null);
				form.getGlobalContext().COE.PainBodyChart.setReadOnlyAssessment(null);

				clearControls();
			}
			populateScreen();
		}

	}
	
	private void copyLastAssessment()
	{
		PainAssessment voPainAssessment = form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO();
		PainAssessment newPainAssessment = new PainAssessment();
		
		
		if (newPainAssessment.getClinicalContact() == null)
			newPainAssessment.setClinicalContact(form.getGlobalContext().Core.getCurrentClinicalContact());
		
		if (newPainAssessment.getCareContext() == null)
			newPainAssessment.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
		
		if (voPainAssessment.getSiteDetailsIsNotNull())
		{
			PainAssessmentFindingsCollection voCollFindings = new PainAssessmentFindingsCollection();
			
			for (int i = 0; i < voPainAssessment.getSiteDetails().size(); i++)
			{
				if (voPainAssessment.getSiteDetails().get(i).getIsDiscontinuedAssessIsNotNull() && !voPainAssessment.getSiteDetails().get(i).getIsDiscontinuedAssess().booleanValue())
				{					
					PainAssessmentFindings voFinding = voPainAssessment.getSiteDetails().get(i);
					
					//force copy of finding
					voFinding.setID_PainAssessmentFindings(null);
					
					if (voFinding.getPainAssessmentReviewIsNotNull())
					{
						//force copy of reviews
						for(int j = 0; j < voFinding.getPainAssessmentReview().size(); j++)
							voFinding.getPainAssessmentReview().get(j).setID_PainAssessmentReview(null);
					}			
					
					voCollFindings.add(voFinding);
				}
			}
			
			newPainAssessment.setSiteDetails(voCollFindings);
		}
		else
			newPainAssessment.setSiteDetails(null);
		
		form.getGlobalContext().COE.PainBodyChart.setPainAssessmentVO(newPainAssessment);
	}
	
	private PainAssessmentReview getReviewVOfromFindingVO(PainAssessmentFindings pafVO)
	{
		PainAssessmentReview painAssVo = getReviewRecord(pafVO);
		if(painAssVo.getID_PainAssessmentReview() == null)
			painAssVo.setID_PainAssessmentReview(pafVO.getID_PainAssessmentFindings());
		painAssVo.setDepth(pafVO.getDepth());
		painAssVo.setOnset(pafVO.getOnset());
		painAssVo.setProgression(pafVO.getProgression());
		painAssVo.setRecordingDateTime(new DateTime(form.dteRecording().getValue(), form.timRecording().getValue()));
		painAssVo.setAssessmentDateTime(new DateTime(form.dteAssessment().getValue(), form.timAssessment().getValue()));
		
		MemberOfStaffShortVo memStaffShortVo = (MemberOfStaffShortVo)domain.getMosUser();
		if(memStaffShortVo != null && memStaffShortVo.getHcp() != null)
			painAssVo.setRecordingHCP(memStaffShortVo.getHcp());
		
		painAssVo.setSeverity(pafVO.getSeverity());
		painAssVo.setSiteDetails(pafVO.getSiteDetails());
		painAssVo.setType(pafVO.getType());
		return painAssVo;
	}
	
	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (form.getGlobalContext().Nursing.getAssessmentComponent() != null)
		{
			engine.open(form.getForms().COE.AssessPain);
			return;
		}
		
		open();
		form.setMode(FormMode.VIEW);
	}
	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{
		//Save details
		PainAssessment painAssVO = form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO();
		PainAssessment painAssVoBackup = painAssVO !=null?(PainAssessment)painAssVO.clone():null;
		try {
			
			if(form.dteAssessment().getValue() == null)
			{
				displayError("Please enter Assessment date");
				return;
			}
			if(form.timAssessment().getValue() == null)
			{
				displayError("Please enter Assessment time");
				return;
			}
			//Populate common details
			painAssVO.setDateTimeInitiated(new DateTime(form.dteRecording().getValue(), form.timRecording().getValue()));
			painAssVO.setAssessmentDateTime(new DateTime(form.dteAssessment().getValue(), form.timAssessment().getValue()));
			
			Object hcpLite = domain.getHcpLiteUser();
			painAssVO.setHcpInitiated(hcpLite != null ? ((HcpLiteVo)hcpLite) : null);
			
			//Get the findings with no details on them
			getFindingsNoDetails(painAssVO);
						
			if(!activeFindings(painAssVO))
			{
				displayError("Please enter at least one finding before saving");
				return;
			}
			
			//Clear the FindingsID
			for(int i=0; i<painAssVO.getSiteDetails().size(); i++)
			{
				painAssVO.getSiteDetails().get(i).setID_PainAssessmentFindings(null);
				
				if(painAssVO.getSiteDetails().get(i).getPainAssessmentReviewIsNotNull())
				{
					for (int j = 0; j < painAssVO.getSiteDetails().get(i).getPainAssessmentReview().size(); j++)
					{
						painAssVO.getSiteDetails().get(i).getPainAssessmentReview().get(j).setID_PainAssessmentReview(null);
					}
				}
				
			}
			
			if (painAssVO.getClinicalContact() == null)
				painAssVO.setClinicalContact(form.getGlobalContext().Core.getCurrentClinicalContact());
			
			if (painAssVO.getCareContext() == null)
				painAssVO.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
			
			String[] error = painAssVO.validate();
			if(error != null)
			{
				displayErrors(error);
				//12/09/2005 - Restore the vo to the context if the validation fails 		
				form.getGlobalContext().COE.PainBodyChart.setPainAssessmentVO(painAssVoBackup);
				return;
			}
			
			// If there is an assesment component in the global context, we want to save that too!
			if (form.getGlobalContext().Nursing.getAssessmentComponent() != null && form.getGlobalContext().Nursing.getAssessmentComponent() instanceof AssessmentPain)
			{
				AssessmentPain comp = (AssessmentPain)form.getGlobalContext().Nursing.getAssessmentComponent();
				comp.setOptionalPainAssessment(painAssVO);
				String[] arrErrors = comp.validate();
				if(arrErrors != null)
				{
					engine.showErrors("Validation Errors", arrErrors);
					return;
				}
				AssessmentHeaderInfo voAssessmentHeaderInfo  = form.getGlobalContext().Nursing.getAssessmentHeaderInfo();
				arrErrors = voAssessmentHeaderInfo.validate();
				if(arrErrors != null)
				{
					engine.showErrors("Validation Errors", arrErrors);
					return;
				}

				AssessmentHeaderInfo headerInfo = null;
				try 
				{
					headerInfo = domain.saveRecord(painAssVO, voAssessmentHeaderInfo, comp);
				} 
				catch (UniqueKeyViolationException e) 
				{
					engine.showMessage(e.getMessage());
					return;
				}
				// Go back to the mobility form
				//form.getGlobalContext().Nursing.setAssessmentHeaderInfo(headerInfo); //WDEV-20145 prevent NPE as no domain implementation for Save //TODO save needs implemented in future release.
				engine.open(form.getForms().COE.AssessPain);
				return;
			} 
			else
			{
				try 
				{
					domain.saveRecord(painAssVO, null, null);
				} 
				catch (UniqueKeyViolationException e) 
				{
					engine.showMessage(e.getMessage());
					return;
				}
			}
		} 
		catch (StaleObjectException e) 
		{
			displayError(ims.configuration.gen.ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
		}
		catch (DomainInterfaceException e)
		{
			displayError(e.getMessage());
		}
		
		
		open();
		form.setMode(FormMode.VIEW);
	}
	
	private void copyPreviousFindings(PainAssessment painAssVO)
	{
		if(form.getLocalContext().getPreviousPainAssessment() != null && form.getLocalContext().getPreviousPainAssessment().getSiteDetails() != null)
		{
			PainAssessmentFindings painFindingVO;
			for(int i=0; i<form.getLocalContext().getPreviousPainAssessment().getSiteDetails().size(); i++)
			{
				painFindingVO = (PainAssessmentFindings)form.getLocalContext().getPreviousPainAssessment().getSiteDetails().get(i).clone();
				//Skip the discontinued ones
				if(painFindingVO.getIsDiscontinuedAssess() != null && 
				   painFindingVO.getIsDiscontinuedAssess().booleanValue())
					continue;

				//Add the FindingVO to the main PainAssessmentVO
				painFindingVO.setIsContinuedAssessment(new Boolean(true));
				painAssVO.getSiteDetails().add(painFindingVO);
			}
		}
	}
	
	protected void onBtnNewClick() throws ims.framework.exceptions.PresentationLogicException
	{
		DateTime now = new DateTime(new Date(), new Time());
		
		if ((form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVOIsNotNull()) && form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO().getCareContext().equals(form.getGlobalContext().Core.getCurrentCareContext()))
		{
			
			PainAssessment lastPainAssessment = form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO();
			
			if(lastPainAssessment.getDateTimeInitiatedIsNotNull() 
			&& lastPainAssessment.getDateTimeInitiated().getDate().equals(now.getDate()) 
			&& lastPainAssessment.getDateTimeInitiated().getTime().equals(now.getTime())) 
			{
				engine.showMessage("An assessment already exists for " + now);
				return;
			}
			else
				populateScreen();
		}
		else if (form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVOIsNotNull())
		{
			form.getGlobalContext().Core.setYesNoDialogMessage("Do you wish to copy this assessment?");
			engine.open(form.getForms().Core.YesNoDialog,false);
		}
		else
		{
			populateScreen();
		}
		
		form.setMode(FormMode.EDIT);
	}
	private void populateScreen()
	{
		DateTime now = new DateTime(new Date(), new Time());
		
		//Prepopulate Controls----------------------------------------------------
		if(domain.getMosUser() != null)
			form.txtBoxHCP().setValue(((MemberOfStaffShortVo)domain.getMosUser()).toString());
				
		form.dteAssessment().setValue(new Date());
		form.timAssessment().setValue(new Time());
		form.dteRecording().setValue(new Date());
		form.timRecording().setValue(new Time());
		//-------------------------------------------------------------------------
		
		//Save previous PainAssessment
		form.getLocalContext().setPreviousPainAssessment(form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO());
		
		PainAssessment painAssessmentVO = new PainAssessment();
		painAssessmentVO.setAssessmentDateTime(now);
		painAssessmentVO.setDateTimeInitiated(now);
	
		Object hcpLite = domain.getHcpLiteUser();
		painAssessmentVO.setHcpInitiated(hcpLite != null ? ((HcpLiteVo)hcpLite) : null);
		
		painAssessmentVO.setSiteDetails(new PainAssessmentFindingsCollection());
		
		DrawingImageVersionVo versionNoVo = new DrawingImageVersionVo();
		versionNoVo.setImageName(getCurrentImageName());
		if(getCurrentVersionNumber() != null)
			versionNoVo.setVersionNumber(getCurrentVersionNumber());
		painAssessmentVO.setImageVersion(versionNoVo);
		//Copy the previous Active(Not Discontinued) Findings
		copyPreviousFindings(painAssessmentVO);
		
		//Add a new VO
		form.getGlobalContext().COE.PainBodyChart.setPainAssessmentVO(painAssessmentVO);

	}
	private boolean open()
	{
		return open(false);
	}
	private boolean open(boolean bInitDrawingCtrl)
	{
		clearControls();
		boolean bLoadData; 
		boolean bInactiveFindings = false;
		//If we have an assessment then display it (the assessment comes from the review Page)
		if(form.getGlobalContext().COE.PainBodyChart.getReadOnlyAssessment() != null)
		{
			form.getGlobalContext().COE.PainBodyChart.setPainAssessmentVO(form.getGlobalContext().COE.PainBodyChart.getReadOnlyAssessment());
			//Clear Context
			form.getGlobalContext().COE.PainBodyChart.setReadOnlyAssessment(null);
			form.btnNew().setEnabled(false);
			bInactiveFindings = true;
		}
		else if (form.getGlobalContext().Nursing.getAssessmentComponent() != null && form.getGlobalContext().Nursing.getAssessmentComponent() instanceof AssessmentPain) // Came from the Pain Assessment screen
		{
			AssessmentPain comp = (AssessmentPain)form.getGlobalContext().Nursing.getAssessmentComponent();
			if (comp.getOptionalPainAssessment() != null)
			{
				form.getGlobalContext().COE.PainBodyChart.setPainAssessmentVO(comp.getOptionalPainAssessment());
				//Clear Context
				form.getGlobalContext().COE.PainBodyChart.setReadOnlyAssessment(null);
				form.btnNew().setEnabled(false);
				bInactiveFindings = true;
			}
			else
			{
				form.getGlobalContext().COE.PainBodyChart.setPainAssessmentVO(domain.geLastPain(form.getGlobalContext().Core.getCurrentCareContext()));
				form.btnNew().setEnabled(true);
			}
		}
		else
		{			
			form.btnNew().setEnabled(true);
			//Do a Get Last			
			form.getGlobalContext().COE.PainBodyChart.setPainAssessmentVO(domain.getLastPainAssessmentByPatient(form.getGlobalContext().Core.getPatientShort().getID_Patient()));
			
		}
		
		initialiseDrawingControl(bInitDrawingCtrl);
		bLoadData = form.getLocalContext().getImageLoaded() != null && form.getLocalContext().getImageLoaded().booleanValue()?true:false;
		//Don't load the configuration as we don't have an image loaded 
		if(bLoadData)
		{
			populateDrawingControl(bInactiveFindings);
			populateControls();
		}
		else
		{
			//displayError("No areas defined.");
			return false;
		}
		
		return true;
	}

	private void populateDrawingControl(boolean vDisplayInactive)
	{
		form.drawingBodyChart().clearShapes();
		PainAssessment painAssVO =  form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO();
		DrawingControlShape shape;
				
		if(painAssVO!= null)
		{
			for(int i=0; i<painAssVO.getSiteDetails().size(); i++)
			{
				//Skip the discontinued
				if(!vDisplayInactive &&  painAssVO.getSiteDetails().get(i).getIsDiscontinuedAssess() != null  	  &&
				   painAssVO.getSiteDetails().get(i).getIsDiscontinuedAssess().booleanValue())
					continue;
				
				shape = new DrawingControlShape(painAssVO.getSiteDetails().get(i).getID_PainAssessmentFindings().intValue(), savedTargetID, painAssVO.getSiteDetails().get(i).getSiteImage(), painAssVO.getSiteDetails().get(i).getPainHypersensitivity().getId(), true, getFindingTooltip(painAssVO, painAssVO.getSiteDetails().get(i)));
				form.drawingBodyChart().addShape(shape);
			}
		}
	}
			
	private void clearControls()
	{
		form.dteAssessment().setValue(null);
		form.timAssessment().setValue(null);
		form.dteRecording().setValue(null);
		form.timRecording().setValue(null);
		form.txtBoxHCP().setValue("");
		form.drawingBodyChart().clearShapes();
	}
	
	private void populateControls()
	{
		PainAssessment painAssessmentVo = form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO();
		if(painAssessmentVo == null)
			return;
		
		if(painAssessmentVo.getDateTimeInitiated() != null)
		{
			if(painAssessmentVo.getDateTimeInitiated().getDate() != null)
				form.dteRecording().setValue(painAssessmentVo.getDateTimeInitiated().getDate());
			
			if(painAssessmentVo.getDateTimeInitiated().getTime() != null)
				form.timRecording().setValue(painAssessmentVo.getDateTimeInitiated().getTime());
		}
		if(painAssessmentVo.getAssessmentDateTime() != null)
		{
			if(painAssessmentVo.getAssessmentDateTime().getDate() != null)
				form.dteAssessment().setValue(painAssessmentVo.getAssessmentDateTime().getDate());
			
			if(painAssessmentVo.getAssessmentDateTime().getTime() != null)
				form.timAssessment().setValue(painAssessmentVo.getAssessmentDateTime().getTime());
		}
			
		if(painAssessmentVo.getHcpInitiated() != null)
			form.txtBoxHCP().setValue(painAssessmentVo.getHcpInitiated().toString());
	}
	
	private void initialiseDrawingControl(boolean bInit)
	{
		//Initialise the control only when the page is visited
		if(bInit)
		{
			form.drawingBodyChart().setImage(form.getImages().COE.BodyChart);
			for(int i=0; i<PainHypersensitivity.getNegativeInstances().length; i++)
				form.drawingBodyChart().addBrush(PainHypersensitivity.getNegativeInstances()[i].getId(), PainHypersensitivity.getNegativeInstances()[i].getText(), PainHypersensitivity.getNegativeInstances()[i].getColor());
		
			//load the image associated image to this Form				
			AppImageVo voAppImage = new AppImageVo();
			voAppImage.setID_AppImage(new Integer(form.getImages().COE.BodyChart.getImageId()));
			
			DrawingImageVo voImage = new DrawingImageVo();
			//TODO: Leave till COE release of 03/03/05 has gone
			voImage.setImage(voAppImage);

			voImage.setImageName(getCurrentImageName());
			//When no version no - the domain will return getLast()
			if(getCurrentVersionNumber() != null)
				voImage.setVersionNumber(getCurrentVersionNumber());
			
			DrawingImageVo voImageRetrieved = domain.getDrawingImage(voImage);
			if(voImageRetrieved != null)
			{
				DrawingControlGroup rootNode = new DrawingControlGroup(voImageRetrieved.getRootGroup().getGroupID().intValue(), voImageRetrieved.getRootGroup().getGroupName());
				convertAllAreasToNode(voImageRetrieved.getRootGroup(), rootNode);
				
				form.drawingBodyChart().setAreas(rootNode);
				
				form.getGlobalContext().COE.PainBodyChart.setAreaList(rootNode.getAllAreas());
				form.getGlobalContext().COE.PainBodyChart.setGroupList(getAllGroups(rootNode));
				form.getLocalContext().setImageLoaded(new Boolean(true));
				form.getLocalContext().setCurrentVersionNo(voImageRetrieved.getVersionNumber());
			}
			else
			{
				//Load default image
				DrawingControlGroup root = new DrawingControlGroup(-1, "Body Chart");
				form.drawingBodyChart().setAreas(root);
				
				form.getLocalContext().setImageLoaded(new Boolean(false));
			}
		}
	}

	private String getCurrentImageName()
	{
		if(form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO() != null && 
		   form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO().getImageVersion() != null)
			return form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO().getImageVersion().getImageName();
		
		//TODO - change this hard code to get the last Image when will be available
		return "Body Chart Assessment";
	}
	
	private Integer getCurrentVersionNumber()
	{
		if(form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO() != null && 
		   form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO().getImageVersion() != null)
			return form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO().getImageVersion().getVersionNumber();
		
		return form.getLocalContext().getCurrentVersionNo();
	}
	
	private void convertAllAreasToNode(DrawingGroupVo voGroup,DrawingControlGroup node)
	{
		node.setName(voGroup.getGroupName());
		//convert all areas to controlgroup
		int i;
		for(i=0;i<voGroup.getAreaCollection().size();i++)
		{
			node.add(createDrawingControlArea(voGroup.getAreaCollection().get(i)));
		}
		
		for(int j=0;j<voGroup.getChildGroups().size();j++)
		{
			DrawingGroupVo  childGroup = voGroup.getChildGroups().get(j);
			node.add(createDrawingControlGroup(childGroup));
			convertAllAreasToNode(childGroup, node.getGroup(i+j));
		}	
	}
	
	private DrawingControlGroup createDrawingControlGroup(DrawingGroupVo voGroup) 
	{
		DrawingControlGroup group = new DrawingControlGroup(voGroup.getGroupID().intValue(),voGroup.getGroupName());
		return group;
	}

	private DrawingControlArea createDrawingControlArea(DrawingAreaVo voArea) 
	{
		DrawingControlArea area = new DrawingControlArea(voArea.getAreaID().intValue(),voArea.getAreaName(),voArea.getAreaVectors());
		return area;
	}	
			
	private void displayErrors(String [] errors)
	{
		engine.showErrors(errors);
	}
	private void displayError(String error)
	{
		engine.showMessage(error);
	}
	
	protected void onDrawingBodyChartNote(DrawingControlShape shape) throws PresentationLogicException 
	{
		PainAssessment painAssVO = form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO();
		if(painAssVO == null)
			painAssVO = new PainAssessment();
		
		Object hcpLite = domain.getHcpLiteUser();
		painAssVO.setHcpInitiated(hcpLite != null ? ((HcpLiteVo)hcpLite) : null);
		
		painAssVO.setDateTimeInitiated(new DateTime(form.dteRecording().getValue(), form.timRecording().getValue()));
		painAssVO.setAssessmentDateTime(new DateTime(form.dteAssessment().getValue(), form.timAssessment().getValue()));

		if(painAssVO.getSiteDetails() == null)
			painAssVO.setSiteDetails(new PainAssessmentFindingsCollection());
		
		PainAssessmentFindings painAssFindingsVO = null; 
		
		boolean bSiteFound = false;
		for(int i=0; i<painAssVO.getSiteDetails().size(); i++)
		{
			//The user can update the record not saved
			if( painAssVO.getSiteDetails().get(i).getID_PainAssessmentFindingsIsNotNull() && 
					painAssVO.getSiteDetails().get(i).getID_PainAssessmentFindings().intValue() == shape.getIndex()) 
			{
				if(shape.getTargetID() == savedTargetID)
				{
					//Saved record
					engine.showMessage("You are not allowed to update an existing finding.");
					return;	
				}
				else
				{
					//Not saved yet record - and as a consequence is updatable
					painAssFindingsVO = painAssVO.getSiteDetails().get(i);
					//02/11/2005 - Update the drawing (uml) as it might have been edited 
					painAssFindingsVO.setSiteImage(shape.getVML());
					painAssVO.getSiteDetails().set(i, painAssFindingsVO);
					bSiteFound = true;
					break;
				}
			}
		}
		//Obsolete check - only if the spec changes and allows updating an existing finding
		if(!bSiteFound)
		{
			painAssFindingsVO = new PainAssessmentFindings();
			//Site name
			painAssFindingsVO.setSiteName(getAreaOrGroupName(shape.getTargetID()));
			//Site Index
//			painAssFindingsVO.setSiteIndex(new Integer(shape.getIndex()));
			painAssFindingsVO.setID_PainAssessmentFindings(new Integer(shape.getIndex()));
			//Site XML image description
			painAssFindingsVO.setSiteImage(shape.getVML());
			//Hypersensitivity
			painAssFindingsVO.setPainHypersensitivity(getHypersensitivity(shape.getBrushID()));
			//Active
			painAssFindingsVO.setIsDiscontinuedAssess(new Boolean(false));
		}

		//Set the Assessment Findings
		form.getGlobalContext().COE.PainBodyChart.setAssessmentFinding(painAssFindingsVO);
		//Set the main Pain Assessment 
		form.getGlobalContext().COE.PainBodyChart.setPainAssessmentVO(painAssVO);
		
		engine.open(form.getForms().Nursing.PainReviewDialog, "New Assessment");
	}

	private PainHypersensitivity getHypersensitivity(int brushID)
	{
		for(int i=0; i<PainHypersensitivity.getNegativeInstances().length; i++)
		{
			if(brushID == PainHypersensitivity.getNegativeInstances()[i].getId())
				return PainHypersensitivity.getNegativeInstances()[i];
		}
		
		return PainHypersensitivity.PAIN;
	}
	
	protected void onDrawingBodyChartRemoved(int index) throws PresentationLogicException 
	{
		//Check to see if the shape it's not saved - if it's saved warn the user that he has to discontinue the finding
		PainAssessment painAssVO = form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO();
		
		//Remove an assessment introduced by defaul
		PainAssessmentFindings assFindingVO;
		DrawingControlShape shape;
		for(int i=0; i<painAssVO.getSiteDetails().size(); i++)
		{
			assFindingVO = painAssVO.getSiteDetails().get(i); 
			if(assFindingVO.getID_PainAssessmentFindingsIsNotNull() && 
			   assFindingVO.getID_PainAssessmentFindings().intValue() == index)
			{
				//The Assessment Finding record was saved (brought from the previous)
				if(assFindingVO.getID_PainAssessmentFindings() != null && recordSaved(assFindingVO.getID_PainAssessmentFindings()))
				{
					displayError("Previous saved findings cannot be updated.");
					//try to redisplay the image
					shape = new DrawingControlShape(assFindingVO.getID_PainAssessmentFindings().intValue(), savedTargetID, assFindingVO.getSiteImage(), assFindingVO.getPainHypersensitivity().getId(), true);
					form.drawingBodyChart().addShape(shape);
					return;
				}
				else
				{
					painAssVO.getSiteDetails().remove(i);
					form.getGlobalContext().COE.PainBodyChart.setPainAssessmentVO(painAssVO);
					return;
				}
			}
		}
	}

	protected void onFormModeChanged() 
	{
		if(form.getMode().equals(FormMode.EDIT))
		{
			form.btnNew().setEnabled(false);
			form.btnClose().setVisible(false);
		}
		else
		{
			if(isAssessmentComponent())
				form.btnClose().setVisible(true);
			else
				form.btnClose().setVisible(false);
		}
	}
	
	private boolean activeFindings(PainAssessment painAssVO)
	{
		if(painAssVO != null && painAssVO.getSiteDetails() != null)
		{
			for(int i=0; i<painAssVO.getSiteDetails().size(); i++)
				if(painAssVO.getSiteDetails().get(i).getIsDiscontinuedAssess() != null && 
				   painAssVO.getSiteDetails().get(i).getIsDiscontinuedAssess().booleanValue())
					continue;
				else
					return true;
		}
		
		return false;
	}
	
	private String getFindingTooltip(PainAssessment painsAssVO, PainAssessmentFindings findingVO)
	{
		StringBuffer tooltip = new StringBuffer();
		String cr = "<br>";
		PainAssessmentReview lastReviewVo = null;
		
		if(findingVO != null && findingVO.getPainAssessmentReviewIsNotNull() && findingVO.getPainAssessmentReview().size() > 0)
		{
			findingVO.getPainAssessmentReview().sort();
			lastReviewVo = findingVO.getPainAssessmentReview().get(findingVO.getPainAssessmentReview().size() - 1);
		
			if(findingVO.getPainAssessmentReview().size() > 1) 
			{
				tooltip.append("<b>Last Review</b>");
				tooltip.append(cr);
				tooltip.append("-----------------------------------------------------------");
				tooltip.append(cr);
			}
			
			tooltip.append(label[0]);
			if(lastReviewVo.getRecordingDateTime() != null)
				tooltip.append(lastReviewVo.getRecordingDateTime().getDate().toString(DateFormat.STANDARD) + "; " + lastReviewVo.getRecordingDateTime().getTime().toString(TimeFormat.DEFAULT) + "hrs");
			tooltip.append(cr);
			
			tooltip.append(label[1]);
			if(lastReviewVo.getAssessmentDateTime() != null)
				tooltip.append(lastReviewVo.getAssessmentDateTime().getDate().toString(DateFormat.STANDARD) + "; " + lastReviewVo.getAssessmentDateTime().getTime().toString(TimeFormat.DEFAULT) + "hrs");
			tooltip.append(cr);
			
			tooltip.append(label[2]);
			if(lastReviewVo.getRecordingHCP() != null)
				tooltip.append(lastReviewVo.getRecordingHCP().toString());
			tooltip.append(cr);
			
			tooltip.append(label[3]);
			if(findingVO.getPainHypersensitivity() != null)
				tooltip.append(findingVO.getPainHypersensitivity().getText() + (findingVO.getIsContinuedAssessment() != null && findingVO.getIsContinuedAssessment().booleanValue()?" <font color=\"#FF0000\">(Copied)</font>":""));
			tooltip.append(cr);
			
			tooltip.append(label[4]);
			tooltip.append(findingVO.getSiteName());
			tooltip.append(cr);
			
			tooltip.append(label[5]);
			if(lastReviewVo.getSiteDetails() != null)
				tooltip.append(lastReviewVo.getSiteDetails());
			tooltip.append(cr);
			
			tooltip.append(label[6]);
			if(lastReviewVo.getType() != null)
				tooltip.append(lastReviewVo.getType().getText());
			tooltip.append(cr);
			
			tooltip.append(label[7]);
			if(lastReviewVo.getSeverity() != null)
				tooltip.append(lastReviewVo.getSeverity().getText());
			tooltip.append(cr);
			
			tooltip.append(label[8]);
			if(lastReviewVo.getOnset() != null)
				tooltip.append(lastReviewVo.getOnset().getText());
			tooltip.append(cr);
			
			tooltip.append(label[9]);
			if(lastReviewVo.getProgression() != null)
				tooltip.append(lastReviewVo.getProgression().getText());
			tooltip.append(cr);
			
			tooltip.append(label[10]);
			if(lastReviewVo.getDepth() != null)
				tooltip.append(lastReviewVo.getDepth().getText());
			tooltip.append(cr);
			
/*			tooltip.append(label[11]);
			tooltip.append(findingVO.getIsContinuedAssessment() != null && findingVO.getIsContinuedAssessment().booleanValue()?yes:no);
			tooltip.append(cr);*/
			
			tooltip.append(label[12]);
			tooltip.append(findingVO.getIsDiscontinuedAssess() == null || !findingVO.getIsDiscontinuedAssess().booleanValue()?no:yes);
		}
		else
		{
			tooltip.append(label[0]);
			if(painsAssVO.getDateTimeInitiated() != null)
				tooltip.append(painsAssVO.getDateTimeInitiated().getDate().toString(DateFormat.STANDARD) + "; " + painsAssVO.getDateTimeInitiated().getTime().toString(TimeFormat.DEFAULT) + "hrs");
			tooltip.append(cr);
			
			tooltip.append(label[1]);
			if(painsAssVO.getAssessmentDateTime() != null)
				tooltip.append(painsAssVO.getAssessmentDateTime().getDate().toString(DateFormat.STANDARD) + "; " + painsAssVO.getAssessmentDateTime().getTime().toString(TimeFormat.DEFAULT) + "hrs");
			tooltip.append(cr);
			
			tooltip.append(label[2]);
			if(painsAssVO.getHcpInitiated() != null)
				tooltip.append(painsAssVO.getHcpInitiated().toString());
			tooltip.append(cr);
			
			tooltip.append(label[3]);
			if(findingVO.getPainHypersensitivity() != null)
				tooltip.append(findingVO.getPainHypersensitivity().getText() + (findingVO.getIsContinuedAssessment() != null && findingVO.getIsContinuedAssessment().booleanValue()?" <font color=\"#FF0000\">(Copied)</font>":""));
			tooltip.append(cr);
			
			tooltip.append(label[4]);
			tooltip.append(findingVO.getSiteName());
			tooltip.append(cr);
			
			tooltip.append(label[5]);
			if(findingVO.getSiteDetails() != null)
				tooltip.append(findingVO.getSiteDetails());
			tooltip.append(cr);
			
			tooltip.append(label[6]);
			if(findingVO.getType() != null)
				tooltip.append(findingVO.getType().getText());
			tooltip.append(cr);
			
			tooltip.append(label[7]);
			if(findingVO.getSeverity() != null)
				tooltip.append(findingVO.getSeverity().getText());
			tooltip.append(cr);
			
			tooltip.append(label[8]);
			if(findingVO.getOnset() != null)
				tooltip.append(findingVO.getOnset().getText());
			tooltip.append(cr);
			
			tooltip.append(label[9]);
			if(findingVO.getProgression() != null)
				tooltip.append(findingVO.getProgression().getText());
			tooltip.append(cr);
			
			tooltip.append(label[10]);
			if(findingVO.getDepth() != null)
				tooltip.append(findingVO.getDepth().getText());
			tooltip.append(cr);
			
/*			tooltip.append(label[11]);
			tooltip.append(findingVO.getIsContinuedAssessment() != null && findingVO.getIsContinuedAssessment().booleanValue()?yes:no);
			tooltip.append(cr);*/
			
			tooltip.append(label[12]);
			tooltip.append(findingVO.getIsDiscontinuedAssess() == null || !findingVO.getIsDiscontinuedAssess().booleanValue()?no:yes);
		}
		
		return tooltip.toString();
	}
	
	private void getFindingsNoDetails(PainAssessment painAssVO)
	{
		PainAssessmentFindings findingVO;
		DrawingControlShape shape;
		boolean shapeSaved;
		for(int i=0; i<form.drawingBodyChart().getShapes().size(); i++)
		{
			shapeSaved = false;
			shape = (DrawingControlShape)form.drawingBodyChart().getShapes().get(i);
			
			for(int j=0; j<painAssVO.getSiteDetails().size(); j++)
			{
				if(painAssVO.getSiteDetails().get(j).getID_PainAssessmentFindingsIsNotNull() && 
				   painAssVO.getSiteDetails().get(j).getID_PainAssessmentFindings().intValue() == shape.getIndex())
				{
					shapeSaved = true;
					break;
				}
			}
			
			if(!shapeSaved)
			{
				findingVO = new PainAssessmentFindings();
				findingVO.setPainHypersensitivity(getHypersensitivity(shape.getBrushID()));
//				findingVO.setSiteIndex(new Integer(shape.getIndex()));
//				findingVO.setSiteID(new Integer(shape.getTargetID()));
				findingVO.setSiteName(getAreaOrGroupName(shape.getTargetID()));
				findingVO.setSiteImage(shape.getVML());
				findingVO.setIsDiscontinuedAssess(new Boolean(false));
				
			
				//painAssVO.getSiteDetails().add(findingVO);
			}
		}
	}
	
	protected void onDrawingBodyChartEdited(int index) throws PresentationLogicException 
	{
		PainAssessment painAssVO = form.getGlobalContext().COE.PainBodyChart.getPainAssessmentVO();
		PainAssessmentFindings painAssFindingsVO = null; 

		boolean bSiteFound = false;
		for(int i=0; i<painAssVO.getSiteDetails().size(); i++)
		{
			//The user can update the record not saved
			if( painAssVO.getSiteDetails().get(i).getID_PainAssessmentFindingsIsNotNull() && 
				painAssVO.getSiteDetails().get(i).getID_PainAssessmentFindings().intValue() == index) 
			{
				painAssFindingsVO = (PainAssessmentFindings)painAssVO.getSiteDetails().get(i).clone();
				painAssFindingsVO.setSiteImage(getVMLfromIndex(index));
				painAssVO.getSiteDetails().remove(i);
				painAssVO.getSiteDetails().add(painAssFindingsVO);
				bSiteFound = true;
				break;
			}
		}
		//05/11/2004 - Code change to cover the bug of not returning the correct GroupID when saving
		if(!bSiteFound)
		{
			//get the Drawing Shape edited
			DrawingControlShape shape = getEditedShape(index);
			if(shape != null)
			{
				painAssFindingsVO = new PainAssessmentFindings();
				painAssFindingsVO.setID_PainAssessmentFindings(new Integer(shape.getIndex()));
				painAssFindingsVO.setPainHypersensitivity(getHypersensitivity(shape.getBrushID()));
				painAssFindingsVO.setSiteName(getAreaOrGroupName(shape.getTargetID()));
				painAssFindingsVO.setSiteImage(shape.getVML());
				painAssFindingsVO.setIsDiscontinuedAssess(new Boolean(false));
				//painAssVO.getSiteDetails().add(painAssFindingsVO);
				bSiteFound = true;
				
				//create a new review from findings
				
				painAssFindingsVO.setPainAssessmentReview(new PainAssessmentReviewCollection());
				PainAssessmentReview parVO = getReviewVOfromFindingVO(painAssFindingsVO);
				painAssFindingsVO.getPainAssessmentReview().add(parVO);
				
				painAssVO.getSiteDetails().add(painAssFindingsVO);
			}
			else
				displayError("No drawing details found.");
		}
		if(bSiteFound)
		{
			//	Set the Assessment Findings
			form.getGlobalContext().COE.PainBodyChart.setAssessmentFinding(painAssFindingsVO);
			//Set the main Pain Assessment 
			form.getGlobalContext().COE.PainBodyChart.setPainAssessmentVO(painAssVO);
		}
	}
	
	//returns the VML of the newly edited image
	private String getVMLfromIndex(int index)
	{
		DrawingControlShape shape;
		for(int i=0; i<form.drawingBodyChart().getShapes().size(); i++)
		{
			shape = (DrawingControlShape)form.drawingBodyChart().getShapes().get(i);
			if(shape.getIndex() == index)
				return shape.getVML();
		}
		
		return "";
	}
	
	private DrawingControlShape getEditedShape(int index)
	{
		for(int i=0; i<form.drawingBodyChart().getShapes().size(); i++)
		{
			DrawingControlShape shape = (DrawingControlShape)form.drawingBodyChart().getShapes().get(i);
			if(shape.getIndex() == index)
				return shape;
		}
		return null;
	}
	
	private void clearContext()
	{
		form.getGlobalContext().COE.PainBodyChart.setAssessmentFinding(null);
		form.getGlobalContext().COE.PainBodyChart.setPainAssessmentVO(null);
	}
	
	private boolean isAssessmentComponent()
	{
		if(form.getGlobalContext().Nursing.getAssessmentComponent() != null && form.getGlobalContext().Nursing.getAssessmentComponent() instanceof AssessmentPain) // Came from the Pain Assessment screen
		{
			AssessmentPain comp = (AssessmentPain)form.getGlobalContext().Nursing.getAssessmentComponent();
			if (comp.getOptionalPainAssessment() != null)
				return true;
		}
		
		return false;
	}
	
	protected void onBtnCloseClick() throws PresentationLogicException 
	{
		engine.open(form.getForms().COE.AssessPain);	
	}

	//Get all the Groups
	private DrawingControlGroupCollection getAllGroups(DrawingControlGroup rootNode)
	{
		DrawingControlGroupCollection groupColl = new DrawingControlGroupCollection();
		if(rootNode.getGroups() != null)
		{
			//Add the parent root
			groupColl.add(rootNode);
			for(int i=0; i<rootNode.getGroups().size(); i++)
			{
				groupColl.add(rootNode.getGroups().get(i));
				getGroups(groupColl, rootNode.getGroups().get(i)); 
			}
		}

		return groupColl;
	}
	
	private void getGroups(DrawingControlGroupCollection groupColl, DrawingControlGroup node)
	{
		if(node.getGroups().size() > 0)
		{
			for(int i=0; i<node.getGroups().size(); i++)
				getGroups(groupColl, node.getGroups().get(i)); 
		}
		else
			groupColl.add(node);
	}
	
	private boolean recordSaved(Integer painAssFindingID)
	{
		for(int i=0; i<form.drawingBodyChart().getShapes().size(); i++)
		{
			if(((DrawingControlShape)form.drawingBodyChart().getShapes().get(i)).getIndex()    == painAssFindingID.intValue() && 
			   ((DrawingControlShape)form.drawingBodyChart().getShapes().get(i)).getTargetID() == savedTargetID)
				return true;
		}
		return false;
	}
	
	// WDEV-1023
	// Drawing Control - Body chart - sitename is null is some cases. 
	// Appears blank on the review records and appears as null on the tooltip.
	private String getAreaOrGroupName(int targetId)
	{
		String areaName = form.drawingBodyChart().getGroupOrAreaName(targetId);
		if(areaName == null || areaName.equals(""))
		{
			DrawingControlGroup areaGroup = form.drawingBodyChart().getAreas();
					
			if(areaGroup != null)
			{
				DrawingControlAreaCollection coll = areaGroup.getAllAreas();
				for (int i = 0; i < coll.size(); i++)
				{
					if(coll.get(i).getID() == targetId)
					{
						areaName = coll.get(i).getName();
						break;
					}
				}
			}
		}
		
		return areaName;
	}

	//Get the already edited or a new one - fixes a bug when clicking Finish before clicking Note which created a blank review
	private PainAssessmentReview getReviewRecord(PainAssessmentFindings findingVo)
	{
 
		if(findingVo.getPainAssessmentReviewIsNotNull())
		{
			for(int i=0; i<findingVo.getPainAssessmentReview().size(); i++)
			{
				if(findingVo.getPainAssessmentReview().get(i).getID_PainAssessmentReviewIsNotNull() && 
				   findingVo.getPainAssessmentReview().get(i).getID_PainAssessmentReview().intValue() == findingVo.getID_PainAssessmentFindings().intValue())
					return findingVo.getPainAssessmentReview().get(i); 
			}
		}
		return new PainAssessmentReview();
	}
	
	//Check if a review was added (e.g. as it might be created after clickining Finished)
	private boolean reviewEdited(PainAssessmentFindings findingVo)
	{
		if(findingVo.getPainAssessmentReviewIsNotNull())
		{
			//Only when having one review - (e.g. when the review was created from the finding)
			if(findingVo.getPainAssessmentReview().size() == 1 && findingVo.getPainAssessmentReview().get(0).getID_PainAssessmentReviewIsNotNull() &&
			   findingVo.getPainAssessmentReview().get(0).getID_PainAssessmentReview().intValue() == findingVo.getID_PainAssessmentFindings().intValue())
					return true; 
		}
		return false;
	}
	
	private String[] label = new String[]{ "<b>" + "Recording Date/Time: " + "</b>", 
										   "<b>" + "Assessment Date/Time: " + "</b>",
										   "<b>" + "HCP:		  " + "</b>",
										   "<b>" + "Finding:	  " + "</b>", 
										   "<b>" + "Site: 		  "	+ "</b>" , 	
										   "<b>" + "Site Details: " + "</b>" ,
										   "<b>" + "Type: 		  "	+ "</b>" ,
										   "<b>" + "Severity: 	  " + "</b>" ,
										   "<b>" + "Onset: 		  " + "</b>" ,
										   "<b>" + "Progression:  " + "</b>" ,  
										   "<b>" + "Depth: 		  " + "</b>" , 
										   "<b>" + PainStatus.CONTINUED + ": "+ "</b>" ,
										   "<b>" + "Discontinued: " + "</b>"};
	private String yes = new String("Yes");
	private String no  = new String("No");
	private int savedTargetID =-1;//ID to flag the saved shapes records  
}
