/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/forms/patientsecs/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 Alexie Ursache using IMS Development Environment (version 1.53 build 2510.31460)
// Copyright (C) 1995-2006 IMS MAXIMS plc. All rights reserved.

package ims.nursing.forms.patientsecs;

import java.util.ArrayList;

import ims.clinical.vo.PatientSECSShortVo;
import ims.clinical.vo.PatientSECSShortVoCollection;
import ims.core.vo.CareContextShortVo;
import ims.framework.utils.Date;
import ims.framework.utils.DateTime;
import ims.framework.utils.Time;
import ims.nursing.forms.patientsecs.GenForm.grdSECSRow;
import ims.nursing.vo.PatientSECSSearchCriteriaVo;

public class Logic extends BaseLogic
{
	private static final long	serialVersionUID	= 1L;

	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		initialize();
		//WDEV-19389 - start
		if (!(form.getGlobalContext().Core.getPatientShortIsNotNull() && form.getGlobalContext().Nursing.getPatientSECSSearchCriteriaIsNotNull() && form.getGlobalContext().Core.getPatientShort().equals(form.getGlobalContext().Nursing.getPatientSECSSearchCriteria().getPatient())))
			form.getGlobalContext().Nursing.setPatientSECSSearchCriteria(null);
		
		if(form.getGlobalContext().Nursing.getPatientSECSSearchCriteriaIsNotNull())
		{
			setSearchCriteria(form.getGlobalContext().Nursing.getPatientSECSSearchCriteria());
			doSearch(false);
		}
		else
			doSearch(true);
		//WDEV-19389 - end
	}

	protected void onImbSearchClick() throws ims.framework.exceptions.PresentationLogicException
	{
		doSearch(false);
	}
	
	private PatientSECSSearchCriteriaVo getSearchCriteria()
	{
		PatientSECSSearchCriteriaVo searchCriteria = new PatientSECSSearchCriteriaVo();
		
		searchCriteria.setFromDate(form.dteStart().getValue());
		searchCriteria.setToDate(form.dteEnd().getValue());
		searchCriteria.setPatient(form.getGlobalContext().Core.getPatientShort());
		
		return searchCriteria;
	}
	
	private void setSearchCriteria(PatientSECSSearchCriteriaVo patientSECSSearchCriteriaVo) 
	{
		form.dteStart().setValue(patientSECSSearchCriteriaVo.getFromDate());
		form.dteEnd().setValue(patientSECSSearchCriteriaVo.getToDate());
	}

	private void initialize()
	{
		Date startDate = new Date();
		Date endDate   = new Date();
		
		CareContextShortVo careContext = form.getGlobalContext().Core.getCurrentCareContext();
		if (careContext != null)
		{
			if (careContext.getStartDateTimeIsNotNull())
				startDate = careContext.getStartDateTime().getDate();

			if (careContext.getEndDateTimeIsNotNull())
				endDate = careContext.getEndDateTime().getDate();
		}
		
		form.dteStart().setValue(startDate);
		form.dteEnd().setValue(endDate);
	}

	private void doSearch(boolean isCalledFromFormOpen)
	{
		clearGrid();

		String[] uiErrors = getUISearchCriteriaErrors();
		if (uiErrors != null && isCalledFromFormOpen == false)
		{
			engine.showErrors(uiErrors);
			return;
		}

		if (uiErrors == null)
		{
			Date startDate = form.dteStart() != null ? form.dteStart().getValue() : null;
			Date endDate = form.dteEnd() != null ? form.dteEnd().getValue() : null;

			DateTime startDT = new DateTime(startDate, new Time(0, 0));
			DateTime endDT = new DateTime(endDate, new Time(23, 59));

			PatientSECSShortVoCollection voColl = domain.listPatientSECS(startDT, endDT,form.getGlobalContext().Core.getPatientShort());
			if (isCalledFromFormOpen == false && (voColl == null || voColl.size() == 0))
			{
				engine.showMessage("No records found for the Search Criteria Provided");
			}

			populateGridControl(voColl);
			
			if (isCalledFromFormOpen == false)
				form.getGlobalContext().Nursing.setPatientSECSSearchCriteria(getSearchCriteria());//WDEV-19389 
		}
	}

	private String[] getUISearchCriteriaErrors()
	{
		ArrayList errors = new ArrayList();

		Date startDate = form.dteStart().getValue();
		if (startDate == null)
		{
			errors.add("From Date is mandatory");
		}

		Date endDate = form.dteEnd().getValue();
		if (endDate == null)
		{
			errors.add("To Date is mandatory");
		}

		if (startDate != null && startDate != null)
		{
			if (startDate.isGreaterThan(endDate))
			{
				errors.add("From Date cannot be greater than To Date");
			}
		}

		if (errors != null && errors.size() > 0)
		{
			String[] uiErrors = new String[errors.size()];
			errors.toArray(uiErrors);
			return uiErrors;
		}

		return null;
	}

	private void populateGridControl(PatientSECSShortVoCollection voColl)
	{
		for (int i = 0; voColl != null && i < voColl.size(); i++)
		{
			PatientSECSShortVo voPatSECS = voColl.get(i);
			grdSECSRow row = form.grdSECS().getRows().newRow();

			row.setColEscalate(voPatSECS.getEscalatedIsNotNull() ? (voPatSECS.getEscalated().booleanValue() ? "Yes" : "No") : "");
			row.setTooltipForColEscalate(row.getColEscalate());
			
			row.setColAuthoringInfo(voPatSECS.getAuthoringInformationIsNotNull() ? voPatSECS.getAuthoringInformation().toString(", ") : "");
			//WDEV-13559 
			if (voPatSECS.getAuthoringInformationIsNotNull() && voPatSECS.getRecordingInformationIsNotNull())
			{
			row.setTooltipForColAuthoringInfo("Authoring Info: "+row.getColAuthoringInfo()+"<br> Recording Info: "+voPatSECS.getRecordingInformation());
			}
			else if (voPatSECS.getAuthoringInformationIsNotNull())
			{
			row.setTooltipForColAuthoringInfo("Authoring Info: "+row.getColAuthoringInfo());
			}
			
			row.setColScore(voPatSECS.getSECSScore());
			row.setTooltipForColScore(row.getColScore().toString());
			
			row.setColDetails(voPatSECS.getScoreDetails());
			row.setTooltipForColDetails(row.getColDetails());
			
			row.setColActionTaken(voPatSECS.getActionTaken());
			row.setTooltipForColActionTaken(row.getColActionTaken());
		}
	}

	private void clearGrid()
	{
		form.grdSECS().getRows().clear();
	}
}
