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

package ims.nursing.forms.careplantemplates;

import ims.framework.exceptions.PresentationLogicException;
import ims.nursing.vo.CarePlanTemplateTitleCollection;
import ims.nursing.vo.CarePlanTemplateTitle;
import ims.nursing.vo.lookups.AssessmentComponentType;

public class Logic extends BaseLogic
{
	protected void onFormOpen() throws ims.framework.exceptions.FormOpenException
	{	
		//27/10/2004 - Display the active records by default 
		form.chkActive().setValue(true);
		populateGrid();
		
		form.btnNew().setEnabled(true);
		form.btnUpdate().setEnabled(false);
	
	}
	protected void onBNewClick() throws ims.framework.exceptions.PresentationLogicException
	{
		form.getGlobalContext().COE.setCarePlanTemplateTitle(null);
		engine.open(form.getForms().Nursing.CarePlanTemplateDetail);
	}

	protected void onGridTemplatesSelectionChanged() throws PresentationLogicException 
	{
		form.btnUpdate().setEnabled(true);
	}
	
	protected void onCareplanTemplateUpdate() throws ims.framework.exceptions.PresentationLogicException
	{
		form.getGlobalContext().COE.setCarePlanTemplateTitle(form.gridTemplates().getValue());						
		engine.open(form.getForms().Nursing.CarePlanTemplateDetail);
	}

	private void populateGrid() 
	{
		CarePlanTemplateTitleCollection templateTitleColl;
		templateTitleColl = domain.listCarePlanTemplates();
		
		form.gridTemplates().getRows().clear();	
		
		GenForm.gridTemplatesRow row;							
		//Fill in the grid if there is any data
		for (int i=0; i<templateTitleColl.size(); i++)
		{
			CarePlanTemplateTitle voCpt = templateTitleColl.get(i);
			//27/10/2004 - Sinead request - if "Active Only" checked than display only the active records
			if(form.chkActive().getValue() && voCpt.getActive().booleanValue() == false)
				continue;
			
			row = form.gridTemplates().getRows().newRow();
			row.setValue(voCpt);

			row.setColCreationDate(voCpt.getCreationDate());
			row.setColTitle(voCpt.getTitle());
			AssessmentComponentType comp = voCpt.getAssessmentComponent();
			if(comp !=null)
				row.setColAssessmentComponent(comp.getText());
			
			if(voCpt.getActive().booleanValue())
			{
				row.setColIsActive(form.getImages().Core.AnswerBox_Yes);
			}
			else
			{
				row.setColIsActive(form.getImages().Core.AnswerBox_No);
			}
		}
	}
	protected void onChkActiveValueChanged() throws PresentationLogicException 
	{
		CarePlanTemplateTitleCollection templateTitleColl;
		templateTitleColl = domain.listCarePlanTemplates();

		form.gridTemplates().getRows().clear();	

		GenForm.gridTemplatesRow row;
		//Fill in the grid with Active Care Plans	
		if(form.chkActive().getValue()== true)
		{
			for (int i=0; i<templateTitleColl.size(); i++)
			{
				CarePlanTemplateTitle voCpt = templateTitleColl.get(i);
				
				if(voCpt.getActive().booleanValue()== true)
				{
					row = form.gridTemplates().getRows().newRow();
					row.setValue(voCpt);
					
					row.setColCreationDate(voCpt.getCreationDate());
					row.setColTitle(voCpt.getTitle());
					AssessmentComponentType comp = voCpt.getAssessmentComponent();
					//WDEV-935 - 3) Extra issue found: - Assessment Component crash in Care Plan Templates
					if(comp != null)
						row.setColAssessmentComponent(comp.getText());
					row.setColIsActive(form.getImages().Core.AnswerBox_Yes);
				}	
			}
		}
		else
		{
			//Fill in the grid with all Care Plans
			populateGrid();
		}
		
		//WDEV-935
		form.btnUpdate().setEnabled(false);
	}
	
}
