package com.eactive.eai.agent.authoutbound;

import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.common.authoutbound.AccessTokenManagerByDB;
import com.eactive.eai.common.util.Logger;

public class RemoveOutboundOAuthCredentialCommand extends Command {
    private static final long serialVersionUID = 1L;
    private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

    @Override
    public Object execute() throws CommandException {
        if (logger.isInfo()) {
            logger.info(this.name + " is executed");
        }

        if (!(args instanceof String)) {
            throwCommandException("RECEAIMCM001", null);
        }

        try {
            String adapterGroupName = (String) args;
            AccessTokenManagerByDB.getInstance().removeAccessTokenVO(adapterGroupName);
        } catch (Exception e) {
            throwCommandException("RECEAIMCM002", e);
        }
        return "success";
    }

    private void throwCommandException(String errorCode, Exception exception) throws CommandException {
        Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
        String msg = makeException(errorCode, exception);
        if (logger.isError()) {
            logger.error(msg, exception);
        }
        throw new CommandException(msg);
    }
}
