/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ryuuji.servlet.estudiante.nota;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import logica.ExNihilo;
import logica.Search;
import orm.Asignatura;
import orm.Curso;
import orm.Estudiante;
import orm.Nota;

/**
 *
 * @author ryuuji
 */
@WebServlet(name = "EliminarNota", urlPatterns = {"/EliminarNota"})
public class EliminarNota extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        String regExp = "[\\x00-\\x20]*[+-]?(((((\\p{Digit}+)(\\.)?((\\p{Digit}+)?)([eE][+-]?(\\p{Digit}+))?)|(\\.((\\p{Digit}+))([eE][+-]?(\\p{Digit}+))?)|(((0[xX](\\p{XDigit}+)(\\.)?)|(0[xX](\\p{XDigit}+)?(\\.)(\\p{XDigit}+)))[pP][+-]?(\\p{Digit}+)))[fFdD]?))[\\x00-\\x20]*";

        String estudianteString = request.getParameter("estudianteEliminar");
        String asignaturaString = request.getParameter("asignaturaEliminar");
        String cursoString = request.getParameter("cursoEliminar");
        String valorString = request.getParameter("valor");
        if (!estudianteString.equals("")) {
            if (!asignaturaString.equals("")) {
                if (!valorString.equals("")) {
                    if (!cursoString.equals("")) {
                        Estudiante estudiante = Search.buscarEstudiante(estudianteString);
                        if (estudiante != null) {
                            Curso curso = Search.buscarCurso(Integer.parseInt(cursoString.split("-")[0]), cursoString.split("-")[1]);
                            if (curso != null) {
                                Asignatura asignatura = Search.buscarAsignatura(asignaturaString, curso);
                                if (asignatura != null) {
                                    if (valorString.matches(regExp)) {
                                        double valor = Double.parseDouble(valorString);
                                        if (valor >= 0 && valor <= 7) {
                                            Nota nota = Search.buscarNota(estudiante, asignatura, valor);
                                            if (nota != null) {
                                                if (ExNihilo.eliminarNota(nota)) {
                                                    request.getRequestDispatcher("NotasServlet").forward(request, response);
                                                } else {
                                                    request.setAttribute("errorEliminar", "A ocurrido un error innesperado");
                                                    request.getRequestDispatcher("NotasServlet").forward(request, response);
                                                }
                                            } else {
                                                request.setAttribute("errorEliminar", "La nota no existe");
                                                request.getRequestDispatcher("NotasServlet").forward(request, response);
                                            }
                                        } else {
                                            request.setAttribute("errorEliminar", "El valor de la nota no es valido");
                                            request.getRequestDispatcher("NotasServlet").forward(request, response);
                                        }
                                    } else {
                                        request.setAttribute("errorEliminar", "Ingrese una nota valida");
                                        request.getRequestDispatcher("NotasServlet").forward(request, response);
                                    }
                                } else {
                                    request.setAttribute("errorEliminar", "La asignatura no existe");
                                    request.getRequestDispatcher("NotasServlet").forward(request, response);
                                }
                            } else {
                                request.setAttribute("errorEliminar", "El curso no existe");
                                request.getRequestDispatcher("NotasServlet").forward(request, response);
                            }
                        } else {
                            request.setAttribute("errorEliminar", "El estudiante no existe");
                            request.getRequestDispatcher("NotasServlet").forward(request, response);
                        }
                    } else {
                        request.setAttribute("errorEliminar", "No hay ningun curso seleccionado");
                        request.getRequestDispatcher("NotasServlet").forward(request, response);
                    }
                } else {
                    request.setAttribute("errorEliminar", "Ingrese una nota");
                    request.getRequestDispatcher("NotasServlet").forward(request, response);
                }
            } else {
                request.setAttribute("errorEliminar", "No hay ninguna asignatura seleccionada");
                request.getRequestDispatcher("NotasServlet").forward(request, response);
            }
        } else {
            request.setAttribute("errorEliminar", "No hay ningun estudiante seleccionado");
            request.getRequestDispatcher("NotasServlet").forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
