package com.eactive.eai.agent.ftp;

import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.inbound.remote.RemoteTransfer;
import com.eactive.eai.inbound.remote.RemoteTransferLogKeys;
import com.eactive.eai.inbound.remote.RemoteTransferLogManager;
import com.eactive.eai.inbound.remote.RemoteTransferLogVO;

public class RemoteTransferCommand extends Command
{
	private static final long serialVersionUID = 1L;

	public RemoteTransferCommand(String name) {
        super(name);
    }

	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);
        }
        
        String[] param = (String[])args;
        String logID   = param[0];
        String userId  = param[1];

        try {
        	logger.debug("[RemoteTransferCommand]logID-->[" + logID + "]");
        	RemoteTransferLogVO log = RemoteTransferLogManager.getInstance().getRemoteTransferLog(logID);
        	RemoteTransfer transfer = new RemoteTransfer();
        	if (log.getSendRecvYn().equals(RemoteTransferLogKeys.SEND)){
        		transfer.sendFile(log.getBjobBzwkDstcd(), log.getOsidInstiDstcd(), log.getTrsmtFileName(), RemoteTransferLogKeys.START_BY_UI, userId, false);
            	return transfer.sendFile(log.getBjobBzwkDstcd(), log.getOsidInstiDstcd(), log.getTrsmtFileName(), RemoteTransferLogKeys.START_BY_UI, userId, true);
        	}else{
        		return transfer.recvFile( log.getRmtFileTrsmtPathName() + "/" + log.getTrsmtFileName(), RemoteTransferLogKeys.START_BY_UI, userId);
        	}

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

}
