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

package ims.nursing.forms.mrsahistory;

import ims.framework.utils.Date;
import ims.nursing.vo.MRSAAssessmentVo;
import ims.nursing.vo.MRSAAssessmentVoCollection;
import ims.nursing.vo.MRSADailyTreatmentVo;
import ims.nursing.vo.MRSADailyTreatmentVoCollection;
import ims.nursing.vo.MRSATreatmentDetailsVo;
import ims.nursing.vo.MRSATreatmentDetailsVoCollection;
import ims.nursing.vo.MRSATreatmentVo;
import ims.core.vo.CareContextShortVo;
import ims.core.vo.CareContextShortVoCollection;
import ims.framework.enumerations.FormMode;
import ims.framework.exceptions.PresentationLogicException;

public class Logic extends BaseLogic
{
	protected void onFormOpen() throws ims.framework.exceptions.FormOpenException
	{
		initialize();
		open();
	}
	private void initialize()
	{
		CareContextShortVoCollection voCareContextList = domain.listMRSACareContexts(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++)
			{
				form.recbrAssessment().newRow(voCareContextList.get(i),voCareContextList.get(i).getStartDateTime().getDate().toString() + " " + voCareContextList.get(i).getStartDateTime().getTime().toString());
			}
		}		
	}
	private void open()
	{
		form.setMode(FormMode.VIEW);
		form.recbrAssessment().setValue(form.getGlobalContext().Core.getCurrentCareContext());
		populateInstance(form.getGlobalContext().Core.getCurrentCareContext());
		
	}
	private void populateInstance(CareContextShortVo voCareContext)
	{
		MRSAAssessmentVoCollection voCollMRSA = domain.listMRSA(voCareContext);
		populateGrid(voCollMRSA);
		form.grdAssessment().expandAll();		
	}
	private void populateGrid(MRSAAssessmentVoCollection voCollMRSA) 
	{
		form.grdAssessment().getRows().clear();
		
		if(voCollMRSA == null)
			return;
		
		MRSAAssessmentVo voMRSA = null;
		MRSATreatmentVo voTreatment = null;
		
		GenForm.grdAssessmentRow row = null;

		for(int i=0; i<voCollMRSA.size(); i++)
		{
			voMRSA = voCollMRSA.get(i);
			row = form.grdAssessment().getRows().newRow();	
			
			//set parent date
			if(voMRSA.getDateScreening() != null)
				row.setColStartDateTreatment(voMRSA.getDateScreening().toString());
			
			Date[] arrDates = null;
			if(voMRSA.getTreatment() != null)
			{
				if(voMRSA.getTreatment().getStartDate() != null)
				{
					arrDates = loadDateArray(voMRSA.getTreatment().getStartDate());
				}
			}
			MRSATreatmentDetailsVoCollection voTreatmentDetailsColl = null;
			voTreatment = voMRSA.getTreatment();
			if(voTreatment != null)
			{
				voTreatmentDetailsColl = voTreatment.getTreatmentDetails().sort();
				if(voTreatmentDetailsColl != null && voTreatmentDetailsColl.size()>0)
				{
					GenForm.grdAssessmentRow cRow;
					for(int z=0;z<voTreatmentDetailsColl.size();z++)
					{
						cRow = row.getRows().newRow();
						if(voTreatment.getRescreenDate() != null)
							cRow.setColRescreenDate(voTreatment.getRescreenDate().toString());
						
						MRSATreatmentDetailsVo voMRSATreatmentDetail = new MRSATreatmentDetailsVo();
						voMRSATreatmentDetail = voTreatmentDetailsColl.get(z);
						if(voMRSATreatmentDetail.getTreatment() != null)
							cRow.setColStartDateTreatment(voMRSATreatmentDetail.getTreatment().getText());
		
						MRSADailyTreatmentVoCollection voDailyColl = new MRSADailyTreatmentVoCollection();
						voDailyColl = voMRSATreatmentDetail.getDailyTreatment();
						
						for(int p=0;p<voDailyColl.size();p++)
						{
							MRSADailyTreatmentVo voDailyTreatment = new MRSADailyTreatmentVo();
							voDailyTreatment = voDailyColl.get(p);
							
								if(voDailyTreatment.getHCPIsNotNull() && arrDates != null)
								{
									if(voDailyTreatment.getDay().toString().equals(arrDates[0].toString()))
									{
										cRow.setColDay1(voDailyTreatment.getHCP().toString());
										cRow.setTooltipForColDay1(voDailyTreatment.getHCP().toString() + "\n" + voDailyTreatment.getDay().toString());
									}
									else if(voDailyTreatment.getDay().toString().equals(arrDates[1].toString()))
									{
										cRow.setColDay2(voDailyTreatment.getHCP().toString());
										cRow.setTooltipForColDay2(voDailyTreatment.getHCP().toString() + "\n" + voDailyTreatment.getDay().toString());
									}
									else if(voDailyTreatment.getDay().toString().equals(arrDates[2].toString()))
									{
										cRow.setColDay3(voDailyTreatment.getHCP().toString());
										cRow.setTooltipForColDay3(voDailyTreatment.getHCP().toString() + "\n" + voDailyTreatment.getDay().toString());
									}
									else if(voDailyTreatment.getDay().toString().equals(arrDates[3].toString()))
									{
										cRow.setColDay4(voDailyTreatment.getHCP().toString());
										cRow.setTooltipForColDay4(voDailyTreatment.getHCP().toString() + "\n" + voDailyTreatment.getDay().toString());
									}
									else if(voDailyTreatment.getDay().toString().equals(arrDates[4].toString()))
									{
										cRow.setColDay5(voDailyTreatment.getHCP().toString());
										cRow.setTooltipForColDay5(voDailyTreatment.getHCP().toString() + "\n" + voDailyTreatment.getDay().toString());
									}
									else 
										break;
								}
							}
						
						cRow.setColDay6("No Tx");
						cRow.setColDay7("No Tx");
						cRow.setColDay8("No Tx");
						
					}
				}
			}
		}						
	}
	
	private Date[] loadDateArray(Date startDate) 
	{
		Date[] arrDates = new Date[8];
		Date dispDate1 = startDate.copy();
		arrDates[0] = dispDate1;
		Date dispDate2 = startDate.copy().addDay(1);
		arrDates[1] = dispDate2;
		Date dispDate3 = startDate.copy().addDay(2);
		arrDates[2] = dispDate3;
		Date dispDate4 = startDate.copy().addDay(3);
		arrDates[3] = dispDate4;
		Date dispDate5 = startDate.copy().addDay(4);
		arrDates[4] = dispDate5;
		Date dispDate6 = startDate.copy().addDay(5);
		arrDates[5] = dispDate6;
		Date dispDate7 = startDate.copy().addDay(6);
		arrDates[6] = dispDate7;
		Date dispDate8 = startDate.copy().addDay(7);
		arrDates[7] = dispDate8;
		
		return arrDates;
	}
	protected void onRecbrAssessmentValueChanged() throws PresentationLogicException
	{
		populateInstance(form.recbrAssessment().getValue());		
	}
}
