gt: 				  if (cpRow.getcolSelect() && !cpRow.isReadOnly())
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 Daniel Laffan using IMS Development Environment (version 1.22 build 41206.1400)
  // Copyright (C) 1995-2004 IMS MAXIMS plc. All rights reserved.
  
  package ims.spinalinjuries.forms.nurassessmentbowels;
  
  import ims.core.vo.AuthoringInformationVo;
  import ims.core.vo.HcpLiteVo;
  import ims.core.vo.MedicationDose;
  import ims.core.vo.MedicationDoseCollection;
  import ims.core.vo.MedicationLiteVo;
  import ims.core.vo.MedicationLiteVoCollection;
  import ims.core.vo.PatientMedicationVo;
  import ims.core.vo.PatientMedicationVoCollection;
  import ims.domain.exceptions.DomainRuntimeException;
  import ims.domain.exceptions.StaleObjectException;
  import ims.framework.Control;
  import ims.framework.enumerations.FormMode;
  import ims.framework.exceptions.PresentationLogicException;
  import ims.framework.utils.DateTime;
  import ims.nursing.vo.AssessmentHeaderInfo;
  import ims.nursing.vo.BowelDiarrhoeaConstipationVo;
  import ims.nursing.vo.BowelDiarrhoeaConstipationVoCollection;
  import ims.nursing.vo.BowelOstomyVo;
  import ims.nursing.vo.BowelRegimeVo;
  import ims.nursing.vo.BowelRegimeVoCollection;
  import ims.nursing.vo.CarePlanTemplate;
  import ims.nursing.vo.CarePlanTemplateCollection;
  import ims.nursing.vo.CarePlanTemplateTitle;
  import ims.nursing.vo.CarePlanTemplateTitleCollection;
  import ims.nursing.vo.lookups.AssessmentComponentType;
  import ims.nursing.vo.lookups.BowelConstipationDiarrhoea;
  import ims.nursing.vo.lookups.BowelConstipationDiarrhoeaCollection;
  import ims.nursing.vo.lookups.BowelRegime;
  import ims.nursing.vo.lookups.BowelRegimeCollection;
  import ims.nursing.vo.lookups.LookupHelper;
  import ims.spinalinjuries.forms.nurassessmentbowels.GenForm.grdMedsRow;
  import ims.spinalinjuries.vo.NurAssessmentBowelsVo;
  
  public class Logic extends BaseLogic
  {
  	protected void onFormOpen() throws ims.framework.exceptions.FormOpenException
  	{		
  		open();
  	}
  	
  	protected void checkForPreviousAssessent()
  	{
  		NurAssessmentBowelsVo assessBowelsVo = (NurAssessmentBowelsVo) domain.getLastAssessmentComponentByCareContext(form.getGlobalContext().Core.getCurrentCareContext(), AssessmentComponentType.SPINAL_BOWELS);
  	
  		if (assessBowelsVo == null)
  		{
  			this.form.chkCopy().setValue(false);
  			return;
  		}
  		
  		form.getLocalContext().setPreviousAssBowels(assessBowelsVo);
  		if(form.getMode().equals(FormMode.EDIT))
  			form.chkCopy().setEnabled(true);
  	}
  	private void clearControls()
  	{
  		form.grdSufferFrom().getRows().clear();
  		form.grdBowelRegime().getRows().clear();
  		form.grdMeds().getRows().clear();
  		form.grdCarePlan().getRows().clear();
  	}
  	private void open() 
  	{
  		if(form.getGlobalContext().Core.getAssessmentIsReadOnly() == null || form.getGlobalContext().Core.getAssessmentIsReadOnly() == false )
  			form.setMode(FormMode.EDIT);
  		else
  			form.setMode(FormMode.VIEW);
  		form.chkCopy().setEnabled(false);
  
  		clearControls();
  		
  		NurAssessmentBowelsVo voBowels =null;
  		AssessmentHeaderInfo assmnt = form.getGlobalContext().Nursing.getAssessmentHeaderInfo();
  		form.getLocalContext().setAssessment(assmnt);
  
  		if (assmnt != null && assmnt.getCurrentComponent() != null)
  		{		
  			voBowels = (NurAssessmentBowelsVo)domain.getComponent(assmnt.getCurrentComponent());
  			form.getLocalContext().setAssessBowels(voBowels);
  		}
  		
  		//Suffer From Grid
  		GenForm.grdSufferFromRow sRow = null;
  		
  		BowelConstipationDiarrhoeaCollection sColl = LookupHelper.getBowelConstipationDiarrhoea(domain.getLookupService());
  		for (int i = 0; i < sColl.size(); i++)
  		{
  			BowelConstipationDiarrhoea bowel  = sColl.get(i);
  			sRow = form.grdSufferFrom().getRows().newRow();
  			sRow.setColSuffer(bowel);
  		}
  
  		//BowelRegime
  		GenForm.grdBowelRegimeRow rRow = null;
  		
  		BowelRegimeCollection rColl = LookupHelper.getBowelRegime(domain.getLookupService());
  		for (int i = 0; i < rColl.size(); i++)
  		{
  			BowelRegime regime  = rColl.get(i);
  			rRow = form.grdBowelRegime().getRows().newRow();
  			rRow.setColBowelRegime(regime);
  		}		
  				
  		//Care Plans
  		CarePlanTemplateTitleCollection tempColl = domain.listCarePlanTemplates(AssessmentComponentType.SPINAL_BOWELS);
  		if (tempColl != null)
  		{
  			form.grdCarePlan().getRows().clear();
  			GenForm.grdCarePlanRow tempRow;
  			for (int i=0; i<tempColl.size(); i++)
  			{
  				CarePlanTemplateTitle template = tempColl.get(i);
  				tempRow = form.grdCarePlan().getRows().newRow();
  				tempRow.setValue(template);
  				tempRow.setcolCarePlans(template.getTitle());
  			}
  		}
  	
  		checkForPreviousAssessent(); 
  		
  		if(voBowels != null)
  			this.populateScreenFromData(voBowels);
  		
  		setRemoveMenuVisibility();		
  	}
  
  	private void removeMeds() 
  	{
  		if (form.grdMeds().getSelectedRowIndex() >= 0)
  			 form.grdMeds().getRows().remove(form.grdMeds().getSelectedRowIndex());
  	}
  	
  	private void newMeds() 
  	{
  		form.grdMeds().getRows().newRow();
  	}
  	
  	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
  	{
  		engine.open(form.getForms().Nursing.AdmissionAssessOverview);
  	}
  
  	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
  	{
  		NurAssessmentBowelsVo voBowels = form.getLocalContext().getAssessBowels();
  		if (voBowels == null)
  			voBowels = new NurAssessmentBowelsVo();
  	
  		voBowels = super.populateDataFromScreen(voBowels);
  		
  		//Bowels Regime
  		BowelRegimeVoCollection voCollRegime = new BowelRegimeVoCollection();
  		GenForm.grdBowelRegimeRow row = null;	
  		for (int i=0; i<form.grdBowelRegime().getRows().size(); i++)
  		{
  			row = form.grdBowelRegime().getRows().get(i);
  			BowelRegimeVo regime = row.getValue();
  			if (regime == null)
  				regime = new BowelRegimeVo();
  		
  			regime.setBowelRegime(row.getColBowelRegime());
  			regime.setDetails(row.getColDetails());
  			regime.setStatus(row.getColSelect());	
  			voCollRegime.add(regime);
  		}
  		voBowels.setBowelRegime(voCollRegime);
  		
  		//Suffer From
  		BowelDiarrhoeaConstipationVoCollection voCollSuffer = new BowelDiarrhoeaConstipationVoCollection();
  		GenForm.grdSufferFromRow sRow = null;	
  		for (int i=0; i<form.grdSufferFrom().getRows().size(); i++)
  		{
  			sRow = form.grdSufferFrom().getRows().get(i);
  			BowelDiarrhoeaConstipationVo suffer = sRow.getValue();
  			if (suffer == null)
  				suffer = new BowelDiarrhoeaConstipationVo();
  
  			suffer.setSufferFrom(sRow.getColSuffer());
  			suffer.setDetails(sRow.getColDetails());
  			suffer.setStatus(sRow.getColSelect());	
  			voCollSuffer.add(suffer);
  		}	
  		voBowels.setSufferFrom(voCollSuffer);	
  		
  		BowelOstomyVo voOstomy = voBowels.getOstomyDetails();
  		if(voOstomy == null)
  			voOstomy = new BowelOstomyVo();
  			
  		voOstomy.setOstomyType(form.cmbOstomy().getValue());
  		voOstomy.setAppliancesUsed(form.txtAppliancesUsed().getValue());
  		voOstomy.setStomaDate(form.dteStomaNurse().getValue());
  		
  		voBowels.setOstomyDetails(voOstomy);
  		
  		PatientMedicationVoCollection voCollPatientMedication = new PatientMedicationVoCollection();
  		PatientMedicationVo voPatientMedication = null;
  		GenForm.grdMedsRow mRow = null;
  		for(int i=0;i<form.grdMeds().getRows().size();i++)
  		{
  			HcpLiteVo voHcp = (HcpLiteVo) domain.getHcpLiteUser();
  			voPatientMedication = new PatientMedicationVo();
  			mRow = form.grdMeds().getRows().get(i);
  			AuthoringInformationVo  voAuthoringInformation  = new AuthoringInformationVo ();
  			voAuthoringInformation.setAuthoringHcp(voHcp);
  			voAuthoringInformation.setAuthoringDateTime(new DateTime());
  			voPatientMedication.setAuthoringInformation(voAuthoringInformation);
  			voPatientMedication.setMedication((MedicationLiteVo) mRow.getColMedication().getValue());
  			voPatientMedication.setOtherMedicationText(mRow.getColMedication().getEditedText());
  			voPatientMedication.setFrequency(mRow.getColFrequency());
  			voPatientMedication.setPatient(form.getGlobalContext().Core.getPatientShort());
  			
  			MedicationDose dose = new MedicationDose();
  			AuthoringInformationVo  voAuthoringInfoDose  = new AuthoringInformationVo ();
  			voAuthoringInfoDose.setAuthoringHcp(voHcp);
  			voAuthoringInfoDose.setAuthoringDateTime(new DateTime());
  			dose.setAuthoringInformation(voAuthoringInfoDose);
  			dose.setDose(mRow.getColDose());
  			dose.setAdminRoute(mRow.getColRoute());
  			
  			if(voPatientMedication.getPrescribedDoses() == null)
  				voPatientMedication.setPrescribedDoses(new MedicationDoseCollection());
  				
  			voPatientMedication.getPrescribedDoses().add(dose);
  			voCollPatientMedication.add(voPatientMedication);
  		}
  		voBowels.setBowelMedication(voCollPatientMedication);
  		
  		//if AssessmentHeaderInfo id == null, check if in the meantime another user from another session has created a new assessment for selected care context
  		if(form.getLocalContext().getAssessment() != null)
  			if(form.getLocalContext().getAssessment().getID_Assessment() == null)
  				if(form.getGlobalContext().Core.getCurrentCareContext() != null)
  				{
  					AssessmentHeaderInfo tempvo = domain.getAssessmentHeaderInfoByCareContext(form.getGlobalContext().Core.getCurrentCareContext());
  					if(tempvo != null)
  					{
  						form.getLocalContext().setAssessment(tempvo);
  						form.getGlobalContext().Nursing.setAssessmentHeaderInfo(form.getLocalContext().getAssessment());
  					}
  				}
  		
  		String[] arrErrors = voBowels.validate();
  	
  		if(arrErrors != null)
  		{
  			engine.showErrors(arrErrors);
  			return;
  		}
  		try
  		{
  			AssessmentHeaderInfo newAssmnt = domain.saveComponent(form.getLocalContext().getAssessment(), voBowels);
  			form.getGlobalContext().Nursing.setAssessmentHeaderInfo(newAssmnt);
  		}
  		catch(StaleObjectException e) 
  		{
  			engine.showMessage(ims.configuration.gen.ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
  			// We may need to reset the current component value in GC for onFormOpen - if there was none previously
  			if (e.getStaleObject() != null)
  			{
  				 AssessmentHeaderInfo assmnt = form.getGlobalContext().Nursing.getAssessmentHeaderInfo();
  				 if (assmnt != null && assmnt.getCurrentComponent() == null)
  				 {
  					 ims.nursing.vo.AssessmentComponent comp = new ims.nursing.vo.AssessmentComponent();
  					 comp.setID_AssessmentComponent(e.getStaleObject().getId());
  					 assmnt.setCurrentComponent(comp);
  					 form.getGlobalContext().Nursing.setAssessmentHeaderInfo(assmnt);
  				 }
  			}
  			open();
  			return;								
  		}
  		catch(DomainRuntimeException e)
  		{
  			engine.showMessage(e.getMessage());
  			AssessmentHeaderInfo newAssmnt = form.getGlobalContext().Nursing.getAssessmentHeaderInfo();
  			AssessmentHeaderInfo assessheaderinfoVo = form.getLocalContext().getAssessment();	
  			newAssmnt.setCurrentComponent( domain.getAssessComponent(assessheaderinfoVo.getID_Assessment(),AssessmentComponentType.SPINAL_BOWELS));
  			form.getGlobalContext().Nursing.setAssessmentHeaderInfo(newAssmnt);
  			open();
  			return;	
  		}
  
  //		If the careplan grid is not readonly, we may need to go to the careplanstep2 form
  		if (form.grdCarePlan().getReadOnly() == false)
  		{
  			  CarePlanTemplateTitleCollection templList = new CarePlanTemplateTitleCollection();
  			  GenForm.grdCarePlanRow cpRow;
  			  for (int i=0; i<form.grdCarePlan().getRows().size(); i++)
  			  {
  				  cpRow = form.grdCarePlan().getRows().get(i);
  