/*
 * Users.java, 5 juin 2006
 * 
 * This file is part of FilmZ.
 *
 * Copyright © 2006 Johan Cwiklinski
 *
 * File :               Users.java
 * Author's email :     johan@x-tnd.be
 * Author's Website :   http://www.x-tnd.be/
 * 
 * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 */

/**
 * 
 */
package be.xtnd.films;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import be.xtnd.commons.db.Database;
import be.xtnd.gui.FilmzMain;

/**
 * @author Johan Cwiklinski
 *
 */
public class Users {
	private static Database db = FilmzMain.db;
	static Logger logger = Logger.getLogger(Users.class.getName());
	/** */
	public static final String TABLE = "users";
	private String login;
	private String password;
	private String email;
	private boolean admin;
	private boolean auth;
	private Timestamp stored;
	private Timestamp last_connect;

	/**
	 * 
	 *
	 */
	public Users(){
		PropertyConfigurator.configure(ClassLoader.getSystemResource("be/xtnd/films/log4j.properties"));
	}
	
	/**
	 * 
	 * @param id
	 */
	public Users(String id){
		this();
		String requete = "SELECT * FROM " + TABLE + " WHERE login='" + id +"'";
		try {
			ResultSet rs = db.execQuery(requete);
			rs.first();
			this.login = rs.getString("login");
			//this.password
			this.email = rs.getString("mail");
			this.admin = rs.getBoolean("admin");
			this.auth = rs.getBoolean("auth");
			this.stored = rs.getTimestamp("date");
			this.last_connect = rs.getTimestamp("last_connect");
		} catch (SQLException e) {
			logger.fatal("SQLException loading user " + e.getMessage() + " (" + e.getErrorCode() +")");
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @return en tête de la table liste
	 */
	public static String[] tableHeaders(){
		String[] s = {"login", "email", "adm.", "aut."};
		return s;
	}
	
	/**
	 * 
	 * @return liste utilisateurs
	 */
	public static ArrayList<Object[]> list(){
		ArrayList<Object[]> ar = new ArrayList<Object[]>(10);
		//Object[][] o = null;
		String requete = "SELECT * FROM " + TABLE;
		try {
			ResultSet rs = db.execQuery(requete);
			rs.beforeFirst();
			while(rs.next()){
				Object[] o = {
						rs.getString("login"),
						rs.getString("mail"),
						rs.getBoolean("admin"),
						rs.getBoolean("auth")};
				ar.add(o);
			}
		} catch (SQLException e) {
			logger.fatal("SQLException listing users "+e.getMessage() + " ("+e.getErrorCode()+")");
			e.printStackTrace();
		}
		ar.trimToSize();
		return ar;
	}

	/**
	 * @return Renvoie admin.
	 */
	public boolean isAdmin() {
		return admin;
	}

	/**
	 * @param admin admin à définir.
	 */
	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	/**
	 * @return Renvoie auth.
	 */
	public boolean isAuth() {
		return auth;
	}

	/**
	 * @param auth auth à définir.
	 */
	public void setAuth(boolean auth) {
		this.auth = auth;
	}

	/**
	 * @return Renvoie email.
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email email à définir.
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return Renvoie login.
	 */
	public String getLogin() {
		return login;
	}

	/**
	 * @param login login à définir.
	 */
	public void setLogin(String login) {
		this.login = login;
	}

	/**
	 * @return Renvoie last_connect.
	 */
	public Timestamp getLast_connect() {
		return last_connect;
	}

	/**
	 * @return Renvoie stored.
	 */
	public Timestamp getStored() {
		return stored;
	}

	/**
	 * @param password password à définir.
	 */
	public void setPassword(String password) {
		this.password = password;
	}


}
