package com.eactive.eai.agent.transaction; 

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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 ProcessingFileTransferCommand extends Command
{ 
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

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

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

        try {
			
        	List<Map<String,Serializable>> 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 List<Map<String,Serializable>> getProcessingBatchJodStatus() throws Exception {
    	List<Map<String,Serializable>> retList = new ArrayList<Map<String,Serializable>>();

		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]);

            Map<String,Serializable> map = new HashMap<String,Serializable>();
            map.put( "Uuid"           , batchDoc.getBatchMsg().getHeader().getUUID());
            map.put( "SubUuid"        , batchDoc.getBatchMsg().getBody().getSubUUID());
            map.put( "ProcessCode"    , batchDoc.getBatchMsg().getHeader().getProcessCode());
            map.put( "ProcessName"    , batchDoc.getBatchMsg().getHeader().getProcessName());
            map.put( "OrganCode"      , batchDoc.getBatchMsg().getHeader().getInstitutionCode());
            map.put( "OrganName"      , batchDoc.getBatchMsg().getHeader().getInstitutionName());
            map.put( "ProcessType"    , batchDoc.getBatchMsg().getHeader().getProcessType());
            map.put( "BatchStartTime" , batchDoc.getBatchMsg().getHeader().getBatchStartTime());
            map.put( "PhaseStartTime" , batchDoc.getBatchMsg().getBody().getPhaseStartTime());
            map.put( "FileName"       , batchDoc.getBatchMsg().getHeader().getFileName());
            map.put( "TotalFileSize"  , batchDoc.getBatchMsg().getHeader().getFileSize());
            map.put( "CurrFileSize"   , batchDoc.getBatchMsg().getBody().getCurFileSize());
            int rate = (int)(100 * batchDoc.getBatchMsg().getBody().getCurFileSize() / batchDoc.getBatchMsg().getHeader().getFileSize());
            if ( rate > 0 && rate <= 100 ){
                map.put( "Rate"       , rate);
            }else{
                map.put( "Rate"       , "-");
            }
            retList.add(map);
        }
        return retList;
    }

} 
