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.doc.BatchDoc;
import com.eactive.eai.batch.running.BatchRunningJobManager;
import com.eactive.eai.common.util.Logger;

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

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

    public Object execute() throws CommandException
    {
        logger.info(this.name + " Agent 가 호출되었습니다.");

        try {
			ArrayList<ProcessingFileStatusVO> alist = getProcessingBatchJodStatus();

			logger.info(this.name + " Agent 가 성공적으로 완료되었습니다.");
			logger.info("###### 이 서버에서 진행중인 작업 건 수 (메모리서 조회) : "+ alist.size());
            return alist;

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

    private ArrayList<ProcessingFileStatusVO> getProcessingBatchJodStatus() throws Exception {
		ArrayList<ProcessingFileStatusVO> retList = new ArrayList<ProcessingFileStatusVO>();

		HashMap<String, BatchDoc> allDocs = BatchRunningJobManager.getInstance().getAllRunningDocuments();
		Object[] uuids = allDocs.keySet().toArray();
        
        for (int i=0; i<allDocs.size(); i++) {
            BatchDoc batchDoc = allDocs.get(uuids[i]);
            
            ProcessingFileStatusVO info = new ProcessingFileStatusVO();
            info.setUuid          (batchDoc.getBatchMsg().getHeader().getUUID());
            info.setSubUuid       (batchDoc.getBatchMsg().getBody().getSubUUID());
            info.setProcessCode   (batchDoc.getBatchMsg().getHeader().getProcessCode());
            info.setProcessName   (batchDoc.getBatchMsg().getHeader().getProcessName());
            info.setOrganCode     (batchDoc.getBatchMsg().getHeader().getInstitutionCode());
            info.setOrganName     (batchDoc.getBatchMsg().getHeader().getInstitutionName());
            info.setProcessType   (batchDoc.getBatchMsg().getHeader().getProcessType());
            info.setBatchStartTime(batchDoc.getBatchMsg().getHeader().getBatchStartTime());
            info.setPhaseStartTime(batchDoc.getBatchMsg().getBody().getPhaseStartTime());
            info.setFileName      (batchDoc.getBatchMsg().getHeader().getFileName());
            info.setTotalFileSize (batchDoc.getBatchMsg().getHeader().getFileSize());
            info.setCurrFileSize  (batchDoc.getBatchMsg().getBody().getCurFileSize());
            retList.add(info);
        }
        return retList;
    }

} 
