/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/forms/mrsatreatment/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 41215.900)
// Copyright (C) 1995-2004 IMS MAXIMS plc. All rights reserved.
// 01/12/2005 - Replace RecordingDate with SystemInformation
// 02/12/2005 - AU - WDEV-208, and code format
// 02/12/2005 - AU - bug fix not reported (When Selecting empty Screening Date then Treatment Grid not cleared)
package ims.nursing.forms.mrsatreatment;

import ims.core.vo.CareContextShortVo;
import ims.core.vo.CareContextShortVoCollection;
import ims.core.vo.HcpLiteVo;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.enumerations.FormMode;
import ims.framework.exceptions.PresentationLogicException;
import ims.framework.utils.Date;
import ims.framework.utils.DateTime;
import ims.nursing.forms.mrsatreatment.GenForm.grdEditRow;
import ims.nursing.vo.MRSAAssessmentVo;
import ims.nursing.vo.MRSAAssessmentVoCollection;
import ims.nursing.vo.MRSADailyTreatmentVo;
import ims.nursing.vo.MRSADailyTreatmentVoCollection;
import ims.nursing.vo.MRSASitesResultsVo;
import ims.nursing.vo.MRSASitesResultsVoCollection;
import ims.nursing.vo.MRSATreatmentDetailsVo;
import ims.nursing.vo.MRSATreatmentDetailsVoCollection;
import ims.nursing.vo.MRSATreatmentVo;
import ims.nursing.vo.lookups.MRSATreatmentCollection;
import ims.nursing.vo.lookups.LookupHelper;

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 populateTreatmentGrid()
	{
		//add lookups to treatment grid
		form.grdEdit().getRows().clear();
		MRSATreatmentCollection collTreatment = LookupHelper.getMRSATreatment(domain.getLookupService());
		GenForm.grdEditRow row = null;
		for (int i = 0; i < collTreatment.size(); i++)
		{
			row = form.grdEdit().getRows().newRow();
			row.setColTreatment(collTreatment.get(i));
			row.setColDay1_Value(null);
			row.setColDay2_Value(null);
			row.setColDay3_Value(null);
			row.setColDay4_Value(null);  
			row.setColDay5_Value(null);
			row.setColDay6("No Tx");
			row.setColDay7("No Tx");
			row.setColDay8("No Tx - Rescreen");
		}
	}

	private void populateScreeningCombo(MRSAAssessmentVoCollection voCollMRSA)
	{
		if (voCollMRSA == null)
		{
			form.btnUpdate().setVisible(false);
			return;
		}

		for (int i = 0; i < voCollMRSA.size(); i++)
		{
			String strDisplay = voCollMRSA.get(i).getDateScreening().toString();
			if (voCollMRSA.get(i).getHcpInitiated() != null)
			{
				strDisplay += " - ";
				strDisplay += voCollMRSA.get(i).getHcpInitiated().toString();
			}
			form.cmbScreeningRecords().newRow(voCollMRSA.get(i), strDisplay);
		}
		//set the combo to hold the latest record.
		if(voCollMRSA.size()>0 && form.cmbScreeningRecords().getValues() != null)
		{
			form.cmbScreeningRecords().setValue((MRSAAssessmentVo) form.cmbScreeningRecords().getValues().get(0));
		}

		try
		{
			onCmbScreeningRecordsValueChanged();
		}
		catch (PresentationLogicException e)
		{
		}
	}

	private void populateScreenWithTreatmentInfo()
	{
		MRSAAssessmentVo voMRSA = form.cmbScreeningRecords().getValue();
		
		updateControlStatus();
		
		if (voMRSA == null)
			return;
		
		MRSATreatmentVo voTreatment = voMRSA.getTreatment();

		if (voTreatment == null)
			return;

		form.dteRescreen().setValue(voTreatment.getRescreenDate());
		form.dteStart().setValue(voTreatment.getStartDate());
		form.intTreatmentNumber().setValue(voTreatment.getTreatmentNumber());

		if (voTreatment.getStartDate() != null)
			try
			{
				onDteStartValueChanged();
			}
			catch (PresentationLogicException e)
			{
			}

		MRSATreatmentDetailsVoCollection voTreatmentDetailColl = new MRSATreatmentDetailsVoCollection();

		if (voTreatment != null && voTreatment.getTreatmentDetails() != null)
			voTreatmentDetailColl = voTreatment.getTreatmentDetails();
		
		for(int z =0; z<voTreatmentDetailColl.size(); z++)
		{
			MRSATreatmentDetailsVo voTreatmentDetail = voTreatmentDetailColl.get(z); 
			int i=0;
			for(i=0; i<form.grdEdit().getRows().size(); i++)
			{
				GenForm.grdEditRow row = form.grdEdit().getRows().get(i);
				if(voTreatmentDetail.getTreatment()!= null && row.getColTreatment() != null)
				{
					if(row.getColTreatment().equals(voTreatmentDetail.getTreatment()))
					{
						setGridEditRowData(voTreatmentDetail, row);
						break;
					}
				}
			}
			if(i == form.grdEdit().getRows().size())
			{
				grdEditRow newRow = form.grdEdit().getRows().newRow();
				newRow.setColTreatment(voTreatmentDetail.getTreatment());
				setGridEditRowData(voTreatmentDetail, newRow);
			}
		}		
	}

	/**
	 * @param arrDates
	 * @param voTreatmentDetail
	 * @param row
	 */
	private void setGridEditRowData(MRSATreatmentDetailsVo voTreatmentDetail, GenForm.grdEditRow row)
	{
		Date[] arrDates = getDateArray();
		String strTooltip;
		row.setValue(voTreatmentDetail);
		for(int t=0; t<voTreatmentDetail.getDailyTreatment().size(); t++)
		{
			MRSADailyTreatmentVo voDailyTreatment = voTreatmentDetail.getDailyTreatment().get(t);
			DateTime creationDateTime = voDailyTreatment.getTreatmentDateTime();
			if (voDailyTreatment.getDay().toString().equals(arrDates[0].toString()))     //equals(arrDates[0])
			{
				row.setColDay1(true);
				strTooltip = prepareToolTip(voDailyTreatment, creationDateTime);

				row.setTooltipForColDay1(strTooltip);
				row.setColDay1_Value(voDailyTreatment);
			}
			if (voDailyTreatment.getDay().toString().equals(arrDates[1].toString()))
			{
				row.setColDay2(true);
				strTooltip = prepareToolTip(voDailyTreatment, creationDateTime);

				row.setTooltipForColDay2(strTooltip);
				row.setColDay2_Value(voDailyTreatment);
			}
			if (voDailyTreatment.getDay().toString().equals(arrDates[2].toString()))
			{
				row.setColDay3(true);
				strTooltip = prepareToolTip(voDailyTreatment, creationDateTime);

				row.setTooltipForColDay3(strTooltip);
				row.setColDay3_Value(voDailyTreatment);
			}
			if (voDailyTreatment.getDay().toString().equals(arrDates[3].toString()))
			{
				row.setColDay4(true);
				strTooltip = prepareToolTip(voDailyTreatment, creationDateTime);

				row.setTooltipForColDay4(strTooltip);
				row.setColDay4_Value(voDailyTreatment);
			}
			if (voDailyTreatment.getDay().toString().equals(arrDates[4].toString()))
			{
				row.setColDay5(true);
				strTooltip = prepareToolTip(voDailyTreatment, creationDateTime);

				row.setTooltipForColDay5(strTooltip);
				row.setColDay5_Value(voDailyTreatment);
			}

		}
		row.setColDay6("No Tx");
		row.setColDay7("No Tx");
		row.setColDay8("No Tx - Rescreen");
	}

	/**
	 * @param voDailyTreatment
	 * @param creationDateTime
	 * @return
	 */
	private String prepareToolTip(MRSADailyTreatmentVo voDailyTreatment, DateTime creationDateTime)
	{
		String strTooltip;
		strTooltip = "<b>Date: </b>" + creationDateTime.getDate() + "<br>" + "<b>Time: </b>" + creationDateTime.getTime();
		if (voDailyTreatment.getHCP() != null)
			strTooltip += "<br><b> Hcp: </b>" + voDailyTreatment.getHCP().toString();
		return strTooltip;
	}

	private Date[] getDateArray()
	{
		Date[] arrDates = new Date[8];
		Date dispDate1 = form.dteStart().getValue().copy();
		arrDates[0] = dispDate1;
		Date dispDate2 = form.dteStart().getValue().copy().addDay(1);
		arrDates[1] = dispDate2;
		Date dispDate3 = form.dteStart().getValue().copy().addDay(2);
		arrDates[2] = dispDate3;
		Date dispDate4 = form.dteStart().getValue().copy().addDay(3);
		arrDates[3] = dispDate4;
		Date dispDate5 = form.dteStart().getValue().copy().addDay(4);
		arrDates[4] = dispDate5;
		Date dispDate6 = form.dteStart().getValue().copy().addDay(5);
		arrDates[5] = dispDate6;
		Date dispDate7 = form.dteStart().getValue().copy().addDay(6);
		arrDates[6] = dispDate7;
		Date dispDate8 = form.dteStart().getValue().copy().addDay(7);
		arrDates[7] = dispDate8;

		return arrDates;
	}

	private void populateScreeningGrid(MRSAAssessmentVo voMRSA)
	{
		form.grdScreening().getRows().clear();

		if (voMRSA == null)
			return;

		MRSASitesResultsVoCollection voSitesResultColl = null;
		MRSASitesResultsVo voSitesResult = null;
		GenForm.grdScreeningRow row = null;
		row = form.grdScreening().getRows().newRow();

		if (voMRSA.getDateScreening() != null)
			row.setColDateScreening(voMRSA.getDateScreening().toString());

		row.setValue(voMRSA);

		voSitesResultColl = voMRSA.getSitesAndResults();
		if (voSitesResultColl != null)
		{
			GenForm.grdScreeningRow cRow = null;
			for (int p = 0; p < voSitesResultColl.size(); p++)
			{
				voSitesResult = voSitesResultColl.get(p);
				cRow = row.getRows().newRow();

				if (voSitesResult.getOther() != null)
					cRow.setColDateScreening(voSitesResult.getOther());
				else if (voSitesResult.getSite() != null)
					cRow.setColDateScreening(voSitesResult.getSite().getText());

				if (voSitesResult.getDateResult() != null)
					cRow.setColDateResult(voSitesResult.getDateResult().toString());

				if (voSitesResult.getResult() != null)
					cRow.setColResult(voSitesResult.getResult().getText());

				cRow.setValue(voSitesResult);
				cRow.setSelectable(false);
			}
		}

		form.grdScreening().expandAll();
	}

	private void open()
	{		 
		form.recbrAssessment().setValue(form.getGlobalContext().Core.getCurrentCareContext());
		populateInstance(form.getGlobalContext().Core.getCurrentCareContext());
		
		if (form.cmbScreeningRecords().getValue() == null)
			form.btnUpdate().setVisible(false);
	}
	private void populateInstance(CareContextShortVo voCareContext)
	{		
		form.cmbScreeningRecords().clear();
		clearScreen();

		MRSAAssessmentVoCollection voCollMRSA = domain.listMRSA(voCareContext);
		populateScreeningCombo(voCollMRSA);
	}	

	private void clearScreen()
	{ 
		
		form.dteRescreen().setValue(null);
		form.dteStart().setValue(null);
		form.intTreatmentNumber().setValue(null);

		//clearLabels
		form.lblDay1().setValue("");
		form.lblDay2().setValue("");
		form.lblDay3().setValue("");
		form.lblDay4().setValue("");
		form.lblDay5().setValue("");
		form.lblDay6().setValue("");
		form.lblDay7().setValue("");
		form.lblDay8().setValue("");
		
		form.grdScreening().getRows().clear();
		
		for (int i = 0; i < form.grdEdit().getRows().size(); i++)
		{
			form.grdEdit().getRows().get(i).setColDay1(false);
			form.grdEdit().getRows().get(i).setColDay1_Value(null);
			form.grdEdit().getRows().get(i).setColDay2(false);
			form.grdEdit().getRows().get(i).setColDay2_Value(null);
			form.grdEdit().getRows().get(i).setColDay3(false);
			form.grdEdit().getRows().get(i).setColDay3_Value(null);
			form.grdEdit().getRows().get(i).setColDay4(false);
			form.grdEdit().getRows().get(i).setColDay4_Value(null);
			form.grdEdit().getRows().get(i).setColDay5(false);
			form.grdEdit().getRows().get(i).setColDay5_Value(null);
		}

		clearDetailGrid();
	}

	protected void onCmbScreeningRecordsValueChanged() throws ims.framework.exceptions.PresentationLogicException
	{
		clearScreen();
		populateTreatmentGrid();
		populateScreeningGrid(form.cmbScreeningRecords().getValue());
		populateScreenWithTreatmentInfo();
	}

	protected void onBtnUpdateClick() throws ims.framework.exceptions.PresentationLogicException
	{
		form.setMode(FormMode.EDIT);
		form.cmbScreeningRecords().setEnabled(false);
		form.btnUpdate().setVisible(false);
	}

	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{
		form.setMode(FormMode.VIEW);
		open();
		form.cmbScreeningRecords().setEnabled(true);
		updateControlStatus();
	}

	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (form.dteStart().getValue() == null)
		{
			engine.showMessage("Please enter a Tx Start Date");
			form.dteStart().setFocus();
			return;
		}
		MRSAAssessmentVo voMRSA = form.cmbScreeningRecords().getValue();
		MRSATreatmentVo voTreatment = voMRSA.getTreatment();
		if (voTreatment == null)
			voTreatment = new MRSATreatmentVo();

		voTreatment.setRescreenDate(form.dteRescreen().getValue());
		voTreatment.setStartDate(form.dteStart().getValue());
		voTreatment.setTreatmentNumber(form.intTreatmentNumber().getValue());

		Date[] arrDates = getDateArray();
		

		MRSATreatmentDetailsVoCollection voCollTreatmentDetails = voTreatment.getTreatmentDetails();
		if(voCollTreatmentDetails == null)
			voCollTreatmentDetails = new MRSATreatmentDetailsVoCollection();
		else
			voCollTreatmentDetails.clear();
		
		GenForm.grdEditRow row = null;
		for (int i = 0; i < form.grdEdit().getRows().size(); i++)
		{
			row = form.grdEdit().getRows().get(i);
			MRSATreatmentDetailsVo voTreatmentDetail = row.getValue();
			if(voTreatmentDetail == null)
				voTreatmentDetail = new MRSATreatmentDetailsVo();
			
			if(voTreatmentDetail.getDailyTreatment() == null)
				voTreatmentDetail.setDailyTreatment(new MRSADailyTreatmentVoCollection());
			
			voTreatmentDetail.getDailyTreatment().clear();
			
			if (row.getColDay1() == true)
				loadDailyTreatment(arrDates[0], row.getColDay1_Value(), voTreatmentDetail, voMRSA);
			
			if (row.getColDay2() == true)
				loadDailyTreatment(arrDates[1], row.getColDay2_Value(), voTreatmentDetail, voMRSA);
			
			if (row.getColDay3() == true)
				loadDailyTreatment(arrDates[2], row.getColDay3_Value(), voTreatmentDetail, voMRSA);

			if (row.getColDay4() == true)
				loadDailyTreatment(arrDates[3], row.getColDay4_Value(), voTreatmentDetail, voMRSA);

			if (row.getColDay5() == true)
				loadDailyTreatment(arrDates[4], row.getColDay5_Value(), voTreatmentDetail, voMRSA);
				
			
			if (voTreatmentDetail != null && voTreatmentDetail.getDailyTreatment() != null && voTreatmentDetail.getDailyTreatment().size()>0)
			{
				voTreatmentDetail.setTreatment(row.getColTreatment());
				voCollTreatmentDetails.add(voTreatmentDetail);
			}
		}
		
		voTreatment.setTreatmentDetails(voCollTreatmentDetails);
		voMRSA.setTreatment(voTreatment);
		
		if (form.getGlobalContext().Core.getCurrentCareContextIsNotNull())
			voMRSA.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());

		String[] messages = voMRSA.validate();
		if (messages != null)
		{
			engine.showErrors("Validation Errors", messages);
			return;
		}

		try
		{
			domain.saveTreatment(voMRSA, form.getGlobalContext().Core.getCurrentClinicalContact());
		}
		catch (StaleObjectException e)
		{
			engine.showMessage(ims.configuration.gen.ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
		}

		form.setMode(FormMode.VIEW);
		open();
		form.cmbScreeningRecords().setEnabled(true);
		updateControlStatus();
	}

	/**
	 * @param date
	 * @param recordingHcp
	 * @param voDailyTreatment
	 * @param voTreatmentDetail
	 */
	private void loadDailyTreatment(Date date, MRSADailyTreatmentVo voDailyTreat, MRSATreatmentDetailsVo voTreatmentDetail, MRSAAssessmentVo voMRSA)
	{
		HcpLiteVo recordingHcp = null;
		if (domain.getHcpLiteUser() != null)
			recordingHcp = (HcpLiteVo) domain.getHcpLiteUser();
		else if(voMRSA.getHcpInitiatedIsNotNull())
		{
			recordingHcp = voMRSA.getHcpInitiated();
		}

		MRSADailyTreatmentVo voDailyTreatment = voDailyTreat;
		if(voDailyTreatment == null)
		{
			voDailyTreatment =  new MRSADailyTreatmentVo();
			voDailyTreatment.setHCP(recordingHcp);
			voDailyTreatment.setDay(date);
			voDailyTreatment.setTreatmentDateTime(new DateTime());
		}
		voTreatmentDetail.getDailyTreatment().add(voDailyTreatment);
	}

	protected void onDteStartValueChanged() throws ims.framework.exceptions.PresentationLogicException
	{
		clearDetailGrid();
		if (form.dteStart().getValue() != null)
		{
			Date sDate = form.dteStart().getValue().copy();
			form.dteRescreen().setValue(sDate.addDay(7));

			Date dispDate1 = form.dteStart().getValue().copy();
			Date dispDate2 = form.dteStart().getValue().copy().addDay(1);
			Date dispDate3 = form.dteStart().getValue().copy().addDay(2);
			Date dispDate4 = form.dteStart().getValue().copy().addDay(3);
			Date dispDate5 = form.dteStart().getValue().copy().addDay(4);
			Date dispDate6 = form.dteStart().getValue().copy().addDay(5);
			Date dispDate7 = form.dteStart().getValue().copy().addDay(6);
			Date dispDate8 = form.dteStart().getValue().copy().addDay(7);

			form.lblDay1().setValue(dispDate1.toString());
			form.lblDay2().setValue(dispDate2.toString());
			form.lblDay3().setValue(dispDate3.toString());
			form.lblDay4().setValue(dispDate4.toString());
			form.lblDay5().setValue(dispDate5.toString());
			form.lblDay6().setValue(dispDate6.toString());
			form.lblDay7().setValue(dispDate7.toString());
			form.lblDay8().setValue(dispDate8.toString());
		}
		else
			form.dteRescreen().setValue(null);

	}

	private void clearDetailGrid()
	{
		//clear grid
		for (int i = 0; i < form.grdEdit().getRows().size(); i++)
		{
			//	form.grdEdit().getRows().get(i).setColDay1(false);
			//form.grdEdit().getRows().get(i).setColDay2(false);
			//	form.grdEdit().getRows().get(i).setColDay3(false);
			//	form.grdEdit().getRows().get(i).setColDay4(false);
			//	form.grdEdit().getRows().get(i).setColDay5(false);

			form.grdEdit().getRows().get(i).setTooltipForColDay1(null);
			form.grdEdit().getRows().get(i).setTooltipForColDay2(null);
			form.grdEdit().getRows().get(i).setTooltipForColDay3(null);
			form.grdEdit().getRows().get(i).setTooltipForColDay4(null);
			form.grdEdit().getRows().get(i).setTooltipForColDay5(null);

			form.grdEdit().getRows().get(i).setColDay1_Value(null);
			form.grdEdit().getRows().get(i).setColDay2_Value(null);
			form.grdEdit().getRows().get(i).setColDay3_Value(null);
			form.grdEdit().getRows().get(i).setColDay4_Value(null);
			form.grdEdit().getRows().get(i).setColDay5_Value(null);

		}
	}

	protected void onRecbrAssessmentValueChanged() throws PresentationLogicException
	{
		populateInstance(form.recbrAssessment().getValue());
		
		updateControlStatus();		
	}
	
	private void updateControlStatus()
	{
		if (form.recbrAssessment().getValue() == null)
		{
			form.btnUpdate().setVisible(false);
			form.setMode(FormMode.VIEW);
		}
		else
		{
			if (form.recbrAssessment().getValue().equals(form.getGlobalContext().Core.getCurrentCareContext()))
			{
				if (form.cmbScreeningRecords().getValue() == null)
					form.btnUpdate().setVisible(false);
				else
					form.btnUpdate().setVisible(true);
			}
			else
			{
				form.btnUpdate().setVisible(false);
				form.setMode(FormMode.VIEW);
			}
		}
	}
	
}
