package fi.tekislauta.db.objects;

import fi.tekislauta.db.Database;
import fi.tekislauta.models.Board;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hugo on 14.10.2016.
 */
public class BoardDao extends ValidatingDao<Board> implements DataAccessObject<Board, String> {

    public BoardDao() {
    }

    @Override
    public Board find(Database db, String abbreviation) throws SQLException {
        PreparedStatement statement = db.getConnection().prepareStatement("SELECT * FROM Board WHERE abbreviation= ?");
        statement.setString(1, abbreviation);

        ResultSet rs = statement.executeQuery();

        if (!rs.next())
            return null;

        Board b = new Board();

        b.setName(rs.getString("name"));
        b.setAbbreviation(rs.getString("abbreviation"));
        b.setDescription(rs.getString("description"));


        return b;
    }

    @Override
    public List<Board> findAll(Database db, String filter) throws SQLException {
        PreparedStatement statement = db.getConnection().prepareStatement("SELECT * FROM Board");

        ResultSet rs = statement.executeQuery();

        ArrayList<Board> res = new ArrayList();

        while (rs.next()) {
            Board b = new Board();

            b.setName(rs.getString("name"));
            b.setAbbreviation(rs.getString("abbreviation"));
            b.setDescription(rs.getString("description"));
            res.add(b);
        }


        return res;
    }

    @Override
    public Board post(Database db, Board o) throws SQLException, ModelValidationException {
        Board b = (Board) o;
        validateOnInsert(b);

        PreparedStatement statement = db.getConnection().prepareStatement(
                "INSERT INTO Board (name, abbreviation, description) VALUES (?,?,?)",
                Statement.RETURN_GENERATED_KEYS
        );
        statement.setString(1, b.getName());
        statement.setString(2, b.getAbbreviation());
        statement.setString(3, b.getDescription());
        statement.executeUpdate();
        return b;
    }

    @Override
    public void delete(Database db, String filter) throws SQLException {
        Connection con = db.getConnection();
        con.setAutoCommit(false);

        PreparedStatement deleteBoard;
        PreparedStatement deletePosts;

        deleteBoard = con.prepareStatement("DELETE FROM Board WHERE abbreviation = ?");
        deletePosts = con.prepareStatement("DELETE FROM Post WHERE board_abbreviation = ?");

        deleteBoard.setString(1, filter);
        deleteBoard.executeUpdate();

        deletePosts.setString(1, filter);
        deletePosts.executeUpdate();
    }

    @Override
    protected void validateOnInsert(Board board) throws ModelValidationException {
        String abbr = board.getAbbreviation();
        if (abbr == null || abbr.trim().equals(""))
            throw new ModelValidationException(board, "Abbreviation of a new board cannot be null or empty");

        String name = board.getName();
        if (name == null || name.trim().equals(""))
            throw new ModelValidationException(board, "Name of a new board cannot be null or empty");

        // description can be null or empty
    }
}

@@ -1145,33 +1145,32 @@
 executeQuery();%0A
-%0A
         ArrayLis
@@ -1257,33 +1257,32 @@
  = new Board();%0A
-%0A
             b.se
@@ -1465,17 +1465,16 @@
      %7D%0A%0A
-%0A
         
@@ -1547,17 +1547,17 @@
 , Board 
-o
+b
 ) throws
@@ -1602,37 +1602,8 @@
 n %7B%0A
-        Board b = (Board) o;%0A
     
@@ -2194,43 +2194,8 @@
 ();%0A
-        con.setAutoCommit(false);%0A%0A
     
@@ -2227,16 +2227,23 @@
 eteBoard
+ = null
 ;%0A      
@@ -2273,19 +2273,83 @@
 etePosts
+ = null
 ;%0A%0A
+        try %7B%0A            con.setAutoCommit(false);%0A%0A    
         
@@ -2424,24 +2424,28 @@
 tion = ?%22);%0A
+    
         dele
@@ -2526,32 +2526,36 @@
 = ?%22);%0A%0A        
+    
 deleteBoard.setS
@@ -2572,32 +2572,36 @@
 ilter);%0A        
+   
+ 
 deleteBoard.exec
@@ -2610,24 +2610,28 @@
 eUpdate();%0A%0A
+    
         dele
@@ -2660,32 +2660,36 @@
 ilter);%0A        
+   
+ 
 deletePosts.exec
@@ -2696,24 +2696,363 @@
 uteUpdate();
+%0A%0A            con.commit();%0A        %7D catch (SQLException ex) %7B%0A            if (con != null)%0A                con.rollback();%0A        %7D finally %7B%0A            if (deleteBoard != null)%0A                deleteBoard.close();%0A            if (deletePosts != null)%0A                deletePosts.close();%0A            con.setAutoCommit(true);%0A        %7D
 %0A    %7D%0A%0A    

Clean some non-generic remnants in BoardDao, make delete safer