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

package ims.nursing.forms.skinreview;

import ims.configuration.gen.ConfigFlag;
import ims.core.vo.MemberOfStaffShortVo;
import ims.core.vo.PatientShort;
import ims.domain.exceptions.DomainInterfaceException;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.enumerations.DialogResult;
import ims.framework.enumerations.FormMode;
import ims.framework.enumerations.SortOrder;
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.nursing.forms.skinreview.GenForm.GroupActiveEnumeration;
import ims.nursing.vo.SkinAssessment;
import ims.nursing.vo.SkinAssessmentCollection;
import ims.nursing.vo.SkinAssessmentFindings;
import ims.nursing.vo.SkinAssessmentReview;
import ims.nursing.vo.SkinAssessmentReviewCollection;
import ims.nursing.vo.SkinReviewSearchCriteriaVo;
import ims.nursing.vo.lookups.PainStatus;
import com.ims.query.builder.client.QueryBuilderClient;
import com.ims.query.builder.client.SeedValue;
import com.ims.query.builder.client.exceptions.QueryBuilderClientException;
import ims.core.vo.CareContextShortVo;
import ims.core.vo.CareContextShortVoCollection;

public class Logic extends BaseLogic
{
	private static final int LIST_ALL			= 2;
	private static final int LIST_ACTIVE		= 1;
	
	protected void onFormOpen() throws ims.framework.exceptions.FormOpenException
	{
		initialize();
		//WDEV-19389 
		if (!(form.getGlobalContext().Core.getCurrentCareContextIsNotNull() && form.getGlobalContext().Nursing.getSkinReviewSearchCriteriaIsNotNull() && form.getGlobalContext().Core.getCurrentCareContext().equals(form.getGlobalContext().Nursing.getSkinReviewSearchCriteria().getCareContext())))
			form.getGlobalContext().Nursing.setSkinReviewSearchCriteria(null);
		
		if(form.getGlobalContext().Nursing.getSkinReviewSearchCriteriaIsNotNull())
		{
			try
			{
				setSearchCriteria(form.getGlobalContext().Nursing.getSkinReviewSearchCriteria());
				onComboBoxSiteValueChanged();
			}
			catch (PresentationLogicException e)
			{e.printStackTrace();}

		}
		else
			open();
		//WDEV-19389 - end	
	}
	
	private void initialize()
	{
		
		//WDEV-4496
		
		if(ConfigFlag.UI.CONTEXT_SEARCH_ENABLED.getValue())
		{	
			form.recbrAssessment().setVisible(true);
			form.lbl1().setVisible(true);
					
		}
		else
		{
			form.recbrAssessment().setVisible(false);
			form.lbl1().setVisible(false);
		}
		
		
		CareContextShortVoCollection voCareContextList = domain.listSkinReviewCareContexts(form.getGlobalContext().Core.getPatientShort().getID_Patient());
		if (voCareContextList != null && voCareContextList.size() > 0)
		{
			form.recbrAssessment().newRow(null,null); //empty row
			
			for (int i=0;i<voCareContextList.size();i++)
			{
				String hcpName ="";
				if(voCareContextList.get(i).getEpisodeOfCare().getHcpDisplayString()!= null)
					hcpName = voCareContextList.get(i).getEpisodeOfCare().getHcpDisplayString();
				
				
				form.recbrAssessment().newRow(voCareContextList.get(i),voCareContextList.get(i).getStartDateTime().getDate().toString() + " " + voCareContextList.get(i).getStartDateTime().getTime().toString()+" "+hcpName);
				
			
			}
		}
		
		clearGlobalContext();
		prepopulateWoundGrid();
		addDetailsEmptyRows();
		setControlsDefaultState();
		clearContext();

		if(ims.configuration.gen.ConfigFlag.UI.DISPLAY_CAREPLAN_NOTE_LINK.getValue())
		{
			form.linkClinicalNote().setVisible(true);
		}
		else
		{
			form.linkClinicalNote().setVisible(false);
		}		
	}	
	
	private SkinReviewSearchCriteriaVo getSearchCriteria()
	{
		SkinReviewSearchCriteriaVo searchCriteria = new SkinReviewSearchCriteriaVo();
		
		searchCriteria.setViewType(getViewType());
		searchCriteria.setSelectedCareContext(form.recbrAssessment().getValue());
		searchCriteria.setAssessment(form.comboBoxAssessment().getValue());
		searchCriteria.setFindings(form.comboBoxSite().getValue());
		searchCriteria.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
		
		return searchCriteria;
	}
	
	
	private void setSearchCriteria(SkinReviewSearchCriteriaVo skinReviewSearchCriteriaVo) throws PresentationLogicException
	{
		form.recbrAssessment().setValue(skinReviewSearchCriteriaVo.getSelectedCareContext());
		onRecbrAssessmentValueChanged();
		form.comboBoxAssessment().setValue(skinReviewSearchCriteriaVo.getAssessment());
		onComboBoxAssessmentValueChanged();
		setViewType(skinReviewSearchCriteriaVo.getViewType());
		onRadioButtonGroupActiveValueChanged();
		form.comboBoxSite().setValue(skinReviewSearchCriteriaVo.getFindings());
	}

	private void setViewType(Integer searchType)
	{
		if (searchType == null)
			return;
		
		switch (searchType)
		{
		case LIST_ALL:
			form.GroupActive().setValue(GroupActiveEnumeration.rdoAll);
			break;
		case LIST_ACTIVE:
			form.GroupActive().setValue(GroupActiveEnumeration.rdoActive);
			break;
		}				
	}

	private Integer getViewType()
	{
		GroupActiveEnumeration searchType = form.GroupActive().getValue();
		if (GroupActiveEnumeration.rdoAll.equals(searchType))
		{
			return LIST_ALL;
		}
		if (GroupActiveEnumeration.rdoActive.equals(searchType))
		{
			return LIST_ACTIVE;
		}
		
		return null;
	}
	
	protected void onFormDialogClosed(ims.framework.FormName formName, ims.framework.enumerations.DialogResult result) throws ims.framework.exceptions.PresentationLogicException
	{
		if(formName.equals(form.getForms().Nursing.SkinReviewDialog) && result.equals(DialogResult.OK))
		{
			//Get the value
			SkinAssessmentFindings findingVO = form.getGlobalContext().COE.SkinBodyChart.getFindingsVO();
			if(findingVO.getSkinAssessmentReview() == null)
				findingVO.setSkinAssessmentReview(new SkinAssessmentReviewCollection());
			findingVO.getSkinAssessmentReview().add(form.getGlobalContext().COE.SkinBodyChart.getReviewVO());
			SkinAssessment assessmentVO = form.comboBoxAssessment().getValue();
			addFindingToAssessment(assessmentVO, findingVO);
			
			if (assessmentVO.getClinicalContact() == null)
				assessmentVO.setClinicalContact(form.getGlobalContext().Core.getCurrentClinicalContact());
			
			if (assessmentVO.getCareContext() == null)
				assessmentVO.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
			
			//Save
			try{
				String [] error = assessmentVO.validate();
				if(error != null)
				{
					engine.showErrors(error);
					return;
				}
				
	//			domain.saveSkinAssessment(assessmentVO, form.getGlobalContext().Core.getClinicalEpisode());
				domain.saveSkinAssessment(assessmentVO);
			}
			 catch (StaleObjectException e) 
			{
			 	displayMessage(ims.configuration.gen.ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
			}
			
			 //If the Assessment was discontinued - activate the All radio button  
			 if(findingVO.getIsDiscontinuedAssess() != null && 
			 	findingVO.getIsDiscontinuedAssess().booleanValue())
			 	form.GroupActive().setValue(GenForm.GroupActiveEnumeration.rdoAll);
			 
			//Restore the old Assessment and Display
			 open();
			 if(form.comboBoxSite().getValues() != null)
			 {
			 	form.comboBoxSite().setValue(form.getGlobalContext().COE.SkinBodyChart.getFindingsVO());
			 	refreshFindings();
			 }
		}
		else if(result.equals(DialogResult.OK) && formName.equals(form.getForms().Core.Reports))
		{
			printReport();
		}

	}
	
	private void addFindingToAssessment(SkinAssessment assessmentVO, SkinAssessmentFindings findingVO)
	{
		for(int i=0; i<assessmentVO.getSkinAssessmentFindings().size(); i++)
		{
			if(assessmentVO.getSkinAssessmentFindings().get(i).equals(findingVO))
			{
				assessmentVO.getSkinAssessmentFindings().remove(i);
				assessmentVO.getSkinAssessmentFindings().add(findingVO);
				break;
			}
		}
	}
		
	protected void onLinkViewBodyChartClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if(form.comboBoxAssessment().getValue() == null)
		{
			displayMessage("Please select an Assessment");
			return;
		}
		form.getGlobalContext().COE.SkinBodyChart.setReadOnlyAssessment(form.comboBoxAssessment().getValue());
		engine.open(form.getForms().Nursing.SkinBodyChart);
	}
	protected void onComboBoxSiteValueChanged() throws ims.framework.exceptions.PresentationLogicException
	{
		form.getLocalContext().setCurrentVOIndex(new Integer(0));
		form.grdSkinDetails().getRows().clear();
		addDetailsEmptyRows();
		if(form.comboBoxSite().getValue() != null)
		{
			form.btnPrint().setEnabled(true);
			//Build the pain Review List
			SkinAssessmentReviewCollection reviewVOCollection = new SkinAssessmentReviewCollection();
			//	Add the header record to the first position
			//reviewVOCollection.add(getReviewVOfromFindingVO());
			if(form.comboBoxSite().getValue().getSkinAssessmentReview() != null)
			{
				for(int i=0; i<form.comboBoxSite().getValue().getSkinAssessmentReview().size(); i++)
				 reviewVOCollection.add(form.comboBoxSite().getValue().getSkinAssessmentReview().get(i));					
			}
			reviewVOCollection.sort(SortOrder.ASCENDING);
				
			//Save the list in the context
			form.getLocalContext().setReviewCollection(reviewVOCollection);
			
			//------------------------------------------------------
			//set the current index to be the one that's displaying the last records first
			int recIndex = 0;
			if(reviewVOCollection.size()%maxcolumns == 0)
				recIndex = (reviewVOCollection.size()/maxcolumns)*maxcolumns - maxcolumns;
			else
				recIndex = (reviewVOCollection.size()/maxcolumns)*maxcolumns;
			
			recIndex = recIndex<0?0:recIndex;
			form.getLocalContext().setCurrentVOIndex(new Integer(recIndex));
			//--------------------------------------------------------
			//Set the findingVO to be used in Review Dialog
			form.getGlobalContext().COE.SkinBodyChart.setFindingsVO(form.comboBoxSite().getValue());
			
			populateReviewDetails();
			
			form.btnNext().setEnabled(false);
			if(reviewVOCollection.size() > maxcolumns)
				form.btnPrevious().setEnabled(true);
			else
				form.btnPrevious().setEnabled(false);
			
			//Check if it's the first record
			if(form.comboBoxAssessment().getValues().get(0).equals(form.comboBoxAssessment().getValue())) 
			{
				//Check if discontinued
				if(form.comboBoxSite().getValue().getIsDiscontinuedAssess() != null && 
				   form.comboBoxSite().getValue().getIsDiscontinuedAssess().booleanValue())
					form.bNew().setEnabled(false);
				else
					form.bNew().setEnabled(true);
			}
			else
				form.bNew().setEnabled(false);
		}
		else
		{
			form.bNew().setEnabled(false);
			form.btnNext().setEnabled(false);
			form.btnPrevious().setEnabled(false);
		}
		
    	form.getGlobalContext().Nursing.setSkinReviewSearchCriteria(getSearchCriteria());//WDEV-19389 
	}

	private void populateReviewDetails()
	{
		SkinAssessmentFindings findingVO = form.comboBoxSite().getValue();
		SkinAssessmentReviewCollection  skinReviewCollection = form.getLocalContext().getReviewCollection();
		int currentIndex = form.getLocalContext().getCurrentVOIndex() != null?form.getLocalContext().getCurrentVOIndex().intValue():0;
		int i = currentIndex;
		StringBuffer size;
		String date, time;
		
		form.grdSkinDetails().getRows().clear();
		addDetailsEmptyRows();
		
		if(currentIndex < skinReviewCollection.size())
		{
			SkinAssessmentReview reviewVO = null;
			for(i=currentIndex; i<skinReviewCollection.size(); i++)
			{
				reviewVO = skinReviewCollection.get(i);
				if(i<(currentIndex + maxcolumns))
				{
					switch(i-currentIndex)
					{
						case 0:
							//Date/Time
							if(reviewVO.getDateTimeReview() != null)
							{
								date = reviewVO.getDateTimeReview().getDate() == null?"":reviewVO.getDateTimeReview().getDate().toString(DateFormat.STANDARD).toString();
								time = reviewVO.getDateTimeReview().getTime() == null?"":reviewVO.getDateTimeReview().getTime().toString(TimeFormat.DEFAULT);
								form.grdSkinDetails().getRows().get(0).setColumn1(date + "; " + time + "hrs");
							}
							//HCP
							if(reviewVO.getHCPReview() != null)
								form.grdSkinDetails().getRows().get(1).setColumn1(reviewVO.getHCPReview().toString());
							//Site
							form.grdSkinDetails().getRows().get(2).setColumn1(findingVO.getSiteName());
							//Site Details
							form.grdSkinDetails().getRows().get(3).setColumn1(reviewVO.getSiteDetails());
							//How long it's present
							form.grdSkinDetails().getRows().get(4).setColumn1(findingVO.getHowLongIsItPresent());
							//Pressure Sore Grade
							if(reviewVO.getPressureSoreGrade() != null)
								form.grdSkinDetails().getRows().get(5).setColumn1(reviewVO.getPressureSoreGrade().getText());
							if(reviewVO.getLength() == null && reviewVO.getWidth() == null && reviewVO.getDepth() == null)
								form.grdSkinDetails().getRows().get(6).setColumn1("");
							else
							{
								size = new StringBuffer();
								if(reviewVO.getLength() != null)
									size.append(reviewVO.getLength());
								size.append(", ");
								if(reviewVO.getWidth() != null)
									size.append(reviewVO.getWidth());
								size.append(", ");
								if(reviewVO.getDepth() != null)
									size.append(reviewVO.getDepth());
								//Size: Length, Width, Depth
								form.grdSkinDetails().getRows().get(6).setColumn1(size.toString());
							}
							//Wound Bed
							if(reviewVO.getWoundBed() != null)
								form.grdSkinDetails().getRows().get(7).setColumn1(reviewVO.getWoundBed().getText());
							//Surrounding Skin
							if(reviewVO.getSurroundingSkin() != null)
								form.grdSkinDetails().getRows().get(8).setColumn1(reviewVO.getSurroundingSkin().getText());
							//Exudate Amount
							if(reviewVO.getExudateAmount() != null)
								form.grdSkinDetails().getRows().get(9).setColumn1(reviewVO.getExudateAmount().getText());
							//Exudate Type
							if(reviewVO.getExudateType() != null)
								form.grdSkinDetails().getRows().get(10).setColumn1(reviewVO.getExudateType().getText());
							//Odour
							if(reviewVO.getOdour() != null)
								form.grdSkinDetails().getRows().get(11).setColumn1(reviewVO.getOdour().getText());
							//Pain
							if(reviewVO.getPain() != null)
								form.grdSkinDetails().getRows().get(12).setColumn1(reviewVO.getPain().getText());
							//Infection Suspected
							if(reviewVO.getInfectionSuspected() != null)
								form.grdSkinDetails().getRows().get(13).setColumn1(reviewVO.getInfectionSuspected().booleanValue()?yes:no);
							//Swab taken
							if(reviewVO.getSwabTaken() != null)
								form.grdSkinDetails().getRows().get(14).setColumn1(reviewVO.getSwabTaken().booleanValue()?yes:no);
							//Wound Traced
							if(reviewVO.getWoundTraced() != null)
								form.grdSkinDetails().getRows().get(15).setColumn1(reviewVO.getWoundTraced().booleanValue()?yes:no);
							//Cleansed With
							if(reviewVO.getCleansedWith() != null)
								form.grdSkinDetails().getRows().get(16).setColumn1(reviewVO.getCleansedWith().getText());
							//Primary Dressing
							if(reviewVO.getPrimaryDressing() != null)
							{
								form.grdSkinDetails().getRows().get(17).setColumn1(reviewVO.getPrimaryDressing().getText());
								form.grdSkinDetails().getRows().get(17).setTooltipForColumn1(reviewVO.getPrimaryDressing().getText());
							}
							//Secondary Dressing
							if(reviewVO.getSecondaryDressing() != null)
							{
								form.grdSkinDetails().getRows().get(18).setColumn1(reviewVO.getSecondaryDressing().getText());
								form.grdSkinDetails().getRows().get(18).setTooltipForColumn1(reviewVO.getSecondaryDressing().getText());
							}
							//Frequency of Change
							if(reviewVO.getFrequencyOfChange() != null)
								form.grdSkinDetails().getRows().get(19).setColumn1(reviewVO.getFrequencyOfChange().getText());
							//Comment
							form.grdSkinDetails().getRows().get(20).setColumn1(reviewVO.getComment());
							form.grdSkinDetails().getRows().get(20).setTooltipForColumn1(reviewVO.getComment());
							break;
						case 1:
							//Date/Time
							if(reviewVO.getDateTimeReview() != null)
							{
								date = reviewVO.getDateTimeReview().getDate() == null?"":reviewVO.getDateTimeReview().getDate().toString(DateFormat.STANDARD).toString();
								time = reviewVO.getDateTimeReview().getTime() == null?"":reviewVO.getDateTimeReview().getTime().toString(TimeFormat.DEFAULT);
								form.grdSkinDetails().getRows().get(0).setColumn2(date + "; " + time + "hrs");
							}
							//HCP
							if(reviewVO.getHCPReview() != null)
								form.grdSkinDetails().getRows().get(1).setColumn2(reviewVO.getHCPReview().toString());
							//Site
							form.grdSkinDetails().getRows().get(2).setColumn2(findingVO.getSiteName());
							//Site Details
							form.grdSkinDetails().getRows().get(3).setColumn2(reviewVO.getSiteDetails());
							//How long it's present
							form.grdSkinDetails().getRows().get(4).setColumn2(findingVO.getHowLongIsItPresent());
							//Pressure Sore Grade
							if(reviewVO.getPressureSoreGrade() != null)
								form.grdSkinDetails().getRows().get(5).setColumn2(reviewVO.getPressureSoreGrade().getText());
							if(reviewVO.getLength() == null && reviewVO.getWidth() == null && reviewVO.getDepth() == null)
								form.grdSkinDetails().getRows().get(6).setColumn2("");
							else
							{
								size = new StringBuffer();
								if(reviewVO.getLength() != null)
									size.append(reviewVO.getLength());
								size.append(", ");
								if(reviewVO.getWidth() != null)
									size.append(reviewVO.getWidth());
								size.append(", ");
								if(reviewVO.getDepth() != null)
									size.append(reviewVO.getDepth());
								//Size: Length, Width, Depth
								form.grdSkinDetails().getRows().get(6).setColumn2(size.toString());
							}
							//Wound Bed
							if(reviewVO.getWoundBed() != null)
								form.grdSkinDetails().getRows().get(7).setColumn2(reviewVO.getWoundBed().getText());
							//Surrounding Skin
							if(reviewVO.getSurroundingSkin() != null)
								form.grdSkinDetails().getRows().get(8).setColumn2(reviewVO.getSurroundingSkin().getText());
							//Exudate Amount
							if(reviewVO.getExudateAmount() != null)
								form.grdSkinDetails().getRows().get(9).setColumn2(reviewVO.getExudateAmount().getText());
							//Exudate Type
							if(reviewVO.getExudateType() != null)
								form.grdSkinDetails().getRows().get(10).setColumn2(reviewVO.getExudateType().getText());
							//Odour
							if(reviewVO.getOdour() != null)
								form.grdSkinDetails().getRows().get(11).setColumn2(reviewVO.getOdour().getText());
							//Pain
							if(reviewVO.getPain() != null)
								form.grdSkinDetails().getRows().get(12).setColumn2(reviewVO.getPain().getText());
							//Infection Suspected
							if(reviewVO.getInfectionSuspected() != null)
								form.grdSkinDetails().getRows().get(13).setColumn2(reviewVO.getInfectionSuspected().booleanValue()?yes:no);
							//Swab taken
							if(reviewVO.getSwabTaken() != null)
								form.grdSkinDetails().getRows().get(14).setColumn2(reviewVO.getSwabTaken().booleanValue()?yes:no);
							//Wound Traced
							if(reviewVO.getWoundTraced() != null)
								form.grdSkinDetails().getRows().get(15).setColumn2(reviewVO.getWoundTraced().booleanValue()?yes:no);
							//Cleansed With
							if(reviewVO.getCleansedWith() != null)
								form.grdSkinDetails().getRows().get(16).setColumn2(reviewVO.getCleansedWith().getText());
							//Primary Dressing
							if(reviewVO.getPrimaryDressing() != null)
							{
								form.grdSkinDetails().getRows().get(17).setColumn2(reviewVO.getPrimaryDressing().getText());
								form.grdSkinDetails().getRows().get(17).setTooltipForColumn2(reviewVO.getPrimaryDressing().getText());
							}
							//Secondary Dressing
							if(reviewVO.getSecondaryDressing() != null)
							{
								form.grdSkinDetails().getRows().get(18).setColumn2(reviewVO.getSecondaryDressing().getText());
								form.grdSkinDetails().getRows().get(18).setTooltipForColumn2(reviewVO.getSecondaryDressing().getText());
							}
							//Frequency of Change
							if(reviewVO.getFrequencyOfChange() != null)
								form.grdSkinDetails().getRows().get(19).setColumn2(reviewVO.getFrequencyOfChange().getText());
							//Comment
							form.grdSkinDetails().getRows().get(20).setColumn2(reviewVO.getComment());
							form.grdSkinDetails().getRows().get(20).setTooltipForColumn2(reviewVO.getComment());
							break;
						case 2:
							//Date/Time
							if(reviewVO.getDateTimeReview() != null)
							{
								date = reviewVO.getDateTimeReview().getDate() == null?"":reviewVO.getDateTimeReview().getDate().toString(DateFormat.STANDARD).toString();
								time = reviewVO.getDateTimeReview().getTime() == null?"":reviewVO.getDateTimeReview().getTime().toString(TimeFormat.DEFAULT);
								form.grdSkinDetails().getRows().get(0).setColumn3(date + "; " + time + "hrs");
							}
							//HCP
							if(reviewVO.getHCPReview() != null)
								form.grdSkinDetails().getRows().get(1).setColumn3(reviewVO.getHCPReview().toString());
							//Site
							form.grdSkinDetails().getRows().get(2).setColumn3(findingVO.getSiteName());
							//Site Details
							form.grdSkinDetails().getRows().get(3).setColumn3(reviewVO.getSiteDetails());
							//How long it's present
							form.grdSkinDetails().getRows().get(4).setColumn3(findingVO.getHowLongIsItPresent());
							//Pressure Sore Grade
							if(reviewVO.getPressureSoreGrade() != null)
								form.grdSkinDetails().getRows().get(5).setColumn3(reviewVO.getPressureSoreGrade().getText());
							if(reviewVO.getLength() == null && reviewVO.getWidth() == null && reviewVO.getDepth() == null)
								form.grdSkinDetails().getRows().get(6).setColumn3("");
							else
							{
								size = new StringBuffer();
								if(reviewVO.getLength() != null)
									size.append(reviewVO.getLength());
								size.append(", ");
								if(reviewVO.getWidth() != null)
									size.append(reviewVO.getWidth());
								size.append(", ");
								if(reviewVO.getDepth() != null)
									size.append(reviewVO.getDepth());
								//Size: Length, Width, Depth
								form.grdSkinDetails().getRows().get(6).setColumn3(size.toString());
							}
							//Wound Bed
							if(reviewVO.getWoundBed() != null)
								form.grdSkinDetails().getRows().get(7).setColumn3(reviewVO.getWoundBed().getText());
							//Surrounding Skin
							if(reviewVO.getSurroundingSkin() != null)
								form.grdSkinDetails().getRows().get(8).setColumn3(reviewVO.getSurroundingSkin().getText());
							//Exudate Amount
							if(reviewVO.getExudateAmount() != null)
								form.grdSkinDetails().getRows().get(9).setColumn3(reviewVO.getExudateAmount().getText());
							//Exudate Type
							if(reviewVO.getExudateType() != null)
								form.grdSkinDetails().getRows().get(10).setColumn3(reviewVO.getExudateType().getText());
							//Odour
							if(reviewVO.getOdour() != null)
								form.grdSkinDetails().getRows().get(11).setColumn3(reviewVO.getOdour().getText());
							//Pain
							if(reviewVO.getPain() != null)
								form.grdSkinDetails().getRows().get(12).setColumn3(reviewVO.getPain().getText());
							//Infection Suspected
							if(reviewVO.getInfectionSuspected() != null)
								form.grdSkinDetails().getRows().get(13).setColumn3(reviewVO.getInfectionSuspected().booleanValue()?yes:no);
							//Swab taken
							if(reviewVO.getSwabTaken() != null)
								form.grdSkinDetails().getRows().get(14).setColumn3(reviewVO.getSwabTaken().booleanValue()?yes:no);
							//Wound Traced
							if(reviewVO.getWoundTraced() != null)
								form.grdSkinDetails().getRows().get(15).setColumn3(reviewVO.getWoundTraced().booleanValue()?yes:no);
							//Cleansed With
							if(reviewVO.getCleansedWith() != null)
								form.grdSkinDetails().getRows().get(16).setColumn3(reviewVO.getCleansedWith().getText());
							//Primary Dressing
							if(reviewVO.getPrimaryDressing() != null)
							{
								form.grdSkinDetails().getRows().get(17).setColumn3(reviewVO.getPrimaryDressing().getText());
								form.grdSkinDetails().getRows().get(17).setTooltipForColumn3(reviewVO.getPrimaryDressing().getText());
							}
							//Secondary Dressing
							if(reviewVO.getSecondaryDressing() != null)
							{
								form.grdSkinDetails().getRows().get(18).setColumn3(reviewVO.getSecondaryDressing().getText());
								form.grdSkinDetails().getRows().get(18).setTooltipForColumn3(reviewVO.getSecondaryDressing().getText());
							}
							//Frequency of Change
							if(reviewVO.getFrequencyOfChange() != null)
								form.grdSkinDetails().getRows().get(19).setColumn3(reviewVO.getFrequencyOfChange().getText());
							//Comment
							form.grdSkinDetails().getRows().get(20).setColumn3(reviewVO.getComment());
							form.grdSkinDetails().getRows().get(20).setTooltipForColumn3(reviewVO.getComment());
							break;
						case 3:
							//Date/Time
							if(reviewVO.getDateTimeReview() != null)
							{
								date = reviewVO.getDateTimeReview().getDate() == null?"":reviewVO.getDateTimeReview().getDate().toString(DateFormat.STANDARD).toString();
								time = reviewVO.getDateTimeReview().getTime() == null?"":reviewVO.getDateTimeReview().getTime().toString(TimeFormat.DEFAULT);
								form.grdSkinDetails().getRows().get(0).setColumn4(date + "; " + time + "hrs");
							}
							//HCP
							if(reviewVO.getHCPReview() != null)
								form.grdSkinDetails().getRows().get(1).setColumn4(reviewVO.getHCPReview().toString());
							//Site
							form.grdSkinDetails().getRows().get(2).setColumn4(findingVO.getSiteName());
							//Site Details
							form.grdSkinDetails().getRows().get(3).setColumn4(reviewVO.getSiteDetails());
							//How long it's present
							form.grdSkinDetails().getRows().get(4).setColumn4(findingVO.getHowLongIsItPresent());
							//Pressure Sore Grade
							if(reviewVO.getPressureSoreGrade() != null)
								form.grdSkinDetails().getRows().get(5).setColumn4(reviewVO.getPressureSoreGrade().getText());
							if(reviewVO.getLength() == null && reviewVO.getWidth() == null && reviewVO.getDepth() == null)
								form.grdSkinDetails().getRows().get(6).setColumn4("");
							else
							{
								size = new StringBuffer();
								if(reviewVO.getLength() != null)
									size.append(reviewVO.getLength());
								size.append(", ");
								if(reviewVO.getWidth() != null)
									size.append(reviewVO.getWidth());
								size.append(", ");
								if(reviewVO.getDepth() != null)
									size.append(reviewVO.getDepth());
								//Size: Length, Width, Depth
								form.grdSkinDetails().getRows().get(6).setColumn4(size.toString());
							}
							//Wound Bed
							if(reviewVO.getWoundBed() != null)
								form.grdSkinDetails().getRows().get(7).setColumn4(reviewVO.getWoundBed().getText());
							//Surrounding Skin
							if(reviewVO.getSurroundingSkin() != null)
								form.grdSkinDetails().getRows().get(8).setColumn4(reviewVO.getSurroundingSkin().getText());
							//Exudate Amount
							if(reviewVO.getExudateAmount() != null)
								form.grdSkinDetails().getRows().get(9).setColumn4(reviewVO.getExudateAmount().getText());
							//Exudate Type
							if(reviewVO.getExudateType() != null)
								form.grdSkinDetails().getRows().get(10).setColumn4(reviewVO.getExudateType().getText());
							//Odour
							if(reviewVO.getOdour() != null)
								form.grdSkinDetails().getRows().get(11).setColumn4(reviewVO.getOdour().getText());
							//Pain
							if(reviewVO.getPain() != null)
								form.grdSkinDetails().getRows().get(12).setColumn4(reviewVO.getPain().getText());
							//Infection Suspected
							if(reviewVO.getInfectionSuspected() != null)
								form.grdSkinDetails().getRows().get(13).setColumn4(reviewVO.getInfectionSuspected().booleanValue()?yes:no);
							//Swab taken
							if(reviewVO.getSwabTaken() != null)
								form.grdSkinDetails().getRows().get(14).setColumn4(reviewVO.getSwabTaken().booleanValue()?yes:no);
							//Wound Traced
							if(reviewVO.getWoundTraced() != null)
								form.grdSkinDetails().getRows().get(15).setColumn4(reviewVO.getWoundTraced().booleanValue()?yes:no);
							//Cleansed With
							if(reviewVO.getCleansedWith() != null)
								form.grdSkinDetails().getRows().get(16).setColumn4(reviewVO.getCleansedWith().getText());
							//Primary Dressing
							if(reviewVO.getPrimaryDressing() != null)
							{
								form.grdSkinDetails().getRows().get(17).setColumn4(reviewVO.getPrimaryDressing().getText());
								form.grdSkinDetails().getRows().get(17).setTooltipForColumn4(reviewVO.getPrimaryDressing().getText());
							}
							//Secondary Dressing
							if(reviewVO.getSecondaryDressing() != null)
							{
								form.grdSkinDetails().getRows().get(18).setColumn4(reviewVO.getSecondaryDressing().getText());
								form.grdSkinDetails().getRows().get(18).setTooltipForColumn4(reviewVO.getSecondaryDressing().getText());
							}
							//Frequency of Change
							if(reviewVO.getFrequencyOfChange() != null)
								form.grdSkinDetails().getRows().get(19).setColumn4(reviewVO.getFrequencyOfChange().getText());
							//Comment
							form.grdSkinDetails().getRows().get(20).setColumn4(reviewVO.getComment());
							form.grdSkinDetails().getRows().get(20).setTooltipForColumn4(reviewVO.getComment());
							break;
					}
				}
				else
					break;
			}
		}

	}
	
	protected void onComboBoxAssessmentValueChanged() throws ims.framework.exceptions.PresentationLogicException
	{
		form.comboBoxSite().clear();
		form.bNew().setEnabled(false);
		form.btnPrint().setEnabled(false);
		if(form.comboBoxAssessment().getValue() != null)
		{
			form.getLocalContext().setSkinAssessment(form.comboBoxAssessment().getValue());
			SkinAssessmentFindings findingVO;
			if( form.comboBoxAssessment().getValue().getIsSkinIntact() != null && 
				form.comboBoxAssessment().getValue().getIsSkinIntact().booleanValue() && 
				form.comboBoxAssessment().getValue().getSkinAssessmentFindings().size() == 0)
			{
				form.comboBoxSite().newRow(null, "'Skin Intact' checked. No findings entered.");
			}
			
			for(int i=0; i<form.comboBoxAssessment().getValue().getSkinAssessmentFindings().size(); i++)
			{
				findingVO = form.comboBoxAssessment().getValue().getSkinAssessmentFindings().get(i);
				//Skip the discontinued one
				if(form.GroupActive().getValue().equals(GenForm.GroupActiveEnumeration.rdoActive) && 
				   findingVO.getIsDiscontinuedAssess() != null && 
				   findingVO.getIsDiscontinuedAssess().booleanValue())
				   continue;
				form.comboBoxSite().newRow(findingVO, findingVO.getWoundType().getText() + " - " + findingVO.getSiteName() + 
													  (findingVO.getIsContinuedAssessment() != null && findingVO.getIsContinuedAssessment().booleanValue()?"  (" + PainStatus.CONTINUED.toString()   +")":"")+
													  (findingVO.getIsDiscontinuedAssess()  != null && findingVO.getIsDiscontinuedAssess().booleanValue()? "  (" + PainStatus.DISCONTINUED.toString()+")":""));
			}
			form.getGlobalContext().COE.SkinBodyChart.setSelectedSkinAssessment(form.comboBoxAssessment().getValue());
			form.getGlobalContext().Nursing.setSkinReviewSearchCriteria(getSearchCriteria());//WDEV-19389 
			onComboBoxSiteValueChanged();
		}
		else
		{
			form.getLocalContext().setSkinAssessment(null);
			form.getGlobalContext().Nursing.setSkinReviewSearchCriteria(null);//WDEV-19389 
		}
		
		form.bNew().setEnabled(false);
		form.btnNext().setEnabled(false);
		form.btnPrevious().setEnabled(false);
	}
	protected void onLinkCarePlanDetailsClick() throws ims.framework.exceptions.PresentationLogicException
	{
		form.getGlobalContext().COE.ClinicalNotes.setCallingForm(form.getForms().Nursing.SkinReview);
		engine.open(form.getForms().Nursing.ClinicalNotes);
	}
	protected void onBNewClick() throws ims.framework.exceptions.PresentationLogicException
	{
		form.getGlobalContext().COE.SkinBodyChart.setReviewDialog(new Boolean(true));
		SkinAssessmentReview skinAssReviewVO = new SkinAssessmentReview();
		
		MemberOfStaffShortVo memStaffShortVo = (MemberOfStaffShortVo)domain.getMosUser();
		if(memStaffShortVo != null && memStaffShortVo.getHcp() != null)
			skinAssReviewVO.setHCPReview(memStaffShortVo.getHcp());

		skinAssReviewVO.setDateTimeReview(new DateTime(new Date(), new Time()));
		form.getGlobalContext().COE.SkinBodyChart.setReviewVO(skinAssReviewVO);
		engine.open(form.getForms().Nursing.SkinReviewDialog);
	}
	private void setControlsDefaultState()
	{
		form.GroupActive().setValue(GenForm.GroupActiveEnumeration.rdoActive);
	}
	
	private boolean open()
	{ 
		form.setMode(FormMode.VIEW);
		form.recbrAssessment().setValue(form.getGlobalContext().Core.getCurrentCareContext());
		populateInstance(form.getGlobalContext().Core.getCurrentCareContext());
				
		return true;
	}
	
	private void populateInstance(CareContextShortVo voCareContext)
	{		
		clearScreen();
		if (voCareContext != null)
		{	
			SkinAssessmentCollection skinAssVOCollection = domain.listSkinAssessment(voCareContext);
			if (skinAssVOCollection != null)
			{
				skinAssVOCollection = skinAssVOCollection.sort(SortOrder.DESCENDING);
				
				form.bNew().setEnabled(false);
				form.btnNext().setEnabled(false);
				form.btnPrevious().setEnabled(false);
				clearCombos();
				
				//String dateTime = "";
				form.comboBoxAssessment().clear();
				for(int i=0; i<skinAssVOCollection.size(); i++)
				{
					if(skinAssVOCollection.get(i).getDateTimeInitiated() != null && 
					   skinAssVOCollection.get(i).getDateTimeInitiated().getDate() != null && 
					   skinAssVOCollection.get(i).getDateTimeInitiated().getTime() != null)
						form.comboBoxAssessment().newRow(skinAssVOCollection.get(i), skinAssVOCollection.get(i).getDateTimeInitiated().getDate().toString(DateFormat.STANDARD) + " / " + skinAssVOCollection.get(i).getDateTimeInitiated().getTime().toString(TimeFormat.DEFAULT));			
				}
				
				if(form.comboBoxAssessment().getValues() != null && form.comboBoxAssessment().getValues().size() > 0)
				{
					if(form.getGlobalContext().COE.SkinBodyChart.getSelectedSkinAssessment() != null)
						form.comboBoxAssessment().setValue(form.getGlobalContext().COE.SkinBodyChart.getSelectedSkinAssessment());
					else
						form.comboBoxAssessment().setValue((SkinAssessment)form.comboBoxAssessment().getValues().get(0));
					refreshAssessment();
				}
			}
		}
		else
			form.getLocalContext().setSkinAssessment(null);
	}
	private void clearScreen()
	{		
		form.comboBoxAssessment().clear();
		form.comboBoxSite().clear();
		
		for (int i = 0; i < form.grdSkinDetails().getRows().size(); i++)
		{
			form.grdSkinDetails().getRows().get(i).setColumn1(null);
			form.grdSkinDetails().getRows().get(i).setColumn2(null);
			form.grdSkinDetails().getRows().get(i).setColumn3(null);
			form.grdSkinDetails().getRows().get(i).setColumn4(null);
		}
	}
	
	private void refreshFindings()
	{
		try {onComboBoxSiteValueChanged();}
		catch (PresentationLogicException e) 
		{displayMessage(e.getMessage());}
	}
	
	private void refreshAssessment()
	{
		try {
			onComboBoxAssessmentValueChanged();
		} catch (PresentationLogicException e) {
			displayMessage(e.getMessage());
		}
	}
	
	private void prepopulateWoundGrid()
	{
		GenForm.grdWoundDetailsRow row;
		for(int i=0; i<wounds.length; i++)
		{
			row = form.grdWoundDetails().getRows().newRow();
			row.setColWound(wounds[i]);
		}
	}
	
	private void addDetailsEmptyRows()
	{
		for(int i=0; i<wounds.length; i++)
			form.grdSkinDetails().getRows().newRow();
	}
	
	private void displayMessage(String error)
	{
		engine.showMessage(error);
	}
	
	protected void onRadioButtonGroupActiveValueChanged() throws PresentationLogicException 
	{
		refreshAssessment();
	}

	protected void onBtnNextClick() throws PresentationLogicException 
	{
		if(form.getLocalContext().getCurrentVOIndex() != null)
		{
			int nextIndex = form.getLocalContext().getCurrentVOIndex().intValue() + maxcolumns;
			if(nextIndex >= form.getLocalContext().getReviewCollection().size())
			{
				nextIndex = form.getLocalContext().getReviewCollection().size();
				form.btnNext().setEnabled(false);
			}
	
			if((nextIndex + maxcolumns) >= form.getLocalContext().getReviewCollection().size())
				form.btnNext().setEnabled(false);
			
			form.getLocalContext().setCurrentVOIndex(new Integer((nextIndex)));
			
			if(form.getLocalContext().getCurrentVOIndex().intValue() >= maxcolumns)
				form.btnPrevious().setEnabled(true);
			else
				form.btnPrevious().setEnabled(false);
			
			populateReviewDetails();
		}	
	}
	
	protected void onBtnPreviousClick() throws PresentationLogicException 
	{
		if(form.getLocalContext().getCurrentVOIndex() != null)
		{
			int prevIndex = form.getLocalContext().getCurrentVOIndex().intValue() - maxcolumns;
			if(prevIndex <= 0)
			{
				prevIndex = 0;
				form.btnPrevious().setEnabled(false);
			}
			form.getLocalContext().setCurrentVOIndex(new Integer(prevIndex));
			
			if(form.getLocalContext().getReviewCollection().size() > maxcolumns && 
			   form.getLocalContext().getCurrentVOIndex().intValue() <= (form.getLocalContext().getReviewCollection().size() - maxcolumns))
				form.btnNext().setEnabled(true);
			else
				form.btnNext().setEnabled(false);
			
			populateReviewDetails();
		}	
	}
	
	private void clearCombos()
	{
		form.comboBoxAssessment().clear();
		form.comboBoxSite().clear();
	}
	
	private void clearContext()
	{
		form.getGlobalContext().COE.SkinBodyChart.setSelectedSkinAssessment(null);
		form.getLocalContext().setSkinAssessment(null);
	}
	private void clearGlobalContext()
	{
		form.getGlobalContext().COE.ClinicalNotes.setCallingForm(null);
		form.getGlobalContext().COE.SkinBodyChart.setFindingsVO(null);
		form.getGlobalContext().COE.SkinBodyChart.setReviewVO(null);
	}
	
	protected void onBtnPrintClick() throws PresentationLogicException
	{
		engine.open(form.getForms().Core.Reports);
	}
	
	private void printReport()
	{
		SkinAssessmentFindings finding = form.comboBoxSite().getValue();
		
		if(finding == null)
			return;
		
		SkinAssessment assessment = form.comboBoxAssessment().getValue();

		if(assessment == null)
			return;
		
		String urlQueryServer = ConfigFlag.GEN.QUERY_SERVER_URL.getValue();
		String urlReportServer = ConfigFlag.GEN.REPORT_SERVER_URL.getValue();

		String[] obj = null;
		try
		{
			obj = domain.getReportAndTemplate(form.getGlobalContext().Core.getReportID(), form.getGlobalContext().Core.getTemplateID());
		}
		catch (DomainInterfaceException e)
		{
			engine.showMessage("Error retrieving report template !\r\n" + e.getMessage());
			return;
		}
		
		if(obj == null || obj.length == 0)
		{
			engine.showMessage("I could not get the report and template !");
			return;
		}
		
		PatientShort ps = form.getGlobalContext().Core.getPatientShort();
		QueryBuilderClient client = new QueryBuilderClient(urlQueryServer, engine.getSessionId());
		
		client.addSeed(new SeedValue("FINDING_ID", finding.getID_SkinAssessmentFindings(), Integer.class));
		client.addSeed(new SeedValue("ASSESSMENT_ID", assessment.getID_Assessment(), Integer.class));
		
		if(ps != null)
		{
			//client.addSeed(new SeedValue("PID", ps.getHospnum().getValue(), String.class));
			ims.core.vo.PatientId id = ps.getDisplayId();
			if (id != null) 
			{
				client.addSeed(new SeedValue("PID", id.getValue(), String.class));
			}
			
			client.addSeed(new SeedValue("AGE", ps.getAge(), Integer.class));
		}
		
		String resultUrl = "";
		
		try
		{
			resultUrl = client.buildReportAsUrl(obj[0], obj[1], urlReportServer, QueryBuilderClient.PDF, "", 1);
		} 
		catch (QueryBuilderClientException e1)
		{
			engine.showMessage("Error creating report: " + e1.getMessage());
			return;
		}
		
		engine.openUrl(resultUrl);
	}
	
	protected void onRecbrAssessmentValueChanged() throws PresentationLogicException
	{
		clearGlobalContext();
		form.getGlobalContext().Nursing.setSkinReviewSearchCriteria(null);//WDEV-19389 
		populateInstance(form.recbrAssessment().getValue());
		
		updateControlStatus();		
	}
	
	private void updateControlStatus()
	{

		if (form.recbrAssessment().getValue() == null)
		{
			form.bNew().setVisible(false);
			form.btnPrint().setVisible(false);
		}
		else
		{
			if (form.recbrAssessment().getValue().equals(form.getGlobalContext().Core.getCurrentCareContext()))
			{
				form.bNew().setVisible(true);
				form.btnPrint().setVisible(true);
			}
			else
			{
				form.bNew().setVisible(false);
				form.btnPrint().setVisible(true);
			}
		}
	}	
	
	private String[] wounds = new String[]{"Recording Date/Time", "HCP", "Site", "Site Details", "How long is it present", "Pressure Sore Grade", "Size: Length, Width, Depth", "Wound Bed", "Surrounding Skin", "Exudate Amount", "Exudate Type", "Odour", "Pain", "Infection Suspected", "Swab Taken", "Wound Traced", "Cleansed With", "Primary Dressing", "Secondary Dressing", "Frequency of Change", "Comment"};
	private int maxcolumns = 4;
	private String yes = new String("Yes");
	private String no  = new String("No");
	
}
