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

package ims.nursing.forms.nurinterventionchart;

import ims.core.vo.Hcp;
import ims.domain.exceptions.DomainInterfaceException;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.Control;
import ims.framework.enumerations.FormMode;
import ims.framework.exceptions.PresentationLogicException;
import ims.framework.utils.Date;
import ims.framework.utils.DateTime;
import ims.framework.utils.Time;
import ims.nursing.vo.InterventionChartDetailsVo;
import ims.nursing.vo.InterventionChartHeaderVo;
import ims.nursing.vo.InterventionChartHeaderVoCollection;
import ims.nursing.vo.InterventionChartDetailsVoCollection;
import ims.nursing.vo.InterventionChartVo;

public class Logic extends BaseLogic
{
	protected void onFormOpen() throws ims.framework.exceptions.FormOpenException
	{
		initialize();	//WDEV-7044
		open();
	}

	//WDEV-7044
	private void initialize()
	{
		form.btnUpdate().setEnabled(false);
		form.getLocalContext().setSelectedInterventionRecord(null);
	}

	private void open()
	{
		clearScreen();
		if (listInterventionHeaderRecords())
		{
			if(form.getLocalContext().getSelectedInterventionRecordIsNotNull())
				form.cmbAllRecords().setValue(form.getLocalContext().getSelectedInterventionRecord());
			if (form.cmbAllRecords().getValue() != null)
			{
				populateInterventionGrid(form.cmbAllRecords().getValue());
				if (form.getMode().equals(FormMode.VIEW))
				{
					form.btnUpdate().setVisible(true);
					form.btnUpdate().setEnabled(true);
				}
			}
		}
		setInterventionRemoveMenuVisibility();
	}

	private void populateInterventionGrid(InterventionChartHeaderVo header)
	{
		ims.nursing.vo.InterventionChartVo intervention = domain.getInterventionRecord(header);
		if (intervention != null)
		{
			form.getLocalContext().setInterventionChartRecord(intervention);
			form.dteRecording().setValue(intervention.getAuthoringDateTime().getDate());
			form.timRecorded().setValue(intervention.getAuthoringDateTime().getTime());
			if (intervention.getAuthoringHCP() != null)
			{
				form.qmbAuthoringHcp().newRow(intervention.getAuthoringHCP(), intervention.getAuthoringHCP().toString());
				form.qmbAuthoringHcp().setValue(intervention.getAuthoringHCP());
			}
			InterventionChartDetailsVoCollection coll = intervention.getDetails();

			GenForm.grdInterventionsRow row = null;

			// inactive record handling
			for (int i = 0; i < coll.size(); i++)
			{
				ims.nursing.vo.InterventionChartDetailsVo interventionDetail = coll.get(i);

				if (interventionDetail != null)
				{
					row = form.grdInterventions().getRows().newRow();
					row.setValue(interventionDetail);
					if (interventionDetail.getTimeOfIntervention() != null)
						row.setColTime(interventionDetail.getTimeOfIntervention());
					if (interventionDetail.getAssistanceRequired() != null)
						row.setColAssistance(interventionDetail.getAssistanceRequired());
					if (interventionDetail.getLengthOfTime() != null)
						row.setColLengthTime(interventionDetail.getLengthOfTime());
					if (interventionDetail.getDetails() != null)
						row.setColIntervention(interventionDetail.getDetails());
				}
			}
			form.btnUpdate().setVisible(true);
			form.btnUpdate().setEnabled(true);
		}
	}

	boolean listInterventionHeaderRecords()
	{
		form.cmbAllRecords().clear();
		form.cmbAllRecords().setValue(null);
		InterventionChartHeaderVoCollection interventionColl = new InterventionChartHeaderVoCollection();
		try
		{
			interventionColl = domain.listInterventionHeaderRecords(form.getGlobalContext().Core.getCurrentCareContext());
		}
		catch (DomainInterfaceException e)
		{
			throw new DomainRuntimeException("DomainInterfaceException occurred listing interventioncharts.\r\n" + e.getMessage(), e);
		}

		if (interventionColl != null)
		{
			if (interventionColl.size() != 0)
			{
				boolean bFirstRecord = true;
				form.cmbAllRecords().clear();
				for (int i = 0; i < interventionColl.size(); i++)
				{
					InterventionChartHeaderVo intervention = interventionColl.get(i);
					if (intervention != null)
					{
						// Load ComboBox with Records Retrieved
						String strInterventionHeader = "";
						if (intervention.getAuthoringHCP() != null)
							strInterventionHeader = intervention.getAuthoringDateTime().toString()+ " - " + intervention.getAuthoringHCP().toString();
						form.cmbAllRecords().newRow(intervention, strInterventionHeader);
						form.cmbAllRecords().setValue(intervention);
						
						if (bFirstRecord)
						{
							form.cmbAllRecords().setValue(intervention);
							bFirstRecord = false;
						}
					}
				}
				return true;
			}
			else
				return false;
		}
		return false;
	}

	protected void onImbRemoveInterventionClick()
	{
		removeIntervention();
	}

	private void removeIntervention()
	{
		int index = form.grdInterventions().getSelectedRowIndex();
		if (index < 0)
		{
			engine.showMessage("Please select an intervention to remove");
			return;
		}
		form.grdInterventions().getRows().remove(index);
	}

	protected void onImbAddInterventionClick()
	{
		newIntervention();
	}

	private void newIntervention()
	{
		form.grdInterventions().getRows().newRow();
	}

	protected void onBNewClick() throws ims.framework.exceptions.PresentationLogicException
	{
		form.btnUpdate().setEnabled(false);
		clearScreen();
		form.dteRecording().setValue(new Date());
		form.timRecorded().setValue(new Time());
		form.cmbAllRecords().setValue(null);
		Hcp voHcp = (Hcp) domain.getHcpUser();
		if (voHcp != null)
		{
			form.qmbAuthoringHcp().newRow(voHcp, voHcp.toString());
			form.qmbAuthoringHcp().setValue(voHcp);
		}
		
		form.getLocalContext().setInterventionChartRecord(null);
		form.getLocalContext().setSelectedInterventionRecord(null);
		form.setMode(FormMode.EDIT);
	}

	private void clearScreen()
	{
		form.grdInterventions().getRows().clear();
		form.qmbAuthoringHcp().clear();
		form.dteRecording().setValue(null);
		form.timRecorded().setValue(null);
		form.grdInterventions().getRows().clear();
	}

	protected void onBSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{

		InterventionChartVo voIntervention = form.getLocalContext().getInterventionChartRecord();
		if (voIntervention == null)
			voIntervention = new InterventionChartVo();
		
		voIntervention.setAuthoringHCP(form.qmbAuthoringHcp().getValue());
		voIntervention.setAuthoringDateTime(new DateTime(form.dteRecording().getValue(), form.timRecorded().getValue()));

		InterventionChartDetailsVoCollection collInterventionDetails = new InterventionChartDetailsVoCollection();
		GenForm.grdInterventionsRow rowIntervention = null;
		for (int i = 0; i < form.grdInterventions().getRows().size(); i++)
		{
			rowIntervention = form.grdInterventions().getRows().get(i);
			InterventionChartDetailsVo intervention = rowIntervention.getValue();
			if (intervention == null)
				intervention = new InterventionChartDetailsVo();

			intervention.setAssistanceRequired(rowIntervention.getColAssistance());
			intervention.setDetails(rowIntervention.getColIntervention() != null ? rowIntervention.getColIntervention().toString() : "");
			intervention.setTimeOfIntervention(rowIntervention.getColTime());
			intervention.setLengthOfTime(rowIntervention.getColLengthTime() != null ? rowIntervention.getColLengthTime().toString() : "");
			if(intervention.getDetails().length() > 255)
			{
				engine.showMessage("Intervention cannot be greater than 255 characters.");
				return;
			}
			collInterventionDetails.add(intervention);
		}

		if(collInterventionDetails.size() <= 0)
		{
			engine.showMessage("A blank record cannot be saved.  Please provide data before attempting to save.");
			return;
		}
		voIntervention.setDetails(collInterventionDetails);

		if (form.getGlobalContext().Core.getCurrentClinicalContactIsNotNull())
			voIntervention.setClinicalContact(form.getGlobalContext().Core.getCurrentClinicalContact());

		if (form.getGlobalContext().Core.getCurrentCareContextIsNotNull())
			voIntervention.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());

		if(form.qmbAuthoringHcp().getValue() == null || voIntervention.getAuthoringHCP()== null)
		{
			engine.showMessage(" An Authoring HCP is required to record this data. This record will not be saved");
			return;
		
		}
		
		String[] arrErrors = voIntervention.validate();

		if (arrErrors != null)
		{
			engine.showErrors(arrErrors);
			return;
		}
		try
		{
			form.getLocalContext().setSelectedInterventionRecord(domain.saveIntervention(voIntervention, form.getGlobalContext().Core.getCurrentClinicalContact()));
		}
		catch (StaleObjectException e)
		{
			engine.showMessage(ims.configuration.gen.ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
			form.setMode(FormMode.VIEW);
			open();
			return;
		}
		form.setMode(FormMode.VIEW);
		open();

	}

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

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

	protected void onCmbAllRecordsValueChanged() throws ims.framework.exceptions.PresentationLogicException
	{
		if (form.cmbAllRecords().getValue() != null)
		{
			clearScreen();
			form.getLocalContext().setSelectedInterventionRecord(form.cmbAllRecords().getValue());
			populateInterventionGrid(form.getLocalContext().getSelectedInterventionRecord());
		}
		else
		{
			clearScreen();
			form.btnUpdate().setEnabled(false);
		}

	}

	protected void onGrdInterventionsSelectionChanged() throws PresentationLogicException
	{
		setInterventionRemoveMenuVisibility();
	}

	private void setInterventionRemoveMenuVisibility()
	{
		boolean bVisible = form.grdInterventions().getSelectedRowIndex() != -1 && form.getMode().equals(FormMode.EDIT);

		form.getContextMenus().getGenericGridRemoveItem().setVisible(bVisible);
	}

	protected void onContextMenuItemClick(int menuItemID, Control sender) throws PresentationLogicException
	{
		if (sender.equals(form.grdInterventions()))
		{
			switch (menuItemID)
			{
				case GenForm.ContextMenus.GenericGrid.Add :
					newIntervention();
				break;
				case GenForm.ContextMenus.GenericGrid.Remove :
					removeIntervention();
					setInterventionRemoveMenuVisibility();
			}
		}

	}

	protected void onFormModeChanged()
	{
		boolean bVisible = form.getMode().equals(FormMode.EDIT);
		form.getContextMenus().getGenericGridAddItem().setVisible(bVisible);
	}
}
