/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/forms/careplanaddaction/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 Barbara Worwood using IMS Development Environment (version 1.18 build 40630.1415)
// Copyright (C) 1995-2004 IMS MAXIMS plc. All rights reserved.

package ims.nursing.forms.careplanaddaction;

import ims.configuration.gen.ConfigFlag;
import ims.framework.enumerations.DialogResult;
import ims.nursing.vo.CarePlanTemplateIntervention;
import ims.nursing.vo.CarePlanTemplateInterventionCollection;


public class Logic extends BaseLogic
{
	protected void onFormOpen() throws ims.framework.exceptions.FormOpenException
	{
		CarePlanTemplateInterventionCollection coll = form.getGlobalContext().COE.getCarePlanTemplateInterventions();
		if (coll != null)
		{
			GenForm.gridActionsRow row;
			form.gridActions().getRows().clear();
			for (int i=0; i<coll.size(); i++)
			{
				CarePlanTemplateIntervention interv = coll.get(i);
				//27/10/2004 - Sinead request - don't display the inactive
				if(interv.getActiveIsNotNull() && interv.getActive().booleanValue())
				{
					interv.setIntervention(interv.getIntervention());
					row = form.gridActions().getRows().newRow();
					row.setColActions(interv.getIntervention());
					row.setValue(interv);
				}
			}
		}
	}
	
	protected void onBCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{
		form.getGlobalContext().COE.setCarePlanTemplateInterventions(null);
		engine.close(DialogResult.CANCEL);
	}
	protected void onBSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{
		// Build up the collection of selected actions and return them to the careplandetails form
		GenForm.gridActionsRow row;
		CarePlanTemplateInterventionCollection intervList = new CarePlanTemplateInterventionCollection();
		
		for (int i=0; i<form.gridActions().getRows().size(); i++)
		{
			row = form.gridActions().getRows().get(i);
			if (row.getColSelected() == true)
			{
				CarePlanTemplateIntervention interv = row.getValue();
				if (interv == null)
				{
					// Manually entered action
					interv = new CarePlanTemplateIntervention();
					interv.setActive(Boolean.TRUE);
					interv.setIntervention(row.getColActions());
				}
				intervList.add(interv);
			}

		}
		form.getGlobalContext().COE.setCarePlanTemplateInterventions(intervList);
		engine.close(DialogResult.OK);
	}
	protected void onBAddClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (form.textAction().getValue() == null || form.textAction().equals(""))
		{
			engine.showMessage("Cannot add an empty action");
			return;
		}
		
		Integer x = new Integer(form.textAction().getValue().length());
		Integer config = new Integer(ConfigFlag.UI.CAREPLAN_ACTION_TEXT_LENGTH.getValue());
		if(x.intValue() > config.intValue())
		{
			engine.showMessage("Care Plan Action can be no more than " + ConfigFlag.UI.CAREPLAN_ACTION_TEXT_LENGTH.getValue() + " characters");
			return;
		}
		
		GenForm.gridActionsRow row = form.gridActions().getRows().newRow();
		row.setColActions(form.textAction().getValue());
		row.setColSelected(true);
		form.textAction().setValue("");
	}
}
