/*
 * $Id: VKC404Serializer.java,v 1.15 2006/03/27 22:08:15 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKC404Serializer.java,v $
 * Revision 1.15  2006/03/27 22:08:15  marbobo
 * + Bugfixes und Refactoring
 *
 * Revision 1.14  2004/12/01 21:44:15  marbobo
 * + Statement auf PreparedStatement umgestellt
 *
 * Revision 1.13  2004/11/24 13:01:12  lawern
 * Global "conn.setAutoCommit( false );" enfernt, weil überflüssig.
 *
 * Revision 1.12  2004/11/24 09:25:54  lawern
 * Hibernate ist VKC-Geschichte!
 *
 * Revision 1.11  2004/11/18 15:52:31  jonyp
 * changed order of close-stmt to resultset/connect/session  in finally-blocks
 *
 * Revision 1.10  2004/11/16 11:18:13  jonyp
 * bug fix in statement to update server's downtime
 *
 * Revision 1.9  2004/11/16 11:08:10  jonyp
 * changed table availabilities ==> VKC_AVAILS
 *
 * Revision 1.8  2004/10/21 23:49:17  marbobo
 * + CURRENT_TIMESTAMP ersetzt mit java-Anweisung
 *
 * Revision 1.7  2004/07/20 08:23:21  jonyp
 * added copyright info + cvs log
 *
 * Revision 1.6  2004/06/15 15:20:51  jonyp
 * added comment
 *
 *
 */
package vkc.db.serializer;

import vkc.db.entities.VKC404;
import vkc.db.helper.DBHelper;
import vkc.exception.DBException;
import vkc.view.WebShare;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.List;
import java.util.ArrayList;

import java.sql.Connection;

public class VKC404Serializer {
    private static Integer currentVkc404Id = new Integer(0);
    
	private VKC404Serializer() {
	}

	public final static List retrieveAvailabilities() throws DBException {
		Connection conn = null;

		try {
			conn = WebShare.database.getConnection();
			
			return retrieveAvailabilities(conn);
		} catch (Exception e) {
			throw new DBException(e);
		} finally {
			DBHelper.close(conn);
		}
	}

	public final static List retrieveAvailabilities(Connection conn) throws DBException {
		PreparedStatement pStmt = null;
		ResultSet rs = null;

		try {
			pStmt = conn.prepareStatement("SELECT * FROM vkc_avails ORDER BY uptime DESC");
			rs = pStmt.executeQuery();

			ArrayList allEntries = new ArrayList();

			while (rs.next()) {
				VKC404 vkc404 = new VKC404();
				vkc404.setVkc404Id(new Integer(rs.getInt("vkc404Id")));
				vkc404.setUptime(rs.getTimestamp("uptime"));
				vkc404.setUptimeDescription(rs.getString("uptime_descr"));
				vkc404.setDowntime(rs.getTimestamp("downtime"));
				vkc404.setDowntimeDescription(rs.getString("downtime_descr"));

				allEntries.add(vkc404);
			}

			return allEntries;
		} catch (Exception e) {
			throw new DBException(e);
		} finally {
			DBHelper.close(rs);
			DBHelper.close(pStmt);
		}
	}

	public final static void setServerIsUp() throws DBException {
	    setServerIsUp(null);
	}
	
	public final static void setServerIsUp(String comment) throws DBException {
		Connection conn = null;
	    PreparedStatement pStmt = null;
		String reason = "(" + WebShare.getProperties().getProperty("vkc.version") + ") ";
		
		if (comment != null) {
		    reason = reason + comment;
		}
		
		try {
		    conn = WebShare.database.getConnection();
		    pStmt = conn.prepareStatement("INSERT INTO vkc_avails ( vkc404Id, uptime, uptime_descr ) VALUES ( ?, ?, ? )");
		    
		    currentVkc404Id = DBHelper.getNewId(conn, "VKC_AVAILS", "vkc404Id");
		    pStmt.setInt(1, currentVkc404Id.intValue());
		    pStmt.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
		    pStmt.setString(3, reason);
			
			pStmt.execute();
			conn.commit();
		} catch (Exception e) {
			throw new DBException(e);
		} finally {
			DBHelper.close(pStmt);
			DBHelper.close(conn);
		}
	}
	
	public final static void setServerIsDown() throws DBException {
	    setServerIsDown(null);
	}
	
	public final static void setServerIsDown(String comment) throws DBException {
		Connection conn = null;
	    PreparedStatement pStmt = null;
		String reason = "(" + WebShare.getProperties().getProperty("vkc.version") + ") ";
		
		if (comment != null) {
		    reason = reason + comment;
		}
		
		try {
		    conn = WebShare.database.getConnection();
		    pStmt = conn.prepareStatement("UPDATE vkc_avails SET downtime = ?, downtime_descr = ? WHERE vkc404Id = ?");
		    			
		    pStmt.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
		    pStmt.setString(2, reason);
		    pStmt.setInt(3, currentVkc404Id.intValue());
			
			pStmt.execute();
			conn.commit();
		} catch (Exception e) {
			throw new DBException(e);
		} finally {
			DBHelper.close(pStmt);
			DBHelper.close(conn);
		}
	}
}