package com.eactive.eai.agent.flowController;

import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.batch.flowController.BatchTargetVO;
import com.eactive.eai.batch.flowController.FlowControllerDAO;
import com.eactive.eai.batch.flowController.FlowControllerManager;
import com.eactive.eai.common.dao.DAOFactory;
import com.eactive.eai.common.util.Logger;

public class AddBatchTargetCommand extends Command
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
    * 1. 기능 : AddBatchTargetCommand 생성자
    * 2. 처리 개요 :
    * 3. 주의사항
    *
    * @param
    * @return
    * @exception
    **/
    public AddBatchTargetCommand(String name) {
        super(name);
    }

    /**
    * 1. 기능 :  FlowControllerManager BatchTargetVO 추가
    * 2. 처리 개요 : FlowControllerManager BatchTargetVO 추가
    * 3. 주의사항
    *
    * @param
    * @return
    * @exception    CommandException
    **/
    public Object execute() throws CommandException
    {
        Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
        logger.info(this.name + " Agent 가 호출되었습니다.");

        if ( args == null || !(args instanceof String[]) ) {
            String msg = makeException("BECEAICAG001", null);
            logger.error(msg);
            throw new CommandException(msg);
        }

        try {
			String[] argArray = (String[]) args;
			String processCode = argArray[0];
			String organCode   = argArray[1];
			// add by kscheon
//			String adapterGroupName = argArray[2];

			FlowControllerDAO dao = (FlowControllerDAO)DAOFactory.newInstance().create(FlowControllerDAO.class);
			BatchTargetVO vo = dao.getBatchTargetVO(processCode, organCode);

			FlowControllerManager.getInstance().getTargets().put(processCode + organCode, vo);
			
//			//20080429 Adapter에 대외기관 코드 추가 - by kscheon - start
//			AdapterGroupVO current = AdapterManager.getInstance().getAdapterGroupVO(adapterGroupName);
//			if (current == null){
//				logger.info(this.name +" Manager's AdapterGroupVO is null. -- "+adapterGroupName);
//				
//			}else {
//				current.setBatchProcessCode(processCode);
//				current.setBatchInstitutionCode(organCode);		
//			}
//			//20080429 Adapter에 대외기관 코드 추가 - by kscheon - end

			logger.info(this.name + " Agent 가 성공적으로 완료되었습니다.");
            return "success";

        } catch (Exception e) {
            String msg = makeException("BECEAICAG002", e);
            logger.error(msg, e);
            throw new CommandException(msg);
        }
	}
}
