/**
 * 
 */
package org.mynonglam.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.mynonglam.connectdb.ConnectionDTB;
import org.mynonglam.entitys.CommentStatus;

/**
 * @author Dao
 *
 */
public class CommentStatusDAO {

	private static Connection connection;
	private static PreparedStatement statement = null;
	private ResultSet resultSet;
	/**
	 * 
	 */
	public CommentStatusDAO() {
		connection = ConnectionDTB.getConnection();
	}
	
	public boolean insertCommentStatus(CommentStatus status) {
		boolean result = false;
		
		String sql = "INSERT INTO commentstatus(idcomstatus, idstatus, email, content, commentdate) VALUES (?,?,?,?,?)";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, status.getIdcomstatus());
			statement.setString(2, status.getStatus().getIdstatus());
			statement.setString(3, status.getAccount().getEmail());
			statement.setString(4, status.getContent());
			statement.setString(5, status.getCommentdate());
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public String getIDStatusCommentLast() {
		String result = "";
		
		String sql = "SELECT idcomstatus FROM commentstatus ORDER BY idcomstatus DESC LIMIT 0,1";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			ResultSet resultSet = statement.executeQuery();
			
			if(resultSet.next()) {
				result = resultSet.getString("idcomstatus");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public boolean deleteCommentStatus(String idcoment) {
		boolean result = false;
		
		String sql = "DELETE FROM commentstatus WHERE idcomstatus=?";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, idcoment);
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}

}
