151 lines
5.3 KiB
Java
151 lines
5.3 KiB
Java
|
package servlet;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
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 bean.Contatto;
|
||
|
import service.ContattoService;
|
||
|
import service.OrdiniService;
|
||
|
|
||
|
/**
|
||
|
* Servlet implementation class ContattiServlet
|
||
|
*/
|
||
|
@WebServlet("/contatti-ctrl")
|
||
|
public class ContattiServlet extends HttpServlet {
|
||
|
private static final long serialVersionUID = 1L;
|
||
|
|
||
|
/**
|
||
|
* @see HttpServlet#HttpServlet()
|
||
|
*/
|
||
|
public ContattiServlet() {
|
||
|
super();
|
||
|
// TODO Auto-generated constructor stub
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
|
||
|
* response)
|
||
|
*/
|
||
|
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||
|
throws ServletException, IOException {
|
||
|
// TODO Auto-generated method stub
|
||
|
response.getWriter().append("Served at: ").append(request.getContextPath());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
|
||
|
* response)
|
||
|
*/
|
||
|
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||
|
throws ServletException, IOException {
|
||
|
String azione = request.getParameter("azione");
|
||
|
|
||
|
if (azione == null) {
|
||
|
response.sendRedirect("error.jsp");
|
||
|
}
|
||
|
|
||
|
// Cerca
|
||
|
if (azione.equals("cerca")) {
|
||
|
String telefono = request.getParameter("telefono");
|
||
|
|
||
|
if (telefono != null) {
|
||
|
long t = Long.parseLong(telefono);
|
||
|
|
||
|
Contatto c = ContattoService.cercaPerNumero(t);
|
||
|
|
||
|
if (c != null) {
|
||
|
request.setAttribute("contatto", c);
|
||
|
request.getRequestDispatcher("home-contatto.jsp").forward(request, response);
|
||
|
} else {
|
||
|
request.setAttribute("messaggio", "Contatto non trovato");
|
||
|
request.getRequestDispatcher("cerca-aggiungi.jsp").forward(request, response);
|
||
|
|
||
|
}
|
||
|
}
|
||
|
} else if (azione.equals("aggiungi")) {
|
||
|
String nome = request.getParameter("nome");
|
||
|
String cognome = request.getParameter("cognome");
|
||
|
String servizioSociale = request.getParameter("servizioSociale");
|
||
|
String area = request.getParameter("area");
|
||
|
String indirizzo = request.getParameter("indirizzo");
|
||
|
long telefono = Long.parseLong(request.getParameter("telefono"));
|
||
|
String email = request.getParameter("email");
|
||
|
int neonat = Integer.parseInt(request.getParameter("neonat"));
|
||
|
int bambin = Integer.parseInt(request.getParameter("bambin"));
|
||
|
int adult = Integer.parseInt(request.getParameter("adult"));
|
||
|
int anzian = Integer.parseInt(request.getParameter("anzian"));
|
||
|
int componenti = neonat + bambin + adult + anzian;
|
||
|
String composizione = (neonat != 0 ? neonat + " neonat* " : "") + (bambin != 0 ? bambin + " bambin* " : "")
|
||
|
+ (adult != 0 ? adult + " adult* " : "") + (anzian != 0 ? anzian + " anzian* " : "");
|
||
|
|
||
|
String noteSacchetto = request.getParameter("noteSacchetto");
|
||
|
|
||
|
boolean ok = ContattoService.aggiungiContatto(nome, cognome, servizioSociale, area, indirizzo, telefono,
|
||
|
email, componenti, composizione, noteSacchetto);
|
||
|
|
||
|
if (ok) {
|
||
|
Contatto contatto = ContattoService.cercaPerNumero(telefono);
|
||
|
|
||
|
if (contatto != null) {
|
||
|
System.out.println("inserito contatto - link alla home da servlet");
|
||
|
response.sendRedirect("home-contatto.jsp?id=" + telefono);
|
||
|
} else {
|
||
|
request.setAttribute("messaggio", "Impossibile aggiungere contatto");
|
||
|
request.getRequestDispatcher("cerca-aggiungi.jsp").forward(request, response);
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
request.setAttribute("messaggio", "Impossibile aggiungere contatto");
|
||
|
request.getRequestDispatcher("cerca-aggiungi.jsp").forward(request, response);
|
||
|
}
|
||
|
} else if (azione.equals("modifica")) {
|
||
|
String nome = request.getParameter("nome");
|
||
|
String cognome = request.getParameter("cognome");
|
||
|
String servizioSociale = request.getParameter("servizioSociale");
|
||
|
String area = request.getParameter("area");
|
||
|
String indirizzo = request.getParameter("indirizzo");
|
||
|
long telefono = Long.parseLong(request.getParameter("telefono"));
|
||
|
String email = request.getParameter("email");
|
||
|
int componenti = Integer.parseInt(request.getParameter("numero"));
|
||
|
String composizione = request.getParameter("composizioneNucleo");
|
||
|
String noteSacchetto = request.getParameter("noteSacchetto");
|
||
|
|
||
|
boolean ok = ContattoService.aggiorna(nome, cognome, servizioSociale, area, indirizzo, telefono, email,
|
||
|
componenti, composizione, noteSacchetto);
|
||
|
|
||
|
if (ok) {
|
||
|
Contatto contatto = ContattoService.cercaPerNumero(telefono);
|
||
|
System.out.println("Servlet");
|
||
|
|
||
|
if (contatto != null) {
|
||
|
response.sendRedirect("home-contatto.jsp?id=" + telefono);
|
||
|
} else {
|
||
|
request.setAttribute("messaggio", "Impossibile modificare contatto");
|
||
|
request.getRequestDispatcher("cerca-aggiungi.jsp").forward(request, response);
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
request.setAttribute("messaggio", "Impossibile modificare contatto");
|
||
|
request.getRequestDispatcher("cerca-aggiungi.jsp").forward(request, response);
|
||
|
}
|
||
|
} else if (azione.equals("elimina")) {
|
||
|
long telefono = Long.parseLong(request.getParameter("id"));
|
||
|
boolean ok1 = OrdiniService.rimuovi(telefono);
|
||
|
boolean ok = ContattoService.elimina(telefono);
|
||
|
|
||
|
if (ok && ok1) {
|
||
|
response.sendRedirect("elenco-contatti.jsp");
|
||
|
} else {
|
||
|
response.sendRedirect("error.jsp");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Inserisci
|
||
|
}
|
||
|
|
||
|
}
|