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.TransportCommandService;
import com.cim.idm.mwmsextend.transport.management.data.TransportCommand;
import com.cim.idm.mwmsextend.transport.management.data.TransportCommandKey;
import com.cim.idm.mwmsextend.transport.management.info.CreateInfo;
import com.cim.idm.mwmsextend.transport.management.info.SetEventInfo;
@Service("transportCommandService")
public class TransportCommandServiceImpl extends
		CommonServiceDAO<TransportCommandKey, TransportCommand> implements
		TransportCommandService {

	private ActionProcessor<TransportCommandKey, TransportCommand,CreateInfo> createActionProcessor;
	private ActionProcessor<TransportCommandKey, TransportCommand, SetEventInfo> setEventActionProcessor;
	
	public TransportCommandServiceImpl() {
		createActionProcessor = new ActionProcessor<>(this);
		setEventActionProcessor = new ActionProcessor<>(this);
	}
	
	public List<TransportCommand> select(String condition, Object[] bindSet
			) throws IDMFrameDBErrorSignal {
		return this.select(condition, bindSet, TransportCommand.class);
	}

	
	public TransportCommand create(EventInfo eventInfo, CreateInfo createInfo)
			throws NotFoundSignal, DuplicateNameSignal, FrameworkErrorSignal {
		TransportCommandKey[] keyArray = new TransportCommandKey[1];
		TransportCommandKey key = new TransportCommandKey();
		key.setTransportCommandID(createInfo.getTransportCommandID());
		keyArray[0] = key;
		
		CreateInfo[] createInfoArray = new CreateInfo[1];
		createInfoArray[0] = createInfo;
		
		TransportCommand[] returnData = createActionProcessor.doWorkForBatch(keyArray,eventInfo,createInfoArray,"create");
		return returnData[0];
	}
	
	public TransportCommand[] create(EventInfo eventInfo,
			List<CreateInfo> createInfoList) throws NotFoundSignal,
			DuplicateNameSignal, FrameworkErrorSignal {
		TransportCommandKey[] keyArray = new TransportCommandKey[createInfoList.size()];
		for(int i=0 ; i<keyArray.length ; i++){
			TransportCommandKey key = new TransportCommandKey();
			key.setTransportCommandID(createInfoList.get(i).getTransportCommandID());
			keyArray[i] = key;
		}
		CreateInfo[] createInfoArray = new CreateInfo[0];
		createInfoArray = createInfoList.toArray(createInfoArray);
		TransportCommand[] returnData = createActionProcessor.doWorkForBatch(keyArray,eventInfo,createInfoArray,"create");
		return returnData;
	}
	
	@Override
	public TransportCommand setEvent(TransportCommandKey key, EventInfo eventInfo,
			SetEventInfo setEventInfo) throws NotFoundSignal,
			DuplicateNameSignal, FrameworkErrorSignal {
		TransportCommandKey[] keyArray = new TransportCommandKey[1];
		keyArray[0] = key;
		
		SetEventInfo[] setEventInfoArray = new SetEventInfo[1];
		setEventInfoArray[0] = setEventInfo;
		
		TransportCommand[] returnData = setEventActionProcessor.doWorkForBatch(keyArray,eventInfo,setEventInfoArray,"setEvent");
		return returnData[0];
	}

	@Override
	public TransportCommand[] setEvent(List<TransportCommandKey> keyList, EventInfo eventInfo,
			List<SetEventInfo> setEventInfoList) throws NotFoundSignal,
			DuplicateNameSignal, FrameworkErrorSignal {
		TransportCommandKey[] keyArray = new TransportCommandKey[0];
		keyArray = keyList.toArray(keyArray);
		
		SetEventInfo[] setEventInfoArray = new SetEventInfo[0];
		setEventInfoArray = setEventInfoList.toArray(setEventInfoArray);
		
		TransportCommand[] returnData = setEventActionProcessor.doWorkForBatch(keyArray,eventInfo,setEventInfoArray,"setEvent");
		return returnData;
	}

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