/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Pathways/src/ims/pathways/forms/patientjourneybreachreason/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 Cristian Belciug using IMS Development Environment (version 1.80 build 5308.16958)
// Copyright (C) 1995-2014 IMS MAXIMS. All rights reserved.

package ims.pathways.forms.patientjourneybreachreason;

import ims.configuration.gen.ConfigFlag;
import ims.core.vo.MemberOfStaffLiteVo;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.enumerations.DialogResult;
import ims.framework.enumerations.FormMode;
import ims.framework.utils.DateTime;
import ims.pathways.vo.PatientJourneyBreachReasonVo;
import ims.pathways.vo.PatientJourneyBreachReasonVoCollection;
import ims.pathways.vo.PatientJourneyWithBreachReasonVo;

import java.util.ArrayList;
import java.util.List;

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 initialise()
	{
		Object mos = domain.getMosUser();
		
		if(mos instanceof MemberOfStaffLiteVo)
		{
			form.getLocalContext().setCurrentMOS((MemberOfStaffLiteVo) mos);
		}
		
	}

	private void open()
	{
		clearScreen();
		
		form.txtMOS().setValue(form.getLocalContext().getCurrentMOS() != null ? form.getLocalContext().getCurrentMOS().getIMosName() : "");
		form.dtimRecording().setValue(new DateTime());
		
		form.setMode(FormMode.EDIT);
	}

	private void clearScreen()
	{
		form.txtMOS().setValue(null);
		form.dtimRecording().setValue(null);
		form.cmbReason().setValue(null);
		form.richTextComment().setValue(null);
	}

	@Override
	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{
		engine.close(DialogResult.CANCEL);
	}
	
	@Override
	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if(save())
			engine.close(DialogResult.OK);
	}

	private boolean save()
	{
		PatientJourneyBreachReasonVo record = populateDataFromScreen();
		String[] errors = record.validate(validateUIRules());
		
		if(errors != null && errors.length > 0)	
		{
			engine.showErrors(errors);
			return false;
		}
		
		PatientJourneyWithBreachReasonVo recordtoSave = domain.getPatientJourney(form.getGlobalContext().Pathways.getSelectedPatientPathwayJourney());
		
		if (recordtoSave.getPatientJourneyBreachReason() == null)
			recordtoSave.setPatientJourneyBreachReason(new PatientJourneyBreachReasonVoCollection());
		
		recordtoSave.getPatientJourneyBreachReason().add(record);
		errors = recordtoSave.validate();
		
		if(errors != null && errors.length > 0)	
		{
			engine.showErrors(errors);
			return false;
		}
		
		try
		{
			domain.save(recordtoSave);
		}
		catch (StaleObjectException e)
		{
			engine.showMessage(ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
			engine.close(DialogResult.OK);
			return false;
		}
		
		return true;
	}

	private String[] validateUIRules()
	{
		List<String> uiErrors = new ArrayList<String>();
		
		if (form.cmbReason().getValue() == null && (form.richTextComment().getValue() == null || form.richTextComment().getValue().length() == 0))
		{
			uiErrors.add("Please complete Breach Reason or Breach Comments.");
		}
		
		
		String[] uiResults = new String[uiErrors.size()];
		uiErrors.toArray(uiResults);
		
		return uiResults;
	}

	private PatientJourneyBreachReasonVo populateDataFromScreen()
	{
		PatientJourneyBreachReasonVo currentRecord = new PatientJourneyBreachReasonVo();
		
		currentRecord.setBreachReasonRecordingMOS(form.getLocalContext().getCurrentMOS());
		currentRecord.setBreachReasonDate(form.dtimRecording().getValue());

		currentRecord.setBreachReason(form.cmbReason().getValue());
		currentRecord.setBreachReasonComment(form.richTextComment().getValue());
		
		return currentRecord;
	}
}
