/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/forms/careplanworklist/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 Florin Blindu using IMS Development Environment (version 1.80 build 4471.18200)
// Copyright (C) 1995-2012 IMS MAXIMS. All rights reserved.

package ims.nursing.forms.careplanworklist;

import ims.core.vo.HcpLiteVo;
import ims.core.vo.LocationLiteVoCollection;
import ims.core.vo.PatientIdCollection;
import ims.core.vo.enums.MosType;
import ims.core.vo.lookups.PatIdType;
import ims.framework.exceptions.PresentationLogicException;
import ims.framework.utils.Date;
import ims.nursing.forms.careplanworklist.GenForm.grdResultRow;
import ims.nursing.forms.careplanworklist.GenForm.grpStatusEnumeration;
import ims.nursing.vo.CarePlanWorklistSearchVo;
import ims.nursing.vo.CarePlanWorklistVo;
import ims.nursing.vo.CarePlanWorklistVoCollection;

import java.util.ArrayList;

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

	@Override
	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		initialize();
		open();
	}

	private void open()
	{
		if (form.getGlobalContext().Nursing.getCarePlanWorklistSearchCriteriaIsNotNull())
		{
			populateSearchCriteriaScreenFromData(form.getGlobalContext().Nursing.getCarePlanWorklistSearchCriteria());
			search();
		}

	}

	private void initialize()
	{
		form.ccHCP().initialize(MosType.HCP);
		populateWardCmb();
		form.dteFromDate().setValue(new Date());
		form.dteToDate().setValue((new Date()).addDay(7));
		form.grpStatus().setValue(grpStatusEnumeration.rdoActive);
	}

	private void populateSearchCriteriaScreenFromData(CarePlanWorklistSearchVo criteria)
	{
		clearScreen();

		if (criteria == null)
			return;

		form.ccHCP().setValue(criteria.getHCP());
		form.cmbWard().setValue(criteria.getWard());
		form.dteFromDate().setValue(criteria.getDateFrom());
		form.dteToDate().setValue(criteria.getDateTo());
		
		if (Boolean.TRUE.equals(criteria.getActive())) //WDEV-15266
		{
			form.grpStatus().setValue(grpStatusEnumeration.rdoActive);
		}
		else
		{
			form.grpStatus().setValue(grpStatusEnumeration.rdoAll);
		}
			

	}

	private void populateWardCmb()
	{
		LocationLiteVoCollection collWards = domain.getWards();

		if (collWards == null)
			return;

		for (int i = 0; i < collWards.size(); i++)
		{
			form.cmbWard().newRow(collWards.get(i), collWards.get(i).getName());
		}
	}

	@Override
	protected void onImbClearClick() throws ims.framework.exceptions.PresentationLogicException
	{
		clearScreen();
		form.getGlobalContext().Nursing.setCarePlanWorklistSearchCriteria(null);
	}

	@Override
	protected void onImbSearchClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (search())
		{
			form.getGlobalContext().Nursing.setCarePlanWorklistSearchCriteria(populateSearchCriteriaFromScreen());
		}
	}

	private boolean search()
	{
		if (isSearchCriteriaValid())
		{
			populateGridResults(domain.listCarePlans(populateSearchCriteriaFromScreen()));
			// update total
			updateSearchTotal(form.grdResult().getRows().size());

			if (form.grdResult().getRows().size() == 0)
				engine.showMessage("No records found.");

			return true;
		}

		return false;

	}

	private void updateSearchTotal(int count)
	{
		form.lblTotal().setValue("Total : " + count);
	}

	private boolean isSearchCriteriaValid()
	{
		ArrayList<String> uiErrors = new ArrayList<String>();

		Date dateFrom = form.dteFromDate().getValue();
		Date dateTo = form.dteToDate().getValue();

		if (dateFrom != null && dateTo != null && dateTo.isLessThan(dateFrom))
		{
			uiErrors.add("'Date From' cannot be greater than 'Date To'."); //WDEV-18762
		}

		if (uiErrors.size() > 0)
		{
			engine.showErrors(uiErrors.toArray(new String[uiErrors.size()]));
			return false;
		}

		return true;
	}

	private void populateGridResults(CarePlanWorklistVoCollection listCarePlans)
	{
		form.grdResult().getRows().clear();

		if (listCarePlans == null || listCarePlans.size() == 0)
			return;

		for (int i = 0; i < listCarePlans.size(); i++)
		{
			CarePlanWorklistVo record = listCarePlans.get(i);
			if (record == null)
				continue;

			addCarePlanWorklistRow(record);

		}

	}

	private void addCarePlanWorklistRow(CarePlanWorklistVo record)
	{
		if (record == null)
			return;

		// WDEV-15063
		if (record.getPatient() == null || record.getCarePlan() == null)
		{
			return;
		}

		grdResultRow row = form.grdResult().getRows().newRow();
		PatientIdCollection identifiers = record.getPatient().getIdentifiers();

		for (int i = 0; i < identifiers.size(); i++)
		{
			if (identifiers.get(i).getType().equals(PatIdType.HOSPNUM))
				row.setcolHospitalNo(identifiers.get(i).getValue().toString());
		}

		row.setcolName(record.getPatient().getName().toString());
		row.setTooltipForcolName(record.getPatient().getName().toString());
		row.setcolDOB(record.getPatient().getDob());

		if (record.getPatient().getWard() != null)
		{
			row.setcolWard(record.getPatient().getWard().getName());
			row.setTooltipForcolWard(record.getPatient().getWard().getName());
		}

		row.setcolCarePlan(record.getCarePlan().getTitle());
		row.setTooltipForcolCarePlan(record.getCarePlan().getTitle());
		row.setcolNextEvaluetionDate(record.getCarePlan().getNextEvaluationDate().toString());

		if (record.getCarePlan().getConfirmedBy() != null)
		{
			row.setcolHCP(record.getCarePlan().getConfirmedBy().getName().toString());
			row.setTooltipForcolHCP(record.getCarePlan().getConfirmedBy().getName().toString());
		}
		row.setcolStatus(record.getCarePlan().getCurrentCarePlanStatus().getStatus().toString());

		if (record.getCarePlan().getNextEvaluationDate().isLessThan(new Date()))
		{
			row.setBackColor(ims.configuration.gen.ConfigFlag.UI.CAREPLAN_WORKLIST_OVERDUE_COLOUR.getValue());
		}
		else if (record.getCarePlan().getNextEvaluationDate().isGreaterOrEqualThan(new Date()) && record.getCarePlan().getNextEvaluationDate().isLessOrEqualThan((new Date()).addDay(ims.configuration.gen.ConfigFlag.DOM.CAREPLAN_WORKLIST_TO_BE_REVIEWED_RANGE.getValue())))
		{
			row.setBackColor(ims.configuration.gen.ConfigFlag.UI.CAREPLAN_WORKLIST_DUE_COLOUR.getValue());
		}

		row.setValue(record);

	}

	private CarePlanWorklistSearchVo populateSearchCriteriaFromScreen()
	{
		CarePlanWorklistSearchVo criteria = new CarePlanWorklistSearchVo();

		criteria.setHCP((HcpLiteVo) form.ccHCP().getValue());
		criteria.setWard(form.cmbWard().getValue());

		if (form.dteFromDate().getValue() != null)
		{
			criteria.setDateFrom(form.dteFromDate().getValue());
		}

		if (form.dteToDate().getValue() != null)
		{
			criteria.setDateTo(form.dteToDate().getValue());
		}

		// if (form.dteFromDate().getValue()==null &&
		// form.dteToDate().getValue()==null)
		// {
		// criteria.setDateTo((new
		// Date()).addDay(ims.configuration.gen.ConfigFlag.DOM.CAREPLAN_WORKLIST_TO_BE_REVIEWED_RANGE.getValue()));
		// }

		if (form.grpStatus().getValue().equals(grpStatusEnumeration.rdoActive))
			criteria.setActive(true);
		else
			criteria.setActive(false);

		return criteria;
	}

	private void clearScreen()
	{
		form.ccHCP().setValue(null);
		form.cmbWard().setValue(null);
		form.dteFromDate().setValue(null);
		form.dteToDate().setValue(null);
		form.grpStatus().setValue(grpStatusEnumeration.rdoActive);
		form.grdResult().setValue(null);
		form.grdResult().getRows().clear();
		form.lblTotal().setValue("Total : 0");

	}

	@Override
	protected void onGrdResultSelectionChanged() throws PresentationLogicException
	{
		form.getGlobalContext().Core.setCurrentCareContext(form.grdResult().getSelectedRow().getValue().getCarePlan().getCareContext());
		form.getGlobalContext().COE.setCarePlanID(form.grdResult().getSelectedRow().getValue().getCarePlan().getID_CarePlan());
		engine.open(form.getForms().Nursing.CarePlanDetails);

	}

	@Override
	protected void onRadioButtongrpStatusValueChanged() throws PresentationLogicException
	{
		// TODO Auto-generated method stub

	}
}
