/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/forms/envirnmentalchecklistdialog/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 Peter Martin using IMS Development Environment (version 1.60 build 2907.25848)
// Copyright (C) 1995-2007 IMS MAXIMS plc. All rights reserved.

package ims.nursing.forms.envirnmentalchecklistdialog;

import ims.coe.vo.AssessmentMobility;
import ims.core.vo.AuthoringInformationVo;
import ims.core.vo.HcpLiteVo;
import ims.core.vo.MemberOfStaffShortVo;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.enumerations.DialogResult;
import ims.framework.enumerations.FormMode;
import ims.framework.utils.Date;
import ims.framework.utils.DateTime;
import ims.framework.utils.Time;
import ims.nursing.vo.EnvironmentalChecklistDetailVo;
import ims.nursing.vo.EnvironmentalChecklistDetailVoCollection;
import ims.nursing.vo.EnvironmentalChecklistVo;
import ims.nursing.vo.lookups.EnvironmentalChecklistCollection;
import ims.nursing.vo.lookups.LookupHelper;
public class Logic extends BaseLogic
{
	private static final long serialVersionUID = 1L;

	@Override
	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{ 
		
		if(form.getGlobalContext().COE.getAssessmentMobility().getIsComplete())
		{
			form.setMode(FormMode.VIEW);
			
		}
		else
		{
			form.setMode(FormMode.EDIT);
		
		}
		
		EnvironmentalChecklistVo voEnvironmentalChecklistVo = form.getGlobalContext().COE.getAssessmentMobility().getEnvironmentChecklist();
		form.htmQuestion().setHTML("<h2 style=font-family:verdana align=center>Can you answer YES to all of the above questions?<br> If not, remedy any deficiencies without delay.</h2>");
		if(voEnvironmentalChecklistVo == null)
		{
			loadChecklistGridFromLookup();
			form.dteDate().setValue(new Date());
			form.timTime().setValue(new Time());
			if(domain.getMosUser() != null)
				form.txtHcp().setValue(((MemberOfStaffShortVo)domain.getMosUser()).toString());
			return;
		}
		
		
		
		
		EnvironmentalChecklistDetailVoCollection collDetails = voEnvironmentalChecklistVo.getChecklistDetails();
		
		//form.getLocalContext().setChecklistItem(voEnvironmentalChecklistVo); 
		//contine here displaying header info then get the details
		if (voEnvironmentalChecklistVo.getAuthoringInformationIsNotNull()){
			if(voEnvironmentalChecklistVo.getAuthoringInformation().getAuthoringDateTimeIsNotNull()){
				form.dteDate().setValue(voEnvironmentalChecklistVo.getAuthoringInformation().getAuthoringDateTime().getDate());
				form.timTime().setValue(voEnvironmentalChecklistVo.getAuthoringInformation().getAuthoringDateTime().getTime());
			}
			if (voEnvironmentalChecklistVo.getAuthoringInformation().getAuthoringHcpIsNotNull())
				form.txtHcp().setValue(voEnvironmentalChecklistVo.getAuthoringInformation().getAuthoringHcp().toString());
		}
		
		if(collDetails != null)
		{
			loadChecklistGridFromLookup();
			
			for(int i=0;i<collDetails.size();i++)
			{
				GenForm.grdChecklistRow row = null;
				int r=0;
				boolean bChecklistTypeFound = false;
				for(r=0;r<form.grdChecklist().getRows().size(); r++)
				{	
					row = form.grdChecklist().getRows().get(r);
				
					if(collDetails.get(i).getChecklistType().equals(row.getcolChecklistItem())){
						row.setValue(collDetails.get(i));
						row.setcolStatus(collDetails.get(i).getAnswer()); 
						
						bChecklistTypeFound = true;
					}
				}
				if(!bChecklistTypeFound)
				{
					row = form.grdChecklist().getRows().newRow();
					row.setValue(collDetails.get(i));
					row.setcolChecklistItem(collDetails.get(i).getChecklistType());
					row.setcolStatus(collDetails.get(i).getAnswer()); 
				}
			}
		}
		
	}
	private void loadChecklistGridFromLookup() {
		form.grdChecklist().getRows().clear();
		EnvironmentalChecklistCollection coll = LookupHelper.getEnvironmentalChecklist(domain.getLookupService());
		for(int i=0;i<coll.size();i++)
		{
			GenForm.grdChecklistRow row = form.grdChecklist().getRows().newRow();
			row.setcolChecklistItem(coll.get(i));		
			row.setValue(new EnvironmentalChecklistDetailVo());	
		}
		
	}

	@Override
	protected void onFormDialogClosed(ims.framework.FormName formName, ims.framework.enumerations.DialogResult result) throws ims.framework.exceptions.PresentationLogicException
	{
		if (formName.equals(form.getForms().Core.OkDialog))
			engine.close(DialogResult.OK);	
	}
	@Override
	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{
		engine.close(DialogResult.OK);
	}
	@Override
	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{
		EnvironmentalChecklistVo voEnvironmentalChecklist = populateChecklistDataFromScreen(form.getGlobalContext().COE.getAssessmentMobility().getEnvironmentChecklist());
		if (voEnvironmentalChecklist == null)
			return ;
		
		String[] arrErrors = voEnvironmentalChecklist.validate();
		if(arrErrors == null)
		{
			try
			{
				AssessmentMobility comp = form.getGlobalContext().COE.getAssessmentMobility();
				comp = domain.saveEnvironmentalChecklist(voEnvironmentalChecklist, comp);
				
				if (comp != null)
				{
					form.getGlobalContext().COE.setAssessmentMobility(comp);
				}
			}
			catch (StaleObjectException e)
			{
				engine.showMessage(ims.configuration.gen.ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
				return;
			}
			
		}	
		else
		{
			engine.showErrors("Validation Errors", arrErrors);
			return;
		}
		
		engine.close(DialogResult.OK);
	}
	private EnvironmentalChecklistVo populateChecklistDataFromScreen(EnvironmentalChecklistVo environmentalChecklist) {
		if (environmentalChecklist == null)
			environmentalChecklist = new EnvironmentalChecklistVo();
		
		if (form.getGlobalContext().Core.getCurrentCareContextIsNotNull())
			environmentalChecklist.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
		
		if (form.getGlobalContext().Core.getCurrentClinicalContactIsNotNull())
			environmentalChecklist.setClinicalContact(form.getGlobalContext().Core.getCurrentClinicalContact());
		
		AuthoringInformationVo voAuthoringInformation =  environmentalChecklist.getAuthoringInformation();
		if(voAuthoringInformation==null)
			voAuthoringInformation = new AuthoringInformationVo();
		voAuthoringInformation.setAuthoringDateTime(new DateTime(form.dteDate().getValue(),form.timTime().getValue()));
		voAuthoringInformation.setAuthoringHcp((HcpLiteVo) domain.getHcpLiteUser());
		environmentalChecklist.setAuthoringInformation(voAuthoringInformation);
		
		//retrieve details from the grid
		EnvironmentalChecklistDetailVoCollection detailsColl = new EnvironmentalChecklistDetailVoCollection();
		
		for(int i=0; i<form.grdChecklist().getRows().size();i++)
		{
			GenForm.grdChecklistRow row = form.grdChecklist().getRows().get(i);
			
			EnvironmentalChecklistDetailVo voEnvironmentalChecklistDetail = new EnvironmentalChecklistDetailVo();
			voEnvironmentalChecklistDetail.setChecklistType(row.getcolChecklistItem());
			voEnvironmentalChecklistDetail.setAnswer(row.getcolStatus());
						
			detailsColl.add(voEnvironmentalChecklistDetail);
		}	
		
		if(detailsColl.size() == 0)
		{
			engine.showMessage("You must save some Infection Control details");
			return null;
		}
		
		environmentalChecklist.setChecklistDetails(detailsColl);
		return environmentalChecklist;
	}
}
