/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Pathways/src/ims/pathways/forms/patientjourneycomments/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 Catalin Tomozei using IMS Development Environment (version 1.70 build 3341.28572)
// Copyright (C) 1995-2009 IMS MAXIMS plc. All rights reserved.

package ims.pathways.forms.patientjourneycomments;

import ims.framework.FormName;
import ims.framework.enumerations.DialogResult;
import ims.framework.exceptions.PresentationLogicException;
import ims.pathways.forms.patientjourneycomments.GenForm.grdCommentsRow;
import ims.pathways.vo.PatientJourneyCommentLiteVo;
import ims.pathways.vo.PatientJourneyCommentLiteVoCollection;

public class Logic extends BaseLogic
{
	private static final long serialVersionUID = 1L;
	
	private static final Integer VIEW_COMMENT_MODE = new Integer(4);

	@Override
	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		open();
		
	}
	private void open() 
	{
		if (form.getGlobalContext().Pathways.getSelectedPatientPathwayJourneyIsNotNull())
		{
			form.getLocalContext().setSelectedJourneyRef(form.getGlobalContext().Pathways.getSelectedPatientPathwayJourney());			
		}
		PatientJourneyCommentLiteVoCollection commentsColl = domain.listComments(form.getLocalContext().getSelectedJourneyRef());
		populateScreenFromData(commentsColl);
		
	}
	
	private void populateScreenFromData(PatientJourneyCommentLiteVoCollection commentsColl)
	{
		form.grdComments().getRows().clear();
		
		if (commentsColl == null)
			return;
		for (int i=0;i<commentsColl.size();i++)
		{
			if (commentsColl.get(i) == null)
				continue;
			
			addNewRow(commentsColl.get(i));			
		}		
	}

	private void addNewRow(PatientJourneyCommentLiteVo commentVo)
	{
		grdCommentsRow commentRow = form.grdComments().getRows().newRow();
		
		commentRow.setColRecByDateTime((commentVo.getRecordingInfoIsNotNull() && commentVo.getRecordingInfo().getRecordingUserIsNotNull() ? ( Boolean.TRUE.equals(commentVo.getCorrected()) ?  "<s>" + commentVo.getRecordingInfo().getRecordingUser().getIMosName() + "</s>" : commentVo.getRecordingInfo().getRecordingUser().getIMosName() ) : null)  + (commentVo.getRecordingInfoIsNotNull() && commentVo.getRecordingInfo().getRecordingDateTimeIsNotNull() ?  ( Boolean.TRUE.equals(commentVo.getCorrected()) ?  "<s>" +  ", " + commentVo.getRecordingInfo().getRecordingDateTime().toString() + "</s>" :  ", " + commentVo.getRecordingInfo().getRecordingDateTime().toString() ) : null));
		commentRow.setColComment(commentVo.getCommentIsNotNull() ? ( Boolean.TRUE.equals(commentVo.getCorrected()) ?  "<s>" +  commentVo.getComment()  + "</s>" : commentVo.getComment()) : null);
		commentRow.setColCommentLevel(commentVo.getCommentLevelIsNotNull() ? ( Boolean.TRUE.equals(commentVo.getCorrected()) ? "<s>" +  commentVo.getCommentLevel().getText() + "</s>" : commentVo.getCommentLevel().getText()) : null);
		
		String tooltip = formatTooltip(commentVo);
		
		commentRow.setCellColCommentLevelTooltip(tooltip);
		commentRow.setCellColCommentTooltip(tooltip);
		commentRow.setCellColRecByDateTimeTooltip(tooltip);
		
		commentRow.setValue(commentVo);	
	}
	
	private String formatTooltip(PatientJourneyCommentLiteVo commentVo)
	{
		String tooltip = "";
		
		tooltip = (commentVo.getRecordingInfoIsNotNull() && commentVo.getRecordingInfo().getRecordingUserIsNotNull() ? "Recorded By: <b>" + commentVo.getRecordingInfo().getRecordingUser().getIMosName() + "</b>" : null)  + (commentVo.getRecordingInfoIsNotNull() && commentVo.getRecordingInfo().getRecordingDateTimeIsNotNull() ?  "<br/> Recorded Date/Time: <b>" + commentVo.getRecordingInfo().getRecordingDateTime().toString() + "</b>" : "");
		tooltip = tooltip + (commentVo.getCommentLevelIsNotNull() ? "<br/> Comment Level:  <b>" + commentVo.getCommentLevel().getText() + "</b>" : " ");
		
		if (Boolean.TRUE.equals(commentVo.getCorrected()))
		{
			tooltip = tooltip + ((commentVo.getCorrectedAuthoringInformationIsNotNull() && commentVo.getCorrectedAuthoringInformation().getAuthoringHcpIsNotNull() ? "<br/> Corrected By: <b>" + commentVo.getCorrectedAuthoringInformation().getAuthoringHcp().getIMosName() + "</b>" : "")  + (commentVo.getCorrectedAuthoringInformationIsNotNull() && commentVo.getCorrectedAuthoringInformation().getAuthoringDateTimeIsNotNull() ?  "<br/> Corrected Date/Time: <b>" + commentVo.getCorrectedAuthoringInformation().getAuthoringDateTime().toString() + "</b>" : ""));
			tooltip = tooltip + (commentVo.getCorrectionCommentsIsNotNull() ?  "<br/> Correction Comments:  <b>" + commentVo.getCorrectionComments() +  "</b>" : " ");
		}
		
		return tooltip;
	}
	@Override
	protected void onBtnCloseClick() throws PresentationLogicException 
	{
		engine.close(DialogResult.OK);	
	}
	
	@Override
	protected void onGrdCommentsSelectionChanged() throws PresentationLogicException
	{
		form.getGlobalContext().Pathways.setPatientJourneySelectedComment(form.grdComments().getValue());
		engine.open(form.getForms().Pathways.PatientJourneyComment, new Object[] { VIEW_COMMENT_MODE, null, null});		
	}
	
	@Override
	protected void onFormDialogClosed(FormName formName, DialogResult result) throws PresentationLogicException
	{
		open();
	}
}
