/*--
  This file is a part of ZetaGrid, a simple and secure Grid Computing
  kernel.

  Copyright (c) 2001-2004 Sebastian Wedeniwski.  All rights reserved.

  Use in source and binary forms, with or without modification,
  are permitted provided that the following conditions are met:

  1. The source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

  2. The origin of this software must not be misrepresented; you must 
     not claim that you wrote the original software.  If you plan to
     use this software in a product, please contact the author.

  3. Altered source versions must be plainly marked as such, and must
     not be misrepresented as being the original software. The author
     must be informed about these changes.

  4. The name of the author may not be used to endorse or promote 
     products derived from this software without specific prior written 
     permission.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

  Version 1.8.6, July 4, 2003

  This program is based on the work of:
     S. Wedeniwski
--*/

package zeta.handler.statistic;

import java.awt.Color;
import java.awt.Paint;
import java.awt.image.BufferedImage;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletException;

import zeta.ZetaServlet;
import zeta.util.Charts;

/**
 *  Handles a GET request for the statistic 'active work units'.
**/
public class ActiveWorkUnitsHandler extends AbstractHandler {
  /**
   *  @param servlet  servlet which owns this handler.
  **/
  public ActiveWorkUnitsHandler(ZetaServlet servlet) {
    super(servlet, 1200000, 1200000, 43200000); // 20 min., 20 min., 12 h
  }

  /**
   *  Creates HTML page with the content of the statistic 'active work units'.
   *  @param  con  connection to the back-end database
   *  @return HTML page with the content of the statistic 'active work units'.
  **/
  public String createPage(Connection con) throws SQLException, ServletException {
    return "";
    /*Map names = new HashMap();
    Statement stmt = null;
    try {
      stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("SELECT name FROM zeta.user WHERE id>0 ORDER BY id");
      while (rs.next()) {
        String name = rs.getString(1);
        String lowerName = name.toLowerCase();
        if (names.get(lowerName) == null) names.put(lowerName, name);
      }
      rs.close();
    } catch (SQLException e) {
      throw new ServletException(e);
    } finally {
      DatabaseUtils.close(stmt);
    }
    StringBuffer buffer = new StringBuffer(400*1024);
    buffer.append("<tr><td colspan=\"6\">");
    buffer.append("<p><center><img src=\"activeworkunits?image=reserved_work_units\"/ width=\"");
    buffer.append(imgWidth);
    buffer.append("\" height=\"");
    buffer.append(imgHeight);
    buffer.append("\"></center>");
    buffer.append("</td></tr>");
    buffer.append("<tr><td colspan=\"6\" height=\"30pt\" class=\"second-head-gray\"><center>Active work units:</center></td></tr>");
    buffer.append("<tr><td colspan=\"6\">");
    buffer.append("<br><center>");

    HtmlTableGenerator generator = new HtmlTableGeneratorWithSum(servlet);
    Table table = new QueryWithSum(names, "SELECT LOWER(b.name) AS \"user\","
                                        + "COUNT(*) AS \"active work units\","
                                        + "COUNT(DISTINCT a.workstation_id) AS \"active computers\","
                                        + "CAST(MIN(a.start) AS DATE) AS \"oldest work unit\","
                                        + "SUM(CAST(a.range AS DECIMAL(15, 0))) AS \"zeros\","
                                        + "MIN(a.work_unit_id) FROM zeta.computation a, zeta.user b"
                                        + " WHERE a.task_id=1"
                                        + " AND a.work_unit_id NOT IN (SELECT work_unit_id FROM zeta.result WHERE task_id=a.task_id)"
                                        + " AND a.user_id<>0 AND a.user_id=b.id AND a.server_id=b.server_id"
                                        + " GROUP BY LOWER(b.name) ORDER BY \"oldest work unit\", MIN(a.work_unit_id)", con, servlet).getResult();
    buffer.append(generator.generate(table));

    buffer.append("</center></td></tr>");
    return buffer.toString();*/
  }

  /**
   *  Creates PNG image for a specified name which is defined in the HTML page.
   *  @param  con  connection to the back-end database
   *  @param  imageName name of the image
   *  @return PNG image for a specified name which is defined in the HTML page.
  **/
  public BufferedImage createImage(Connection con, String imageName) throws SQLException, ServletException {
    final String[] verticalAxisLabels = { "Reserved zeros today", "Reserved zeros yesterday", "Number of reserved work units today", "Number of reserved work units yesterday" };
    final Paint[] colors = { Color.blue, Color.cyan, Color.red, Color.orange };
    final int[] weight = { 6, 1 };
    final int[][] combination = { {0, 1}, {2, 3} };
    return Charts.generateChart(getImageWidth(imageName), getImageHeight(imageName), "Reserved zeros", "Hour", verticalAxisLabels, colors, weight, combination, Charts.createNewReservedZeros(con, 1));
  }
}