/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Pathways/src/ims/pathways/forms/rttstatuspointselectdialog/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 Gabriel Maxim using IMS Development Environment (version 1.80 build 5007.25751)
// Copyright (C) 1995-2013 IMS MAXIMS. All rights reserved.

package ims.pathways.forms.rttstatuspointselectdialog;

import ims.framework.enumerations.DialogResult;
import ims.framework.enumerations.FormMode;
import ims.framework.enumerations.SortOrder;
import ims.pathways.forms.rttstatuspointselectdialog.GenForm.grdRecordsRow;
import ims.pathways.vo.RTTStatusPointVo;
import ims.pathways.vo.RTTStatusPointVoCollection;

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

	@Override
	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		if (args != null && args.length > 0)
		{
			if (args[0] instanceof RTTStatusPointVo)
			{

				RTTStatusPointVoCollection coll = listRTTPoints(args);
				if (args[1] instanceof RTTStatusPointVoCollection)
				{
					form.getGlobalContext().Pathways.setFollowingRTTStatusPoints((RTTStatusPointVoCollection) args[1]);
					
				}
				populateGrid(coll.sort(SortOrder.ASCENDING), form.getGlobalContext().Pathways.getFollowingRTTStatusPoints());
			}		
		}
		form.setMode(FormMode.EDIT);
	}

	private RTTStatusPointVoCollection listRTTPoints(Object[] args)
	{
		RTTStatusPointVoCollection coll = domain.listRTTStatusPoints();
	
		if (coll == null || coll.size() == 0)
			return null;
		//WDEV-18594 - now it is possible to have a RTT status child of itself
		/*for (int i=0; i<coll.size(); i++)
		{
			if (coll.get(i) != null && (coll.get(i).getID_RTTStatusPoint().equals(((RTTStatusPointVo) args[0]).getID_RTTStatusPoint())))
			{
				coll.remove(i);
				break;
			}		
		}*/
		return coll;
	}

	private void populateGrid(RTTStatusPointVoCollection coll,RTTStatusPointVoCollection existingChilds)
	{
		form.grdRecords().getRows().clear();
		
		if (coll == null || coll.size() == 0)
			return;	
		
		for (int i =0;i<coll.size();i++)
		{	
			if (coll.get(i) == null)
				continue;	
			grdRecordsRow row = form.grdRecords().getRows().newRow();
			row.setcolNatCode(coll.get(i).getNationalCode());
			row.setcolDescription(coll.get(i).getDescriptionIsNotNull() ? coll.get(i).getDescription() : "");
			row.setCellcolDescriptionTooltip(coll.get(i).getDescriptionIsNotNull() ? coll.get(i).getDescription() : "");
			row.setcolLocalCode(coll.get(i).getLocalCodeIsNotNull() ? coll.get(i).getLocalCode() : "");
			if (existingChilds != null && existingChilds.size() > 0)
			{
				for (RTTStatusPointVo vo : existingChilds)
				{
					if (coll.get(i).getID_RTTStatusPointIsNotNull() && vo.getID_RTTStatusPoint().equals(coll.get(i).getID_RTTStatusPoint()))
						row.setcolSelect(Boolean.TRUE);
				}
			}	
			row.setValue(coll.get(i));

		}
	}
	
	@Override
	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{
		engine.close(DialogResult.CANCEL);
	}
	
	@Override
	protected void onBtnOKClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (form.getGlobalContext().Pathways.getFollowingRTTStatusPoints() == null)
		{
			form.getGlobalContext().Pathways.setFollowingRTTStatusPoints(new RTTStatusPointVoCollection());
		}
		for (int i=0; i< form.grdRecords().getRows().size(); i++)
		{
			grdRecordsRow row = form.grdRecords().getRows().get(i);

			if (row.getValue() instanceof RTTStatusPointVo && row.getcolSelect())
			{	
				form.getGlobalContext().Pathways.getFollowingRTTStatusPoints().add(row.getValue());
			}
			else
			{	
				RTTStatusPointVoCollection dummycoll = form.getGlobalContext().Pathways.getFollowingRTTStatusPoints();
				if (dummycoll.size() > 0)
				{	
					for (int j=0; j<dummycoll.size();j++)
					{					
						if (row.getValue().getID_RTTStatusPoint().equals(dummycoll.get(j).getID_RTTStatusPoint()) && !row.getcolSelect())
							dummycoll.remove(j);
					}
				}
				form.getGlobalContext().Pathways.setFollowingRTTStatusPoints(dummycoll);	
			}
		}
		engine.close(DialogResult.OK);
	}	
}
