/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Pathways/src/ims/pathways/forms/worklist/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 Daniel Laffan using IMS Development Environment (version 1.60 build 2852.26300)
// Copyright (C) 1995-2007 IMS MAXIMS plc. All rights reserved.

package ims.pathways.forms.worklist;

import ims.configuration.gen.ConfigFlag;
import ims.core.vo.PatientShort;
import ims.core.vo.enums.MosType;
import ims.core.vo.lookups.PatIdType;
import ims.core.vo.lookups.SpecialtyCollection;
import ims.domain.exceptions.DomainInterfaceException;
import ims.framework.MessageButtons;
import ims.framework.enumerations.DialogResult;
import ims.framework.exceptions.CodingRuntimeException;
import ims.framework.exceptions.PresentationLogicException;
import ims.pathways.configuration.vo.PathwayRefVoCollection;
import ims.pathways.configuration.vo.TargetRefVo;
import ims.pathways.forms.worklist.GenForm.grdDetailsRow;
import ims.pathways.forms.worklist.GenForm.grdPathwaysRow;
import ims.pathways.forms.worklist.GenForm.grdTargetsRow;
import ims.pathways.vo.PathwayLiteVoCollection;
import ims.pathways.vo.PatientJourneyTargetVo;
import ims.pathways.vo.PatientJourneyTargetVoCollection;
import ims.pathways.vo.TargetConfigurationLiteVo;
import ims.pathways.vo.TargetConfigurationLiteVoCollection;
import ims.pathways.vo.WorkListSearchCriteriaVo;
import ims.pathways.vo.WorkListTargetCriteriaVo;
import ims.pathways.vo.WorkListTargetCriteriaVoCollection;
import ims.pathways.vo.lookups.EventLinkType;

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

import com.ims.query.builder.client.QueryBuilderClient;
import com.ims.query.builder.client.SeedValue;
import com.ims.query.builder.client.exceptions.QueryBuilderClientException;

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

	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		initialize();
	}
	private void initialize()
	{
		loadSpecialtiesCombo();
		form.cmbSpecialty().setFocus();
		//form.customControlConsultant().setHcpType(HcpDisType.MEDICAL);
		
		//WDEV-5193 
		form.customControlMos().initialize(MosType.MEDIC);
		
		//setting the text of the PatId Column
		PatIdType dispIdType = PatIdType.getNegativeInstance(ConfigFlag.UI.DISPLAY_PATID_TYPE.getValue()); 
		form.grdDetails().setColHospNoCaption(dispIdType.getText());

		restoreSearchCriteria();
	}
	
	private void restoreSearchCriteria()
	{
		WorkListSearchCriteriaVo voCriteria = form.getGlobalContext().Pathways.getWorkListSearchCriteria();
		
		if(voCriteria != null)
		{
			form.cmbSpecialty().setValue(voCriteria.getSpecialty());
			//form.customControlConsultant().setValue(voCriteria.getConsultant());
			form.customControlMos().setValue(voCriteria.getConsultant());
			displayPathways();
			
			PathwayRefVoCollection voCollPathways = voCriteria.getPathways();
			if(voCollPathways != null && form.grdPathways().getRows().size() > 0)
			{
				if(voCollPathways.size() <= form.getLocalContext().getPathways().size())
				{
					for(int i=0;i<form.grdPathways().getRows().size();i++)
					{
						grdPathwaysRow pRow = form.grdPathways().getRows().get(i);
						if(voCollPathways.contains(pRow.getColName()))
						{
							pRow.setColSelect(true);
						}
					}
				}
			}
			
			WorkListTargetCriteriaVoCollection voCollTargetCriteria = voCriteria.getConditions();
			if(voCollTargetCriteria != null)
			{
				for(int i=0;i<voCollTargetCriteria.size();i++)
					addTargetCriteriaRow(voCollTargetCriteria.get(i));
			}
			
			search();
		}
	}
	
	private void addTargetCriteriaRow(WorkListTargetCriteriaVo voCriteria)
	{
		grdTargetsRow row = addTargetRow();
		if(row == null)
			return;
		
		row.getColTarget().setValue(voCriteria.getTarget());
		row.setColBreachStatus(voCriteria.getStatus());
		row.setColWeeks(voCriteria.getNoOfWeeks());
	}
	
	private void loadSpecialtiesCombo()
	{
		form.cmbSpecialty().clear();
		SpecialtyCollection collSpecialties = domain.listSpecialtiesForActivePathways();
		if(collSpecialties != null)
		{
			for(int i=0;i<collSpecialties.size();i++)
			{
				form.cmbSpecialty().newRow(collSpecialties.get(i), collSpecialties.get(i).getText());
			}
		}
	}
	
	protected void onFormDialogClosed(ims.framework.FormName formName, ims.framework.enumerations.DialogResult result) throws ims.framework.exceptions.PresentationLogicException
	{
		if (result.equals(DialogResult.OK) && formName.equals(form.getForms().Core.Reports))
		{
			printReport();
		}
	}

	protected void onGrdPathwaysSelectionChanged() throws ims.framework.exceptions.PresentationLogicException
	{
		updateContextMenuState();
	}

	protected void onImbClearClick() throws ims.framework.exceptions.PresentationLogicException
	{
		clearScreen();
		form.getGlobalContext().Pathways.setWorkListSearchCriteria(null);
		form.getGlobalContext().Core.setPatientShort(null); //WDEV-17914
		form.getLocalContext().setSearchCriteria(null);
	}

	private void clearScreen()
	{
		form.cmbSpecialty().setValue(null);
		//form.customControlConsultant().clear();
		form.customControlMos().clear();
		form.grdPathways().getRows().clear();
		form.grdTargets().getRows().clear();
		form.grdDetails().getRows().clear();
		form.grdDetails().setFooterValue("Total records : 0");
		updateContextMenuState();
	}
	
	protected void onImbSearchClick() throws ims.framework.exceptions.PresentationLogicException
	{	
		form.grdDetails().getRows().clear();
		form.getGlobalContext().Core.setPatientShort(null); //WDEV-17914
		
		String[] errors = validateSearchCriteria();
		if(errors == null)
			search();
		else
			engine.showErrors(errors);
		
		updateContextMenuState();
	}

	private void search()
	{		
		storeSearchCriteria();
		PatientJourneyTargetVoCollection journeyTargets = domain.listPatientJourneyTargetsByCriteria(getSelectedPathways(),getTargetCriteria(),form.customControlMos().getValue());
		populateJourneyTargetGridFromData(journeyTargets);	
		form.grdDetails().setFooterValue("Total records : " + form.grdDetails().getRows().size());
	}
	
	private void storeSearchCriteria()
	{
		WorkListSearchCriteriaVo voCriteria = getSearchCriteria();
		form.getGlobalContext().Pathways.setWorkListSearchCriteria(voCriteria);
		form.getLocalContext().setSearchCriteria(voCriteria);
	}
	private WorkListSearchCriteriaVo getSearchCriteria()
	{
		WorkListSearchCriteriaVo voCriteria = new WorkListSearchCriteriaVo();
		
		voCriteria.setSpecialty(form.cmbSpecialty().getValue());
		//voCriteria.setConsultant(form.customControlConsultant().getValue());
		voCriteria.setConsultant(form.customControlMos().getValue());
		
		voCriteria.setPathways(getSelectedPathways());
		voCriteria.setConditions(getTargetCriteria());
		return voCriteria;
	}
	
	private void populateJourneyTargetGridFromData(PatientJourneyTargetVoCollection journeyTargets)
	{
		form.grdDetails().getRows().clear();
		if(journeyTargets == null || journeyTargets.size() == 0)
		{
			engine.showMessage("No matching records found");
			return;
		}
		
		for(PatientJourneyTargetVo item : journeyTargets)
		{
			GenForm.grdDetailsRow row = form.grdDetails().getRows().newRow();
			
			if(item.getPathwayJourneyIsNotNull())
			{
				PatientShort voPatientShort = item.getPathwayJourney().getPatient();
				if(voPatientShort != null)
				{
					if(voPatientShort.getDisplayId() != null)
						row.setColHospNo(voPatientShort.getDisplayId().getValue());
					
					if(voPatientShort.getNameIsNotNull())
					{
						row.setColSurname(voPatientShort.getName().getSurname());
						row.setColForename(voPatientShort.getName().getForename());
					}
					if(voPatientShort.getDobIsNotNull())
						row.setColDob(voPatientShort.getDob().toString());
				}
				
				if(item.getPathwayJourney().getPathwayIsNotNull())
				{
					if(item.getPathwayJourney().getPathway().getSpecialtyIsNotNull())
						row.setColSpecialty(item.getPathwayJourney().getPathway().getSpecialty().getText());
					
					row.setColPathway(item.getPathwayJourney().getPathway().getName());
				}	
			}
			
			if(item.getPathwayTargetIsNotNull())
			{
				if(item.getPathwayTarget().getTargetIsNotNull())
				{
					row.setColTarget(item.getPathwayTarget().getTarget().getName());
				}
			}
			
			if(item.getCurrentStatusIsNotNull() && item.getCurrentStatus().getStatusIsNotNull())
				row.setColStatus(item.getCurrentStatus().getStatus().getText());
			
		
			row.setTextColor(item.getColour());
			row.setValue(item);
		}
	}
	
	private WorkListTargetCriteriaVoCollection getTargetCriteria()
	{
		WorkListTargetCriteriaVoCollection criteriaColl = new WorkListTargetCriteriaVoCollection();
		
		for(int i=0;i<form.grdTargets().getRows().size();i++)
		{
			WorkListTargetCriteriaVo criteria = new WorkListTargetCriteriaVo();
			grdTargetsRow row = form.grdTargets().getRows().get(i);
			criteria.setStatus(row.getColBreachStatus());
			criteria.setNoOfWeeks(row.getColWeeks());
			criteria.setTarget((TargetRefVo) row.getColTarget().getValue());
			criteriaColl.add(criteria);
		}
		return criteriaColl;
	}
	private PathwayRefVoCollection getSelectedPathways()
	{
		PathwayLiteVoCollection pathways = new PathwayLiteVoCollection();
		if(form.grdPathways().getRows().size() > 0)
		{
			for(int i=0;i<form.grdPathways().getRows().size();i++)
			{
				grdPathwaysRow row = form.grdPathways().getRows().get(i);
				if(row.getColSelect())
					pathways.add(row.getColName());
			}
		}
		
		if(pathways.size() == 0 && form.getLocalContext().getPathwaysIsNotNull())
			pathways = form.getLocalContext().getPathways();
		
		return pathways.toRefVoCollection();
	}
	private String[] validateSearchCriteria()
	{
		List<String> errors = new ArrayList<String>();
		
		if(form.cmbSpecialty().getValue() == null)
			errors.add("Specialty is mandatory");
		if(!targetRowExistsWithStatusSelected())
			errors.add("One Target is mandatory");
		if(toManyWeeks())
			errors.add("Target 'No. of Weeks' cannot be greater than 9999");
		
		WorkListTargetCriteriaVoCollection voCollCriteria = getTargetCriteria();
		String[] critErrors = voCollCriteria.validate();
		
			
		String[] arrErrors = null;
		
		if(errors.size() > 0 || critErrors != null)
		{
			arrErrors = new String[errors.size() + (critErrors != null ? critErrors.length : 0)];
			for(int i=0;i<errors.size();i++)
				arrErrors[i] = errors.get(i);
			
			if(critErrors != null)
			{
				for(int i=0;i<critErrors.length;i++)
					arrErrors[errors.size() + i] = critErrors[i];
			}
		}
			
		return arrErrors;
	}
	
	private boolean toManyWeeks() {
		if(form.grdTargets().getRows().size() == 0)
			return false;
		
		for(int i=0;i<form.grdTargets().getRows().size();i++)
		{
			grdTargetsRow row = form.grdTargets().getRows().get(i);
			if(row.getColWeeks() != null && row.getColWeeks()>9999)
				return true;
		}
		
		return false;
	}
	private boolean targetRowExistsWithStatusSelected()
	{	
		if(form.grdTargets().getRows().size() == 0)
			return false;
		
		for(int i=0;i<form.grdTargets().getRows().size();i++)
		{
			grdTargetsRow row = form.grdTargets().getRows().get(i);
			if(row.getColBreachStatus() != null)
				return true;
		}
		
		return false;
	}
	
	protected void onGrdDetailsSelectionChanged() throws ims.framework.exceptions.PresentationLogicException
	{
		//WDEV-17529
		if (form.grdDetails().getValue() != null && form.grdDetails().getValue().getPathwayJourney() != null)
		{
			form.getGlobalContext().Core.setPatientShort(form.grdDetails().getValue().getPathwayJourney().getPatient());
		}
		
		updateContextMenuState();
	}

	protected void onContextMenuItemClick(int menuItemID, ims.framework.Control sender) throws ims.framework.exceptions.PresentationLogicException
	{
		if(sender.equals(form.grdDetails()))
		{
			switch (menuItemID)
			{
				case GenForm.ContextMenus.PathwaysNamespace.WorkListJourneyTarget.NEW_EVENT:
					addPatientEvent();
				break;
						
				case GenForm.ContextMenus.PathwaysNamespace.WorkListJourneyTarget.VIEW_PAT_JOURNEY:
					viewPatientJourney();
				break;
				
				default:
					
			}
		}
		else if(sender.equals(form.grdTargets()))
		{
			switch (menuItemID)
			{
				case GenForm.ContextMenus.PathwaysNamespace.WokListPathwayTarget.ADDTARGET:
					addTargetRow();
				break;
				
				case GenForm.ContextMenus.PathwaysNamespace.WokListPathwayTarget.REMOVETARGET:
					removeTargetRow();
				break;
				
				default:
			}
		}
	}
	
	private void addAllPathways()
	{
		PathwayLiteVoCollection pathways = form.getLocalContext().getPathways();
		form.grdPathways().getRows().clear();
		
		if(pathways == null)
			return;
		
		for(int i=0;i<pathways.size();i++)
		{
			grdPathwaysRow row = form.grdPathways().getRows().newRow();
			row.setColName(pathways.get(i));
		}
	}
	
	private void viewPatientJourney()
	{
		if(form.grdDetails().getSelectedRow() == null)
			throw new CodingRuntimeException("No selected row in grdDetails in method viewPatientJourney");
		
		form.getGlobalContext().Pathways.setSelectedPatientPathwayJourney(form.grdDetails().getValue().getPathwayJourney());
		form.getGlobalContext().Core.setPatientShort(form.grdDetails().getValue().getPathwayJourney().getPatient());
		engine.open(form.getForms().Pathways.PatientJourney);
	}
	
	private void addPatientEvent()
	{
		form.getGlobalContext().Pathways.setSelectedPatientJourneyTarget(form.grdDetails().getValue());
		engine.open(form.getForms().Pathways.CreateNewEvent);
	}
	
	private void removeTargetRow()
	{
		form.grdTargets().removeSelectedRow();
		updateContextMenuState();
	}
	
	private grdTargetsRow addTargetRow()
	{
		if(form.getLocalContext().getTargets() == null)
			form.getLocalContext().setTargets(domain.listTargetsForSelectedPathways(getSelectedPathways()));	
			
		TargetConfigurationLiteVoCollection targets = form.getLocalContext().getTargets();
		
		grdTargetsRow row = null;
		if(targets != null && targets.size() > 0)
		{
			row = form.grdTargets().getRows().newRow();
			for(int i=0;i<targets.size();i++)
			{
				TargetConfigurationLiteVo target = targets.get(i);
				row.getColTarget().newRow(target, target.getName());
			}
		}
		
		return row;
	}
	
	protected void onCmbSpecialtyValueChanged() throws PresentationLogicException
	{
		clearSearchCriteriaGrids();
		displayPathways();
	}
	private void displayPathways()
	{
		listPathways();
		addAllPathways();
		updateContextMenuState();
		form.getLocalContext().setTargets(null);
	}
	
	private void updateContextMenuState()
	{	
		form.getContextMenus().Pathways.getWorkListJourneyTargetNEW_EVENTItem().setVisible(form.grdDetails().getSelectedRow() != null && !isTargetTypeAutomatic());
		form.getContextMenus().Pathways.getWorkListJourneyTargetVIEW_PAT_JOURNEYItem().setVisible(form.grdDetails().getSelectedRow() != null);

		form.getContextMenus().Pathways.getWokListPathwayTargetADDTARGETItem().setVisible(getSelectedPathways().size() > 0);
		form.getContextMenus().Pathways.getWokListPathwayTargetREMOVETARGETItem().setVisible(form.grdTargets().getSelectedRow() != null);
		if(form.cmbSpecialty().getValue()==null)
			form.getContextMenus().Pathways.getWokListPathwayTargetADDTARGETItem().setVisible(false);
			
	}
	
	private boolean isTargetTypeAutomatic()
	{
		if(form.grdDetails().getSelectedRow() == null)
			throw new CodingRuntimeException("Cannot check target type - selected row is null in method isTargetTypeAutomatic");
		
		if(form.grdDetails().getSelectedRow().getValue() != null &&  form.grdDetails().getSelectedRow().getValue().getPathwayTargetIsNotNull() && form.grdDetails().getSelectedRow().getValue().getPathwayTarget().getTargetIsNotNull() && form.grdDetails().getSelectedRow().getValue().getPathwayTarget().getTarget().getEventLinkTypeIsNotNull())
			return form.grdDetails().getSelectedRow().getValue().getPathwayTarget().getTarget().getEventLinkType().equals(EventLinkType.AUTOMATIC);
		
		return false;
	}
	private void listPathways()
	{
		form.getLocalContext().setPathways(domain.listActivePathwaysBySpecialty(form.cmbSpecialty().getValue()));
	}
	
	private void clearSearchCriteriaGrids()
	{
		form.grdPathways().getRows().clear();
		form.grdTargets().getRows().clear();
	}
	
	protected void onGrdPathwaysGridCheckBoxClicked(int column, grdPathwaysRow row, boolean isChecked) throws PresentationLogicException
	{
		form.grdTargets().getRows().clear();	
		updateContextMenuState();
	}
	
	protected void onBtnPrintClick() throws PresentationLogicException
	{
		if(form.grdDetails().getRows().size() == 0)
		{
			engine.showMessage("There are no records to print !");
			return;
		}
		
		if(isSearchDirty(getSearchCriteria(), form.getLocalContext().getSearchCriteria()))
			engine.showMessage("Search Results do not reflect the criteria, Do you still wish to Print?", "Warning", MessageButtons.YESNO);
		else
			engine.open(form.getForms().Core.Reports);	
	}
	
	/**
	 * compare all the fields used to search currently against what was originally searched - if any are different return true
	 * @param currentCriteria
	 * @param localStoredCriteria
	 * @return
	 */
	private boolean isSearchDirty(WorkListSearchCriteriaVo currentCriteria, WorkListSearchCriteriaVo localStoredCriteria)
	{
		if(currentCriteria == null || localStoredCriteria == null)
			return false;
	
		if(!currentCriteria.getSpecialty().equals(localStoredCriteria.getSpecialty()))
			return true;
		
		if(currentCriteria.getConsultantIsNotNull())
		{
			if(!currentCriteria.getConsultant().equals(localStoredCriteria.getConsultant()))
				return true;
		}
		
		//check pathways
		if(currentCriteria.getPathways().size() != localStoredCriteria.getPathways().size())
			return true;
		
		for(int i=0;i<currentCriteria.getPathways().size();i++)
		{
			if(!localStoredCriteria.getPathways().contains(currentCriteria.getPathways().get(i)))
				return true;
		}
		
		//check conditions
		if(currentCriteria.getConditions().size() != localStoredCriteria.getConditions().size())
			return true;
		
		int equalCount = 0;
		for(int i=0;i<currentCriteria.getConditions().size();i++)
		{
			WorkListTargetCriteriaVo voStoredCondition = currentCriteria.getConditions().get(i);
			for(int p=0;p<localStoredCriteria.getConditions().size();p++)
			{
				WorkListTargetCriteriaVo voLocalCondition = localStoredCriteria.getConditions().get(p);
				if(voLocalCondition.equals(voStoredCondition))
					equalCount++;
			}
		}
	
		if(equalCount != currentCriteria.getConditions().size())
			return true;
		
		
		return false;
	}
	private void printReport()
	{
		String urlQueryServer = ConfigFlag.GEN.QUERY_SERVER_URL.getValue();
		String urlReportServer = ConfigFlag.GEN.REPORT_SERVER_URL.getValue();

		String[] obj = null;
		try
		{
			obj = domain.getReportAndTemplate(form.getGlobalContext().Core.getReportID(), form.getGlobalContext().Core.getTemplateID());
		}
		catch (DomainInterfaceException e)
		{
			engine.showMessage("Error retrieving report template !\r\n" + e.getMessage());
			return;
		}

		if (obj == null || obj.length == 0)
		{
			engine.showMessage("I could not get the report and template !");
			return;
		}

		PatIdType dispIdType = null; 
		
		if(ConfigFlag.UI.DISPLAY_PATID_TYPE.getValue() == null || ConfigFlag.UI.DISPLAY_PATID_TYPE.getValue().length() == 0)
			dispIdType = ims.core.vo.lookups.PatIdType.HOSPNUM;
		else
			dispIdType = PatIdType.getNegativeInstance(ConfigFlag.UI.DISPLAY_PATID_TYPE.getValue());
		
		if (dispIdType == null) 
			dispIdType = ims.core.vo.lookups.PatIdType.HOSPNUM;		
		
		QueryBuilderClient client = new QueryBuilderClient(urlQueryServer, engine.getSessionId());

		for (int i = 0; i < form.grdDetails().getRows().size(); i++)
		{
			grdDetailsRow row = form.grdDetails().getRows().get(i);
			
			if(row.getValue() != null)
			{
				client.addSeed(new SeedValue("ID", row.getValue().getID_PatientJourneyTarget(), Integer.class));		
				
				if(row.getValue().getColour() == null || row.getValue().getColour().getValue() == null || row.getValue().getColour().getValue().length() != 7)
					client.addSeed(new SeedValue("COLOR", "0x000000", String.class));
				else
				{
					String color = row.getValue().getColour().getValue().substring(1);
					
					int r = Integer.parseInt(color.substring(0, 2), 16);
					int g = Integer.parseInt(color.substring(2, 4), 16);
					int b = Integer.parseInt(color.substring(4, 6), 16);
					
					int rgb = r | g << 8 | b << 16;
					
					client.addSeed(new SeedValue("COLOR", "0x" + Integer.toHexString(rgb), String.class));
				}
			}
		}
		
		//To-do dummy value, to be deleted
		//client.addSeed(new SeedValue("ID", new Integer(3), Integer.class));

		client.addSeed(new SeedValue("SPECIALTY", form.cmbSpecialty().getValue() != null ? form.cmbSpecialty().getValue().getText() : "", String.class));
		client.addSeed(new SeedValue("CONSULTANT", form.customControlMos().getValue() != null ? form.customControlMos().getValue().getIMosName() : "", String.class));

		boolean added = false;
		for (int i = 0; i < form.grdPathways().getRows().size(); i++)
		{
			grdPathwaysRow row = form.grdPathways().getRows().get(i);
			
			if(row.getColSelect())
			{
				client.addSeed(new SeedValue("PATHWAY", row.getColName(), String.class));
				added = true;
			}
		}
		
		if(added == false)
			client.addSeed(new SeedValue("PATHWAY", null, String.class));
		
		added = false;
		for (int i = 0; i < form.grdTargets().getRows().size(); i++)
		{
			grdTargetsRow row = form.grdTargets().getRows().get(i);
			
			client.addSeed(new SeedValue("TARGET", row.getColTarget() != null ? row.getColTarget().getEditedText() : null, String.class));		
			client.addSeed(new SeedValue("BREACH_STATUS", row.getColBreachStatus(), String.class));
			client.addSeed(new SeedValue("WEEKS", row.getColWeeks(), Integer.class));
			
			added = true;
		}

		if(added == false)
		{
			client.addSeed(new SeedValue("TARGET", "", String.class));		
			client.addSeed(new SeedValue("BREACH_STATUS", "", String.class));
			client.addSeed(new SeedValue("WEEKS", "", Integer.class));
		}
		
		client.addSeed(new SeedValue("PAT_ID_TYPE", new Integer(dispIdType.getId()), Integer.class));

		String resultUrl = "";

		try
		{
			resultUrl = client.buildReportAsUrl(obj[0], obj[1], urlReportServer, QueryBuilderClient.PDF, "", 1);
		}
		catch (QueryBuilderClientException e1)
		{
			engine.showMessage("Error creating report: " + e1.getMessage());
			return;
		}

		engine.openUrl(resultUrl);
	}
	
	protected void onGrdTargetsSelectionChanged() throws PresentationLogicException
	{
		updateContextMenuState();
	}
	
	protected void onMessageBoxClosed(int messageBoxId, DialogResult result) throws PresentationLogicException
	{
		if(result.equals(DialogResult.OK))
			printReport();
	}
	
}
