package nc.ui.wa.ruleex.action;

import java.awt.event.ActionEvent;

import javax.swing.Action;

import nc.bs.logging.Logger;
import nc.hr.utils.CommonUtils;
import nc.hr.utils.ResHelper;
import nc.ui.hr.uif2.action.AddAction;
import nc.ui.hr.uif2.action.HrActionInterceptor;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.uif2.ShowStatusBarMsgUtil;
import nc.ui.uif2.model.AbstractAppModel;
import nc.vo.bd.pub.NODE_TYPE;
import nc.vo.pub.BusinessException;
import nc.vo.uif2.LoginContext;
import nc.vo.util.ManageModeUtil;

/**
 * WaRuleManagerModelCheckInterceptor<BR>
 * 新增，修改，删除 action 拦截器<BR>
 * @author: xuhw
 * @date: 2009-11-16 下午03:33:47
 * @since: eHR V6.0
 * @走查人:
 * @走查日期:
 * @修改人:
 * @修改日期:
 */
public class WaRuleManagerModelCheckInterceptor extends HrActionInterceptor
{
	private AbstractAppModel appModel;

	/**
	 * 新增，修改，删除前的校验<BR>
	 * 1)组织级的节点是否选择了人力资源组织<BR>
	 * <BR>
	 * 2)管控模式的校验<BR>
	 * @author xuhw on 2009-11-16
	 */
	@Override
	public boolean beforeDoAction(Action action, ActionEvent e)
	{
		LoginContext context = getAppModel().getContext();


		//检查是否已经选择了人力资源组织人力资源组织
		try
		{
			if (context.getNodeType() != NODE_TYPE.GROUP_NODE && context.getNodeType() != NODE_TYPE.GROUP_NODE)
			{
				CommonUtils.checkOrg(context);
			}

		}
		catch (BusinessException ex)
		{
			Logger.error(ex.getMessage(), ex);
			MessageDialog.showErrorDlg(context.getEntranceUI(), null, ResHelper.getString("60130payrule","060130payrule0005")/*@res "请选择人力资源组织！"*/);
			return false;
		}

		//增加的场合,没有选中数据的场合不进行管控模式的校验
		if (action instanceof AddAction || action instanceof WaRuleCopyTableAction || getAppModel().getSelectedData() == null)
		{
			return true;
		}

		//管控模式的校验
		boolean blnIsCanOperator = ManageModeUtil.manageable(getAppModel().getSelectedData(), context);
		if (!blnIsCanOperator)
		{
			//MessageDialog.showErrorDlg(context.getEntranceUI(), null, ManageModeUtil.getDisManageableMsg(context.getNodeType()));
			ShowStatusBarMsgUtil.showErrorMsg(ResHelper.getString("60130payrule","060130payrule0018")/*@res "操作失败！"*/
					, ManageModeUtil.getDisManageableMsg(context.getNodeType()), getContext(action));
			return false;
		}
		return true;
	}

	public AbstractAppModel getAppModel()
	{
		return appModel;
	}

	public void setAppModel(AbstractAppModel appModel)
	{
		this.appModel = appModel;
	}

}