<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"%>
<%@page errorPage="Exception.jsp"%>
<%@page import="java.lang.Long"%>
<%@page import="java.util.Hashtable"%>
<%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"%>
<%@page import="com.fujitsu.iflow.model.workflow.Plan"%>
<%@page import="com.fujitsu.iflow.model.workflow.Dashboard"%>
<%@page import="com.fujitsu.iflow.model.workflow.KPIDefStruct"%>
<%@page import="com.fujitsu.iflow.model.bpm.BPMSession"%>
<%@page import="com.fujitsu.iflow.model.util.ModelException"%>
<%@page import="com.fujitsu.iflow.model.util.QuickForm"%>
<%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
    BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
    if (bps == null) {
        %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
        return;
    }
    
    // check whether the logged in user is a administrator
    boolean isAdminUser = false;
    try
    {
        bps.getAdmin();
        isAdminUser = true;
    }
    catch(ModelException mde)
    {
        isAdminUser = false;
    }
    
    Dashboard dash = (Dashboard)session.getAttribute("Dashboard");
    if(dash == null)
    {
        dash = bps.getDashboard();
        session.setAttribute("Dashboard",dash);
    }
    KPIDefStruct[] kpiThreshDefs = dash.getKPIThresholdDefs();
    Hashtable allTemplates = dash.getAllTemplates();
    Hashtable allActivity = dash.getAllActivities(0);
    
    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String rowColor = bcolor[nextColor];
%>
<%!
    private
    long
    convertToMins(long timeVal)
    {
        // return the value in minutes
        return (timeVal/1000)/60;
    }
%>
<html>
<head>
<title>Interstage Business Process Manager | Dashboard</title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
</head>
<body class="bodyClass">
<%
    session.setAttribute("TabIndex","3");
%>
<jsp:include page="Header.jsp" flush="true"/>
        <br>
              <table width="80%" border="0" cellspacing="0" cellpadding="0" align="center">
                <tr>
                  <td align="right" valign="top" height="35">
        
        <table border="0" cellpadding="0" cellspacing="0"><tr><td>
        
        <table border="0" cellspacing="0" cellpadding="0">
          <tr background="images/buttons/com_button_bg.gif">
            <td width="15"><img src="images/buttons/but_dashboard_view.gif"></td>
            <td background="images/buttons/com_button_bg.gif" nowrap><a href="Dashboard.jsp"><div align="center" class="buttonlink"> Graphical View</div></a></td>
            <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
          </tr>
        </table>
        </td><td>&nbsp;</td><td>
    <%  
        if(isAdminUser)
        {
    %>
        <table border="0" cellspacing="0" cellpadding="0">
          <tr background="images/buttons/com_button_bg.gif">
            <td width="15"><img src="images/buttons/but_kpi_settings.gif"></td>
            <td background="images/buttons/com_button_bg.gif" nowrap><a href="KPISettings.jsp"><div align="center" class="buttonlink"> KPI Settings</div></a></td>
            <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
          </tr>
        </table>
        </td><td>&nbsp;</td><td>
        <table border="0" cellspacing="0" cellpadding="0">
          <tr background="images/buttons/com_button_bg.gif">
            <td width="15"><img src="images/buttons/add_kpi.gif"></td>
            <td background="images/buttons/com_button_bg.gif" nowrap><a href="AddKPISettings.jsp?AddKPI=Add+KPI"><div align="center" class="buttonlink"> Add KPI</div></a></td>
            <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
          </tr>
        </table>
    <%
        }
    %>
        </td></tr></table>
          </td>
        </tr>
      </table>
      <TABLE bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="80%" align="center">
        <tr align="center" class="headrow"> 
          <td colspan="7" bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')"><font color="#000000" size="2">Key 
            Performance Indicators</font> 
            <div align="right"></div>
          </td>
        </tr>
      <tr align="center" class="headrow" bgcolor="#0066CC">
        <th> KPI Name </th>
        <th> KPI Scope </th>
        <th> Process Instance</th>
        <th> Activity </th>
        <th> Wait/Delay Time (mins) </th>
        <th> Amount waiting </th>
        <th> Cycle Time (mins) </th>
      </tr>
      <%
        if(kpiThreshDefs != null)
        {
          for(int i=0; i<kpiThreshDefs.length; i++)
          {
            if(kpiThreshDefs[i].getKpiDisplay() == 1)
            {
                String kpiScope = null;
                String procDefName = null;
                String actName = null;
                String AvgRunProcTime = null;
                String NoProcWaiting = null;
                String cycleTime = null;
                if(kpiThreshDefs[i].getKpiScope() == 0)
                {
                    kpiScope = "System";
                    procDefName = "--";
                    actName = "--";
                    cycleTime = "N/A";
                    if(kpiThreshDefs[i].getKpiMeasure()  == 0) //Wait Time
                    {
                        long waitTime = dash.getAvgTimeAllProcesses();
                        AvgRunProcTime = Long.toString(waitTime);
                        if(AvgRunProcTime.equals("-1"))
                            AvgRunProcTime = "No Process Instances or Activities running";
                        else
                        {
                            waitTime = convertToMins(waitTime);
                            AvgRunProcTime = Long.toString(waitTime);
                        }
                        NoProcWaiting = "--";
                    }
                    else if(kpiThreshDefs[i].getKpiMeasure() == 1) // Amount waiting
                    {
                        AvgRunProcTime = "--";
                        NoProcWaiting = Long.toString(dash.getAllProcessesWaiting());
                        if(NoProcWaiting.equals("-1"))
                            NoProcWaiting = "No Process Instances or Activities running";    
                    }
                    else
                    {
                         NoProcWaiting = "--";
                         AvgRunProcTime = "--";
                         cycleTime = "N/A";
                    }
                }
                if(kpiThreshDefs[i].getKpiScope() == 1)
                {
                    actName = "--";
                    long procDefId = kpiThreshDefs[i].getProcessDefinitionId();
                    kpiScope = "Process";
                    procDefName = (String)allTemplates.get(new Long(procDefId));
                    if(kpiThreshDefs[i].getKpiMeasure() == 0)
                    {
                        long procWaitTime = dash.getProcessWaitTime(procDefId);
                        AvgRunProcTime = Long.toString(procWaitTime);
                        if(AvgRunProcTime.equals("-1"))
                            AvgRunProcTime = "No Process Instances running";
                        else
                        {
                            procWaitTime = convertToMins(procWaitTime);
                            AvgRunProcTime = Long.toString(procWaitTime);
                        }
                        NoProcWaiting = "--";
                        cycleTime = "--";
                    }
                    else if(kpiThreshDefs[i].getKpiMeasure() == 1)
                    {
                        NoProcWaiting = Long.toString(dash.getNoOfProcessesWaiting(procDefId));
                        if(NoProcWaiting.equals("-1"))
                            NoProcWaiting = "No Process Instances running";
                        AvgRunProcTime = "--";
                        cycleTime = "--";                
                    }
                    else if(kpiThreshDefs[i].getKpiMeasure() == 2) //Avg Cycle Time
                    {
                        long procAvgCycleTime = dash.getProcessCycleTime(procDefId);
                        cycleTime = Long.toString(procAvgCycleTime);
                        if(cycleTime.equals("-1")) //Not found any process closed
                            cycleTime = "No Process Instances Completed";
                        else
                        {
                            procAvgCycleTime = convertToMins(procAvgCycleTime);
                            cycleTime = Long.toString(procAvgCycleTime);
                        }
                        NoProcWaiting = "--";
                        AvgRunProcTime = "--";
                    }
                }
                if(kpiThreshDefs[i].getKpiScope() == 2)
                {
                    long procDefId = kpiThreshDefs[i].getProcessDefinitionId();
                    procDefName = (String)allTemplates.get(new Long(procDefId));
                    long actDefId = kpiThreshDefs[i].getActivityDefinitionId();
                    kpiScope = "Activity";      
                    actName = (String)allActivity.get(new Long(actDefId));
                    if(kpiThreshDefs[i].getKpiMeasure() == 0)
                    {
                        long actWaitTime = dash.getActivityWaitTime(actDefId);
                        AvgRunProcTime = Long.toString(actWaitTime);
                        if(AvgRunProcTime.equals("-1"))
                            AvgRunProcTime = "No Activities activated";
                        else
                        {
                            actWaitTime = convertToMins(actWaitTime);
                            AvgRunProcTime = Long.toString(actWaitTime);
                        }
                        NoProcWaiting = "--";
                        cycleTime = "--";
                    }
                    else if(kpiThreshDefs[i].getKpiMeasure() == 1)
                    {
                        NoProcWaiting = Long.toString(dash.getNoOfActivitiesWaiting(actDefId));
                        if(NoProcWaiting.equals("-1"))
                            NoProcWaiting = "No Activities activated";

                        AvgRunProcTime = "--";
                        cycleTime = "--";                
                    }
                    else if(kpiThreshDefs[i].getKpiMeasure() == 2) //Avg Cycle Time
                    {
                        long actAvgCycleTime = dash.getActivityCycleTime(actDefId);
                        cycleTime = Long.toString(actAvgCycleTime);
                        if(cycleTime.equals("-1")) //Not found any activity closed
                            cycleTime = "No Activities Completed";
                        else
                        {
                            actAvgCycleTime = convertToMins(actAvgCycleTime);
                            cycleTime = Long.toString(actAvgCycleTime);
                        }
                        NoProcWaiting = "--";
                        AvgRunProcTime = "--";
                    }
                }
            rowColor = bcolor[nextColor];
            nextColor = (nextColor + 1)%2;
      %>
       <tr class="<%=rowColor%>">
            <td> <%= kpiThreshDefs[i].getName() %></td>
            <td> <%= kpiScope %></td>
            <td> <% QuickForm.writeHtml(out, procDefName); %></td>
            <td> <% QuickForm.writeHtml(out, actName); %></td>
            <td> <%= AvgRunProcTime %></td>
            <td> <%= NoProcWaiting %></td>
            <td> <%= cycleTime %></td> 
       </tr>
        <%
              }
            }
          }
        %>
    </TABLE>
</body>
</html>
