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

package ims.nursing.forms.mnaview;

import java.util.ArrayList;
import ims.coe.vo.RiskAssessment;
import ims.coe.vo.RiskAssessmentCollection;
import ims.coe.vo.RiskDescription;
import ims.configuration.gen.ConfigFlag;
import ims.core.vo.PatientShort;
import ims.domain.exceptions.DomainInterfaceException;
import ims.framework.enumerations.DialogResult;
import ims.framework.exceptions.PresentationLogicException;
import ims.framework.utils.Color;
import ims.framework.utils.Date;
import ims.nursing.vo.MNAViewSearchCriteriaVo;
import ims.nursing.vo.MiniNutritionalAssessment;
import ims.nursing.vo.MiniNutritionalAssessmentCollection;
import ims.nursing.vo.MiniNutritionalAssessmentDetails;
import ims.nursing.vo.MiniNutritionalAssessmentDetailsCollection;
import com.ims.query.builder.client.QueryBuilderClient;
import com.ims.query.builder.client.SeedValue;
import com.ims.query.builder.client.exceptions.QueryBuilderClientException;

public class Logic extends BaseLogic
{
	private final static String	LAST_WEEK		= "1";
	private final static String	LAST_MONTH		= "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
	{
		form.imgCopyright().setValue(form.getImages().Core.Copyright);
		form.btnPrint().setEnabled(false);
		form.btnUpdate().setEnabled(false);
		getMNAAssessment();

		form.cmbFilter().clear();
		form.cmbFilter().newRow(LAST_WEEK, "Within the last week");
		form.cmbFilter().newRow(LAST_MONTH, "Within the last month");
		form.cmbFilter().newRow(LAST_YEAR, "Within the last year");
		form.cmbFilter().newRow(ALL, "All");

		form.cmbFilter().setValue(LAST_WEEK);
		
		//WDEV-19389 --- start
		if (!(form.getGlobalContext().Core.getCurrentCareContextIsNotNull() && form.getGlobalContext().Nursing.getMNAViewSearchCriteriaIsNotNull() && form.getGlobalContext().Nursing.getMNAViewSearchCriteria().getCareContextIsNotNull() && form.getGlobalContext().Core.getCurrentCareContext().equals(form.getGlobalContext().Nursing.getMNAViewSearchCriteria().getCareContext())))
			form.getGlobalContext().Nursing.setMNAViewSearchCriteria(null);
		
		if(form.getGlobalContext().Nursing.getMNAViewSearchCriteriaIsNotNull())
		{
			setSearchCriteria(form.getGlobalContext().Nursing.getMNAViewSearchCriteria());
		}	
		//WDEV-19389 --- end
		
		filterValueChanged();

	}
	
	private MNAViewSearchCriteriaVo getSearchCriteria()
	{
		MNAViewSearchCriteriaVo searchCriteria = new MNAViewSearchCriteriaVo();
		
		searchCriteria.setFilter(form.cmbFilter().getValue());
		searchCriteria.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
		
		return searchCriteria;
	}
	
	
	private void setSearchCriteria(MNAViewSearchCriteriaVo mnaViewSearchCriteriaVo)
	{
		form.cmbFilter().setValue(mnaViewSearchCriteriaVo.getFilter());	
	}

	private void filterValueChanged()
	{
		MiniNutritionalAssessmentCollection mnas = null;
		Date dateFrom = null;
		Date dateTo = null;

		if (form.cmbFilter().getValue().equals(LAST_WEEK))
		{
			dateFrom = new Date();
			dateFrom.addDay(-7);
		}
		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
		{
			mnas = domain.listAssessments(dateFrom, dateTo, form.getGlobalContext().Core.getCurrentCareContext());
		}
		catch (DomainInterfaceException e)
		{
			engine.showMessage(e.getMessage());
		}

		form.getLocalContext().setMNAAssessments(mnas);
		form.getLocalContext().setGroupIndex(new Integer(0));

		populateScreenFromData();

		try
		{
			onCmbRecordToUpdateValueChanged();
		}
		catch (PresentationLogicException e1)
		{
			engine.showMessage(e1.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	private void populateScreenFromData()
	{
		float nScore;
		float nTotalScore = 0;
		float nGrandTotalScore = 0;
		int nCurrentRowIndex = -1;
		// int ii;

		MiniNutritionalAssessmentCollection mnas = form.getLocalContext().getMNAAssessments();

		clearGridRows();
		form.cmbRecordToUpdate().clear();

		if (form.getLocalContext().getMNAIdsIsNotNull())
			form.getLocalContext().getMNAIds().clear();
		else
			form.getLocalContext().setMNAIds(new ArrayList());

		int startIndex = form.getLocalContext().getGroupIndex().intValue() * NUM_COLS;
		int endIndex = startIndex + NUM_COLS <= mnas.size() ? startIndex + NUM_COLS : mnas.size();

		GenForm.gridMNARow row;
		for (int i = startIndex; i < endIndex; i++)
		{
			form.cmbRecordToUpdate().newRow(mnas.get(i), (mnas.get(i).getDateTimeInitiated() == null ? "-" : mnas.get(i).getDateTimeInitiated().toString()) + ", " + (mnas.get(i).getHcpInitiated() == null ? "-" : mnas.get(i).getHcpInitiated().getName().toString()));
			form.getLocalContext().getMNAIds().add(mnas.get(i).getID_Assessment());
			form.btnPrint().setEnabled(true);
		}

		int ix = startIndex;

		// first record
		if (ix < endIndex)
		{
			nTotalScore = 0;
			nGrandTotalScore = 0;

			row = form.gridMNA().getRows().get(INDEX_ROW_DT);
			row.setColAss1(mnas.get(ix).getDateTimeInitiated() != null ? mnas.get(ix).getDateTimeInitiated().toString() : "");
			row = form.gridMNA().getRows().get(INDEX_ROW_HCP);
			row.setColAss1(mnas.get(ix).getHcpInitiated() != null ? mnas.get(ix).getHcpInitiated().getName().toString() : "");

			// screening
			nCurrentRowIndex = INDEX_ROW_HCP + 1;
			for (int i = 0; i < form.getGlobalContext().COE.getMNAAssessments().size(); i++)
			{
				if ((form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().length() > 0 && (form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) >= 'A' && (form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) <= 'F')
				{
					nCurrentRowIndex++;
					row = form.gridMNA().getRows().get(nCurrentRowIndex);

					nScore = getScoreByAssessmentName(form.gridMNA().getRows().get(nCurrentRowIndex).getMNA(), mnas.get(ix).getAssessmentDetails());
					nTotalScore += nScore;
					row.setColAss1(String.valueOf(nScore));
				}
			}
			// the total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss1(String.valueOf(nTotalScore));
				nCurrentRowIndex++;
			}

			nGrandTotalScore += nTotalScore;
			nCurrentRowIndex++;

			// assessment
			nTotalScore = 0;
			for (int i = 0; i < form.getGlobalContext().COE.getMNAAssessments().size(); i++)
			{
				if ((form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) >= 'G')
				{
					nCurrentRowIndex++;
					row = form.gridMNA().getRows().get(nCurrentRowIndex);

					nScore = getScoreByAssessmentName(form.gridMNA().getRows().get(nCurrentRowIndex).getMNA(), mnas.get(ix).getAssessmentDetails());
					nTotalScore += nScore;
					row.setColAss1(String.valueOf(nScore));
				}
			}
			// the total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss1(String.valueOf(nTotalScore));
				nCurrentRowIndex++;
			}

			nGrandTotalScore += nTotalScore;

			// the grand total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss1(String.valueOf(nGrandTotalScore));
				nCurrentRowIndex++;
			}

			ix++;
		}

		// second record
		if (ix < endIndex)
		{
			nTotalScore = 0;
			nGrandTotalScore = 0;

			row = form.gridMNA().getRows().get(INDEX_ROW_DT);
			row.setColAss2(mnas.get(ix).getDateTimeInitiated() != null ? mnas.get(ix).getDateTimeInitiated().toString() : "");
			row = form.gridMNA().getRows().get(INDEX_ROW_HCP);
			row.setColAss2(mnas.get(ix).getHcpInitiated() != null ? mnas.get(ix).getHcpInitiated().getName().toString() : "");

			// screening
			nCurrentRowIndex = INDEX_ROW_HCP + 1;
			for (int i = 0; i < form.getGlobalContext().COE.getMNAAssessments().size(); i++)
			{
				if ((form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().length() > 0 && (form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) >= 'A' && (form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) <= 'F')
				{
					nCurrentRowIndex++;
					row = form.gridMNA().getRows().get(nCurrentRowIndex);

					nScore = getScoreByAssessmentName(form.gridMNA().getRows().get(nCurrentRowIndex).getMNA(), mnas.get(ix).getAssessmentDetails());
					nTotalScore += nScore;
					row.setColAss2(String.valueOf(nScore));
				}
			}
			// the total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss2(String.valueOf(nTotalScore));
				nCurrentRowIndex++;
			}

			nGrandTotalScore += nTotalScore;
			nCurrentRowIndex++;

			// assessment
			nTotalScore = 0;
			for (int i = 0; i < form.getGlobalContext().COE.getMNAAssessments().size(); i++)
			{
				if ((form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) >= 'G')
				{
					nCurrentRowIndex++;
					row = form.gridMNA().getRows().get(nCurrentRowIndex);

					nScore = getScoreByAssessmentName(form.gridMNA().getRows().get(nCurrentRowIndex).getMNA(), mnas.get(ix).getAssessmentDetails());
					nTotalScore += nScore;
					row.setColAss2(String.valueOf(nScore));
				}
			}
			// the total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss2(String.valueOf(nTotalScore));
				nCurrentRowIndex++;
			}

			nGrandTotalScore += nTotalScore;

			// the grand total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss2(String.valueOf(nGrandTotalScore));
				nCurrentRowIndex++;
			}

			ix++;
		}

		// third record
		if (ix < endIndex)
		{
			nTotalScore = 0;
			nGrandTotalScore = 0;

			row = form.gridMNA().getRows().get(INDEX_ROW_DT);
			row.setColAss3(mnas.get(ix).getDateTimeInitiated() != null ? mnas.get(ix).getDateTimeInitiated().toString() : "");
			row = form.gridMNA().getRows().get(INDEX_ROW_HCP);
			row.setColAss3(mnas.get(ix).getHcpInitiated() != null ? mnas.get(ix).getHcpInitiated().getName().toString() : "");

			// screening
			nCurrentRowIndex = INDEX_ROW_HCP + 1;
			for (int i = 0; i < form.getGlobalContext().COE.getMNAAssessments().size(); i++)
			{
				if ((form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().length() > 0 && (form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) >= 'A' && (form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) <= 'F')
				{
					nCurrentRowIndex++;
					row = form.gridMNA().getRows().get(nCurrentRowIndex);

					nScore = getScoreByAssessmentName(form.gridMNA().getRows().get(nCurrentRowIndex).getMNA(), mnas.get(ix).getAssessmentDetails());
					nTotalScore += nScore;
					row.setColAss3(String.valueOf(nScore));
				}
			}
			// the total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss3(String.valueOf(nTotalScore));
				nCurrentRowIndex++;
			}

			nGrandTotalScore += nTotalScore;
			nCurrentRowIndex++;

			// assessment
			nTotalScore = 0;
			for (int i = 0; i < form.getGlobalContext().COE.getMNAAssessments().size(); i++)
			{
				if ((form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) >= 'G')
				{
					nCurrentRowIndex++;
					row = form.gridMNA().getRows().get(nCurrentRowIndex);

					nScore = getScoreByAssessmentName(form.gridMNA().getRows().get(nCurrentRowIndex).getMNA(), mnas.get(ix).getAssessmentDetails());
					nTotalScore += nScore;
					row.setColAss3(String.valueOf(nScore));
				}
			}
			// the total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss3(String.valueOf(nTotalScore));
				nCurrentRowIndex++;
			}

			nGrandTotalScore += nTotalScore;

			// the grand total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss3(String.valueOf(nGrandTotalScore));
				nCurrentRowIndex++;
			}

			ix++;
		}

		// fourth record
		if (ix < endIndex)
		{
			nTotalScore = 0;
			nGrandTotalScore = 0;

			row = form.gridMNA().getRows().get(INDEX_ROW_DT);
			row.setColAss4(mnas.get(ix).getDateTimeInitiated() != null ? mnas.get(ix).getDateTimeInitiated().toString() : "");
			row = form.gridMNA().getRows().get(INDEX_ROW_HCP);
			row.setColAss4(mnas.get(ix).getHcpInitiated() != null ? mnas.get(ix).getHcpInitiated().getName().toString() : "");

			// screening
			nCurrentRowIndex = INDEX_ROW_HCP + 1;
			for (int i = 0; i < form.getGlobalContext().COE.getMNAAssessments().size(); i++)
			{
				if ((form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().length() > 0 && (form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) >= 'A' && (form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) <= 'F')
				{
					nCurrentRowIndex++;
					row = form.gridMNA().getRows().get(nCurrentRowIndex);

					nScore = getScoreByAssessmentName(form.gridMNA().getRows().get(nCurrentRowIndex).getMNA(), mnas.get(ix).getAssessmentDetails());
					nTotalScore += nScore;
					row.setColAss4(String.valueOf(nScore));
				}
			}
			// the total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss4(String.valueOf(nTotalScore));
				nCurrentRowIndex++;
			}

			nGrandTotalScore += nTotalScore;
			nCurrentRowIndex++;

			// assessment
			nTotalScore = 0;
			for (int i = 0; i < form.getGlobalContext().COE.getMNAAssessments().size(); i++)
			{
				if ((form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) >= 'G')
				{
					nCurrentRowIndex++;
					row = form.gridMNA().getRows().get(nCurrentRowIndex);

					nScore = getScoreByAssessmentName(form.gridMNA().getRows().get(nCurrentRowIndex).getMNA(), mnas.get(ix).getAssessmentDetails());
					nTotalScore += nScore;
					row.setColAss4(String.valueOf(nScore));
				}
			}
			// the total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss4(String.valueOf(nTotalScore));
				nCurrentRowIndex++;
			}

			nGrandTotalScore += nTotalScore;

			// the grand total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss4(String.valueOf(nGrandTotalScore));
				nCurrentRowIndex++;
			}

			ix++;
		}

		// fifth record
		if (ix < endIndex)
		{
			nTotalScore = 0;
			nGrandTotalScore = 0;

			row = form.gridMNA().getRows().get(INDEX_ROW_DT);
			row.setColAss5(mnas.get(ix).getDateTimeInitiated() != null ? mnas.get(ix).getDateTimeInitiated().toString() : "");
			row = form.gridMNA().getRows().get(INDEX_ROW_HCP);
			row.setColAss5(mnas.get(ix).getHcpInitiated() != null ? mnas.get(ix).getHcpInitiated().getName().toString() : "");

			// screening
			nCurrentRowIndex = INDEX_ROW_HCP + 1;
			for (int i = 0; i < form.getGlobalContext().COE.getMNAAssessments().size(); i++)
			{
				if ((form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().length() > 0 && (form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) >= 'A' && (form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) <= 'F')
				{
					nCurrentRowIndex++;
					row = form.gridMNA().getRows().get(nCurrentRowIndex);

					nScore = getScoreByAssessmentName(form.gridMNA().getRows().get(nCurrentRowIndex).getMNA(), mnas.get(ix).getAssessmentDetails());
					nTotalScore += nScore;
					row.setColAss5(String.valueOf(nScore));
				}
			}
			// the total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss5(String.valueOf(nTotalScore));
				nCurrentRowIndex++;
			}

			nGrandTotalScore += nTotalScore;
			nCurrentRowIndex++;

			// assessment
			nTotalScore = 0;
			for (int i = 0; i < form.getGlobalContext().COE.getMNAAssessments().size(); i++)
			{
				if ((form.getGlobalContext().COE.getMNAAssessments().get(i)).getName().charAt(0) >= 'G')
				{
					nCurrentRowIndex++;
					row = form.gridMNA().getRows().get(nCurrentRowIndex);

					nScore = getScoreByAssessmentName(form.gridMNA().getRows().get(nCurrentRowIndex).getMNA(), mnas.get(ix).getAssessmentDetails());
					nTotalScore += nScore;
					row.setColAss5(String.valueOf(nScore));
				}
			}
			// the total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss5(String.valueOf(nTotalScore));
				nCurrentRowIndex++;
			}

			nGrandTotalScore += nTotalScore;

			// the grand total
			if (nCurrentRowIndex >= 0)
			{
				row = form.gridMNA().getRows().get(nCurrentRowIndex + 1);
				row.setColAss5(String.valueOf(nGrandTotalScore));
				nCurrentRowIndex++;
			}

			ix++;
		}

		// enable/disable Prev/Next buttons
		if (form.getLocalContext().getGroupIndex().intValue() > 0)
			form.btnPrev().setEnabled(true);
		else
			form.btnPrev().setEnabled(false);

		if (endIndex >= mnas.size())
			form.btnNext().setEnabled(false);
		else
			form.btnNext().setEnabled(true);

		String legend;

		if (mnas.size() > 0)
			legend = "<B><CENTER>Rec. " + String.valueOf(form.getLocalContext().getGroupIndex().intValue() * NUM_COLS + 1) + "-" + String.valueOf(endIndex) + " of " + String.valueOf(mnas.size()) + "</CENTER></B>";
		else
			legend = "<B><CENTER>No records</CENTER></B>";

		form.htmLegend().setHTML(legend);
	}

	private boolean getMNAAssessment()
	{
		// Document doc = null;

		RiskAssessmentCollection risks = form.getGlobalContext().COE.getMNAAssessments();

		if (risks == null)
		{
			try
			{
				risks = ims.nursing.helper.RiskAssessmentHelper.getMNARisks();
				form.getGlobalContext().COE.setMNAAssessments(risks);
			}
			catch (PresentationLogicException e)
			{
				engine.showMessage(e.getMessage());
				return false;
			}
		}

		// Date/time
		GenForm.gridMNARow row = form.gridMNA().getRows().newRow();
		row.setMNA("Date/Time");
		row.setBold(true);
		row.setBackColor(Color.Beige);
		row = form.gridMNA().getRows().newRow();
		row.setMNA("HCP");
		row.setBold(true);
		row.setBackColor(Color.Beige);
		// Screening
		row = form.gridMNA().getRows().newRow();
		row.setMNA("Screening");
		row.setBold(true);
		row.setBackColor(Color.Bisque);
		row.setTooltipForMNA("<b>Screening score</b> (subtotal max. 14 points)<br>12 points or greater Normal &#45; not at risk &#45; no need to complete assessment <br> 11 points or below Possible malnutrition &#45; continue assessment");

		for (int i = 0; i < risks.size(); i++)
		{
			RiskAssessment risk = risks.get(i);

			if (risk.getName().charAt(0) >= 'A' && risk.getName().charAt(0) <= 'F')
			{
				row = form.gridMNA().getRows().newRow();
				row.setMNA(risk.getDescription());
				row.setTooltipForMNA(risk.getDescription());
			}
		}

		row = form.gridMNA().getRows().newRow();
		row.setMNA("TOTAL");
		row.setTooltipForMNA("<b>Screening score</b> (subtotal max. 14 points)<br>12 points or greater Normal &#45; not at risk &#45; no need to complete assessment <br> 11 points or below Possible malnutrition &#45; continue assessment");
		row.setBold(true);
		row.setBackColor(Color.Beige);

		// Assessment
		row = form.gridMNA().getRows().newRow();
		row.setMNA("Assessment");
		row.setTooltipForMNA("Assessment(max. 16 points)");
		row.setBold(true);
		row.setBackColor(Color.Bisque);

		for (int i = 0; i < risks.size(); i++)
		{
			RiskAssessment risk = risks.get(i);

			if (risk.getName().charAt(0) >= 'G')
			{
				row = form.gridMNA().getRows().newRow();
				row.setMNA(risk.getDescription());
				row.setTooltipForMNA(risk.getDescription());
			}
		}

		row = form.gridMNA().getRows().newRow();
		row.setMNA("TOTAL");
		row.setTooltipForMNA("Assessment(max. 16 points)");
		row.setBold(true);
		row.setBackColor(Color.Beige);

		// grand total
		row = form.gridMNA().getRows().newRow();
		row.setMNA("TOTAL Assessment + Screening score");
		row.setTooltipForMNA("Total Assessment (max. 30 points)<br> <b>Malnutrition Indicator Score </b><br>17 to 23.5 points at risk of malnutrition <br>Less than 17 points malnourished");
		row.setBold(true);
		row.setBackColor(Color.Beige);

		return true;
	}

	protected void onFormDialogClosed(ims.framework.FormName formName, ims.framework.enumerations.DialogResult result) throws ims.framework.exceptions.PresentationLogicException
	{
		if (result.equals(DialogResult.OK) && formName.equals(form.getForms().Core.Reports))
		{
			printReport();
		}
		else if (result.equals(DialogResult.OK) && formName.equals(form.getForms().Nursing.MNAInput))
		{
			filterValueChanged();
		}
	}

	protected void onBtnNextClick() throws ims.framework.exceptions.PresentationLogicException
	{
		form.getLocalContext().setGroupIndex(new Integer(form.getLocalContext().getGroupIndex().intValue() + 1));
		populateScreenFromData();
	}

	protected void onBtnPrevClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (form.getLocalContext().getGroupIndex().intValue() == 0)
			return;

		form.getLocalContext().setGroupIndex(new Integer(form.getLocalContext().getGroupIndex().intValue() - 1));
		populateScreenFromData();
	}

	protected void onCmbFilterValueChanged() throws ims.framework.exceptions.PresentationLogicException
	{
		filterValueChanged();
		form.getGlobalContext().Nursing.setMNAViewSearchCriteria(getSearchCriteria());//WDEV-19389 
	}

	protected void onBtnUpdateClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (form.cmbRecordToUpdate().getValue() == null)
		{
			engine.showMessage("Please select a record to update !");
			return;
		}
		form.getGlobalContext().COE.setAddNew(Boolean.FALSE);
		form.getGlobalContext().COE.setMNARecord(form.cmbRecordToUpdate().getValue());
		engine.open(form.getForms().Nursing.MNAInput, "Update assessment");
	}

	protected void onBtnNewClick() throws ims.framework.exceptions.PresentationLogicException
	{
		form.getGlobalContext().COE.setAddNew(Boolean.TRUE);
		form.getGlobalContext().COE.setMNARecord(null);
		engine.open(form.getForms().Nursing.MNAInput, "Add new MNA assessment");
	}

	private float getScoreByAssessmentName(String colFall, MiniNutritionalAssessmentDetailsCollection assessmentDetails)
	{
		float nScore = 0;

		RiskAssessment risk;
		RiskDescription descr;

		for (int i = 0; i < form.getGlobalContext().COE.getMNAAssessments().size(); i++)
		{
			risk = form.getGlobalContext().COE.getMNAAssessments().get(i);

			if (risk.getDescription().equalsIgnoreCase(colFall))
			{
				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))
						{
							nScore += Float.valueOf(descr.getScore()).floatValue();
						}
					}
				}

				if (risk.getName().length() > 0 && risk.getName().charAt(0) == 'K')
				{
					if (nScore <= 1)
						nScore = 0;
					else if (nScore == 2)
						nScore = (float) 0.5;
					else if (nScore == 3)
						nScore = 1;
				}

				break;
			}
		}

		return nScore;
	}

	private void clearGridRows()
	{
		for (int i = 0; i < form.gridMNA().getRows().size(); i++)
		{
			GenForm.gridMNARow row = form.gridMNA().getRows().get(i);

			row.setColAss1("");
			row.setColAss2("");
			row.setColAss3("");
			row.setColAss4("");
			row.setColAss5("");
			row.setValue(null);
		}
	}

	protected void onCmbRecordToUpdateValueChanged() throws PresentationLogicException
	{
		if (form.cmbRecordToUpdate().getValue() == null)
		{
		
			form.btnUpdate().setEnabled(false);
			return;
		
		
		}
		
		form.btnUpdate().setEnabled(true);
		
		
		try
		{
			if (domain.isPartOfEatingAssessment(form.cmbRecordToUpdate().getValue()) == Boolean.TRUE)
				form.btnUpdate().setText("View");
			else
				form.btnUpdate().setText("Edit");
		}
		catch (DomainInterfaceException e)
		{
			engine.showMessage("Error retrieving eating assessment record: " + e.getMessage());
			return;
		}
	}

	protected void onBtnPrintClick() throws PresentationLogicException
	{
		// form.getGlobalContext().COE.setMNARecord(form.cmbRecordToUpdate().getValue());
		engine.open(form.getForms().Core.Reports);
	}

	@SuppressWarnings("unchecked")
	private void printReport()
	{
		MiniNutritionalAssessment mna = form.cmbRecordToUpdate().getValue();

		if (mna == null)
			return;

		int nCol = -1;
		ArrayList ids = form.getLocalContext().getMNAIds();

		for (int i = 0; i < ids.size(); i++)
		{
			if (mna.getID_Assessment().equals(ids.get(i)))
			{
				nCol = i;
				break;
			}
		}

		if (nCol == -1)
		{
			engine.showMessage("Internal error, could not find the MNA record to print !");
			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("MNA_ID", mna.getID_Assessment(), Integer.class));
		if (ps != null)
		{
			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));
		}

		addExtraSeeds(nCol, client, mna);

		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);
	}

	private void addExtraSeeds(int nCol, QueryBuilderClient client, MiniNutritionalAssessment mna)
	{
		float fScreeningScore = 0;
		float fTotalScore = 0;

		if (nCol == 0)
		{
			client.addSeed(new SeedValue("A", form.gridMNA().getRows().get(3).getColAss1(), String.class));
			client.addSeed(new SeedValue("B", form.gridMNA().getRows().get(4).getColAss1(), String.class));
			client.addSeed(new SeedValue("C", form.gridMNA().getRows().get(5).getColAss1(), String.class));
			client.addSeed(new SeedValue("D", form.gridMNA().getRows().get(6).getColAss1(), String.class));
			client.addSeed(new SeedValue("E", form.gridMNA().getRows().get(7).getColAss1(), String.class));
			client.addSeed(new SeedValue("F", form.gridMNA().getRows().get(8).getColAss1(), String.class));
			client.addSeed(new SeedValue("SCREENING_SCORE", form.gridMNA().getRows().get(9).getColAss1(), String.class));
			fScreeningScore = Float.parseFloat(form.gridMNA().getRows().get(9).getColAss1());
			if (fScreeningScore < 12)
			{
				client.addSeed(new SeedValue("G", form.gridMNA().getRows().get(11).getColAss1(), String.class));
				client.addSeed(new SeedValue("H", form.gridMNA().getRows().get(12).getColAss1(), String.class));
				client.addSeed(new SeedValue("I", form.gridMNA().getRows().get(13).getColAss1(), String.class));
				client.addSeed(new SeedValue("J", form.gridMNA().getRows().get(14).getColAss1(), String.class));
				client.addSeed(new SeedValue("K", form.gridMNA().getRows().get(15).getColAss1(), String.class));
				client.addSeed(new SeedValue("L", form.gridMNA().getRows().get(16).getColAss1(), String.class));
				client.addSeed(new SeedValue("M", form.gridMNA().getRows().get(17).getColAss1(), String.class));
				client.addSeed(new SeedValue("N", form.gridMNA().getRows().get(18).getColAss1(), String.class));
				client.addSeed(new SeedValue("O", form.gridMNA().getRows().get(19).getColAss1(), String.class));
				client.addSeed(new SeedValue("P", form.gridMNA().getRows().get(20).getColAss1(), String.class));
				client.addSeed(new SeedValue("Q", form.gridMNA().getRows().get(21).getColAss1(), String.class));
				client.addSeed(new SeedValue("R", form.gridMNA().getRows().get(22).getColAss1(), String.class));
				client.addSeed(new SeedValue("ASSESSMENT_SCORE", form.gridMNA().getRows().get(23).getColAss1(), String.class));

				fTotalScore = Float.parseFloat(form.gridMNA().getRows().get(24).getColAss1());
				if (fTotalScore < 17)
				{
					client.addSeed(new SeedValue("MIS1", "", String.class));
					client.addSeed(new SeedValue("MIS2", "Yes", String.class));
				}
				else if (fTotalScore >= 17 && fTotalScore <= 23.5)
				{
					client.addSeed(new SeedValue("MIS1", "Yes", String.class));
					client.addSeed(new SeedValue("MIS2", "", String.class));
				}
				else
				{
					client.addSeed(new SeedValue("MIS1", "", String.class));
					client.addSeed(new SeedValue("MIS2", "", String.class));
				}
			}
			else
				addNullAssessmentSeeds(client);

			client.addSeed(new SeedValue("TOTAL_SCORE", form.gridMNA().getRows().get(24).getColAss1(), String.class));
		}
		else if (nCol == 1)
		{
			client.addSeed(new SeedValue("A", form.gridMNA().getRows().get(3).getColAss2(), String.class));
			client.addSeed(new SeedValue("B", form.gridMNA().getRows().get(4).getColAss2(), String.class));
			client.addSeed(new SeedValue("C", form.gridMNA().getRows().get(5).getColAss2(), String.class));
			client.addSeed(new SeedValue("D", form.gridMNA().getRows().get(6).getColAss2(), String.class));
			client.addSeed(new SeedValue("E", form.gridMNA().getRows().get(7).getColAss2(), String.class));
			client.addSeed(new SeedValue("F", form.gridMNA().getRows().get(8).getColAss2(), String.class));
			client.addSeed(new SeedValue("SCREENING_SCORE", form.gridMNA().getRows().get(9).getColAss2(), String.class));
			fScreeningScore = Float.parseFloat(form.gridMNA().getRows().get(9).getColAss2());
			if (fScreeningScore < 12)
			{
				client.addSeed(new SeedValue("G", form.gridMNA().getRows().get(11).getColAss2(), String.class));
				client.addSeed(new SeedValue("H", form.gridMNA().getRows().get(12).getColAss2(), String.class));
				client.addSeed(new SeedValue("I", form.gridMNA().getRows().get(13).getColAss2(), String.class));
				client.addSeed(new SeedValue("J", form.gridMNA().getRows().get(14).getColAss2(), String.class));
				client.addSeed(new SeedValue("K", form.gridMNA().getRows().get(15).getColAss2(), String.class));
				client.addSeed(new SeedValue("L", form.gridMNA().getRows().get(16).getColAss2(), String.class));
				client.addSeed(new SeedValue("M", form.gridMNA().getRows().get(17).getColAss2(), String.class));
				client.addSeed(new SeedValue("N", form.gridMNA().getRows().get(18).getColAss2(), String.class));
				client.addSeed(new SeedValue("O", form.gridMNA().getRows().get(19).getColAss2(), String.class));
				client.addSeed(new SeedValue("P", form.gridMNA().getRows().get(20).getColAss2(), String.class));
				client.addSeed(new SeedValue("Q", form.gridMNA().getRows().get(21).getColAss2(), String.class));
				client.addSeed(new SeedValue("R", form.gridMNA().getRows().get(22).getColAss2(), String.class));
				client.addSeed(new SeedValue("ASSESSMENT_SCORE", form.gridMNA().getRows().get(23).getColAss2(), String.class));

				fTotalScore = Float.parseFloat(form.gridMNA().getRows().get(24).getColAss2());
				if (fTotalScore < 17)
				{
					client.addSeed(new SeedValue("MIS1", "", String.class));
					client.addSeed(new SeedValue("MIS2", "Yes", String.class));
				}
				else if (fTotalScore >= 17 && fTotalScore <= 23.5)
				{
					client.addSeed(new SeedValue("MIS1", "Yes", String.class));
					client.addSeed(new SeedValue("MIS2", "", String.class));
				}
				else
				{
					client.addSeed(new SeedValue("MIS1", "", String.class));
					client.addSeed(new SeedValue("MIS2", "", String.class));
				}
			}
			else
				addNullAssessmentSeeds(client);

			client.addSeed(new SeedValue("TOTAL_SCORE", form.gridMNA().getRows().get(24).getColAss2(), String.class));
		}
		else if (nCol == 2)
		{
			client.addSeed(new SeedValue("A", form.gridMNA().getRows().get(3).getColAss3(), String.class));
			client.addSeed(new SeedValue("B", form.gridMNA().getRows().get(4).getColAss3(), String.class));
			client.addSeed(new SeedValue("C", form.gridMNA().getRows().get(5).getColAss3(), String.class));
			client.addSeed(new SeedValue("D", form.gridMNA().getRows().get(6).getColAss3(), String.class));
			client.addSeed(new SeedValue("E", form.gridMNA().getRows().get(7).getColAss3(), String.class));
			client.addSeed(new SeedValue("F", form.gridMNA().getRows().get(8).getColAss3(), String.class));
			client.addSeed(new SeedValue("SCREENING_SCORE", form.gridMNA().getRows().get(9).getColAss3(), String.class));
			fScreeningScore = Float.parseFloat(form.gridMNA().getRows().get(9).getColAss3());
			if (fScreeningScore < 12)
			{
				client.addSeed(new SeedValue("G", form.gridMNA().getRows().get(11).getColAss3(), String.class));
				client.addSeed(new SeedValue("H", form.gridMNA().getRows().get(12).getColAss3(), String.class));
				client.addSeed(new SeedValue("I", form.gridMNA().getRows().get(13).getColAss3(), String.class));
				client.addSeed(new SeedValue("J", form.gridMNA().getRows().get(14).getColAss3(), String.class));
				client.addSeed(new SeedValue("K", form.gridMNA().getRows().get(15).getColAss3(), String.class));
				client.addSeed(new SeedValue("L", form.gridMNA().getRows().get(16).getColAss3(), String.class));
				client.addSeed(new SeedValue("M", form.gridMNA().getRows().get(17).getColAss3(), String.class));
				client.addSeed(new SeedValue("N", form.gridMNA().getRows().get(18).getColAss3(), String.class));
				client.addSeed(new SeedValue("O", form.gridMNA().getRows().get(19).getColAss3(), String.class));
				client.addSeed(new SeedValue("P", form.gridMNA().getRows().get(20).getColAss3(), String.class));
				client.addSeed(new SeedValue("Q", form.gridMNA().getRows().get(21).getColAss3(), String.class));
				client.addSeed(new SeedValue("R", form.gridMNA().getRows().get(22).getColAss3(), String.class));
				client.addSeed(new SeedValue("ASSESSMENT_SCORE", form.gridMNA().getRows().get(23).getColAss3(), String.class));

				fTotalScore = Float.parseFloat(form.gridMNA().getRows().get(24).getColAss3());
				if (fTotalScore < 17)
				{
					client.addSeed(new SeedValue("MIS1", "", String.class));
					client.addSeed(new SeedValue("MIS2", "Yes", String.class));
				}
				else if (fTotalScore >= 17 && fTotalScore <= 23.5)
				{
					client.addSeed(new SeedValue("MIS1", "Yes", String.class));
					client.addSeed(new SeedValue("MIS2", "", String.class));
				}
				else
				{
					client.addSeed(new SeedValue("MIS1", "", String.class));
					client.addSeed(new SeedValue("MIS2", "", String.class));
				}
			}
			else
				addNullAssessmentSeeds(client);

			client.addSeed(new SeedValue("TOTAL_SCORE", form.gridMNA().getRows().get(24).getColAss3(), String.class));
		}
		else if (nCol == 3)
		{
			client.addSeed(new SeedValue("A", form.gridMNA().getRows().get(3).getColAss4(), String.class));
			client.addSeed(new SeedValue("B", form.gridMNA().getRows().get(4).getColAss4(), String.class));
			client.addSeed(new SeedValue("C", form.gridMNA().getRows().get(5).getColAss4(), String.class));
			client.addSeed(new SeedValue("D", form.gridMNA().getRows().get(6).getColAss4(), String.class));
			client.addSeed(new SeedValue("E", form.gridMNA().getRows().get(7).getColAss4(), String.class));
			client.addSeed(new SeedValue("F", form.gridMNA().getRows().get(8).getColAss4(), String.class));
			client.addSeed(new SeedValue("SCREENING_SCORE", form.gridMNA().getRows().get(9).getColAss4(), String.class));
			fScreeningScore = Float.parseFloat(form.gridMNA().getRows().get(9).getColAss4());
			if (fScreeningScore < 12)
			{
				client.addSeed(new SeedValue("G", form.gridMNA().getRows().get(11).getColAss4(), String.class));
				client.addSeed(new SeedValue("H", form.gridMNA().getRows().get(12).getColAss4(), String.class));
				client.addSeed(new SeedValue("I", form.gridMNA().getRows().get(13).getColAss4(), String.class));
				client.addSeed(new SeedValue("J", form.gridMNA().getRows().get(14).getColAss4(), String.class));
				client.addSeed(new SeedValue("K", form.gridMNA().getRows().get(15).getColAss4(), String.class));
				client.addSeed(new SeedValue("L", form.gridMNA().getRows().get(16).getColAss4(), String.class));
				client.addSeed(new SeedValue("M", form.gridMNA().getRows().get(17).getColAss4(), String.class));
				client.addSeed(new SeedValue("N", form.gridMNA().getRows().get(18).getColAss4(), String.class));
				client.addSeed(new SeedValue("O", form.gridMNA().getRows().get(19).getColAss4(), String.class));
				client.addSeed(new SeedValue("P", form.gridMNA().getRows().get(20).getColAss4(), String.class));
				client.addSeed(new SeedValue("Q", form.gridMNA().getRows().get(21).getColAss4(), String.class));
				client.addSeed(new SeedValue("R", form.gridMNA().getRows().get(22).getColAss4(), String.class));
				client.addSeed(new SeedValue("ASSESSMENT_SCORE", form.gridMNA().getRows().get(23).getColAss4(), String.class));

				fTotalScore = Float.parseFloat(form.gridMNA().getRows().get(24).getColAss4());
				if (fTotalScore < 17)
				{
					client.addSeed(new SeedValue("MIS1", "", String.class));
					client.addSeed(new SeedValue("MIS2", "Yes", String.class));
				}
				else if (fTotalScore >= 17 && fTotalScore <= 23.5)
				{
					client.addSeed(new SeedValue("MIS1", "Yes", String.class));
					client.addSeed(new SeedValue("MIS2", "", String.class));
				}
				else
				{
					client.addSeed(new SeedValue("MIS1", "", String.class));
					client.addSeed(new SeedValue("MIS2", "", String.class));
				}
			}
			else
				addNullAssessmentSeeds(client);

			client.addSeed(new SeedValue("TOTAL_SCORE", form.gridMNA().getRows().get(24).getColAss4(), String.class));
		}
		else if (nCol == 4)
		{
			client.addSeed(new SeedValue("A", form.gridMNA().getRows().get(3).getColAss5(), String.class));
			client.addSeed(new SeedValue("B", form.gridMNA().getRows().get(4).getColAss5(), String.class));
			client.addSeed(new SeedValue("C", form.gridMNA().getRows().get(5).getColAss5(), String.class));
			client.addSeed(new SeedValue("D", form.gridMNA().getRows().get(6).getColAss5(), String.class));
			client.addSeed(new SeedValue("E", form.gridMNA().getRows().get(7).getColAss5(), String.class));
			client.addSeed(new SeedValue("F", form.gridMNA().getRows().get(8).getColAss5(), String.class));
			client.addSeed(new SeedValue("SCREENING_SCORE", form.gridMNA().getRows().get(9).getColAss5(), String.class));
			fScreeningScore = Float.parseFloat(form.gridMNA().getRows().get(9).getColAss5());
			if (fScreeningScore < 12)
			{
				client.addSeed(new SeedValue("G", form.gridMNA().getRows().get(11).getColAss5(), String.class));
				client.addSeed(new SeedValue("H", form.gridMNA().getRows().get(12).getColAss5(), String.class));
				client.addSeed(new SeedValue("I", form.gridMNA().getRows().get(13).getColAss5(), String.class));
				client.addSeed(new SeedValue("J", form.gridMNA().getRows().get(14).getColAss5(), String.class));
				client.addSeed(new SeedValue("K", form.gridMNA().getRows().get(15).getColAss5(), String.class));
				client.addSeed(new SeedValue("L", form.gridMNA().getRows().get(16).getColAss5(), String.class));
				client.addSeed(new SeedValue("M", form.gridMNA().getRows().get(17).getColAss5(), String.class));
				client.addSeed(new SeedValue("N", form.gridMNA().getRows().get(18).getColAss5(), String.class));
				client.addSeed(new SeedValue("O", form.gridMNA().getRows().get(19).getColAss5(), String.class));
				client.addSeed(new SeedValue("P", form.gridMNA().getRows().get(20).getColAss5(), String.class));
				client.addSeed(new SeedValue("Q", form.gridMNA().getRows().get(21).getColAss5(), String.class));
				client.addSeed(new SeedValue("R", form.gridMNA().getRows().get(22).getColAss5(), String.class));
				client.addSeed(new SeedValue("ASSESSMENT_SCORE", form.gridMNA().getRows().get(23).getColAss5(), String.class));

				fTotalScore = Float.parseFloat(form.gridMNA().getRows().get(24).getColAss5());
				if (fTotalScore < 17)
				{
					client.addSeed(new SeedValue("MIS1", "", String.class));
					client.addSeed(new SeedValue("MIS2", "Yes", String.class));
				}
				else if (fTotalScore >= 17 && fTotalScore <= 23.5)
				{
					client.addSeed(new SeedValue("MIS1", "Yes", String.class));
					client.addSeed(new SeedValue("MIS2", "", String.class));
				}
				else
				{
					client.addSeed(new SeedValue("MIS1", "", String.class));
					client.addSeed(new SeedValue("MIS2", "", String.class));
				}
			}
			else
				addNullAssessmentSeeds(client);

			client.addSeed(new SeedValue("TOTAL_SCORE", form.gridMNA().getRows().get(24).getColAss5(), String.class));
		}

		if (mna.getAssessmentDetailsIsNotNull())
		{
			for (int i = 0; i < mna.getAssessmentDetails().size(); i++)
			{
				MiniNutritionalAssessmentDetails detail = mna.getAssessmentDetails().get(i);

				SeedValue seed = null;
				
				if (detail.getAssessment().equals(K1))
				{
					seed = getSeedByName(client, "K1");
					
					if(seed == null)
						client.addSeed(new SeedValue("K1", (detail.getSelectIsNotNull() ? (detail.getSelect().equals(Boolean.TRUE) ? "Yes" : "") : ""), String.class));
					else
						seed.setValue((detail.getSelectIsNotNull() ? (detail.getSelect().equals(Boolean.TRUE) ? "Yes" : "") : ""));
				}
				if (detail.getAssessment().equals(K2))
				{
					seed = getSeedByName(client, "K2");
					
					if(seed == null)
						client.addSeed(new SeedValue("K2", (detail.getSelectIsNotNull() ? (detail.getSelect().equals(Boolean.TRUE) ? "Yes" : "") : ""), String.class));
					else
						seed.setValue((detail.getSelectIsNotNull() ? (detail.getSelect().equals(Boolean.TRUE) ? "Yes" : "") : ""));
				}
				if (detail.getAssessment().equals(K3))
				{
					seed = getSeedByName(client, "K3");
					
					if(seed == null)
						client.addSeed(new SeedValue("K3", (detail.getSelectIsNotNull() ? (detail.getSelect().equals(Boolean.TRUE) ? "Yes" : "") : ""), String.class));
					else
						seed.setValue((detail.getSelectIsNotNull() ? (detail.getSelect().equals(Boolean.TRUE) ? "Yes" : "") : ""));
				}
			}
		}
	}

	private SeedValue getSeedByName(QueryBuilderClient client, String name)
	{
		if(client == null || name == null)
			return null;
		
		for (int i = 0; i < client.getSeeds().size(); i++)
		{
			SeedValue seed = (SeedValue) client.getSeeds().get(i);
			
			if(name.equalsIgnoreCase(seed.getName()))
				return seed;
		}
		return null;
	}

	private void addNullAssessmentSeeds(QueryBuilderClient client)
	{
		client.addSeed(new SeedValue("G", "", String.class));
		client.addSeed(new SeedValue("H", "", String.class));
		client.addSeed(new SeedValue("I", "", String.class));
		client.addSeed(new SeedValue("J", "", String.class));
		client.addSeed(new SeedValue("K", "", String.class));
		client.addSeed(new SeedValue("L", "", String.class));
		client.addSeed(new SeedValue("M", "", String.class));
		client.addSeed(new SeedValue("N", "", String.class));
		client.addSeed(new SeedValue("O", "", String.class));
		client.addSeed(new SeedValue("P", "", String.class));
		client.addSeed(new SeedValue("Q", "", String.class));
		client.addSeed(new SeedValue("R", "", String.class));

		client.addSeed(new SeedValue("K1", "", String.class));
		client.addSeed(new SeedValue("K2", "", String.class));
		client.addSeed(new SeedValue("K3", "", String.class));

		client.addSeed(new SeedValue("MIS1", "", String.class));
		client.addSeed(new SeedValue("MIS2", "", String.class));
	}

	static private final Integer	K1	= new Integer(29);
	static private final Integer	K2	= new Integer(30);
	static private final Integer	K3	= new Integer(31);
}
