import hep.analysis.*;
import jas.hist.*;
import jas.hep.PartitionAdapter;
import java.sql.*;
import java.io.*;
import javax.swing.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Downloads extends SwingServlet implements Runnable
{		
	public Downloads()
	{
		hist1 = new Histogram("Version 1");
		hist2 = new Histogram("Version 2");
		histp = new Histogram("Platform");
	}
	public void init(ServletConfig config) throws ServletException
	{
		try
		{
			m_driver = Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}
		catch (ClassNotFoundException x)
		{
			throw new ServletException("Could not load database driver");
		}

		super.init(config);
		
		fillHistograms();
		
		// Create a background thread that will refill the histograms 
		// from the database each hour
		
		update_thread = new Thread(this);
		update_thread.start();
	}
	public void destroy()
	{
		update_thread.interrupt();
		update_thread = null;
		super.destroy();
	}
	
	protected synchronized JComponent createComponent(HttpServletRequest req)
	{				
		JASHist plot1 = new JASHist();
		plot1.setPreferredSize(new java.awt.Dimension(300,225));
		plot1.setTitle("Downloads vs Time");
		JASHistData data1 = plot1.addData(PartitionAdapter.create(
								hist1.getPartition(),hist1.getName()));
		((JASHist1DHistogramStyle) data1.getStyle()).setShowErrorBars(false);
		data1.show(true);
		JASHistData data2 = plot1.addData(PartitionAdapter.create(
								hist2.getPartition(),hist2.getName()));
		((JASHist1DHistogramStyle) data2.getStyle()).setShowErrorBars(false);
		data2.show(true);
		
		JASHist plot2 = new JASHist();
		plot2.setPreferredSize(new java.awt.Dimension(200,225));
		plot2.setTitle("Downloads by Platform");
		JASHistData data3 = plot2.addData(PartitionAdapter.create(
								histp.getPartition(),histp.getName()));
		((JASHist1DHistogramStyle) data3.getStyle()).setShowErrorBars(false);
		data3.show(true);
		
		JPanel panel = new JPanel();
		panel.add(plot1);
		panel.add(plot2);
		
		return panel;
	}
	/**
	 * This is the method run by the update thread, it just calls fillHistograms 
	 * once an hour.
	 */
	public void run()
	{
		try
		{
			for (;;)
			{
				// Wait an hour between updates
				Thread.sleep(1000*60*60);
				fillHistograms();
			}
		}
		catch (InterruptedException x) {}
	}
	private synchronized void fillHistograms()
	{
		// Open the downloads database.		
		
		Connection con = null;
		try
		{
			String url = "jdbc:odbc:jas";
			con = DriverManager.getConnection(url, null, null);
	
			// Get the download list
		
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT * FROM [Downloads]");
		
			hist1.clear();
			hist2.clear();
			histp.clear();
			
			while (rs.next())
			{
				Date d = rs.getDate("When");
				if (d == null) continue;
				String version = rs.getString("Version");
				String platform = rs.getString("Platform");
				if (platform == null || platform.length() == 0 ||
					platform.equals(" ") || platform.equals("none")) continue;

				// We want the version 1 entries to "stack" on top of the version 2 
				// entries, so we histogram both v1 and v2 into hist1, and only v2
				// into hist2
				
				hist1.fill(d);
				if (version.startsWith("2")) hist2.fill(d);
				histp.fill(platform);
			}
			rs.close();
			stmt.close();
			
			log("Download histograms updated histp.entries="+histp.getEntries());
		}
		catch (SQLException x) 
		{ 
			getServletContext().log(x,"Unable to update download histograms"); 
		}
		finally
		{
			try
			{
				if (con != null) con.close();
			}
			catch (SQLException x) {}
		}
	}
	private Histogram hist1;
	private Histogram hist2;
	private Histogram histp;
	private static Object m_driver;
	private Thread update_thread;
}
