/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/forms/careplanreviewdialog/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 Rory Fitzpatrick using IMS Development Environment (version 1.20 build 40726.1130)
// Copyright (C) 1995-2004 IMS MAXIMS plc. All rights reserved.

package ims.nursing.forms.careplanreviewdialog;

import ims.framework.FormName;
import ims.framework.exceptions.FormOpenException;
import ims.framework.exceptions.PresentationLogicException;
import ims.nursing.vo.CarePlan;
import ims.nursing.vo.CarePlanReviewHistory;
import ims.nursing.vo.CarePlanReviewHistoryCollection;
import ims.core.vo.HcpLiteVo;
import ims.core.vo.MemberOfStaffShortVo;
import ims.domain.exceptions.DomainInterfaceException;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.enumerations.DialogResult;
import ims.framework.enumerations.SortOrder;
import ims.framework.utils.DateTime;

public class Logic extends BaseLogic
{
	protected void onCheckBoxConfirmReviewValueChanged() throws PresentationLogicException 
	{
		if (form.chkConfirmReview().getValue())
			form.btnSave().setEnabled(true);
		else
			form.btnSave().setEnabled(false);
	}
	protected void loadCarePlanFromScreen()
	{
		CarePlan cp;
		if ( (cp = form.getLocalContext().getCarePlan()) == null) 
			cp = new CarePlan();
	
		//Review Grid
		GenForm.grdReviewRowCollection reviewRows = form.grdReview().getRows();
		CarePlanReviewHistoryCollection reviewColl= new CarePlanReviewHistoryCollection();

		CarePlanReviewHistory review = null;
		review = new CarePlanReviewHistory();
		for (int i = 0; i < reviewRows.size(); i++)				
		{			
			review = reviewRows.get(i).getValue();
			
			if(review != null)
				reviewColl.add(review);
		}
		
		review = new CarePlanReviewHistory();
		
		MemberOfStaffShortVo memStaffShortVo = (MemberOfStaffShortVo)domain.getMosUser();
		if(memStaffShortVo != null && memStaffShortVo.getHcp() != null)
		{
			HcpLiteVo hcpLite = new HcpLiteVo();
			hcpLite.setID_Hcp(memStaffShortVo.getHcp().getIHcpId());
			hcpLite.setMos(memStaffShortVo.getHcp().getMos());
			hcpLite.setHcpType(memStaffShortVo.getHcp().getHcpType());
			review.setReviewBy(hcpLite);
		}
		
		
		review.setReviewDateTime(new DateTime());
		
		reviewColl.add(review);
		
		cp.setCarePlanReviews(reviewColl);
		
		if(memStaffShortVo != null && memStaffShortVo.getHcp() != null)
			cp.setLastReviewedBy(memStaffShortVo.getHcp());
		
		cp.setLastReviewedDate(new DateTime());
		
		form.getLocalContext().setCarePlan(cp);
	}
	protected void onBtnSaveClick() throws PresentationLogicException 
	{
		loadCarePlanFromScreen();
		
		CarePlan cp = form.getLocalContext().getCarePlan();
		
		String[] arrErrors = cp.validate();
		if(arrErrors == null)
		{	
			try 
			{
				cp = domain.saveCarePlan(cp);
			} 
			catch (StaleObjectException e) 
			{
				form.getGlobalContext().Core.setYesNoDialogMessage(ims.configuration.gen.ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
				engine.open(form.getForms().Core.OkDialog, "Data Warning");			
				return;
			}
		}
		else
		{
			if (arrErrors != null)
			{
				engine.showErrors("Validation Errors", arrErrors);
				return;
			}			
		}
		
		engine.close(DialogResult.OK);
	}
	protected void onBtnCancelClick() throws PresentationLogicException 
	{
		engine.close(DialogResult.CANCEL);
	}
	protected void onFormOpen() throws FormOpenException 
	{
		CarePlan cp = new CarePlan();
		cp.setID_CarePlan(form.getGlobalContext().COE.getCarePlanID());
		try 
		{
			cp = domain.getCarePlan(cp);
		} 
		catch (DomainInterfaceException e) 
		{
			throw new DomainRuntimeException("DomainInterfaceException occurred getting careplan.\r\n" + e.getMessage(), e);
		}
		
		if (cp != null)
		{
			if (cp.getCarePlanReviews() != null)
			{
				CarePlanReviewHistoryCollection coll = cp.getCarePlanReviews();
				coll.sort(SortOrder.DESCENDING);
				CarePlanReviewHistory voCpd;
				GenForm.grdReviewRow row;
				for (int i = 0; i < coll.size(); i++)
				{
					voCpd = coll.get(i);
					row = form.grdReview().getRows().newRow();
					
					if (voCpd.getReviewBy() != null)
						if(voCpd.getReviewBy().getMos()!= null)
						{
						
							row.setColHCP(voCpd.getReviewBy().getMos().getName().toString());
						
						}
						if (voCpd.getReviewDateTime() != null)
						row.setColDate(voCpd.getReviewDateTime().toString());//getDate().toString(), voCpd.getReviewDateTime().getTime().toString());
					
					
					row.setValue(voCpd);
				}
			}		
			form.getLocalContext().setCarePlan(cp);	
		}
		if(form.getGlobalContext().Nursing.getShowCarePlanRvwDialogIsNotNull())
			if(form.getGlobalContext().Nursing.getShowCarePlanRvwDialog().equals(Boolean.FALSE))
			{
				form.btnSave().setVisible(false);
				form.chkConfirmReview().setVisible(false);
				form.labelConfirmreview().setVisible(false);
			}
			else
			{
				if (engine.getPreviousNonDialogFormName() != null 
						&& engine.getPreviousNonDialogFormName().equals(form.getForms().Nursing.CarePlanReview))
				{
					form.btnSave().setVisible(false);
					form.chkConfirmReview().setVisible(false);
					form.labelConfirmreview().setVisible(false);
				}
				else
				{
					GenForm.grdReviewRow row;
					row = form.grdReview().getRows().newRow();
					if (domain.getMosUser() != null)
						row.setColHCP(((MemberOfStaffShortVo)domain.getMosUser()).toString());
					row.setColDate(new DateTime().toString());
					
					form.btnSave().setEnabled(false);
				}
			}
	}
	
	protected void onFormDialogClosed(FormName formName, DialogResult result) throws PresentationLogicException 
	{
		if (formName.equals(form.getForms().Core.OkDialog))
			engine.close(DialogResult.CANCEL);		
	}
	
}
