package com.motodb.controller;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.motodb.model.Championship;
import com.motodb.model.Classes;
import com.motodb.view.alert.AlertTypes;
import com.motodb.view.alert.AlertTypesImpl;

public class ChampionshipManager {    
    
    public void insertChampionship(final int year, final int edition) {
        final DBManager db = new DBManager();
        final Connection conn  = db.getConnection();
        
        final java.sql.PreparedStatement statement;
        final String insert = "insert into CAMPIONATO(anno, edizione) values (?,?)";
        try {
            statement = conn.prepareStatement(insert);
            statement.setInt(1, year);
            statement.setInt(2, edition);
            statement.executeUpdate();
        } catch (SQLException e) {
            AlertTypes alert = new AlertTypesImpl();
            alert.showError(e);
        }

    }
    
    /*public void insertClasses(final int year, ) {
        final DBManager db = new DBManager();
        final Connection conn  = db.getConnection();
        
        final java.sql.PreparedStatement statement;
        final String insert = "insert into CAMPIONATO(anno, edizione) values (?,?)";
        try {
            statement = conn.prepareStatement(insert);
            statement.setInt(1, year);
            statement.setInt(2, edition);
            statement.executeUpdate();
        } catch (SQLException e) {
            AlertTypes alert = new AlertTypesImpl();
            alert.showError(e);
        }

    }*/
    
    public List<Championship> showChampionship() {

        final DBManager db = new DBManager();
        final Connection conn  = db.getConnection();
        
        List<Championship> listChampionship = new LinkedList<>();
        final String retrieve = "select * from CAMPIONATO";
        try {
            final PreparedStatement statement = conn.prepareStatement(retrieve);
            final ResultSet result = statement.executeQuery();
            while (result.next()) {
                Championship championship = new Championship();
                championship.setYear(result.getInt("anno"));
                championship.setEdition(result.getInt("edizione"));
                listChampionship.add(championship);
            }
        } catch (SQLException e) {
            AlertTypes alert = new AlertTypesImpl();
            alert.showError(e);
        }
        
        return listChampionship;
        
    }
    
    public List<Classes> showClasses(int year) {

        final DBManager db = new DBManager();
        final Connection conn  = db.getConnection();
        
        List<Classes> listClasses = new LinkedList<>();
        final String retrieve = "SELECT cla.nomeClasse " +
                                "from CLASSE_IN_CAMPIONATO c, CAMPIONATO ca, CLASSE cla " +
                                "WHERE c.annoCampionato=? " + 
                                "AND ca.anno=? " +
                                "AND cla.nomeClasse = c.nomeClasse " +
                                "order by indiceImportanza";
        
        try {
            final PreparedStatement statement = conn.prepareStatement(retrieve);
            statement.setInt(1, year);
            statement.setInt(2, year);
            final ResultSet result = statement.executeQuery();
            while (result.next()) {
                Classes classe = new Classes();
                classe.setName(result.getString("nomeClasse"));
                classe.setRules(result.getString("regolamento"));
                listClasses.add(classe);
            }
        } catch (SQLException e) {
            AlertTypes alert = new AlertTypesImpl();
            alert.showError(e);
        }
        
        return listClasses;
        
    }
    
}


@@ -356,16 +356,189 @@
 sImpl;%0A%0A
+import javafx.collections.FXCollections;%0Aimport javafx.collections.ObservableArray;%0Aimport javafx.collections.ObservableList;%0Aimport javafx.collections.ObservableListBase;%0A%0A
 public c
@@ -2789,24 +2789,34 @@
 %0A    public 
+Observable
 List%3CClasses
@@ -2954,24 +2954,34 @@
    %0A        
+Observable
 List%3CClasses
@@ -2996,32 +2996,49 @@
 asses = 
-new Linked
+FXCollections.observableArray
 List
-%3C%3E
 ();%0A    
@@ -3831,32 +3831,34 @@
                 
+//
 classe.setRules(

Fix sql