package com.fuse.storage.sql.data_objects;

/*
	FUSE Light Server - multiuser server application
	Copyright (C) 2000 Aapo Kyrola / Sulake Oy  Helsinki, Finland

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


import java.util.Date;
import java.util.*;
import java.io.*;
import java.text.*;
import java.sql.*;

import com.fuse.Log;
import com.fuse.storage.*;
import com.fuse.storage.data_objects.*;
import com.fuse.storage.sql.*;

/**
  * Access
  * @author Aapo Kyrola
  */
public class SQLAccess extends Access implements SQLDataObject {
    private long id = 0;
    private Date date = null;
    private FUSEUser user = null;
    private String ip = "";
    private String domain = "";
    private Date logoutTime = null;
    
    private static SimpleDateFormat sdf;
    
    private void setPreparedValues(PreparedStatement pstmt) throws SQLException {
	// 'YYYY-MM-DD HH:MM:SS' is ok says MySQL manual
	if (sdf == null) sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		pstmt.setObject(1, sdf.format(date));
		pstmt.setLong(2, user.getId());
		pstmt.setString(3, ip);
		pstmt.setString(4, domain);
		pstmt.setObject(5, sdf.format(logoutTime));
    }
    

    public void insert(Connection con) throws DatabaseException {
	    PreparedStatement pstmt = null;
	    String cmd = null;
	    
	    /* Write the properties file to DB */
	    try {
		cmd = new String("INSERT INTO access (date, user_id, ip, domain, logout_time) VALUES (?,?,?,?,?)");
		
		pstmt = con.prepareStatement(cmd);
		setPreparedValues(pstmt);		
		pstmt.executeUpdate();
		pstmt.close();
	    } catch (SQLException ioe) {
		Log.error(ioe);
		throw new DatabaseException("SQL Error");
	    }
	    finally {
		try {
		    if (pstmt != null)
			pstmt.close();
		}
		catch (SQLException e)
		    {
			Log.error(e);
			throw new DatabaseException("SQL Error");
		    }
	    }
    }

    public void update(Connection con) throws DatabaseException {
	Log.error("SQLAccessLog::update() not implemented.");
    }
    
    public void delete(Connection con) throws DatabaseException {
	Log.error("SQLAccessLog::delete() not implemented.");
    }
    
    

    /* Unique id */
    public long getId() {
	return id;
    }
	
    public void setId(long id) {
	this.id = id;
    }
    
    /* Time */
    public void setTime(Date d) {
	this.date = d;
    }
    
    public Date getTime() {
	return date;
    }
    
    /* Logout time */
    public void setLogoutTime(Date d) {
	this.logoutTime = d;
    }
	
    public Date getLogoutTime() {
	return logoutTime;
    }
	
    /* User */
    public FUSEUser getUser() {
	return user;
    }

    
    public void setUser(FUSEUser user) {
	this.user = user;
    }
	
    /* Ip & domain */
    public void setIp(String s) {
	this.ip = s;
    }
	
    public String getIp() {
	return ip;
    }
    
    public void setDomain(String d) {
	this.domain = d;
    }
	
    public String getDomain() {
	return domain;
    }
	
	
}
