/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/RACPC/src/ims/racpc/forms/racpcexerciseecg/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.71 build 3729.19612)
// Copyright (C) 1995-2010 IMS MAXIMS. All rights reserved.

package ims.racpc.forms.racpcexerciseecg;

import ims.clinical.vo.RACPExerciseECGVo;
import ims.clinical.vo.lookups.LookupHelper;
import ims.clinical.vo.lookups.RACPExerciseECGFindings;
import ims.clinical.vo.lookups.RACPExerciseECGFindingsCollection;
import ims.clinical.vo.lookups.RACPMedicationAtTest;
import ims.clinical.vo.lookups.RACPMedicationAtTestCollection;
import ims.clinical.vo.lookups.RACPStopTestReason;
import ims.clinical.vo.lookups.RACPStopTestReasonCollection;
import ims.core.vo.VSBloodPressure;
import ims.core.vo.VSPulse;
import ims.core.vo.VitalSignsVo;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.exceptions.UniqueKeyViolationException;
import ims.framework.enumerations.FormMode;
import ims.racpc.forms.racpcexerciseecg.GenForm.grdFindingRow;
import ims.racpc.forms.racpcexerciseecg.GenForm.grdMedsRow;
import ims.racpc.forms.racpcexerciseecg.GenForm.grdReasonRow;

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

	@Override
	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		initialise();
		open();
	}
	private void open() 
	{
		clearScreen();
		
		if(form.getGlobalContext().Core.getCurrentCareContextIsNotNull())
		{
			RACPExerciseECGVo voRACPExerciseECGVo = domain.getRACPExerciseECGForrefCareContext(form.getGlobalContext().Core.getCurrentCareContext());
			form.getLocalContext().setRACPExerciseECGVo(voRACPExerciseECGVo);
			if(voRACPExerciseECGVo!=null)
			{
				populateScreen(voRACPExerciseECGVo);
			}
		}
		
		form.setMode(FormMode.VIEW);//	WDEV-13437
	}
	
	private void populateScreen(RACPExerciseECGVo voRACPExerciseECGVo) {
		if(voRACPExerciseECGVo.getMedicationAtTestIsNotNull()){
			RACPMedicationAtTestCollection collRACPMedication = voRACPExerciseECGVo.getMedicationAtTest();
			form.grdMeds().getRows().clear();
			for (int i = 0; i < collRACPMedication.size(); i++)
			{
				RACPMedicationAtTest lkupRACPMedication = collRACPMedication.get(i);
				ims.racpc.forms.racpcexerciseecg.GenForm.grdMedsRow row = form.grdMeds().getRows().newRow();
				row.setcolMedication(lkupRACPMedication.getText());
				row.setcolSelect(true);
			}
		}
		
		if(voRACPExerciseECGVo.getStopReasonsIsNotNull()){
			RACPStopTestReasonCollection collRACPStopTestReason = voRACPExerciseECGVo.getStopReasons();
			form.grdReason().getRows().clear();
			for (int i = 0; i < collRACPStopTestReason.size(); i++)
			{
				RACPStopTestReason lkupRACPStopTestReason = collRACPStopTestReason.get(i);
				ims.racpc.forms.racpcexerciseecg.GenForm.grdReasonRow row = form.grdReason().getRows().newRow();
				row.setcolReason(lkupRACPStopTestReason.getText());
				row.setcolSelect(true);
			}
		}
		
		if(voRACPExerciseECGVo.getOtherFindingsIsNotNull()){
			RACPExerciseECGFindingsCollection collRACPExerciseECGFindings = voRACPExerciseECGVo.getOtherFindings();
			form.grdFinding().getRows().clear();
			for (int i = 0; i < collRACPExerciseECGFindings.size(); i++)
			{
				RACPExerciseECGFindings lkupRACPExerciseECGFindings = collRACPExerciseECGFindings.get(i);
				ims.racpc.forms.racpcexerciseecg.GenForm.grdFindingRow row = form.grdFinding().getRows().newRow();
				row.setcolFinding(lkupRACPExerciseECGFindings.getText());
				row.setcolSelect(true);
			}
		}
		
		form.cmbCMD().setValue(voRACPExerciseECGVo.getPreAssessesCHD());
		form.cmbProtocol().setValue(voRACPExerciseECGVo.getProtocol());
		
		// WDEV-12241 - comment out for build
		form.cmbPain().setValue(voRACPExerciseECGVo.getPainExperienced());
		form.cmbDysponea().setValue(voRACPExerciseECGVo.getDysponea());
		form.cmbResult().setValue(voRACPExerciseECGVo.getResult());
		form.cmbInterpretation().setValue(voRACPExerciseECGVo.getInterpretation());
		
		form.intDurMin().setValue(voRACPExerciseECGVo.getDurationMins());
		form.intDurSecs().setValue(voRACPExerciseECGVo.getDurationSecs());
		if(voRACPExerciseECGVo.getVSPulseIsNotNull())
			form.intPeakHeartRate().setValue(voRACPExerciseECGVo.getVSPulse().getPulseRateRadial());
		if(voRACPExerciseECGVo.getVSBPIsNotNull() && voRACPExerciseECGVo.getVSBP().getBPSittingSysIsNotNull())
			form.intPeakSysBP().setValue(voRACPExerciseECGVo.getVSBP().getBPSittingSys().intValue());
		 
		VitalSignsVo voVitalSign = domain.getVitalSignsForCareContext(form.getGlobalContext().Core.getCurrentCareContext());
		if(voVitalSign!=null){
			if(voVitalSign.getPulseIsNotNull() && voVitalSign.getPulse().getPulseRateRadialIsNotNull())
				form.lblInitialHR().setValue(voVitalSign.getPulse().getPulseRateRadial().toString());
			if(voVitalSign.getBloodPressureIsNotNull() && voVitalSign.getBloodPressure().getBPSittingSysIsNotNull())
				form.lblInitialBP().setValue(voVitalSign.getBloodPressure().getBPSittingSys().toString());
		}
		form.intPercentMac().setValue(voRACPExerciseECGVo.getMaxPredHeartRate());
		form.intEarliestSymptom().setValue(voRACPExerciseECGVo.getSympomOnset());
		form.intMAXSt().setValue(voRACPExerciseECGVo.getMaxSTChangeMM());
		form.intTimeToMin().setValue(voRACPExerciseECGVo.getMinsTo1mmChange());
		form.intTimeToSec().setValue(voRACPExerciseECGVo.getSecsTo1mmChange());
		
	}
	private void clearScreen() {
		for (int i = 0; i < form.grdMeds().getRows().size(); i++)
		{
			ims.racpc.forms.racpcexerciseecg.GenForm.grdMedsRow row = form.grdMeds().getRows().get(i);
			row.setcolSelect(false);
		}
		for (int i = 0; i < form.grdFinding().getRows().size(); i++)
		{
			ims.racpc.forms.racpcexerciseecg.GenForm.grdFindingRow row = form.grdFinding().getRows().get(i);
			row.setcolSelect(false);
		}
		for (int i = 0; i < form.grdReason().getRows().size(); i++)
		{
			ims.racpc.forms.racpcexerciseecg.GenForm.grdReasonRow row = form.grdReason().getRows().get(i);
			row.setcolSelect(false);
		}
		form.cmbCMD().setValue(null);
		form.cmbProtocol().setValue(null);
		form.cmbPain().setValue(null);
		form.cmbDysponea().setValue(null);
		form.cmbResult().setValue(null);
		form.cmbInterpretation().setValue(null);
		
		form.intDurMin().setValue(null);
		form.intDurSecs().setValue(null);
		form.intPeakHeartRate().setValue(null);
		form.intPeakSysBP().setValue(null);
		form.intPercentMac().setValue(null);
		form.intEarliestSymptom().setValue(null);
		form.intMAXSt().setValue(null);
		form.intTimeToMin().setValue(null);
		form.intTimeToSec().setValue(null);
		
		form.lblInitialBP().setValue("");
		form.lblInitialHR().setValue("");
		
	}
	private void initialise() {
		loadGrids(false);
	}
	private void loadGrids(boolean bOnUpdate) {
		loadMedicationAtTestGrid(bOnUpdate);
		loadFindingsGrid(bOnUpdate);
		loadReasonsAtTestGrid(bOnUpdate);
	}
	private void loadReasonsAtTestGrid(boolean bOnUpdate) {
		form.grdReason().getRows().clear();
		
		RACPStopTestReasonCollection collRACPStopTestReason = LookupHelper.getRACPStopTestReason(domain.getLookupService());
		form.getLocalContext().setRACPStopTestReasonCollection(collRACPStopTestReason);
		
		for (int i = 0; i < collRACPStopTestReason.size(); i++)
		{
			grdReasonRow row = form.grdReason().getRows().newRow();
			row.setcolReason(collRACPStopTestReason.get(i).getText());
			row.setValue(collRACPStopTestReason.get(i));
		}		
		
		if(bOnUpdate && form.getLocalContext().getRACPExerciseECGVo().getStopReasonsIsNotNull()){
			RACPStopTestReasonCollection currentRACPStopTestReasonColl = form.getLocalContext().getRACPExerciseECGVo().getStopReasons();
			for (int i = 0; i < form.grdReason().getRows().size(); i++)
			{
				grdReasonRow row = form.grdReason().getRows().get(i);
				for (int j = 0; j < currentRACPStopTestReasonColl.size(); j++)
				{
					if(row.getValue().equals(currentRACPStopTestReasonColl.get(j)))
						row.setcolSelect(true);
				}
			}
		}
		
	}
	private void loadFindingsGrid(boolean bOnUpdate) {
		form.grdFinding().getRows().clear();
		
		RACPExerciseECGFindingsCollection collRACPExerciseECGFindings = LookupHelper.getRACPExerciseECGFindings(domain.getLookupService());
		form.getLocalContext().setRACPExerciseECGFindingsCollection(collRACPExerciseECGFindings);
		
		for (int i = 0; i < collRACPExerciseECGFindings.size(); i++)
		{
			grdFindingRow row = form.grdFinding().getRows().newRow();
			row.setcolFinding(collRACPExerciseECGFindings.get(i).getText());
			row.setValue(collRACPExerciseECGFindings.get(i));
		}		
		
		if(bOnUpdate && form.getLocalContext().getRACPExerciseECGVo().getOtherFindingsIsNotNull()){
			RACPExerciseECGFindingsCollection currentRACPExerciseECGFindingsColl = form.getLocalContext().getRACPExerciseECGVo().getOtherFindings();
			for (int i = 0; i < form.grdFinding().getRows().size(); i++)
			{
				grdFindingRow row = form.grdFinding().getRows().get(i);
				for (int j = 0; j < currentRACPExerciseECGFindingsColl.size(); j++)
				{
					if(row.getValue().equals(currentRACPExerciseECGFindingsColl.get(j)))
						row.setcolSelect(true);
				}
			}
		}		
	}
	private void loadMedicationAtTestGrid(boolean bOnUpdate) {
		form.grdMeds().getRows().clear();
		
		RACPMedicationAtTestCollection collRACPMedicationAtTest = LookupHelper.getRACPMedicationAtTest(domain.getLookupService());
		form.getLocalContext().setRACPMedicationAtTestCollection(collRACPMedicationAtTest);
		
		for (int i = 0; i < collRACPMedicationAtTest.size(); i++)
		{
			grdMedsRow row = form.grdMeds().getRows().newRow();
			row.setcolMedication(collRACPMedicationAtTest.get(i).getText());
			row.setValue(collRACPMedicationAtTest.get(i));
		}		
		
		if(bOnUpdate && form.getLocalContext().getRACPExerciseECGVo().getMedicationAtTestIsNotNull()){
			RACPMedicationAtTestCollection currentRACPMedicationAtTestColl = form.getLocalContext().getRACPExerciseECGVo().getMedicationAtTest();
			for (int i = 0; i < form.grdMeds().getRows().size(); i++)
			{
				grdMedsRow row = form.grdMeds().getRows().get(i);
				for (int j = 0; j < currentRACPMedicationAtTestColl.size(); j++)
				{
					if(row.getValue().equals(currentRACPMedicationAtTestColl.get(j)))
						row.setcolSelect(true);
				}
			}
		}
	}
	@Override
	protected void onBtnNewClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if(isRACPCComplete())//	WDEV-13662
			return;
		
		form.getLocalContext().setRACPExerciseECGVo(null);
		form.setMode(FormMode.EDIT);
		clearScreen();
	}
	@Override
	protected void onBtnEditClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if(isRACPCComplete())//	WDEV-13662
			return;
		
		form.setMode(FormMode.EDIT);
		loadGrids(true);
	}
	@Override
	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if(save())
			open();
	}
	private boolean save() 
	{
		if(isRACPCComplete())//	WDEV-13662
			return false;
		
		RACPExerciseECGVo voRACPExerciseECG  = populateDataFromScreen(form.getLocalContext().getRACPExerciseECGVo());
		
		String[] arrErrors = voRACPExerciseECG.validate();
	
		if (arrErrors != null)
		{
			engine.showErrors(arrErrors);
			return false;
		}
		try
		{
			voRACPExerciseECG = domain.saveRACPExerciseECGVo(voRACPExerciseECG);
		}
		catch (StaleObjectException e)
		{
			engine.showMessage(ims.configuration.gen.ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
			open();
			return false;
		}
		catch (UniqueKeyViolationException e) 
		{
			engine.showMessage("A record with the same criteria already exists.   " + e.getMessage());
			open();
			return false;			
		}
				
		form.getLocalContext().setRACPExerciseECGVo(voRACPExerciseECG);
		
		return true;
	}
	
	//	WDEV-13662
	private boolean isRACPCComplete() 
	{	
		boolean isRACPCSummaryComplete = domain.isRACPCSummaryComplete(form.getGlobalContext().Core.getCurrentCareContext());//	WDEV-13662
		
		if(isRACPCSummaryComplete)//	WDEV-13662
		{
			engine.showMessage("RACPC Summary has already been completed. The screen will be refreshed.");
			open();
			return true;
		}
		
		return false;
	}
	
	private RACPExerciseECGVo populateDataFromScreen(RACPExerciseECGVo voRACPExerciseECG) {
		if(voRACPExerciseECG == null)
			voRACPExerciseECG = new RACPExerciseECGVo();
		
		if(voRACPExerciseECG.getCareContext()==null)
			voRACPExerciseECG.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
			
		voRACPExerciseECG.setPreAssessesCHD(form.cmbCMD().getValue());
		voRACPExerciseECG.setProtocol(form.cmbProtocol().getValue());
		
		// WDEV-12241
		voRACPExerciseECG.setPainExperienced(form.cmbPain().getValue());

		voRACPExerciseECG.setDysponea(form.cmbDysponea().getValue());
		voRACPExerciseECG.setResult(form.cmbResult().getValue());
		voRACPExerciseECG.setInterpretation(form.cmbInterpretation().getValue());
		
		voRACPExerciseECG.setDurationMins(form.intDurMin().getValue());
		voRACPExerciseECG.setDurationSecs(form.intDurSecs().getValue());
		if(form.intPeakHeartRate().getValue()!=null){
			if(voRACPExerciseECG.getVSPulse()==null)
				voRACPExerciseECG.setVSPulse(new VSPulse ());
			voRACPExerciseECG.getVSPulse().setPulseRateRadial(form.intPeakHeartRate().getValue());
		}
		else
			voRACPExerciseECG.setVSPulse(null);
		if(form.intPeakSysBP().getValue()!=null){
			if(voRACPExerciseECG.getVSBP()==null)
				voRACPExerciseECG.setVSBP(new VSBloodPressure());
			voRACPExerciseECG.getVSBP().setBPSittingSys(form.intPeakSysBP().getValue().floatValue());
		}
		else
			voRACPExerciseECG.setVSBP(null);			
		voRACPExerciseECG.setMaxPredHeartRate(form.intPercentMac().getValue());
		voRACPExerciseECG.setSympomOnset(form.intEarliestSymptom().getValue());
		voRACPExerciseECG.setMaxSTChangeMM(form.intMAXSt().getValue());
		voRACPExerciseECG.setMinsTo1mmChange(form.intTimeToMin().getValue());
		voRACPExerciseECG.setSecsTo1mmChange(form.intTimeToSec().getValue());
		
		voRACPExerciseECG.setMedicationAtTest(populateMedicationAtTestFromGrd());
		voRACPExerciseECG.setOtherFindings(populateOtherFindingsFromGrd());
		voRACPExerciseECG.setStopReasons(populateStopReasonsFromGrd());
		
		return voRACPExerciseECG;
	}
	private RACPStopTestReasonCollection populateStopReasonsFromGrd() {
		RACPStopTestReasonCollection collRACPStopTestReason = new RACPStopTestReasonCollection();
		
		for (int i = 0; i < form.grdReason().getRows().size(); i++)
		{
			grdReasonRow row = form.grdReason().getRows().get(i);
			if(row.getcolSelect())
				collRACPStopTestReason.add(row.getValue());
		}
		
		if(collRACPStopTestReason.size()==0)
			return null;
		
		return collRACPStopTestReason;
	}
	private RACPExerciseECGFindingsCollection populateOtherFindingsFromGrd() {
		RACPExerciseECGFindingsCollection collRACPExerciseECGFindings = new RACPExerciseECGFindingsCollection();
		
		for (int i = 0; i < form.grdFinding().getRows().size(); i++)
		{
			grdFindingRow row = form.grdFinding().getRows().get(i);
			if(row.getcolSelect())
				collRACPExerciseECGFindings.add(row.getValue());
		}
		
		if(collRACPExerciseECGFindings.size()==0)
			return null;
		
		return collRACPExerciseECGFindings;
	}
	private RACPMedicationAtTestCollection populateMedicationAtTestFromGrd() {
		RACPMedicationAtTestCollection collRACPMedicationAtTest = new RACPMedicationAtTestCollection();
		
		for (int i = 0; i < form.grdMeds().getRows().size(); i++)
		{
			grdMedsRow row = form.grdMeds().getRows().get(i);
			if(row.getcolSelect())
				collRACPMedicationAtTest.add(row.getValue());
		}
		
		if(collRACPMedicationAtTest.size()==0)
			return null;
		
		return collRACPMedicationAtTest;
	}
	@Override
	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{
		open();
	}
	
	//WDEV-13437
	@Override
	protected void onFormModeChanged() 
	{
		updateControlsState();
	}
	
	//	WDEV-13437
	private void updateControlsState() 
	{
		if(FormMode.VIEW.equals(form.getMode()))
		{
			boolean isReadOnly = domain.isRACPCSummaryComplete(form.getGlobalContext().Core.getCurrentCareContext());
			
			form.btnNew().setVisible(form.getLocalContext().getRACPExerciseECGVo() == null && !isReadOnly);
			form.btnEdit().setVisible(form.getLocalContext().getRACPExerciseECGVo() != null && !isReadOnly);
		}
	}
}
