package com.cim.idm.mwmsextend.transport.management.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.cim.idm.framework.action.dowork.ActionProcessor;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.framework.exception.IDMFrameDBErrorSignal;
import com.cim.idm.framework.exception.ext.DuplicateNameSignal;
import com.cim.idm.framework.exception.ext.FrameworkErrorSignal;
import com.cim.idm.framework.exception.ext.NotFoundSignal;
import com.cim.idm.framework.orm.service.CommonServiceDAO;
import com.cim.idm.mwmsextend.transport.management.TransportCommandDetailService;
import com.cim.idm.mwmsextend.transport.management.data.TransportCommandDetail;
import com.cim.idm.mwmsextend.transport.management.data.TransportCommandDetailKey;
import com.cim.idm.mwmsextend.transport.management.info.CreateDetailInfo;
import com.cim.idm.mwmsextend.transport.management.info.SetDetailEventInfo;
@Service("transportCommandDetailService")
public class TransportCommandDetailServiceImpl extends
		CommonServiceDAO<TransportCommandDetailKey, TransportCommandDetail> implements
		TransportCommandDetailService {
	private ActionProcessor<TransportCommandDetailKey, TransportCommandDetail,CreateDetailInfo>  createActionProcessor;
	private ActionProcessor<TransportCommandDetailKey, TransportCommandDetail, SetDetailEventInfo> setEventActionProcessor;
	
	
	public TransportCommandDetailServiceImpl() {
		createActionProcessor = new ActionProcessor<>(this);
		setEventActionProcessor =  new ActionProcessor<>(this);
	}
	
	public List<TransportCommandDetail> select(String condition, Object[] bindSet
			) throws IDMFrameDBErrorSignal {
		return this.select(condition, bindSet, TransportCommandDetail.class);
		
	}

	@Override
	public TransportCommandDetail create(EventInfo eventInfo,
			CreateDetailInfo createDetailInfo) throws NotFoundSignal,
			DuplicateNameSignal, FrameworkErrorSignal {
		TransportCommandDetailKey[] keyArray = new TransportCommandDetailKey[1];
		TransportCommandDetailKey key = new TransportCommandDetailKey();
		key.setTransportCommandID(createDetailInfo.getTransportCommandID());
		key.setDurableName(createDetailInfo.getDurableName());
		keyArray[0] = key;
		
		CreateDetailInfo[] createDetailInfoArray = new CreateDetailInfo[1];
		createDetailInfoArray[0] = createDetailInfo;
		
		TransportCommandDetail[]  returnData = createActionProcessor.doWorkForBatch(keyArray,eventInfo,createDetailInfoArray,"create");
		return returnData[0];
	}

	@Override
	public TransportCommandDetail[] create(EventInfo eventInfo,
			List<CreateDetailInfo> createDetailInfoList) throws NotFoundSignal,
			DuplicateNameSignal, FrameworkErrorSignal {
		TransportCommandDetailKey[] keyArray = new TransportCommandDetailKey[createDetailInfoList.size()];
		for(int i=0 ; i<keyArray.length ; i++){
			TransportCommandDetailKey key = new TransportCommandDetailKey();
			key.setTransportCommandID(createDetailInfoList.get(i).getTransportCommandID());
			key.setDurableName(createDetailInfoList.get(i).getDurableName());
			keyArray[i] = key;
		}
		CreateDetailInfo[] createDetailInfoArray = new CreateDetailInfo[0];
		createDetailInfoArray = createDetailInfoList.toArray(createDetailInfoArray);
		TransportCommandDetail[] returnData = createActionProcessor.doWorkForBatch(keyArray,eventInfo,createDetailInfoArray,"create");
		return returnData;
	}

	@Override
	public TransportCommandDetail setEvent(TransportCommandDetailKey key,
			EventInfo eventInfo, SetDetailEventInfo setDetailEventInfo)
			throws NotFoundSignal, DuplicateNameSignal, FrameworkErrorSignal {
		TransportCommandDetailKey[] keyArray = new TransportCommandDetailKey[1];
		keyArray[0] = key;
		
		SetDetailEventInfo[] setEventDetailInfoArray = new SetDetailEventInfo[1];
		setEventDetailInfoArray[0] = setDetailEventInfo;
		
		TransportCommandDetail[] returnData = setEventActionProcessor.doWorkForBatch(keyArray,eventInfo,setEventDetailInfoArray,"setEvent");
		return returnData[0];
	}

	@Override
	public TransportCommandDetail[] setEvent(
			List<TransportCommandDetailKey> keyList, EventInfo eventInfo,
			List<SetDetailEventInfo> setDetailEventInfoList)
			throws NotFoundSignal, DuplicateNameSignal, FrameworkErrorSignal {
		TransportCommandDetailKey[] keyArray = new TransportCommandDetailKey[0];
		keyArray = keyList.toArray(keyArray);
		
		SetDetailEventInfo[] setDetailEventInfoArray = new SetDetailEventInfo[0];
		setDetailEventInfoArray = setDetailEventInfoList.toArray(setDetailEventInfoArray);
		
		TransportCommandDetail[] returnData = setEventActionProcessor.doWorkForBatch(keyArray,eventInfo,setDetailEventInfoArray,"setEvent");
		return returnData;
	}

	@Override
	public TransportCommandDetail[] setEvent(
			List<TransportCommandDetailKey> keyList, EventInfo eventInfo,
			SetDetailEventInfo setDetailEventInfo) throws NotFoundSignal,
			DuplicateNameSignal, FrameworkErrorSignal {
		TransportCommandDetailKey[] keyArray = new TransportCommandDetailKey[0];
		keyArray = keyList.toArray(keyArray);
		
		TransportCommandDetail[] returnData = setEventActionProcessor.doWorkAtOnce(keyArray,eventInfo,setDetailEventInfo,"setEvent");
		return returnData;
	}
	
	
}
