/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/forms/waterlowview/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 Vasile Purdila using IMS Development Environment (version 1.22 build 41209.1630)
// Copyright (C) 1995-2004 IMS MAXIMS plc. All rights reserved.

package ims.nursing.forms.waterlowview;

import ims.coe.vo.RiskAssessment;
import ims.coe.vo.RiskAssessmentCollection;
import ims.coe.vo.RiskDescription;
import ims.configuration.gen.ConfigFlag;
import ims.domain.exceptions.DomainInterfaceException;
import ims.framework.exceptions.PresentationLogicException;
import ims.framework.utils.Color;
import ims.framework.utils.Date;
import ims.nursing.vo.WaterlowAssessmentDetailsVo;
import ims.nursing.vo.WaterlowAssessmentDetailsVoCollection;
import ims.nursing.vo.WaterlowAssessmentVo;
import ims.nursing.vo.WaterlowAssessmentVoCollection;
import ims.nursing.vo.WaterlowViewSearchCriteriaVo;

public class Logic extends BaseLogic
{
	private final static String LAST_MONTH = "1";
	private final static String LAST_SIX_MONTHS = "2";	
	private final static String LAST_YEAR = "3";
	private final static String ALL = "4";
	private final static int INDEX_ROW_DT = 0;
	private final static int INDEX_ROW_HCP = 1;
	private final static int NUM_COLS = 5;

	protected void onFormOpen() throws ims.framework.exceptions.FormOpenException
	{
		if(!getWaterlowAssessment())
		{
			engine.showMessage("There has been a problem retrieving the Waterlow Assessments.");
			return;
		}
		
		form.cmbFilter().clear();
		form.cmbFilter().newRow(LAST_MONTH,"Within the last month");
		form.cmbFilter().newRow(LAST_SIX_MONTHS,"Within the last 6 months");
		form.cmbFilter().newRow(LAST_YEAR,"Within the last year");
		form.cmbFilter().newRow(ALL,"All");
		
		form.cmbFilter().setValue(LAST_SIX_MONTHS);
		
		//WDEV-19389 
		if (!(form.getGlobalContext().Core.getCurrentCareContextIsNotNull() && form.getGlobalContext().Nursing.getWaterlowViewSearchCriteriaIsNotNull() && form.getGlobalContext().Core.getCurrentCareContext().equals(form.getGlobalContext().Nursing.getWaterlowViewSearchCriteria().getCareContext())))
			form.getGlobalContext().Nursing.setWaterlowViewSearchCriteria(null);
		
		if(form.getGlobalContext().Nursing.getWaterlowViewSearchCriteriaIsNotNull())
		{
			setSearchCriteria(form.getGlobalContext().Nursing.getWaterlowViewSearchCriteria());
			try
			{
				onCmbFilterValueChanged();
			}catch (PresentationLogicException e){e.printStackTrace();}
		}
		else
			filterValueChanged();
		//WDEV-19389 - end

	
		if(form.cmbRecordToUpdate().getValue() ==null)
		{
			form.btnUpdate().setEnabled(false);
			form.getLocalContext().setWaterlowAssessmentVo(null);
		}
		else
			form.getLocalContext().setWaterlowAssessmentVo(form.cmbRecordToUpdate().getValue());
	}
	
	
	private WaterlowViewSearchCriteriaVo getSearchCriteria()
	{
		WaterlowViewSearchCriteriaVo searchCriteria = new WaterlowViewSearchCriteriaVo();
		
		searchCriteria.setFilter(form.cmbFilter().getValue());
		searchCriteria.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
		
		return searchCriteria;
	}
		
	private void setSearchCriteria(WaterlowViewSearchCriteriaVo waterlowViewSearchCriteriaVo)
	{
		form.cmbFilter().setValue(waterlowViewSearchCriteriaVo.getFilter());		
	}

//	private void setViewType(Integer searchType)
//	{
//		if (searchType == null)
//			return;
//		
//		switch (searchType)
//		{
//		case LIST_ALL:
//			form.Group1().setValue(Group1Enumeration.rdoAll);
//			break;
//		case LIST_ACTIVE:
//			form.Group1().setValue(Group1Enumeration.rdoCurrent);
//			break;
//		}				
//	}
//
//	private String getViewType()
//	{
//		if (LAST_SIX_MONTHS.equals(form.cmbFilter().getValue()))
//		{
//			return LAST_SIX_MONTHS;
//		}
//		if (LAST_MONTH.equals(form.cmbFilter().getValue()))
//		{
//			return LAST_MONTH;
//		}
//		if (LAST_YEAR.equals(form.cmbFilter().getValue()))
//		{
//			return LAST_YEAR;
//		}
//		if (ALL.equals(form.cmbFilter().getValue()))
//		{
//			return ALL;
//		}
//		
//		return null;
//	}
	
	private void filterValueChanged()
	{
		form.getLocalContext().setGroupIndex(new Integer(0));
		
		loadAssessmentRecords();
	}
	
	private void loadAssessmentRecords()
	{	
		WaterlowAssessmentVoCollection waters = null;
		Date dateFrom = null;
		Date dateTo = null;
		
		if(form.cmbFilter().getValue() != null)
		{
			if(form.cmbFilter().getValue().equals(LAST_SIX_MONTHS))
			{
				dateFrom = new Date();
				dateFrom.addDay(-183);
			}
			else
			if(form.cmbFilter().getValue().equals(LAST_MONTH))
			{
				dateFrom = new Date();
				dateFrom.addDay(-31);
			}
			else
			if(form.cmbFilter().getValue().equals(LAST_YEAR))
			{
				dateFrom = new Date();
				dateFrom.addDay(-365);
			}
		}
		try
		{
			//list all assessments from dateFrom to dateTo
			waters = domain.listAssessments(dateFrom, dateTo, null, form.getGlobalContext().Core.getPatientShort().getID_Patient());
		} 
		catch (DomainInterfaceException e)
		{
			engine.showMessage(e.getMessage());
		}
	
		form.getLocalContext().setWaterAssessments(waters);
		
		populateScreenFromData();	
		
		//copy last functionality (WDEV-10253)
		if(ConfigFlag.UI.COPY_LAST_FUNCTIONALITY.getValue() && Boolean.TRUE.equals(domain.hasWaterlowForCurrentAdmission(form.getGlobalContext().Core.getCurrentCareContext())))
		{
			form.btnCopyLast().setEnabled(true);
		}
		else
		{
			form.btnCopyLast().setEnabled(false);
		}
		
		//WDEV-10834
		if(ConfigFlag.UI.COPY_LAST_FUNCTIONALITY.getValue() == false)
		{
			form.btnCopyLast().setVisible(false);
		}
	}
	
	protected void onFormDialogClosed(ims.framework.FormName formName, ims.framework.enumerations.DialogResult result) throws ims.framework.exceptions.PresentationLogicException
	{
		
			refreshCurrentRecord();
	}
	protected void refreshCurrentRecord()
	{
		loadAssessmentRecords();
	}
	protected void onBtnPreviousClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if(form.getLocalContext().getGroupIndex().intValue() == 0)
			return; 
		
		form.getGlobalContext().Nursing.setWaterlowRecord(null);
		form.getLocalContext().setGroupIndex(new Integer(form.getLocalContext().getGroupIndex().intValue() - 1));
		populateScreenFromData();
	}
	protected void onBtnNextClick() throws ims.framework.exceptions.PresentationLogicException
	{
		form.getGlobalContext().Nursing.setWaterlowRecord(null);
		form.getLocalContext().setGroupIndex(new Integer(form.getLocalContext().getGroupIndex().intValue() + 1));
		populateScreenFromData();
	}
	protected void onBtnUpdateClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if(form.cmbRecordToUpdate().getValue() == null)
		{
			engine.showMessage("Please select a record to update !");
			return;
		}
		
		//update if care context is equal to currently selected care context
		form.getGlobalContext().Nursing.setEdit(new Boolean(form.cmbRecordToUpdate().getValue().getCareContext().equals(form.getGlobalContext().Core.getCurrentCareContext())));
		form.getGlobalContext().Nursing.setWaterlowRecord(form.cmbRecordToUpdate().getValue());
		engine.open(form.getForms().Nursing.WaterlowDialog, "Edit waterlow assessment");
	}
	protected void onCmbFilterValueChanged() throws ims.framework.exceptions.PresentationLogicException
	{
		form.getLocalContext().setGroupIndex(new Integer(0));
		loadAssessmentRecords();
		form.getGlobalContext().Nursing.setWaterlowViewSearchCriteria(getSearchCriteria()); // WDEV-19389 
		updateControlsState();
	}
	protected void updateControlsState()
	{
		if(form.cmbRecordToUpdate().getValue() == null)
		{
			form.btnUpdate().setEnabled(false);
			form.getLocalContext().setWaterlowAssessmentVo(null);
			return;
		}
		
		form.btnUpdate().setEnabled(true);
		form.getLocalContext().setWaterlowAssessmentVo(form.cmbRecordToUpdate().getValue());
				
		try
		{
			if(domain.isPartOfSkinAssessment(form.cmbRecordToUpdate().getValue()) == Boolean.TRUE)
				form.btnUpdate().setText("View");
			else if (!form.cmbRecordToUpdate().getValue().getCareContext().equals(form.getGlobalContext().Core.getCurrentCareContext()))
				form.btnUpdate().setText("View");
			else
				form.btnUpdate().setText("Edit");
		} catch (DomainInterfaceException e)
		{
			engine.showMessage("Error retrieving eating assessment record: " + e.getMessage());
			return;
		}	
		
	}
	protected void onBtnNewClick() throws ims.framework.exceptions.PresentationLogicException
	{
		form.getGlobalContext().Nursing.setEdit(Boolean.TRUE);
		form.getGlobalContext().Nursing.setWaterlowRecord(null);
		engine.open(form.getForms().Nursing.WaterlowDialog, "Add new waterlow assessment");
	}
	private boolean getWaterlowAssessment()
	{
		clearGrids();//WDEV-15059
		
		RiskAssessmentCollection risks = form.getGlobalContext().Nursing.getWaterlowAssessments();
		
		if(risks == null)
		{
			try
			{
				risks = ims.nursing.helper.RiskAssessmentHelper.getWaterlowRisks();
			}
			catch (PresentationLogicException e)
			{
				engine.showMessage(e.getMessage());
				return false;
			}
			form.getGlobalContext().Nursing.setWaterlowAssessments(risks);
			if(risks == null)
			{
				engine.showMessage("The call to get the Waterlow Risks came back as empty.");
			}
		}

		//Date/time
		GenForm.gridWaterRow row = form.gridWater().getRows().newRow();
		row.setcolWater("Date/Time");
		row.setBold(true);
		row.setBackColor(Color.Beige);
		row = form.gridWater().getRows().newRow();
		row.setcolWater("HCP");
		row.setBold(true);
		row.setBackColor(Color.Beige);

		GenForm.gridAssessmentsRow ar = form.gridAssessments().getRows().newRow();
		ar.setBold(true);
		ar.setBackColor(Color.Beige);
		ar = form.gridAssessments().getRows().newRow();
		ar.setBold(true);
		ar.setBackColor(Color.Beige);
		
		for(int i = 0; i < risks.size(); i++)
		{
			RiskAssessment risk = risks.get(i);
			
			row = form.gridWater().getRows().newRow();
			row.setcolWater(risk.getName());
			if(i == 0)
				row.setTooltip("<b>Sex</b><br><br><b>1</b>&nbsp;&nbsp;&nbsp;Male<br><b>2</b>&nbsp;&nbsp;&nbsp;Female");
			else if(i == 1)
				row.setTooltip("<b>Age</b><br><br><b>1</b>&nbsp;&nbsp;&nbsp;14 - 49<br><b>2</b>&nbsp;&nbsp;&nbsp;50 - 64<br><b>3</b>&nbsp;&nbsp;&nbsp;65 - 74<br><b>4</b>&nbsp;&nbsp;&nbsp;75 - 80<br><b>5</b>&nbsp;&nbsp;&nbsp;80 +");
			else if(i == 2)
				row.setTooltip("<b>Appetite</b><br><br><b>0</b>&nbsp;&nbsp;&nbsp; Average <br><b>1</b>&nbsp;&nbsp;&nbsp; Poor <br><b>2</b>&nbsp;&nbsp;&nbsp; NG tube/fluids only <br><b>3</b>&nbsp;&nbsp;&nbsp; Anorexic <br>");
			else if(i == 3)
				row.setTooltip("<b>Continence</b><br><br><b>0</b>&nbsp;&nbsp;&nbsp; Complete/catheterized <br><b>1</b>&nbsp;&nbsp;&nbsp; Occasional incontinence <br><b>2</b>&nbsp;&nbsp;&nbsp; Catheter/incontinent of faeces <br><b>3</b>&nbsp;&nbsp;&nbsp; Doubly incontinent <br>");
			else if(i == 4)
				row.setTooltip("<b>Skin</b><br><br><b>0</b>&nbsp;&nbsp;&nbsp; Healthy <br><b>1</b>&nbsp;&nbsp;&nbsp; Tissue paper/dry <br><b>1</b>&nbsp;&nbsp;&nbsp; Clammy/oedametous <br><b>2</b>&nbsp;&nbsp;&nbsp; Discoloured <br><b>3</b>&nbsp;&nbsp;&nbsp; Broken spot <br>");
			else if(i == 5)
				row.setTooltip("<b>Mobility</b><br><br><b>0</b>&nbsp;&nbsp;&nbsp; Full <br><b>1</b>&nbsp;&nbsp;&nbsp; Restless/fidgety <br><b>2</b>&nbsp;&nbsp;&nbsp; Apathetic <br><b>3</b>&nbsp;&nbsp;&nbsp; Restricted <br><b>4</b>&nbsp;&nbsp;&nbsp; Inert/traction <br><b>5</b>&nbsp;&nbsp;&nbsp; Chair bound <br>");
			else if(i == 6)
				row.setTooltip("<b>Build / weight for height</b><br><br><b>0</b>&nbsp;&nbsp;&nbsp; Average <br><b>1</b>&nbsp;&nbsp;&nbsp; Above average <br><b>2</b>&nbsp;&nbsp;&nbsp; Obese <br><b>3</b>&nbsp;&nbsp;&nbsp; Below average <br>");
			else if(i == 7)
				row.setTooltip("<b>Cardiovascular</b><br><br><b>9</b>&nbsp;&nbsp;&nbsp; Terminal cachexia etc.<br><b>5</b>&nbsp;&nbsp;&nbsp; Cardiac failure	<br><b>5</b>&nbsp;&nbsp;&nbsp; Peripheral vascular disease <br><b>2</b>&nbsp;&nbsp;&nbsp; Anaemia <br><b>1</b>&nbsp;&nbsp;&nbsp; Smoker <br>");
			else if(i == 8)
				row.setTooltip("<b>Neurological - Diabetes, CVA, MS, Paralegia, Motor Sensor</b>&nbsp;&nbsp;&nbsp;<br><br><b>0</b>&nbsp;&nbsp;&nbsp; None <br><b>4</b>&nbsp;&nbsp;&nbsp; Slight <br><b>5</b>&nbsp;&nbsp;&nbsp; Moderate <br><b>6</b>&nbsp;&nbsp;&nbsp; Severe<br>");
			else if(i == 9)
				row.setTooltip("<b>Medication - Steroids, Cytotoxic, High Dose Anti-inflammatory</b>&nbsp;&nbsp;&nbsp;<br><br>Score <b>4</b> if any of the above selected&nbsp;&nbsp;&nbsp;<br>");
			else if(i == 10)
				row.setTooltip("<b>Surgery / Trauma</b>&nbsp;&nbsp;&nbsp;<br><br><b>5</b>&nbsp;&nbsp;&nbsp; Orthopedic(below waist)<br><b>5</b>&nbsp;&nbsp;&nbsp; Op more than 2 hours <br>");
			
			ar = form.gridAssessments().getRows().newRow();
		}

		row = form.gridWater().getRows().newRow();
		row.setcolWater("TOTAL");
		row.setTooltip("<b>AT RISK:</b> 10 + <br> <b>HIGH RISK:</b> 15 +<br><b>VERY HIGH RISK:</b> 20 +");
		row.setBold(true);
		row.setBackColor(Color.Beige);

		ar = form.gridAssessments().getRows().newRow();
		ar.setBold(true);
		ar.setBackColor(Color.Beige);
		
		return true;
	}
	
	private void clearGrids() 
	{
		form.gridWater().getRows().clear();
		form.gridAssessments().getRows().clear();
	}

	private void populateScreenFromData()
	{
		String nScore;
		int nTotalScore = 0;
		
		WaterlowAssessmentVoCollection waters = form.getLocalContext().getWaterAssessments();
		if(waters == null)
		{
			engine.showMessage("There are no assessments available at this time. Screen will not be populated.");
			return;
		}
		
		clearGridRows();
		form.cmbRecordToUpdate().clear();

		int startIndex = form.getLocalContext().getGroupIndex().intValue() * NUM_COLS;
		int endIndex = startIndex + NUM_COLS <= waters.size() ? startIndex + NUM_COLS : waters.size();
		
		GenForm.gridAssessmentsRow row;
		for(int i = startIndex; i < endIndex; i++)
		{
			if (waters.get(i).getCareContext().equals(form.getGlobalContext().Core.getCurrentCareContext()))
				form.cmbRecordToUpdate().newRow(waters.get(i), (waters.get(i).getDateTimeInitiated() == null ? "-" : waters.get(i).getDateTimeInitiated().toString()) + ", " + (waters.get(i).getHcpInitiated() == null ? "-" : waters.get(i).getHcpInitiated().getName().toString()),Color.Green);
			else
				form.cmbRecordToUpdate().newRow(waters.get(i), (waters.get(i).getDateTimeInitiated() == null ? "-" : waters.get(i).getDateTimeInitiated().toString()) + ", " + (waters.get(i).getHcpInitiated() == null ? "-" : waters.get(i).getHcpInitiated().getName().toString()),Color.Red);			
		}

		//set the combo to hold the latest record or last edited record if any
		if (form.getGlobalContext().Nursing.getWaterlowRecordIsNotNull())
		{
			form.cmbRecordToUpdate().setValue(form.getGlobalContext().Nursing.getWaterlowRecord());
		}
		else if(form.cmbRecordToUpdate().getValues() != null && endIndex > 0)
		{
			form.cmbRecordToUpdate().setValue((WaterlowAssessmentVo) form.cmbRecordToUpdate().getValues().get((endIndex > startIndex)?((endIndex - startIndex)-1):0));
		}
		
		int ix = startIndex;

		//first record
		if(ix < endIndex)
		{
			nTotalScore = 0;
			
			row = form.gridAssessments().getRows().get(INDEX_ROW_DT);
			row.setColAss1(waters.get(ix).getDateTimeInitiated() != null ? waters.get(ix).getDateTimeInitiated().toString() : "");
			row.setTooltipForColAss1(waters.get(ix).getComment());
			row = form.gridAssessments().getRows().get(INDEX_ROW_HCP);
			row.setColAss1(waters.get(ix).getHcpInitiated() != null ? waters.get(ix).getHcpInitiated().getName().toString() : "");
			row.setTooltipForColAss1(waters.get(ix).getComment());
			
			for(int i = 0; i < form.getGlobalContext().Nursing.getWaterlowAssessments().size(); i++)
			{
				//both grids must have the same number of rows
				row = form.gridAssessments().getRows().get(i + INDEX_ROW_HCP + 1);
				
				nScore = getScoreByAssessmentName(form.gridWater().getRows().get(i + INDEX_ROW_HCP + 1).getcolWater(), waters.get(ix).getAssessmentDetails());
				if (nScore != "") //WDEV-15129
				nTotalScore += Integer.valueOf(nScore).intValue();
				row.setColAss1(nScore);
			}

			//the total
			row = form.gridAssessments().getRows().get(form.gridAssessments().getRows().size() - 1);
			row.setColAss1(String.valueOf(nTotalScore));

			ix++;
		}

		//second record
		if(ix < endIndex)
		{
			nTotalScore = 0;
			
			row = form.gridAssessments().getRows().get(INDEX_ROW_DT);
			row.setColAss2(waters.get(ix).getDateTimeInitiated() != null ? waters.get(ix).getDateTimeInitiated().toString() : "");
			row.setTooltipForColAss2(waters.get(ix).getComment());
			row = form.gridAssessments().getRows().get(INDEX_ROW_HCP);
			row.setColAss2(waters.get(ix).getHcpInitiated() != null ? waters.get(ix).getHcpInitiated().getName().toString() : "");
			row.setTooltipForColAss2(waters.get(ix).getComment());
			
			for(int i = 0; i < form.getGlobalContext().Nursing.getWaterlowAssessments().size(); i++)
			{
				//both grids must have the same number of rows
				row = form.gridAssessments().getRows().get(i + INDEX_ROW_HCP + 1);
				
				nScore = getScoreByAssessmentName(form.gridWater().getRows().get(i + INDEX_ROW_HCP + 1).getcolWater(), waters.get(ix).getAssessmentDetails());
				if (nScore != "")
				nTotalScore += Integer.valueOf(nScore).intValue();
				row.setColAss2(nScore);
			}

			//the total
			row = form.gridAssessments().getRows().get(form.gridAssessments().getRows().size() - 1);
			row.setColAss2(String.valueOf(nTotalScore));
			
			ix++;
		}

		//third record
		if(ix < endIndex)
		{
			nTotalScore = 0;
			
			row = form.gridAssessments().getRows().get(INDEX_ROW_DT);
			row.setColAss3(waters.get(ix).getDateTimeInitiated() != null ? waters.get(ix).getDateTimeInitiated().toString() : "");
			row.setTooltipForColAss3(waters.get(ix).getComment());
			row = form.gridAssessments().getRows().get(INDEX_ROW_HCP);
			row.setColAss3(waters.get(ix).getHcpInitiated() != null ? waters.get(ix).getHcpInitiated().getName().toString() : "");
			row.setTooltipForColAss3(waters.get(ix).getComment());
			
			for(int i = 0; i < form.getGlobalContext().Nursing.getWaterlowAssessments().size(); i++)
			{
				//both grids must have the same number of rows
				row = form.gridAssessments().getRows().get(i + INDEX_ROW_HCP + 1);
				
				nScore = getScoreByAssessmentName(form.gridWater().getRows().get(i + INDEX_ROW_HCP + 1).getcolWater(), waters.get(ix).getAssessmentDetails());
				if (nScore != "")
				nTotalScore += Integer.valueOf(nScore).intValue();
				row.setColAss3(nScore);
			}

			//the total
			row = form.gridAssessments().getRows().get(form.gridAssessments().getRows().size() - 1);
			row.setColAss3(String.valueOf(nTotalScore));
			
			ix++;
		}
		
		//fourth record
		if(ix < endIndex)
		{
			nTotalScore = 0;
			
			row = form.gridAssessments().getRows().get(INDEX_ROW_DT);
			row.setColAss4(waters.get(ix).getDateTimeInitiated() != null ? waters.get(ix).getDateTimeInitiated().toString() : "");
			row.setTooltipForColAss4(waters.get(ix).getComment());
			row = form.gridAssessments().getRows().get(INDEX_ROW_HCP);
			row.setColAss4(waters.get(ix).getHcpInitiated() != null ? waters.get(ix).getHcpInitiated().getName().toString() : "");
			row.setTooltipForColAss4(waters.get(ix).getComment());
			
			for(int i = 0; i < form.getGlobalContext().Nursing.getWaterlowAssessments().size(); i++)
			{
				//both grids must have the same number of rows
				row = form.gridAssessments().getRows().get(i + INDEX_ROW_HCP + 1);
				
				nScore = getScoreByAssessmentName(form.gridWater().getRows().get(i + INDEX_ROW_HCP + 1).getcolWater(), waters.get(ix).getAssessmentDetails());
				if (nScore != "") 
				nTotalScore += Integer.valueOf(nScore).intValue();
				row.setColAss4(nScore);
			}

			//the total
			row = form.gridAssessments().getRows().get(form.gridAssessments().getRows().size() - 1);
			row.setColAss4(String.valueOf(nTotalScore));
			
			ix++;
		}
		
		//fifth record
		if(ix < endIndex)
		{
			nTotalScore = 0;
			
			row = form.gridAssessments().getRows().get(INDEX_ROW_DT);
			row.setColAss5(waters.get(ix).getDateTimeInitiated() != null ? waters.get(ix).getDateTimeInitiated().toString() : "");
			row.setTooltipForColAss5(waters.get(ix).getComment());
			row = form.gridAssessments().getRows().get(INDEX_ROW_HCP);
			row.setColAss5(waters.get(ix).getHcpInitiated() != null ? waters.get(ix).getHcpInitiated().getName().toString() : "");
			row.setTooltipForColAss5(waters.get(ix).getComment());
			
			for(int i = 0; i < form.getGlobalContext().Nursing.getWaterlowAssessments().size(); i++)
			{
				//both grids must have the same number of rows
				row = form.gridAssessments().getRows().get(i + INDEX_ROW_HCP + 1);
				
				nScore = getScoreByAssessmentName(form.gridWater().getRows().get(i + INDEX_ROW_HCP + 1).getcolWater(), waters.get(ix).getAssessmentDetails());
				if (nScore != "")
				nTotalScore += Integer.valueOf(nScore).intValue();
				row.setColAss5(nScore);
			}

			//the total
			row = form.gridAssessments().getRows().get(form.gridAssessments().getRows().size() - 1);
			row.setColAss5(String.valueOf(nTotalScore));
			
			ix++;
		}
		
		// enable/disable Prev/Next buttons
		if(form.getLocalContext().getGroupIndex().intValue() > 0)
			form.btnPrev().setEnabled(true);
		else
			form.btnPrev().setEnabled(false);
		
		if(endIndex >= waters.size())
			form.btnNext().setEnabled(false);
		else
			form.btnNext().setEnabled(true);
		
		String legend;
		
		if(waters.size() > 0)
			legend = "<B><CENTER>Rec. " + String.valueOf(form.getLocalContext().getGroupIndex().intValue() * NUM_COLS + 1) + "-" + String.valueOf(endIndex) + " of " + String.valueOf(waters.size()) + "</CENTER></B>";
		else
			legend = "<B><CENTER>No records</CENTER></B>";
			
		form.htmLegend().setHTML(legend);
		
		updateControlsState();		
	}

	private void clearGridRows()
	{
		for (int i = 0; i < form.gridAssessments().getRows().size(); i++)
		{
			GenForm.gridAssessmentsRow row = form.gridAssessments().getRows().get(i);
			
			row.setColAss1("");
			row.setColAss2("");
			row.setColAss3("");
			row.setColAss4("");
			row.setColAss5("");
			row.setValue(null);
		}
	}
	
	private String getScoreByAssessmentName(String colWater, WaterlowAssessmentDetailsVoCollection assessmentDetails)
	{
		String nScore = "";
		
		RiskAssessment risk;
		RiskDescription descr;
		
		for(int i = 0; i < form.getGlobalContext().Nursing.getWaterlowAssessments().size(); i++)
		{
			risk = form.getGlobalContext().Nursing.getWaterlowAssessments().get(i);
			
			if(risk.getName().equalsIgnoreCase(colWater))
			{
				for(int j = 0; j < risk.getRiskFactors().size(); j++)
				{
					descr = risk.getRiskFactors().get(j);
					
					for(int k = 0; k < assessmentDetails.size(); k++)
					{
						if(assessmentDetails.get(k).getAssessment().toString().equals(descr.getId()) && assessmentDetails.get(k).getSelect().equals(Boolean.TRUE))
						{
							if (nScore.equals(""))//WDEV-15129
							{
								nScore =  descr.getScore();
							}
							else
							{
								Integer	intScore = Integer.valueOf(nScore).intValue();
								intScore += Integer.valueOf(descr.getScore()).intValue();
								nScore = intScore.toString();
							}
						}
					}
				}
				
				break;
			}
		}
		
		return nScore;
	}

	protected void onCmbRecordToUpdateValueChanged() throws PresentationLogicException
	{
		updateControlsState();
	}

	@Override
	protected void onBtnCopyLastClick() throws PresentationLogicException
	{
		form.getGlobalContext().Nursing.setCopyLast(Boolean.TRUE);
		form.getGlobalContext().Nursing.setEdit(Boolean.TRUE);
		form.getGlobalContext().Nursing.setWaterlowRecord(null);
		engine.open(form.getForms().Nursing.WaterlowDialog, "Copy last waterlow assessment");
	}

	private int getAssessmentScore(WaterlowAssessmentDetailsVoCollection coll)
	{
		int nScore = 0;

		RiskAssessment risk = null;
		RiskDescription descr = null;

		int partScore;
		for (int i = 0; i < coll.size(); i++)
		{
			WaterlowAssessmentDetailsVo child = coll.get(i);

			risk = null;
			partScore = 0;
			
			for(int k = 0; k < form.getGlobalContext().Nursing.getWaterlowAssessments().size(); k++)
			{
				risk = form.getGlobalContext().Nursing.getWaterlowAssessments().get(k);
				
				for(int m = 0; m < risk.getRiskFactors().size(); m++)
				{
					descr = risk.getRiskFactors().get(m);
					
					if(child.getAssessment().toString().equals(descr.getId()) && Boolean.TRUE.equals(child.getSelect()))
					{
						partScore += Integer.valueOf(descr.getScore()).intValue();
					}
				}
			}
			
			nScore += partScore;
		}
		
		return nScore;
	}
	
}
