package com.eactive.eai.agent.transaction; 

import java.util.ArrayList;
import java.util.HashMap;

import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.batch.rule.dirInfo.BatchJobPriorityManager;
import com.eactive.eai.batch.rule.dirInfo.BatchJobPriorityVO;
import com.eactive.eai.common.util.Logger;

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

	public static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	HashMap<String, ArrayList<BatchJobPriorityVO>> resultMap = null;

    public BatchJobPriorityCommand() {
        super("BatchJobPriorityCommand");
    }

    public Object execute() throws CommandException
    {
        logger.info(this.name + " Agent 가 호출되었습니다.");
        
        try{
        	if( args instanceof String[]){//메모리 업데이트
        		try{
        			String[] params = (String[])args;
        			
        			ArrayList<BatchJobPriorityVO> alist = null;
        			
        			if (params != null && params.length >= 4){
        				HashMap<String, ArrayList<BatchJobPriorityVO>> jobmap = BatchJobPriorityManager.getInstance().getPriorityDatas();
        				
        				alist = jobmap.get(params[0]);
        				
        				if (alist != null && alist.size()>0){
        					for (int i =0; i<alist.size(); i++){
        						BatchJobPriorityVO vo = (BatchJobPriorityVO)alist.get(i);
        						
        						if (params[1].equals(vo.getBizCode())){
        							vo.setDataCount(Integer.parseInt(params[2]));
        							vo.setExecutedFlag(Boolean.parseBoolean(params[3]));
        						}
        					}
        				}
        			}
        			
        			BatchJobPriorityManager.getInstance().addJobPriorityData(params[0], alist);
        			logger.debug(this.name + " add JobPriorityData - BizKey : \n["+params[0]+"]");
        			
        			resultMap = BatchJobPriorityManager.getInstance().getPriorityDatas();
        			return resultMap;
        		} catch (Exception ex) {
        			String msg = makeException("BECEAICAG002", ex);
        			logger.error(msg, ex);
        			throw new CommandException(msg);
        		}
        	}else if (args instanceof String){//메모리 삭제
        		try {
        			String bizKey = (String)args;
        			BatchJobPriorityManager.getInstance().delPriorityData(bizKey);
        			
        			logger.debug(this.name + "del JobPriorityData - BizKey : \n["+bizKey+"]");
        			
        			resultMap = BatchJobPriorityManager.getInstance().getPriorityDatas();
        			
        			return resultMap;
        		}catch (Exception ex) {
        			String msg = makeException("BECEAICAG002", ex);
        			logger.error(msg, ex);
        			throw new CommandException(msg);
        		}
        	}else {
        		resultMap = BatchJobPriorityManager.getInstance().getPriorityDatas();
        		return resultMap;
        	}
        	
        }catch (Exception e){
        	logger.error( e.getMessage(), e);
        	throw new CommandException(e.getMessage());
        }
    }
}