<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"%>
<%@page errorPage="Exception.jsp"%>
<%@page import="com.fujitsu.iflow.model.workflow.WFSession"%>
<%@page import="com.fujitsu.iflow.model.workflow.Dashboard"%>
<%@page import="com.fujitsu.iflow.model.workflow.KPIDefStruct"%>
<%@page import="com.fujitsu.iflow.model.workflow.ThresholdDefStruct"%>
<%@page import="com.fujitsu.iflow.model.workflow.ResponseDefStruct"%>
<%@page import="java.lang.Long"%>
<%@page import="java.util.Enumeration"%>
<%@page import="java.util.Hashtable"%>
<%
    boolean updateKPI = false;
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
    WFSession wfSession = (WFSession)session.getAttribute("ModelsessionID");
    if (wfSession == null) {
        %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
        return;
    }

    String addKPI = (String)session.getAttribute("AddKPI");

    Dashboard dash = (Dashboard)session.getAttribute("Dashboard");
    KPIDefStruct kpiDefStruct = new KPIDefStruct();
    
    String opnPerform = (String)session.getAttribute("opnToPerform");
    if(opnPerform != null && opnPerform.equals("Change"))
    {
        kpiDefStruct = (KPIDefStruct)session.getAttribute("KPIDefStruct");
        updateKPI = true;
    }
    else
        updateKPI = false;
        
    kpiDefStruct.setName(request.getParameter("KPIName"));
    kpiDefStruct.setDesc(kpiDefStruct.getName());
    String kpiScope = request.getParameter("KPIScope");
    if(kpiScope != null)
    {
        if(kpiScope.equals("System"))
            kpiDefStruct.setKpiScope(0);
        else if(kpiScope.equals("Process"))
        {
            kpiDefStruct.setKpiScope(1);
            String TemplateName = request.getParameter("ProcessList");
            Hashtable allTempl = (Hashtable)session.getAttribute("AllTemplates");
            for(Enumeration keyEnum = allTempl.keys(); keyEnum.hasMoreElements();)
            {
                Long procDefId = (Long)keyEnum.nextElement();
                String tName = (String)allTempl.get(procDefId);
                if(tName.equals(TemplateName))
                {
                    kpiDefStruct.setProcessDefinitionId(procDefId.longValue());
                    break;
                }
            }
        }
        else
        {
            String TemplateName = request.getParameter("ProcessList");
            if(TemplateName != null)
            {
                Hashtable allTempl = (Hashtable)session.getAttribute("AllTemplates");
                for(Enumeration keyEnum = allTempl.keys(); keyEnum.hasMoreElements();)
                {
                    Long procDefId = (Long)keyEnum.nextElement();
                    String tName = (String)allTempl.get(procDefId);
                    if(tName.equals(TemplateName))
                    {
                        kpiDefStruct.setProcessDefinitionId(procDefId.longValue());
                        break;
                    }
                }
            }
            kpiDefStruct.setKpiScope(2); // for Activity
            String ActivityName = request.getParameter("ActivityList");
            Hashtable allAct = (Hashtable)session.getAttribute("AllActivities");
            for(Enumeration keyEnum = allAct.keys(); keyEnum.hasMoreElements();)
            {
                Long actDefId = (Long)keyEnum.nextElement();
                String tName = (String)allAct.get(actDefId);
                if(tName.equals(ActivityName))
                {
                    kpiDefStruct.setActivityDefinitionId(actDefId.longValue());
                    break;
                }
            }
        }
    }
    else
        kpiDefStruct.setKpiScope(0);
    kpiDefStruct.setDisplayType(0);
    String display = request.getParameter("Display");
    if(display != null)
    {
        if(display.equals("0"))
            kpiDefStruct.setKpiDisplay(0); //don't show the kpi in dashboard
        else
            kpiDefStruct.setKpiDisplay(1); // show the kpi in dashboard
    }
    else
        kpiDefStruct.setKpiDisplay(1); // default
    
    long kpiTgtVal = Long.parseLong(request.getParameter("Target"));  
    kpiDefStruct.setTargetValue(kpiTgtVal);
    String kpiMeasure = request.getParameter("Measure");
    if(kpiMeasure.equals("WaitTime"))
    {
        kpiDefStruct.setKpiMeasure(0);
        if(addKPI != null || updateKPI)
            kpiDefStruct.setTargetValue(convertToMilis(kpiTgtVal));
    }
    else if(kpiMeasure.equals("AmountWaiting"))
    {
        kpiDefStruct.setKpiMeasure(1);
    }
    else
    {
        kpiDefStruct.setKpiMeasure(2);
        if(addKPI != null || updateKPI)
            kpiDefStruct.setTargetValue(convertToMilis(kpiTgtVal));
    }
    
    // Threshold information
    ThresholdDefStruct[] thresholds = new ThresholdDefStruct[2];
    
    // Initial Threshold information
    thresholds[0] = new ThresholdDefStruct();
    if(updateKPI)
    {
        thresholds[0].thresholdId = kpiDefStruct.getThresholds()[0].thresholdId;
        thresholds[0].kpiId = kpiDefStruct.getKpiId();
    }
    thresholds[0].thresholdType = 1; // Initial threshold
    thresholds[0].thresholdValue = Long.parseLong(request.getParameter("InitialThreshold"));
    if(kpiDefStruct.getKpiMeasure() != 1)
    {
        if(addKPI != null || updateKPI)
            thresholds[0].thresholdValue = convertToMilis(thresholds[0].thresholdValue);
    }
    thresholds[0].alarmFrequency = convertToMilis(Long.parseLong(request.getParameter("InitialAlarmFrequency"))); 
    
    String tempInitAlertName = request.getParameter("AlertListProcessInit");
    Hashtable allInitAlertTempl = (Hashtable)session.getAttribute("AllTemplates");
    for(Enumeration keyEnum = allInitAlertTempl.keys(); keyEnum.hasMoreElements();)
    {
        Long procDefId = (Long)keyEnum.nextElement();
        String tName = (String)allInitAlertTempl.get(procDefId);
        if(tName.equals(tempInitAlertName))
        {
            thresholds[0].processDefinitionId = procDefId.longValue();
            break;
        }
    }
    thresholds[0].description = request.getParameter("AlertMessageInitial");
    
    // Initial Threshold reposnse information
    ResponseDefStruct initResponses = new ResponseDefStruct();
    if(updateKPI)
    {
        initResponses.responseId = kpiDefStruct.getThresholds()[0].responses[0].responseId;   
        initResponses.thresholdId = kpiDefStruct.getThresholds()[0].thresholdId;
    }
    initResponses.actionName = "Not Used";
    initResponses.actionDetails = "Not Used";
    initResponses.actionType = new Long(thresholds[0].processDefinitionId).intValue();
    thresholds[0].responses = new ResponseDefStruct[1];
    thresholds[0].responses[0] = initResponses;
    
    //critical threshold information
    thresholds[1] = new ThresholdDefStruct();
    if(updateKPI)
    {
        thresholds[1].thresholdId = kpiDefStruct.getThresholds()[1].thresholdId;
        thresholds[1].kpiId = kpiDefStruct.getKpiId();
    }
    thresholds[1].thresholdType = 2; // Critical threshold
    thresholds[1].thresholdValue = Long.parseLong(request.getParameter("CriticalThreshold"));
    if(kpiDefStruct.getKpiMeasure() != 1)
    {
        if(addKPI != null || updateKPI)
            thresholds[1].thresholdValue = convertToMilis(thresholds[1].thresholdValue);
    }
    thresholds[1].alarmFrequency = convertToMilis(Long.parseLong(request.getParameter("CriticalAlarmFrequency"))); 
    
    String tempAlertCriticName = request.getParameter("AlertListProcessCritic");
    Hashtable allCriticAlertTempl = (Hashtable)session.getAttribute("AllTemplates");
    for(Enumeration keyEnum = allCriticAlertTempl.keys(); keyEnum.hasMoreElements();)
    {
        Long procDefId = (Long)keyEnum.nextElement();
        String tName = (String)allCriticAlertTempl.get(procDefId);
        if(tName.equals(tempAlertCriticName))
        {
            thresholds[1].processDefinitionId = procDefId.longValue();
            break;
        }
    }
    thresholds[1].description = request.getParameter("AlertMessageCritical");
    
    // Critical Threshold reposnse information
    ResponseDefStruct criticResponses = new ResponseDefStruct();
    if(updateKPI)
    {
        criticResponses.responseId = kpiDefStruct.getThresholds()[1].responses[0].responseId;   
        criticResponses.thresholdId = kpiDefStruct.getThresholds()[1].thresholdId;
    }
    criticResponses.actionName = "Not Used";
    criticResponses.actionDetails = "Not Used";
    criticResponses.actionType = new Long(thresholds[1].processDefinitionId).intValue();
    thresholds[1].responses = new ResponseDefStruct[1];
    thresholds[1].responses[0] = criticResponses;
    
    //finally set the entire threshold structure into the KPIDefinition structure
    kpiDefStruct.setThresholds(new ThresholdDefStruct[2]);
    kpiDefStruct.setThresholds(thresholds);
    String procListChange = request.getParameter("ProcessListChanged");
    if(procListChange != null && procListChange.equals("true"))
    {
        session.setAttribute("KPIDefStruct",kpiDefStruct);
        session.setAttribute("opnToPerform","Change");
        response.sendRedirect(response.encodeRedirectURL("AddKPISettings.jsp"));
        return;
    }
    if(updateKPI && addKPI == null )
    {
        dash.updateKPIDef(kpiDefStruct);
    }
    else
    {
        dash.addKPI(kpiDefStruct);
    }
    response.sendRedirect(response.encodeRedirectURL("KPISettings.jsp"));
    
%>

<%!
    private
    long
    convertToMilis(long timeVal)
    {
        // return the value in milis
        return 1000*60*timeVal;
    }
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Interstage Business Process Manager | Save KPIs</title>
</head>
<body>
</body>
</html>
