/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/forms/repositioningplan/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 Charlotte Murphy using IMS Development Environment (version 1.45 build 2369.16338)
// Copyright (C) 1995-2006 IMS MAXIMS plc. All rights reserved.

package ims.nursing.forms.repositioningplan;

import java.util.ArrayList;
import ims.core.vo.AuthoringInformationVo;
import ims.core.vo.lookups.LookupHelper;
import ims.core.vo.lookups.PatientPositionCollection;
import ims.core.vo.lookups.YesNo;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.enumerations.FormMode;
import ims.framework.exceptions.PresentationLogicException;
import ims.nursing.vo.RepositioningPlanVo;
import ims.nursing.vo.RepositioningPlanVoCollection;

public class Logic extends BaseLogic
{
	private static final long serialVersionUID = 1L;

	protected void onFormOpen() throws ims.framework.exceptions.PresentationLogicException
	{
		initialize();
		open();
	}
	private void initialize() throws PresentationLogicException 
	{
		StringBuffer exceptions = new StringBuffer();	
		if (ims.nursing.vo.lookups.LookupHelper.getTurnsDuration(domain.getLookupService()).size() == 0)
		{
			exceptions.append("Please configure TurnsDuration lookup");
			exceptions.append("\n");
		}
		
		if (LookupHelper.getPatientPosition(domain.getLookupService()).size()== 0)
		{
			exceptions.append("Please configure PatientPosition lookup");
			exceptions.append("\n");
		}

		if(exceptions.length() > 0)
			throw new PresentationLogicException(exceptions.toString());
		
		form.ctnRepo().customAuthoring().setIsRequiredPropertyToControls(true);	//WDEV-13175
	}
	private void open() 
	{
		clear();
		form.ctnRepo().ansFrequency().setValue(null);
		form.setMode(FormMode.VIEW);
		form.getLocalContext().setpreviousRepositioningPlan(null);
		populateListControl(domain.listRepositioningPlanByCareContext(form.getGlobalContext().Core.getCurrentCareContext()));
		
		form.grdDetails().setValue(form.getLocalContext().getRepositioningPlan());
		form.getLocalContext().setRepositioningPlan(form.grdDetails().getValue());
		resetUpdatedInstance();
	}
	private void resetUpdatedInstance()
	{
		if(form.getLocalContext().getRepositioningPlanIsNotNull())
		{
			form.grdDetails().setValue(form.getLocalContext().getRepositioningPlan());
			getSelectedInstance();
		}
		
	}
	private void populateListControl(RepositioningPlanVoCollection voRepositioningPlanColl) 
	{
		if(voRepositioningPlanColl == null) return;
		
		for(int i=0; i<voRepositioningPlanColl.size(); i++)
		{
			RepositioningPlanVo voRepositioningPlan = voRepositioningPlanColl.get(i);
			GenForm.grdDetailsRow row = form.grdDetails().getRows().newRow();
			if(voRepositioningPlan.getAuthoringInformation() != null)
			{
				AuthoringInformationVo voInfo = voRepositioningPlan.getAuthoringInformation();
				String authoringInfo = "";
				if(voInfo.getAuthoringDateTimeIsNotNull())
					authoringInfo += voRepositioningPlan.getAuthoringInformation().getAuthoringDateTime().toString();
				if(voInfo.getAuthoringHcpIsNotNull())
					authoringInfo += " - " + voRepositioningPlan.getAuthoringInformation().getAuthoringHcp().toString();
				row.setColDateTime(authoringInfo);
			}
			if(voRepositioningPlan.getPositioningFreqIsNotNull())
				row.setColFrequency(voRepositioningPlan.getPositioningFreq().toString());
			if(voRepositioningPlan.getBlanchingIsNotNull())
				row.setColBlanching(voRepositioningPlan.getBlanching().toString());
			row.setColSides(getPosition(voRepositioningPlan.getPosition()));
			row.setColSkinTest(voRepositioningPlan.getSkinToleranceTest());
			
			row.setValue(voRepositioningPlan);
		}
		
		if(voRepositioningPlanColl != null && voRepositioningPlanColl.size() != 0)
			form.getLocalContext().setpreviousRepositioningPlan(voRepositioningPlanColl.get(0));
	}
	private String getPosition(PatientPositionCollection positionColl) 
	{
		String positions = "";
		for(int i=0; i<positionColl.size(); i++)
			positions += positionColl.get(i).getText()+" \n";
		
		return positions;
	}
	private void clear()
	{
		form.grdDetails().getRows().clear();
		clearInstanceControls();		
	}
	private void clearInstanceControls() 
	{
		form.ctnRepo().cmbFrequency().setValue(null);
		form.ctnRepo().chkSides().clear();
		form.ctnRepo().cmbBlanching().setValue(null);
		form.ctnRepo().ansTest().setValue(null);
		form.ctnRepo().txtComments().setValue(null);
		form.ctnRepo().customAuthoring().setValue(null);	
		this.bindchkSidesLookup();
	}
	
	protected void onGrdDetailsSelectionChanged() throws ims.framework.exceptions.PresentationLogicException
	{
		// Set the local context
		form.getLocalContext().setRepositioningPlan(form.grdDetails().getValue());
		
		getSelectedInstance();
	}
	private void getSelectedInstance() 
	{
		form.ctnRepo().setCollapsed(false);
		form.btnUpdate().setEnabled(true);
		form.ctnRepo().ansFrequency().setValue(null);
		populateInstanceControls(form.grdDetails().getValue(), false);
		updateContextMenusState();
	}
	private void populateInstanceControls(RepositioningPlanVo voRepositioningPlan, boolean bAnsFrequencyValueChanged) 
	{
		if(voRepositioningPlan == null) return;
		
		form.ctnRepo().cmbFrequency().setValue(voRepositioningPlan.getPositioningFreq());
		form.ctnRepo().ansTest().setValue(voRepositioningPlan.getSkinToleranceTest());
		form.ctnRepo().cmbBlanching().setValue(voRepositioningPlan.getBlanching());
		
		if (!bAnsFrequencyValueChanged)
			form.ctnRepo().ansFrequency().setValue(voRepositioningPlan.getPositioningSuitable());
		
		form.ctnRepo().txtComments().setValue(voRepositioningPlan.getComment());
		form.ctnRepo().chkSides().setValues(voRepositioningPlan.getPosition());
		
		if(voRepositioningPlan.getAuthoringInformationIsNotNull())
		{
			form.ctnRepo().customAuthoring().setValue(voRepositioningPlan.getAuthoringInformation());
		}
	}
	protected void onBtnNewClick() throws ims.framework.exceptions.PresentationLogicException
	{	
		newInstance();
	}
	private void newInstance() 
	{
		form.setMode(FormMode.EDIT);
		if (form.getLocalContext().getpreviousRepositioningPlanIsNotNull())
		{
			form.ctnRepo().ansFrequency().setValue(null);
			form.ctnRepo().ansFrequency().setEnabled(true);
		}
		else
		{
			form.ctnRepo().ansFrequency().setValue(YesNo.NO);
			form.ctnRepo().ansFrequency().setEnabled(false);
		}
		clearInstanceControls();	
		setDefaultValues();
		form.ctnRepo().setcustomAuthoringEnabled(true);
		form.getLocalContext().setRepositioningPlan(null);
	}
	private void setDefaultValues() 
	{
		form.ctnRepo().customAuthoring().initializeComponent();
	}
	private void updateContextMenusState()
	{
		form.getContextMenus().getGenericGridAddItem().setText("New Repositioning Plan");
		form.getContextMenus().getGenericGridAddItem().setVisible(form.getMode().equals(FormMode.VIEW));
		
		form.getContextMenus().getGenericGridUpdateItem().setText("Edit Repositioning Plan");
		form.getContextMenus().getGenericGridUpdateItem().setVisible(form.getMode().equals(FormMode.VIEW) && form.grdDetails().getSelectedRowIndex()>=0);
	}
	protected void onBtnUpdateClick() throws ims.framework.exceptions.PresentationLogicException
	{
		updateInstance();
	}
	private void updateInstance() 
	{
		form.setMode(FormMode.EDIT);
		form.ctnRepo().ansFrequency().setEnabled(false);
		form.getLocalContext().setRepositioningPlan(form.grdDetails().getValue());
		form.ctnRepo().setcustomAuthoringEnabled(false);
	}
	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if (save())
			open();	
	}
	private boolean save() 
	{
		RepositioningPlanVo voRepositioningPlan = populateInstanceData();
		
		voRepositioningPlan.setClinicalContact(form.getGlobalContext().Core.getCurrentClinicalContact());
		voRepositioningPlan.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
		
		/*	WDEV-13175ArrayList screenErrors = new ArrayList();
		
		if(form.ctnRepo().customAuthoring().getValue() == null)
		{
			if (form.ctnRepo().customAuthoring().getValue().getAuthoringHcp() == null)
			{
				screenErrors.add("Authoring HCP is mandatory.");
			}
			if (form.ctnRepo().customAuthoring().getValue().getAuthoringDateTime() == null)
			{
				screenErrors.add("Authoring Date/Time is mandatory.");				
			}
		}*/
		
		String[] errors =  voRepositioningPlan.validate();	//	WDEV-13175
		//String[] arrScreenAndVoErrors = addScreenErrorsToVOErrors(screenErrors, arrErrors);	WDEV-13175
		if(errors!= null && errors.length != 0)//	WDEV-13175
		{
			engine.showErrors(errors);
			return false;
		}
		try 
		{
			form.getLocalContext().setRepositioningPlan(domain.saveRepositioningPlan(voRepositioningPlan));
		} 
		catch (StaleObjectException e) 
		{
			engine.showMessage(ims.configuration.gen.ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
			open();
			return false;
		}
		return true;
	}
	
	/*private String[] addScreenErrorsToVOErrors(ArrayList screenErrors, String[] arrErrors)
	{
		String[] arrAllErrors = null;
		
		if(arrErrors != null)
		{
			arrAllErrors = new String[screenErrors.size() + arrErrors.length];
			for(int i=0;i<screenErrors.size();i++)
				arrAllErrors[i] = (String)screenErrors.get(i);
			
			int i=0;	
			for(int p=screenErrors.size();p<arrAllErrors.length;p++)
			{
				arrAllErrors[p] = arrErrors[i];
				i++;
			}
		}	
		else
		{
			arrAllErrors = new String[screenErrors.size()];		
			
			for(int i=0;i<screenErrors.size();i++)
				arrAllErrors[i] = (String)screenErrors.get(i);	
		}	
		
		return arrAllErrors;
	}
	*/
	
	private RepositioningPlanVo populateInstanceData() 
	{
		RepositioningPlanVo voRepositioningPlan = form.getLocalContext().getRepositioningPlan();
		if(voRepositioningPlan == null)
			voRepositioningPlan = new RepositioningPlanVo();
		
		if (form.ctnRepo().customAuthoring().getValue() != null)
			voRepositioningPlan.setAuthoringInformation(form.ctnRepo().customAuthoring().getValue());
		
		voRepositioningPlan.setPositioningFreq(form.ctnRepo().cmbFrequency().getValue());
		voRepositioningPlan.setPosition(form.ctnRepo().chkSides().getValues());
		voRepositioningPlan.setSkinToleranceTest(form.ctnRepo().ansTest().getValue());
		voRepositioningPlan.setBlanching(form.ctnRepo().cmbBlanching().getValue());
		voRepositioningPlan.setPositioningSuitable(form.ctnRepo().ansFrequency().getValue());
		voRepositioningPlan.setComment(form.ctnRepo().txtComments().getValue());
		
		return voRepositioningPlan;
	}
	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{
		open();
	}
	protected void onContextMenuItemClick(int menuItemID, ims.framework.Control sender) throws ims.framework.exceptions.PresentationLogicException
	{
		switch(menuItemID)
		{
			case GenForm.ContextMenus.GenericGrid.Add:
				newInstance();
				break;
			case GenForm.ContextMenus.GenericGrid.Update:
				updateInstance();
				break;
		}
	}
	
	protected void onFormModeChanged() 
	{
		updateContextMenusState();
		
		if(form.getMode().equals(FormMode.EDIT))
		{
			form.ctnRepo().setCollapsed(false);
		}
		else
		{
			form.ctnRepo().setCollapsed(true);
			form.btnUpdate().setVisible(true);
			form.btnUpdate().setEnabled(false);
		}
	}
	protected void onAnsFrequencyValueChanged() throws PresentationLogicException 
	{
		if(form.ctnRepo().ansFrequency().getValue() != null)
		{
			if(form.ctnRepo().ansFrequency().getValue().equals(YesNo.YES))
			{
				populateInstanceControls(form.getLocalContext().getpreviousRepositioningPlan(), true);
				form.ctnRepo().customAuthoring().setValue(null);
				setDefaultValues();
			}
			else
			{
				clearInstanceControls();
				setDefaultValues();
			}
		}
		
	}
}
