gt: 							currentNote = populateData(form.dyngrdNotes().getRows().get(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 Catalin Tomozei using IMS Development Environment (version 1.70 build 3545.21176)
  // Copyright (C) 1995-2009 IMS MAXIMS. All rights reserved.
  
  package ims.RefMan.forms.tltsummarynotescomponent;
  
  import ims.RefMan.vo.CatsReferralRefVo;
  import ims.RefMan.vo.TltSummaryNoteLiteVo;
  import ims.RefMan.vo.TltSummaryNoteVo;
  import ims.RefMan.vo.TltSummaryNoteVoCollection;
  import ims.core.vo.AuthoringInformationVo;
  import ims.core.vo.HcpLiteVo;
  import ims.core.vo.MemberOfStaffLiteVo;
  import ims.domain.exceptions.StaleObjectException;
  import ims.framework.Control;
  import ims.framework.controls.DynamicGridCell;
  import ims.framework.controls.DynamicGridColumn;
  import ims.framework.controls.DynamicGridRow;
  import ims.framework.enumerations.DynamicCellDecoratorMode;
  import ims.framework.enumerations.DynamicCellType;
  import ims.framework.enumerations.FormMode;
  import ims.framework.exceptions.CodingRuntimeException;
  import ims.framework.exceptions.PresentationLogicException;
  import ims.framework.utils.Color;
  import ims.framework.utils.DateTime;
  
  public class Logic extends BaseLogic
  {
  	private static final long serialVersionUID = 1L;
  	private static final int COL_DATE = 1;
  	private static final int COL_USER = 2;
  	private static final int COL_NOTES = 3;	
  	private static final int NEW_ROW_IDENTIFIER = 5;
  	private static final int EDITED_ROW_IDENTIFIER = 6;
  
  	@Override
  	protected void onContextMenuItemClick(int menuItemID, Control sender)
  			throws PresentationLogicException 
  	{	
  		switch (menuItemID)
  		{
  			case GenForm.ContextMenus.RefManNamespace.FollowUpNotes.Add:
  				addNote();
  				break;
  			case GenForm.ContextMenus.RefManNamespace.FollowUpNotes.Edit:
  				editNote();
  				break;	
  		}
  				
  	}
  
  	private void editNote() 
  	{
  		form.setMode(FormMode.EDIT);		
  		
  		form.dyngrdNotes().getSelectedRow().setIdentifier(EDITED_ROW_IDENTIFIER);
  		form.getLocalContext().setEditedNote((TltSummaryNoteVo) form.dyngrdNotes().getSelectedRow().getValue());
  		
  		int size = form.dyngrdNotes().getSelectedRow().getCellArray().length;
  		for (int i = 0; i < size; i++) {
  			form.dyngrdNotes().getSelectedRow().getCellArray()[i].setBackColor(Color.White);
  			form.dyngrdNotes().getSelectedRow().getCellArray()[size-1].setReadOnly(false);
  		}
  		
  		form.dyngrdNotes().getSelectedRow().setReadOnly(false);
  		
  		//wdev-11019
  		fireOnFormModeChange(FormMode.EDIT);
  		//-----------
  		//---------
  	}
  
  	private void addNote() 
  	{
  		form.setMode(FormMode.EDIT);
  		
  		int size = form.dyngrdNotes().getRows().size();
  		if (size > 0) {
  			newNote(form.dyngrdNotes().getRows().newRowBefore(form.dyngrdNotes().getRows().get(0), true), null, true);
  		}
  		else {
  			newNote(form.dyngrdNotes().getRows().newRow(), null, true);
  		}
  		//wdev-11019
  		fireOnFormModeChange(FormMode.EDIT);
  		//-----------
  	}
  	//wdev-11019
  	private void fireOnFormModeChange(FormMode mode)
  	{
  		form.getGlobalContext().RefMan.setIsComponentInEditMode(mode);
  		form.fireCustomControlValueChanged();
  	}
  	private void newNote(DynamicGridRow row, TltSummaryNoteVo note, boolean isEditMode) 
  	{			
  		if (row == null) {
  			throw new CodingRuntimeException("DynamicGridRow is null");
  		}
  		
  		if (note == null) {
  			row.setIdentifier(NEW_ROW_IDENTIFIER);
  		}
  		
  		DynamicGridColumn 	coll;
  		DynamicGridCell 	cell;
  				
  		coll = form.dyngrdNotes().getColumns().getByIdentifier(COL_DATE);
  		cell = row.getCells().newCell(coll, DynamicCellType.DATETIME);		
  		cell.setValue(note != null ? (note.getAuthoringInformationIsNotNull() ? note.getAuthoringInformation().getAuthoringDateTime() : new DateTime())  : new DateTime());				
  		cell.setBackColor(Color.fromRGB(224, 224, 224));		
  		cell.setReadOnly(true);
  		
  		coll = form.dyngrdNotes().getColumns().getByIdentifier(COL_USER);
  		cell = row.getCells().newCell(coll, DynamicCellType.STRING);		
  		cell.setValue(note != null ? (note.getAuthoringInformationIsNotNull() ? (note.getAuthoringInformation().getAuthoringHcpIsNotNull() ? (note.getAuthoringInformation().getAuthoringHcp().getName() != null ? note.getAuthoringInformation().getAuthoringHcp().getName().toString() : null) : null ) : null)  : domain.getHcpLiteUser() != null ? ((HcpLiteVo)domain.getHcpLiteUser()).getName() != null ? ((HcpLiteVo)domain.getHcpLiteUser()).getName().toString() : null : null);		
  		cell.setBackColor(Color.fromRGB(224, 224, 224));
  		cell.setReadOnly(true);
  				
  		coll = form.dyngrdNotes().getColumns().getByIdentifier(COL_NOTES);		
  		cell = row.getCells().newCell(coll, DynamicCellType.WRAPTEXT);						
  		cell.setValue(note != null ? note.getSummaryNote() : null);
  		cell.setDecoratorType(DynamicCellDecoratorMode.NEVER);	
  		cell.setReadOnly(isEditMode ? false : true);
  		cell.setBackColor(isEditMode ? Color.White : Color.fromRGB(224, 224, 224));
  		cell.setReadOnly(!isEditMode);		
  		cell.setStringMaxLength(3000);
  		
  		row.setValue(note);		
  		
  		if (isEditMode) {
  			form.getLocalContext().setEditedNote(note);
  		}			
  	}
  	
  	
  	public void initialize(Boolean displayReadOnly, TltSummaryNoteLiteVo value) 
  	{	
  		initializeDynamicGrid();
  		
  		form.getLocalContext().setDisplayReadOnly(displayReadOnly);
  		
  		TltSummaryNoteVo note = null;
  		if (value != null && value.getCatsReferralIsNotNull()) {
  			note = domain.getTLTNote(value);
  			TltSummaryNoteVoCollection notesColl = new TltSummaryNoteVoCollection();
  			notesColl.add(note);
  			populateScreen(notesColl);				
  			form.setMode(FormMode.VIEW);
  			return;
  		}		
  		
  		open();				
  	}
  
  	private void initializeDynamicGrid()
  	{
  		form.dyngrdNotes().clear();
  		form.dyngrdNotes().setSelectable(true);	
  		
  		DynamicGridColumn column;
  		column = form.dyngrdNotes().getColumns().newColumn("HCP", COL_USER);
  		column.setWidth(200);
  		column.setCanGrow(true);
  				
  		column = form.dyngrdNotes().getColumns().newColumn("DateTime", COL_DATE);
  		column.setWidth(100);		
  		column.setCanGrow(false);				
  				
  		column = form.dyngrdNotes().getColumns().newColumn("Summary Note", COL_NOTES);
  		column.setCanGrow(false);
  		column.setWidth(480);
  		
  	}
  
  	@Override
  	protected void onBtnCancelClick() throws PresentationLogicException 
  	{	
  		open();
  		//wdev-11019
  		fireOnFormModeChange(FormMode.VIEW);
  		//-----------
  	}
  
  	@Override
  	protected void onBtnSaveClick() throws PresentationLogicException 
  	{	
  		if(save()) {
  			open();
  		}
  	}
  
  	@Override
  	protected void onFormModeChanged() 
  	{
  		if (form.getMode().equals(FormMode.VIEW)) {
  			updateContextMenuState();
  		}
  		else {
  			form.getContextMenus().RefMan.hideAllFollowUpNotesMenuItems();
  		}				
  	}
  	private void open() 
  	{
  		form.setMode(FormMode.VIEW);
  		CatsReferralRefVo currentCatsReferral = form.getGlobalContext().RefMan.getCatsReferral();
  		TltSummaryNoteVoCollection currentNotes = domain.listTLTSummaryNotes(currentCatsReferral);
  		
  		populateScreen(currentNotes);		
  		updateContextMenuState();
  	}
  	private void populateScreen(TltSummaryNoteVoCollection currentNotes) 
  	{			
  		form.dyngrdNotes().getRows().clear();
  		if (currentNotes != null) 
  		{
  			for (int i = 0; i < currentNotes.size(); i++) 
  			{				
  				newNote(form.dyngrdNotes().getRows().newRow(), currentNotes.get(i), false); 
  			}
  		}
  	}
  	private DynamicGridColumn getColumn(Integer colName)
  	{
  		return form.dyngrdNotes().getColumns().getByIdentifier(colName);
  	}	
  	private TltSummaryNoteVo populateData(DynamicGridRow dynamicGridRow) 
  	{	
  		TltSummaryNoteVo toBeSavedNote = form.getLocalContext().getEditedNote();
  		if (toBeSavedNote == null)
  			toBeSavedNote = new TltSummaryNoteVo();
  		
  		toBeSavedNote.setSummaryNote((String) dynamicGridRow.getCells().get(getColumn(COL_NOTES)).getValue());
  		
  
  		AuthoringInformationVo authoring = new AuthoringInformationVo();
  		authoring.setAuthoringDateTime(new DateTime());
  		HcpLiteVo hcpVo = new HcpLiteVo();
  		hcpVo.setID_Hcp(engine.getLoggedInUser().getHcpId());
  		hcpVo.setMos(domain.getMosUser() != null ? (MemberOfStaffLiteVo) domain.getMosUser() : null);
  		authoring.setAuthoringHcp(hcpVo);		
  		
  		toBeSavedNote.setAuthoringInformation(authoring);
  		
  		AuthoringInformationVo recording = new AuthoringInformationVo();
  		recording.setAuthoringDateTime(new DateTime());
  		HcpLiteVo hcp = new HcpLiteVo();
  		hcp.setID_Hcp(engine.getLoggedInUser().getHcpId());
  		hcp.setMos(domain.getMosUser() != null ? (MemberOfStaffLiteVo) domain.getMosUser() : null);
  		recording.setAuthoringHcp(hcp);					
  		toBeSavedNote.setRecordingInformation(recording);
  		
  		return toBeSavedNote;
  	}
  	
  	private boolean save() 
  	{			
  		TltSummaryNoteVo currentNote = null;
  		
  		int size = form.dyngrdNotes().getRows().size();
  		for (int i = 0; i < size; i++) {
  			if (form.dyngrdNotes().getRows().get(i).getIdentifier() != null) { 
  				if (form.dyngrdNotes().getRows().get(i).getIdentifier().equals(NEW_ROW_IDENTIFIER) ||
  						form.dyngrdNotes().getRows().get(i).getIdentifier().equals(EDITED_ROW_IDENTIFIER)) {
  							if(form.dyngrdNotes().getRows().get(i).getValue() != null) {
  								currentNote = (TltSummaryNoteVo) form.dyngrdNotes().getRows().get(i).getValue();
  							}
  							else {
  								currentNote = new TltSummaryNoteVo();
  							}	
  