//#############################################################################
//#                                                                           #
//#  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 Marius Mihalec using IMS Development Environment (version 1.62 build 3110.16630)
// Copyright (C) 1995-2008 IMS MAXIMS plc. All rights reserved.

package ims.core.forms.bayfloorplandesigner;

import ims.core.vo.FloorLayoutVo;
import ims.core.vo.lookups.PreActiveActiveInactiveStatus;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.enumerations.FormMode;
import ims.framework.exceptions.PresentationLogicException;

public class Logic extends BaseLogic
{
	private static final long serialVersionUID = 1L;
 
	@Override
	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		initialize();
		open();
	}
	@Override
	protected void onBtnSaveClick() throws ims.framework.exceptions.PresentationLogicException
	{
		if(save())
		{
			form.setMode(FormMode.VIEW);
		}
	}
	@Override
	protected void onBtnCancelClick() throws ims.framework.exceptions.PresentationLogicException
	{		
		if(form.getLocalContext().getCurrentRecord() == null)
		{
			returnToList();
		}
		else
		{
			open();
			form.setMode(FormMode.VIEW);
		}
	}
	@Override
	protected void onBtnEditClick() throws PresentationLogicException 
	{
		open();
		form.setMode(FormMode.EDIT);
	}
	
	@Override
	protected void onLnkReturnClick() throws PresentationLogicException 
	{
		returnToList();		
	}
	
	private void initialize()
	{
		if(form.getGlobalContext().Core.FloorLayout.getReadOnly() != null && form.getGlobalContext().Core.FloorLayout.getReadOnly().booleanValue())
		{
			form.setMode(FormMode.VIEW);
		}
		else
		{
			form.setMode(FormMode.EDIT);
			form.txtName().setFocus();
			if(form.getGlobalContext().Core.FloorLayout.getSelection() == null)
			{
				form.cmbStatus().setValue(PreActiveActiveInactiveStatus.PREACTIVE);
			}
		}
	}
	private void open()
	{		
		if(form.getGlobalContext().Core.FloorLayout.getSelection() != null)
		{
			populateScreenFromData(domain.get(form.getGlobalContext().Core.FloorLayout.getSelection()));
		}
	}
	private FloorLayoutVo populateDataFromScreen(FloorLayoutVo value)
	{
		if(value == null)
			value = new FloorLayoutVo();
		
		value.setName(form.txtName().getValue());
		value.setStatus(form.cmbStatus().getValue());
		
		value.setVml(form.layoutDesigner().getPlan());
		
		return value;
	}
	private void populateScreenFromData(FloorLayoutVo value)
	{
		form.getLocalContext().setCurrentRecord(value);
		
		form.layoutDesigner().clearAreas();
		form.layoutDesigner().setPlan("");		
		
		if(value != null)
		{
			form.txtName().setValue(value.getName());
			form.cmbStatus().setValue(value.getStatus());
			
			form.layoutDesigner().setPlan(value.getVml());			
		}
	}
	private boolean save() 
	{
		FloorLayoutVo value = populateDataFromScreen(form.getLocalContext().getCurrentRecord());		
		String[] errors = value.validate(); 
		if(errors != null && errors.length > 0)
		{
			engine.showErrors(errors);
			return false;
		}
		
		try 
		{
			value = domain.save(value);
		} 
		catch (StaleObjectException e) 
		{
			engine.showMessage(ims.configuration.gen.ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
			open();
			return false;
		}
		
		form.getLocalContext().setCurrentRecord(value);
		form.getGlobalContext().Core.FloorLayout.setSelection(value);
		
		return true;
	}
	private void returnToList()
	{
		engine.open(form.getForms().Core.FloorLayoutList);
	}	
}
