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

package ims.pathways.forms.pathwaynotecorrectiondialog;

import java.util.ArrayList;

import ims.configuration.gen.ConfigFlag;
import ims.core.vo.enums.AuthoringLabelType;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.enumerations.DialogResult;
import ims.framework.exceptions.PresentationLogicException;
import ims.pathways.vo.PatientJourneyCommentVo;

public class Logic extends BaseLogic
{
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void onFormOpen(Object[] args) throws PresentationLogicException
	{
		initialise();		
	}
	
	private void initialise()
	{
		form.ccAuthoring().initializeComponent();
		form.ccAuthoring().setLabels(AuthoringLabelType.AUTHORING);
		form.ccAuthoring().setIsRequiredPropertyToControls(Boolean.TRUE);	
	}

	@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()
	{
		PatientJourneyCommentVo recordToSave = populateDataFromScreen();
		
		String[] errors1 = recordToSave.validate(validateUiRules());
		
		if (errors1 != null && errors1.length > 0)
		{
			engine.showErrors(errors1);
			return false;
		}
		
		try
		{
			domain.saveCurrentNote(recordToSave);
		}
		catch (StaleObjectException e)
		{
			engine.showMessage(ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
			engine.close(DialogResult.ABORT);
		}
		
		return true;
	}

	private String[] validateUiRules()
	{
		ArrayList<String> errors = new ArrayList<String>();
		
		if (form.txtCorrection().getValue() == null)
		{
			errors.add("Correction Comments is mandatory.");				
		}

		if (form.ccAuthoring().getErrors() != null)
		{
			errors.add(form.ccAuthoring().getErrors());
			
		}
		
		if (errors.size() == 0)
			return null;
		
		return errors.toArray(new String[errors.size()]); 
	}

	private PatientJourneyCommentVo populateDataFromScreen()
	{
		PatientJourneyCommentVo recordToSave = domain.getCurrentNote(form.getGlobalContext().Pathways.getPatientJourneySelectedComment());
		
		if (recordToSave == null)
			return null;
			
		recordToSave.setCorrectedAuthoringInformation(form.ccAuthoring().getValue());
		recordToSave.setCorrected(true);
		recordToSave.setCorrectionComments(form.txtCorrection().getValue());

		return recordToSave;
	}
}
