<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"%>
<%@page errorPage="AdminException.jsp"%>
<%@page import="java.lang.Long"%>
<%@page import="java.util.Hashtable"%>
<%@page import="com.fujitsu.iflow.model.workflow.WFSession"%>
<%@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"%>
<%
    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
    String isAdminUser = (String)session.getAttribute("isAdminUser");
     
    Dashboard dash = (Dashboard)session.getAttribute("Dashboard");
    if(dash == null)
    {
        dash = bps.getDashboard();
        session.setAttribute("Dashboard",dash);
    }
    KPIDefStruct[] kpiThreshDefs = dash.getKPIThresholdDefs();
    String display = request.getParameter("Display");
    if(display!= null && display.equals("Hide"))
    {
        // this operation is allowed only for Admin user, otherwise throw exception
        bps.getAdmin();
        
        String kpiidStr = request.getParameter("KPIId");
        long kpiid = Long.parseLong(kpiidStr);
        for(int j=0; j<kpiThreshDefs.length; j++)
        {
            if(kpiThreshDefs[j].getKpiId() == kpiid)
            {
                kpiThreshDefs[j].setKpiDisplay(0);
                dash.updateKPIDef(kpiThreshDefs[j]);
                break;
            }
        }
    }
    
    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String rowColor = bcolor[nextColor];

    String pluginVersion = (String) session.getAttribute("PluginVersion");
    String appletClassid = (String) session.getAttribute("AppletClassid");
%>
<%!
    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="90%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr><td height="35" align="right"  valign="top">
        <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_table_view.gif"></td>
            <td background="images/buttons/com_button_bg.gif" nowrap><a href="DashboardText.jsp"><div align="center" class="buttonlink"> Text 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.equalsIgnoreCase("true"))
        {
    %>
        <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 border="0" cellpadding="1" width="90%" align="center" cellspacing="0" bgcolor="#CCCCCC"><tr><td>
<table border="0" width="100%" cellpadding="5" cellspacing="0" class="tableClass"><tr>
<td bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')">
<b>Key Performance Indicators</b>
</td></tr></table>
</td></tr></table>
<br>
      <TABLE border="0" cellpadding="3" cellspacing="3" align="center" width="91%">
        <%
        if(kpiThreshDefs != null) 
        {
            int displItem = 0;
            for(int i=0; i<kpiThreshDefs.length; i++)
            {
                if(kpiThreshDefs[i].getKpiDisplay() == 1)
                {
                    displItem++;
                    String units = " ";
                    long actualVal = dash.getKPIValue(kpiThreshDefs[i].getKpiId());
                    if(actualVal == -1)
                        actualVal = 0;
                    long targetVal = kpiThreshDefs[i].getTargetValue();
                    long ithVal = kpiThreshDefs[i].getThresholds()[0].thresholdValue;
                    long criticalVal = kpiThreshDefs[i].getThresholds()[1].thresholdValue;
                    if(kpiThreshDefs[i].getKpiMeasure() != 1)
                    {
                        targetVal = convertToMins(targetVal);
                        ithVal = convertToMins(ithVal);
                        criticalVal = convertToMins(criticalVal);
                        actualVal = convertToMins(actualVal);
                        units = "minutes";
                    }
                    double maxVal = 0;
                    if(criticalVal < ithVal)
                        maxVal = (ithVal * 1.2) + (10-(ithVal * 1.2) % 10);
                    else 
                        maxVal = (criticalVal * 1.2) + (10-(criticalVal * 1.2) % 10);

                    rowColor = bcolor[nextColor];
                    nextColor = (nextColor + 1)%2;
          %>
          <td> 


	<table width="100%" border="0" cellspacing="0" cellpadding="0" class="headrow" align="center">
		<tr><td>
            <table width="100%" border="0" cellspacing="1" cellpadding="0">
              <tr><td style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')">
			  <table border="0" width="100%" cellpadding="3" cellspacing="0">
			  <td width="14"><img src="images/icons/kpi_arrow.gif" border="0"></td>
                <td ><font face="arial" size="2"><b><%= kpiThreshDefs[i].getName() %></b></font></td>
				<td align="right">
				<a href="KPISettings.jsp?Modify=Change&KPIId=<%=kpiThreshDefs[i].getKpiId()%>">
				<img src="images/icons/kpi_edit.gif" border="0"></a>
				&nbsp;
				<a href="Dashboard.jsp?KPIId=<%=kpiThreshDefs[i].getKpiId()%>&Display=Hide">
				<img src="images/icons/kpi_close.gif" border="0"></a></td>
				</tr></table></td>
              </tr><tr>
                <td align="center" bgcolor="#EEEEEE"><object classid="<%=appletClassid%>"
                    width = 250px height = 165px name = "KPI<%=i%>" align = top>
                    <param name = CODE value = "com.fujitsu.iflow.utilities.Meter.class" >
                    <param name = "cache_option" value = "Plugin" >
                    <param name = "cache_archive" value = "iFlow.jar,jfreechart-0.9.21.jar,jcommon-0.9.7.jar" >
                    <param name = NAME value = "KPI<%=i%>" >
                    <param name = "type" value="application/x-java-applet;version=<%=pluginVersion%>">
                    <param name = "scriptable" value="true">
                    <param name = "MinValue" value="0">
                    <param name = "MaxValue" value="<%= maxVal %>">
                    <param name = "ActualValue" value="<%= actualVal %>">
                    <param name = "InitialTHold" value="<%= ithVal %>">
                    <param name = "CriticalValue" value="<%= criticalVal %>">
                    <param name = "Units" value="<%= units %>">
                    <param name = "FiresScriptEvents" value ="true">
                  </object>

                </td>
              </tr>
            </table>
			</td></tr></table>
          </td>
         <%
            if(displItem%3==0){
         %>
            </tr>
        <%
            }
        %>
    <%
          }
			}
      }
    %>
      </TABLE>
</body>
</html>
