gt: 			for (int i = 0; i < screenErrors.size(); i++)
context: //#############################################################################
  //#                                                                           #
  //#  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 Peter Martin using IMS Development Environment (version 1.71 build 3642.24101)
  // Copyright (C) 1995-2010 IMS MAXIMS. All rights reserved.
  
  package ims.racpc.forms.racpcpmhexam;
  
  import java.util.ArrayList;
  
  import ims.clinical.vo.RACPPastMedicalHistoryVo;
  import ims.core.vo.AuthoringInformationVo;
  import ims.core.vo.HcpLiteVo;
  import ims.core.vo.VSBloodPressure;
  import ims.core.vo.VSMetrics;
  import ims.core.vo.VSPulse;
  import ims.core.vo.VitalSignsVo;
  import ims.core.vo.lookups.AuscultationFinding;
  import ims.core.vo.lookups.AuscultationFindingCollection;
  import ims.core.vo.lookups.ECGFindingsCollection;
  import ims.core.vo.lookups.LookupHelper;
  import ims.core.vo.lookups.RACPRelevantDiagnosisListCollection;
  import ims.domain.exceptions.StaleObjectException;
  import ims.framework.enumerations.FormMode;
  import ims.framework.utils.DateTime;
  import ims.racpc.forms.racpcpmhexam.GenForm.grdAuscultationRow;
  import ims.racpc.forms.racpcpmhexam.GenForm.grdAuscultationRowCollection;
  import ims.racpc.forms.racpcpmhexam.GenForm.grdPMHRow;
  import ims.racpc.forms.racpcpmhexam.GenForm.grdRestingECGRow;
  
  public class Logic extends BaseLogic
  {
  	private static final long serialVersionUID = 1L;
  
  	@Override
  	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
  	{
  		initialise();
  		open();
  	}
  	private void open() 
  	{
  		clearScreen();
  		
  		if(form.getGlobalContext().Core.getCurrentCareContextIsNotNull())
  		{
  			RACPPastMedicalHistoryVo voRACPPastMedicalHistory = domain.getRACPPastMedicalHistoryForrefCareContext(form.getGlobalContext().Core.getCurrentCareContext());
  			form.getLocalContext().setRACPPastMedicalHistoryVo(voRACPPastMedicalHistory);
  			if(voRACPPastMedicalHistory!=null)
  			{
  				populateScreen(voRACPPastMedicalHistory);
  				removeUnselectedAuscultations(null);
  			}
  		}
  		
  		form.setMode(FormMode.VIEW);//	WDEV-13437
  	}
  	private void populateScreen(RACPPastMedicalHistoryVo voRACPPastMedicalHistory) {
  		form.chkNone().setValue(voRACPPastMedicalHistory.getPMHFlag());
  		form.txtOtherNotes().setValue(voRACPPastMedicalHistory.getPMHNotes());
  		if (voRACPPastMedicalHistory.getVSBPIsNotNull())
  		{
  			// Siting
  			if (voRACPPastMedicalHistory.getVSBP().getBPSittingSysIsNotNull())
  			{
  				form.intBPDia().setValue(new Integer(voRACPPastMedicalHistory.getVSBP().getBPSittingDias().intValue()));
  				form.intBPSys().setValue(new Integer(voRACPPastMedicalHistory.getVSBP().getBPSittingSys().intValue()));
  			}
  		}
  		if (voRACPPastMedicalHistory.getVSPulseIsNotNull())
  		{
  			if (voRACPPastMedicalHistory.getVSPulse().getPulseRateRadial() != null)
  				form.intHeartRate().setValue(voRACPPastMedicalHistory.getVSPulse().getPulseRateRadial());
  		}
  		if (voRACPPastMedicalHistory.getHeightWeightIsNotNull())
  		{
  			form.decHeight().setValue(voRACPPastMedicalHistory.getHeightWeight().getHeightValue());
  			form.decWeight().setValue(voRACPPastMedicalHistory.getHeightWeight().getWeightValue());
  		}
  		form.txtExaminationNotes().setValue(voRACPPastMedicalHistory.getExamNotes());
  		form.txtRestingECGNotes().setValue(voRACPPastMedicalHistory.getECGNotes());
  	
  		loadRelevantDiagnosisGrid(true);
  		
  		if(voRACPPastMedicalHistory.getExamFindingsIsNotNull())
  			populateAuscultationsGrid(voRACPPastMedicalHistory.getExamFindings());
  		
  		loadRestingECGGrid(true);
  		
  	}
  	private void populateAuscultationsGrid(AuscultationFindingCollection findings) {
  		
  		form.grdAuscultation().getRows().clear();
  		
  		AuscultationFindingCollection collAuscultationFinding = form.getLocalContext().getAuscultationFindingCollection();
  		
  		for (int i = 0; i < collAuscultationFinding.size(); i++)
  		{
  			if(collAuscultationFinding.get(i).getParent()==null && hasSelectedItem(collAuscultationFinding.get(i), findings))
  					putItemHierarchyOnGrid(collAuscultationFinding.get(i), form.grdAuscultation().getRows().newRow());
  		}
  		
  		setSelectedAuscultations();
  		
  		form.grdAuscultation().expandAll();
  	}
  	private void setSelectedAuscultations() {
  		AuscultationFindingCollection collFindings = form.getLocalContext().getRACPPastMedicalHistoryVo().getExamFindings();
  		if(collFindings != null){
  			boolean bFindingsFound = false; 
  			for (int i = 0; i < collFindings.size(); i++)
  			{
  				bFindingsFound = false;
  				for (int j = 0; j < form.grdAuscultation().getRows().size(); j++)
  				{
  					grdAuscultationRow rowParent = form.grdAuscultation().getRows().get(j);
  					bFindingsFound = searchTreeHierarchy(rowParent, collFindings.get(i));
  					if(bFindingsFound)
  						j = form.grdAuscultation().getRows().size();
  				}
  			}
  		}
  		
  	}
  	private boolean searchTreeHierarchy(grdAuscultationRow rowParent, AuscultationFinding finding) {
  		boolean bPlanFound = false;
  		if(rowParent.getRows().size()==0)
  			if(rowParent.getcolFinding().equals(finding)){
  				rowParent.setcolSelect(true);
  				return true;
  			}
  		for (int j = 0; j < rowParent.getRows().size(); j++)
  		{
  			grdAuscultationRow rowChild = rowParent.getRows().get(j);
  			if(rowChild.getcolFinding().equals(finding)){
  				rowChild.setcolSelect(true);
  				return true;
  			}
  			
  			bPlanFound = searchTreeHierarchy(rowChild, finding);
  			
  		}
  		
  		return bPlanFound;		
  	}
  	private void putItemHierarchyOnGrid(AuscultationFinding finding, grdAuscultationRow row) {
  		row.setcolFinding(finding);
  		if(finding.getChildInstances().size()>0)
  			for (int i = 0; i < finding.getChildInstances().size(); i++)
  			{
  				putItemHierarchyOnGrid((AuscultationFinding)finding.getChildInstances().get(i), row.getRows().newRow());
  			}
  	}
  	private boolean hasSelectedItem(AuscultationFinding finding, AuscultationFindingCollection findings) {
  		form.getLocalContext().setHasSelectedItem(false);
  			
  		if(!form.getLocalContext().getHasSelectedItem())
  			for (int i = 0; i < findings.size(); i++)
  				searchHierarchyForFinding( finding, findings.get(i));
  		
  		return form.getLocalContext().getHasSelectedItem();
  	}
  	private void searchHierarchyForFinding(AuscultationFinding selectedFinding, AuscultationFinding finding) {
  		if(selectedFinding.getChildInstances().size()==0)
  			if(selectedFinding.equals(finding))
  				form.getLocalContext().setHasSelectedItem(true);
  		for (int j = 0; j < selectedFinding.getChildInstances().size(); j++)
  		{
  			searchHierarchyForFinding( (AuscultationFinding) selectedFinding.getChildInstances().get(j), finding);
  		}		
  	}
  	
  	private void clearScreen() {
  		form.chkNone().setValue(false);
  		form.txtOtherNotes().setValue("");
  		form.intBPDia().setValue(null);
  		form.intBPSys().setValue(null);
  		form.intHeartRate().setValue(null);
  		form.decHeight().setValue(null);
  		form.decWeight().setValue(null);
  		form.txtExaminationNotes().setValue("");
  		form.txtRestingECGNotes().setValue("");
  		
  		for (int i = 0; i < form.grdAuscultation().getRows().size(); i++)
  		{
  			grdAuscultationRow row = form.grdAuscultation().getRows().get(i);
  			row.setcolSelect(false);
  			
  			clearTreeHierarchy(row);
  		}
  		for (int i = 0; i < form.grdPMH().getRows().size(); i++)
  		{
  			grdPMHRow row = form.grdPMH().getRows().get(i);
  			row.setcolSelect(false);
  		}
  		for (int i = 0; i < form.grdRestingECG().getRows().size(); i++)
  		{
  			grdRestingECGRow row = form.grdRestingECG().getRows().get(i);
  			row.setcolSelect(false);
  		}
  	}
  	private void clearTreeHierarchy(grdAuscultationRow row) {
  		for (int j = 0; j < row.getRows().size(); j++)
  		{
  			grdAuscultationRow rowChild = row.getRows().get(j);
  			rowChild.setcolSelect(false);
  			
  			clearTreeHierarchy(rowChild);
  		}
  	}
  	private void initialise() {
  		prePopulateGrids();
  		
  	}
  	private void prePopulateGrids() {
  		loadRelevantDiagnosisGrid(false);
  		loadAuscultationGrid(false);		
  		loadRestingECGGrid(false);	
  		
  	}
  	private void loadRestingECGGrid(boolean bOnUpdate) {
  		form.grdRestingECG().getRows().clear();
  		
  		ECGFindingsCollection collECGFindings = LookupHelper.getECGFindings(domain.getLookupService());
  		form.getLocalContext().setECGFindingsCollection(collECGFindings);	
  		
  		for (int i = 0; i < collECGFindings.size(); i++)
  		{
  			grdRestingECGRow row = form.grdRestingECG().getRows().newRow();
  			row.setcolFinding(collECGFindings.get(i).getText());
  			row.setValue(collECGFindings.get(i));
  		}		
  		
  		if(bOnUpdate && form.getLocalContext().getRACPPastMedicalHistoryVoIsNotNull()
  				&& form.getLocalContext().getRACPPastMedicalHistoryVo().getECGFindingsIsNotNull()){
  			ECGFindingsCollection currentECGFindingsColl = form.getLocalContext().getRACPPastMedicalHistoryVo().getECGFindings();
  			for (int i = 0; i < form.grdRestingECG().getRows().size(); i++)
  			{
  				grdRestingECGRow row = form.grdRestingECG().getRows().get(i);
  				for (int j = 0; j < currentECGFindingsColl.size(); j++)
  				{
  					if(row.getValue()!=null && row.getValue().equals(currentECGFindingsColl.get(j)))
  						row.setcolSelect(true);
  				}
  			}
  		}
  	}
  	private void loadAuscultationGrid(boolean bOnUpdate) {
  		AuscultationFindingCollection collAuscultationFinding = LookupHelper.getAuscultationFinding(domain.getLookupService());
  		form.getLocalContext().setAuscultationFindingCollection(collAuscultationFinding);
  		form.grdAuscultation().populateTree(collAuscultationFinding);
  		form.grdAuscultation().expandAll();
  		if(form.getLocalContext().getRACPPastMedicalHistoryVoIsNotNull()
  				&& form.getLocalContext().getRACPPastMedicalHistoryVo().getExamFindingsIsNotNull()
  				&& bOnUpdate){
  			setSelectedAuscultations(form.getLocalContext().getRACPPastMedicalHistoryVo().getExamFindings());
  		}
  	}
  	private void setSelectedAuscultations(AuscultationFindingCollection examFindings) {
  		for (int i = 0; i < form.grdAuscultation().getRows().size(); i++){
  			grdAuscultationRow row = form.grdAuscultation().getRows().get(i);
  			AuscultationFinding value = row.getcolFinding();
  			for (int j = 0; j < examFindings.size(); j++){
  				if(value.equals(examFindings.get(j)))							
  							row.setcolSelect(true);
  				else if(value.equals(examFindings.get(j)))						
  						row.setcolSelect(true);
  			}
  			setSelectedChildAuscultations(row, examFindings);
  		}
  	}
  	private void removeUnselectedAuscultations(grdAuscultationRowCollection collection) {
  		if(collection==null)
  			collection = form.grdAuscultation().getRows();
  		for (int i = 0; i <collection.size(); i++)
  		{
  			grdAuscultationRow rowParent = collection.get(i);
  			if(rowParent.getRows()!=null && rowParent.getRows().size()>0)
  				removeUnselectedAuscultations(rowParent.getRows());
  			else{
  				if(!rowParent.getcolSelect())
  					collection.remove(i);
  			}
  		}
  		
  	}
  	private void setSelectedChildAuscultations(grdAuscultationRow row, AuscultationFindingCollection examFindings) {
  		for (int i = 0; i < row.getRows().size(); i++){
  			grdAuscultationRow childRow = row.getRows().get(i);
  			AuscultationFinding value = childRow.getcolFinding();
  			for (int j = 0; j < examFindings.size(); j++){
  				if(value.equals(examFindings.get(j)))							
  						childRow.setcolSelect(true);
  				else if(value.equals(examFindings.get(j)))						
  					childRow.setcolSelect(true);
  			}
  		}
  		
  	}
  	private void loadRelevantDiagnosisGrid(boolean bOnUpdate) {
  		form.grdPMH().getRows().clear();
  		
  		RACPRelevantDiagnosisListCollection collRACPRelevantDiagnosisList = LookupHelper.getRACPRelevantDiagnosisList(domain.getLookupService());
  		form.getLocalContext().setRACPRelevantDiagnosisListCollection(collRACPRelevantDiagnosisList);	
  		
  		for (int i = 0; i < collRACPRelevantDiagnosisList.size(); i++)
  		{
  			grdPMHRow row = form.grdPMH().getRows().newRow();
  			row.setcolDiag(collRACPRelevantDiagnosisList.get(i).getText());
  			row.setValue(collRACPRelevantDiagnosisList.get(i));
  		}		
  		
  		if(bOnUpdate && form.getLocalContext().getRACPPastMedicalHistoryVoIsNotNull()
  				&& form.getLocalContext().getRACPPastMedicalHistoryVo().getRelevantDiagnosisIsNotNull()){
  			RACPRelevantDiagnosisListCollection currentRACPRelevantDiagnosisListColl = form.getLocalContext().getRACPPastMedicalHistoryVo().getRelevantDiagnosis();
  			for (int i = 0; i < form.grdPMH().getRows().size(); i++)
  			{
  				grdPMHRow row = form.grdPMH().getRows().get(i);
  				for (int j = 0; j < currentRACPRelevantDiagnosisListColl.size(); j++)
  				{
  					if(row.getValue()!=null && row.getValue().equals(currentRACPRelevantDiagnosisListColl.get(j)))
  						row.setcolSelect(true);
  				}
  			}
  		}
  	}
  	@Override
  	protected void onBtnNewClick() throws ims.framework.exceptions.PresentationLogicException
  	{
  		if(isRACPCComplete())//	WDEV-13662
  			return;
  		
  		form.getLocalContext().setRACPPastMedicalHistoryVo(null);
  		form.setMode(FormMode.EDIT);
  		clearScreen();
  		loadRelevantDiagnosisGrid(false);
  		loadAuscultationGrid(false);		
  		loadRestingECGGrid(false);	
  	}
  	@Override
  	protected void onBtnEditClick() throws ims.framework.exceptions.PresentationLogicException
  	{
  		if(isRACPCComplete())//	WDEV-13662
  			return;
  		
  		form.setMode(FormMode.EDIT);
  		
  		loadRelevantDiagnosisGrid(true);
  		loadAuscultationGrid(true);		
  		loadRestingECGGrid(true);	
  	}
  	@Override
  	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
  	{
  		if (save())
  			open();
  	}
  	private boolean save() 
  	{
  		if(isRACPCComplete())//	WDEV-13662
  			return false;
  		
  		RACPPastMedicalHistoryVo  voRACPPastMedicalHistory = populateDataFromScreen(form.getLocalContext().getRACPPastMedicalHistoryVo());
  		
  		ArrayList<String> screenErrors = new ArrayList<String>();
  		if ((form.intBPDia().getValue() != null && form.intBPSys().getValue() == null)
  				|| (form.intBPDia().getValue() == null && form.intBPSys().getValue() != null))
  				screenErrors.add("Please enter both Blood Pressure values.");
  		if (form.decHeight().getValue() != null && form.decWeight().getValue() == null)
  				screenErrors.add("If Height is being entered Weight is also being required for BMI.");
  		if(form.decHeight().getValue() == null && form.decWeight().getValue() != null)
  				screenErrors.add("If Weight is being entered Height is also being required for BMI.");
  		
  		VitalSignsVo voVitalSign = domain.getVitalSignsForCareContext(form.getGlobalContext().Core.getCurrentCareContext());
  		if(voVitalSign==null)
  			voVitalSign = new VitalSignsVo();
  		
  		boolean bUpdateVitalSigns = populateVitalSignsVo(voVitalSign, voRACPPastMedicalHistory);
  						
  		String[] arrScreenAndVoErrors = null;
  		String[] arrErrors = voRACPPastMedicalHistory.validate();
  		String[] arrVitalSignErrors = voVitalSign.validate();
  		
  		if(arrErrors!=null)
  			arrScreenAndVoErrors = addScreenErrorsToVOErrors(screenErrors, arrErrors);
  		else
  			arrScreenAndVoErrors = addScreenErrorsToVOErrors(screenErrors, arrVitalSignErrors);
  		if (arrScreenAndVoErrors.length == 0)
  		{
  			try
  			{
  				voRACPPastMedicalHistory = domain.saveRACPPastMedicalHistoryVo(voRACPPastMedicalHistory, voVitalSign, bUpdateVitalSigns);
  			}
  			catch (StaleObjectException e)
  			{
  				engine.showMessage(ims.configuration.gen.ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
  				open();
  				return false;
  			}
  		}else
  		{
  			engine.showErrors(arrScreenAndVoErrors);
  			return false;
  		}
  						
  		form.getLocalContext().setRACPPastMedicalHistoryVo(voRACPPastMedicalHistory);
  		return true;
  	}
  	private boolean populateVitalSignsVo(VitalSignsVo voVitalSign, RACPPastMedicalHistoryVo voRACPPastMedicalHistory) {
  		
  		boolean bUpdateVitalSigns = false;
  		
  		if(voVitalSign.getAuthoringInformation()==null)
  			voVitalSign.setAuthoringInformation(getAuthoringInfo());
  
  		if(voVitalSign.getVitalsTakenDateTime()==null)
  			voVitalSign.setVitalsTakenDateTime(new DateTime());
  
  		if(voVitalSign.getCareContext()==null)
  			voVitalSign.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
  		
  		if(voVitalSign.getBloodPressure()==null 
  				|| (voVitalSign.getBloodPressureIsNotNull() && voVitalSign.getBloodPressure().getBPSittingDias()==null) ){
  			voVitalSign.setBloodPressure(voRACPPastMedicalHistory.getVSBP());
  			bUpdateVitalSigns = true;
  		}
  		
  		if(voVitalSign.getMetrics()==null 
  				|| (voVitalSign.getMetricsIsNotNull() && voVitalSign.getMetrics().getHeightValue()==null) ){
  			voVitalSign.setMetrics(voRACPPastMedicalHistory.getHeightWeight());
  			bUpdateVitalSigns = true;
  		}
  		
  		if(voVitalSign.getPulse()==null 
  				|| (voVitalSign.getPulseIsNotNull() && voVitalSign.getPulse().getPulseRateRadial()==null) ){
  			voVitalSign.setPulse(voRACPPastMedicalHistory.getVSPulse());
  			bUpdateVitalSigns = true;
  		}
  		
  		return bUpdateVitalSigns;
  		
  	}
  	private AuthoringInformationVo getAuthoringInfo() {
  		AuthoringInformationVo voAuthInfo = new AuthoringInformationVo();
  		voAuthInfo.setAuthoringDateTime(new DateTime());
  		voAuthInfo.setAuthoringHcp((HcpLiteVo) domain.getHcpLiteUser());
  				
  		return voAuthInfo;
  	}
  	private String[] addScreenErrorsToVOErrors(ArrayList screenErrors, String[] arrErrors)
  	{
  		String[] arrAllErrors = null;
  
  		if (arrErrors != null)
  		{
  			arrAllErrors = new String[screenErrors.size() + arrErrors.length];
  