package com.eactive.eai.agent.web;

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

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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 BatchJobPriorityMan extends HttpServlet
{

    private static final long serialVersionUID = 1L;
    Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

   /**
    * 1. 기능 :  요청으로 전달된 Command 실행
    * 2. 처리 개요 : 요청으로 전달된 Command 실행하고 실행된 결과를 ObjectOutStream을 통해 전송
    * 3. 주의사항
    *
    * @param    request     HttpServletRequest
    * @param    response    HttpServletResponse
    * @return
    * @exception
    **/
    public void service(HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("[BatchJobPriorityMan] Servlet starting .......");

            String scr_name = request.getParameter("scr_name");

            if ("UPDATE".equals(scr_name)){
                String key = request.getParameter("key");
                int index = Integer.parseInt(request.getParameter("index"));

                String m_bizCode[]   = request.getParameterValues("bizCode");
                String m_dataCount[] = request.getParameterValues("dataCount");
                String m_isexe[]     = request.getParameterValues("isexe");

                BatchJobPriorityManager jobpmanager = BatchJobPriorityManager.getInstance();
                HashMap<String, ArrayList<BatchJobPriorityVO>> jobmap = jobpmanager.getPriorityDatas();

                ArrayList<BatchJobPriorityVO> alist = jobmap.get(key);

                if (alist != null && alist.size()>0){
                    for (int i =0; i<alist.size(); i++){
                        BatchJobPriorityVO vo = alist.get(i);

                        if (m_bizCode[index].equals(vo.getBizCode())){
                            vo.setDataCount(Integer.parseInt(m_dataCount[index]));
                            vo.setExecutedFlag(Boolean.parseBoolean(m_isexe[index]));                        
                        }
                    }
                }
            }else if("DELETE".equals(scr_name)){

                String key = request.getParameter("key");
                BatchJobPriorityManager jobpmanager = BatchJobPriorityManager.getInstance();
                jobpmanager.delPriorityData(key);
            }
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/batchjobpriority.jsp");
            rd.forward(request, response);

        } catch (Exception ex) {
        	logger.error( ex.getMessage(), ex);
            response.setStatus(500);
            try {
                response.getWriter().println(ex.getMessage());
                throw ex;
            } catch (Exception e) {
            }
        }
    }
}
