package nc.bs.arap.busireg;

import nc.bs.arap.util.ARRecbillSendTOSAPUtil;
import nc.bs.businessevent.BdUpdateEvent;
import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.arap.receivable.AggReceivableBillVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;

public class ARRecbillSaveSendTOSAPListener implements IBusinessListener {

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		AggregatedValueObject[] vos=getAggVO(event);
		
		if(MMValueCheck.isNotEmpty(vos)) {
			ARRecbillSendTOSAPUtil.sendTOSAP((AggReceivableBillVO[]) vos);
		}
	}
	
	private AggregatedValueObject[] getAggVO(IBusinessEvent event){
		AggregatedValueObject[] billvos = null;
		Object obj = null;
		if (event instanceof BusinessEvent) {
			BusinessEvent business = (BusinessEvent) event;
			obj = (AggregatedValueObject[]) business.getObject();
		} else if (event instanceof BdUpdateEvent) {
			BdUpdateEvent business = (BdUpdateEvent) event;
			obj = (AggregatedValueObject[]) business.getNewObject();
		}
		if (obj != null) {
			if (obj.getClass().isArray()) {
				billvos = (AggregatedValueObject[]) obj;
			} else {
				billvos = new AggregatedValueObject[] { (AggregatedValueObject) obj };
			}
			if (billvos == null || billvos.length == 0)
				return null;
		}
		return billvos;
	}
}
