/**
 *
 */
package nc.impl.wa.grade;

import nc.bs.dao.BaseDAO;
import nc.bs.logging.Logger;
import nc.bs.uif2.BusinessExceptionAdapter;
import nc.bs.uif2.validation.ValidationFailure;
import nc.bs.uif2.validation.Validator;
import nc.hr.utils.ResHelper;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.md.persist.framework.MDPersistenceService;
import nc.vo.hr.frame.persistence.BooleanProcessor;
import nc.vo.pub.BusinessException;
import nc.vo.wa.grade.AggWaGradeVO;
import nc.vo.wa.grade.WaGradeVO;
import nc.vo.wa.item.WaItemVO;

import nccloud.commons.lang.StringUtils;

/**
 * 更新校验器
 * @author daicy
 */
public class WaGradeUpdateValidator implements Validator
{
	private BaseDAO baseDao;
	    
    /***************************************************************************
     * TODO
     * <br>Created on 2012-9-13 上午11:28:44<br>
     * @return
     * @author daicy
     ***************************************************************************/
    public BaseDAO getBaseDao()
    {
        if (baseDao == null)
        {
            baseDao = new BaseDAO();
        }
        return baseDao;
    }
    
    /***************************************************************************
     * 返回元数据持久化服务对象
     * @return IMDPersistenceQueryService
     *****************************************************************************/
    protected static IMDPersistenceQueryService getMDQueryService()
    {
        return MDPersistenceService.lookupPersistenceQueryService();
    }
    
    
    @Override
    public ValidationFailure validate(Object obj)
    {
        if (obj == null && !(obj instanceof AggWaGradeVO))
        {
            return null;
        }
        StringBuffer message = new StringBuffer();
        AggWaGradeVO aggVo = (AggWaGradeVO) obj;
        
        try
        {
        	WaGradeVO waGradeVO = getMDQueryService().queryBillOfVOByPK(WaGradeVO.class, aggVo.getParentVO().getPk_wa_grd(), false);
        	if(null != waGradeVO && !waGradeVO.getPk_wa_item().equals(aggVo.getParentVO().getPk_wa_item())){
        		  String sql = "select 1 from hi_psndoc_wadoc pw, wa_grade wa where pw.pk_wa_grd=wa.pk_wa_grd and " +
                  		"pw.pk_wa_item=wa.pk_wa_item and wa.pk_wa_grd='"+waGradeVO.getPk_wa_grd()+"'";
                  
                  if ((Boolean) getBaseDao().executeQuery(sql, new BooleanProcessor()))
                  {
                	  WaItemVO waItemVO = getMDQueryService().queryBillOfVOByPK(WaItemVO.class, waGradeVO.getPk_wa_item(), false);
                      message.append(ResHelper.getString("6013salaryadjmgt", "06013salaryadjmgt0143"
                      /* @res "已存在该薪资类别，且薪资项目为{0}的定调资数据，故此薪资项目不允许修改！" */, waItemVO.getMultilangName(), "\n"));
                  }
        	}
          
        }
        catch (BusinessException e)
        {
            Logger.error(e.getMessage(), e);
            throw new BusinessExceptionAdapter(e);
        }
        if (StringUtils.isNotEmpty(message.toString()))
        {
            ValidationFailure vf = new ValidationFailure();
            vf.setMessage( message.toString());
            return vf;
        }
        
        return null;
        
    }
    
}
