/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Pathways/src/ims/pathways/forms/createnewevent/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.60 build 2874.29117)
// Copyright (C) 1995-2007 IMS MAXIMS plc. All rights reserved.

package ims.pathways.forms.createnewevent;

import ims.domain.exceptions.DomainInterfaceException;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.enumerations.DialogResult;
import ims.framework.exceptions.PresentationLogicException;
import ims.framework.utils.DateTime;
import ims.pathways.configuration.vo.EventRefVo;
import ims.pathways.configuration.vo.TargetRefVo;
import ims.pathways.configuration.vo.TargetRefVoCollection;
import ims.pathways.forms.createnewevent.GenForm.grdEventsRow;
import ims.pathways.vo.EventLiteVo;
import ims.pathways.vo.EventTargetLiteVo;
import ims.pathways.vo.EventTargetLiteVoCollection;
import ims.pathways.vo.PatientEventVo;
import ims.pathways.vo.PatientJourneyTargetVo;
import ims.pathways.vo.PatientJourneyTargetVoCollection;
import ims.pathways.vo.lookups.EventCreationType;
import ims.pathways.vo.lookups.EventCreationTypeCollection;

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

	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		form.btnSave().setVisible(false);
		form.dtimEvent().setValue(new DateTime());
		
		form.dtimScheduled().setVisible(false);
		form.lblScheduled().setVisible(false);
		
		listEvents();
	}

	private void listEvents()
	{
		EventTargetLiteVoCollection voCollEventTargets = null;
		EventCreationTypeCollection collTypes = new EventCreationTypeCollection();
		
		collTypes.add(EventCreationType.INTERNAL);
		collTypes.add(EventCreationType.BOTH);
		
		voCollEventTargets = domain.listEventTargetsByTypesAndTargets(collTypes, buildTargetRefVoCollection());
		
		populateGridFromData(voCollEventTargets);
	}


	private TargetRefVoCollection buildTargetRefVoCollection()
	{
		TargetRefVoCollection voCollTargets = new TargetRefVoCollection();
		if(form.getGlobalContext().Pathways.getSelectedPatientJourneyTargetIsNotNull())
			voCollTargets.add(form.getGlobalContext().Pathways.getSelectedPatientJourneyTarget().getPathwayTarget().getTarget());
		else if(form.getGlobalContext().Pathways.getSelectedPatientPathwayJourneyIsNotNull())
		{
			PatientJourneyTargetVoCollection voCollPatientJourneyTarget = domain.listCurrentPathwayJourneyTargets(form.getGlobalContext().Pathways.getSelectedPatientPathwayJourney(), form.getGlobalContext().Pathways.getSelectedPathway());
			if(voCollPatientJourneyTarget != null)
			{
				for (int i = 0; i < voCollPatientJourneyTarget.size(); i++)
				{
					voCollTargets.add(voCollPatientJourneyTarget.get(i).getPathwayTarget().getTarget());
				}
			}
		}
			
		return voCollTargets;
	}

	private void populateGridFromData(EventTargetLiteVoCollection voCollEventTargets)
	{
		form.grdEvents().getRows().clear();
		if(voCollEventTargets == null || voCollEventTargets.size() == 0)
		{
			engine.showMessage("No Events available");
			engine.close(DialogResult.CANCEL);
			return;
		}

		for(int i=0;i<voCollEventTargets.size();i++)
		{
			EventTargetLiteVo voEventTarget = voCollEventTargets.get(i);
			
			grdEventsRow row = null;
			if(i == 0)
				row = addTargetRow(voEventTarget);
			else
			{
				//go through grid finding target rows
				boolean bRowFound = false;
				for(int p=0;p<form.grdEvents().getRows().size();p++)
				{
					row = form.grdEvents().getRows().get(p);
					if(row.getValue().equals(voEventTarget.getTarget()))
					{
						bRowFound = true;
						break;
					}
				}
				
				if(!bRowFound)
					row = addTargetRow(voEventTarget);
			}
			
			addEventRow(row, voEventTarget);
		}
	}

	private void addEventRow(grdEventsRow row, EventTargetLiteVo voEventTarget)
	{
		grdEventsRow eRow = row.getRows().newRow();
		eRow.setColEvent(voEventTarget.getEvent().getName());
		eRow.setValue(voEventTarget.getEvent());
	}

	private grdEventsRow addTargetRow(EventTargetLiteVo voEventTarget)
	{
		grdEventsRow row = form.grdEvents().getRows().newRow();
		row.setColEvent(voEventTarget.getTarget().getName());
		row.setValue(voEventTarget.getTarget());
		row.setSelectable(false);
		row.setExpanded(true);
		
		return row;
	}

	protected void onBtnCancelClick() throws PresentationLogicException
	{
		engine.close(DialogResult.CANCEL);
	}

	protected void onBtnSaveClick() throws PresentationLogicException
	{
		if(form.dtimEvent().getValue() == null)
		{
			engine.showErrors(new String[] {"Event Date time is mandatory"});
			return;
		}
		
		PatientEventVo voEvent = new PatientEventVo();
		voEvent.setEventDateTime(form.dtimEvent().getValue());
		voEvent.setScheduledDate(form.dtimScheduled().getValue());
		voEvent.setEvent((EventRefVo) form.grdEvents().getValue());
		PatientJourneyTargetVo voPatientJourneyTarget = form.getGlobalContext().Pathways.getSelectedPatientJourneyTarget();
		if(voPatientJourneyTarget == null)
		{
			 voPatientJourneyTarget = domain.getPatientJourneyTarget(form.getGlobalContext().Pathways.getSelectedPatientPathwayJourney(), (TargetRefVo)form.grdEvents().getSelectedRow().getParentRow().getValue());
		}
		
		if(voPatientJourneyTarget != null)
		{
			voEvent.setJourney(voPatientJourneyTarget.getPathwayJourney());
			if(voPatientJourneyTarget.getPathwayJourneyIsNotNull())
				voEvent.setPatient(voPatientJourneyTarget.getPathwayJourney().getPatient());
			
			voEvent.setPatientTarget(voPatientJourneyTarget);
			if(voPatientJourneyTarget.getPathwayJourney().getPathwayIsNotNull())
				voEvent.setSpecialty(voPatientJourneyTarget.getPathwayJourney().getPathway().getSpecialty());
		}
		
		try
		{
			domain.savePatientEvent(voEvent);
		}
		catch (DomainInterfaceException e)
		{
			engine.showMessage(e.getMessage());
		}
		catch (StaleObjectException e)
		{
			//unreachable in this instance creating new record
		}
		
		engine.close(DialogResult.OK);
	}

	protected void onGrdEventsSelectionChanged() throws PresentationLogicException
	{
		if(form.grdEvents().getValue() instanceof EventLiteVo)
		{
			boolean isScheduled = ((EventLiteVo)form.grdEvents().getValue()).getIsManualScheduledEventIsNotNull() && ((EventLiteVo)form.grdEvents().getValue()).getIsManualScheduledEvent();
			form.dtimScheduled().setVisible(isScheduled);
			form.lblScheduled().setVisible(isScheduled);
			if(!isScheduled)
				form.dtimScheduled().setValue(null);
		}
		
		form.btnSave().setVisible(form.grdEvents().getValue() != null);
	}
}
