package controller;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import query_facade.QueryManager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
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 javax.servlet.http.HttpSession;
import messaggi.MsgIncrep;
import messaggi.MsgTenlines;
import query_facade.QueryManager;
import messaggi.utente;
import static java.lang.System.out;
import java.util.Scanner;
import javax.servlet.ServletContext;
import javax.servlet.http.Part;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import query_facade.SettaggioQueryGetMessaggi;
//import messaggi.ConnettiDatabase;
public class ControllerServlet extends HttpServlet {
private static final int VISUALIZZA = 1;
private static final int CREA_NUOVO = 2;
private static final int RISPONDI = 3;
private static final int INOLTRA = 4;
private final QueryManager queryManager = QueryManager.getQueryManager();
utente u = new utente();
String tabella="";
String queryMessaggiRicevuti="";
/**
*
* @throws ServletException
*/
@Override
public void init() throws ServletException {
}
public void processRequest(
HttpServletRequest request,
HttpServletResponse response)
throws IOException
{
PrintWriter out = response.getWriter();
ServletContext context = this.getServletContext();
HttpSession session = request.getSession(true);
Integer counter = (Integer)context.getAttribute("counter");
out.print("<html>");
out.print("<body>");
out.print("<h2>There are " + counter + " users</h2>");
out.print("</body>");
out.print("</html>");
}
/**
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override //metodo get non viene utilizzato
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("VISUALIZZA", 1);
request.setAttribute("CREA_NUOVO", 2);
request.setAttribute("RISPONDI", 3);
request.setAttribute("INOLTRA", 4);
request.setAttribute("tabella",tabella);
String userPath = request.getServletPath();
String url = "";
//voglio inviare un nuovo messaggio increp
if (userPath.equals("/messaggioIncrep")) {
HttpSession session = request.getSession();
userPath = "inviomsgincrep";
int status = Integer.valueOf(request.getParameter("status"));
switch (status) {
case VISUALIZZA:
case RISPONDI: {
int idMessaggio = Integer.valueOf(request.getParameter("idMessaggio"));
MsgIncrep msg = queryManager.getSingoloMsgIncrepRicevuto(idMessaggio,tabella,u);
request.setAttribute("status", VISUALIZZA);
request.setAttribute("msg", msg);
break;
}
case INOLTRA: {
int idMessaggio = Integer.valueOf(request.getParameter("idMessaggio"));
MsgIncrep msg = queryManager.getSingoloMsgIncrepRicevuto(idMessaggio,tabella,u);
request.setAttribute("status", INOLTRA);
request.setAttribute("msg", msg);
break;
}
case CREA_NUOVO: {
request.setAttribute("status", CREA_NUOVO);
break;
}
default: {
throw new RuntimeException();
}
}
url = "/WEB-INF/view/" + userPath + ".jsp";
}
try {
request.getRequestDispatcher(url).forward(request, response);
} catch (ServletException | IOException ex) {
ex.printStackTrace();
}
}
/**
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override // metodo post
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("VISUALIZZA", 1);
request.setAttribute("CREA_NUOVO", 2);
request.setAttribute("RISPONDI", 3);
request.setAttribute("INOLTRA", 4);
request.setAttribute("tabella",tabella);
String userPath = request.getServletPath();
String url = "";
if (userPath.equals("/inviaIncrep")) {
HttpSession session = request.getSession();
MsgIncrep msg = new MsgIncrep();
System.out.println(request.getParameter("misureintraprese"));
msg.setOggetto(request.getParameter("oggetto"));
msg.setGdotime(request.getParameter("gdotime"));
msg.setPosizioneutm(request.getParameter("posizioneutm"));
msg.setDescrizioneevento(request.getParameter("descrizioneevento"));
msg.setValutazione(request.getParameter("valutazione"));
msg.setMisureintraprese(request.getParameter("misureintraprese"));
System.out.println(request.getParameter("misureintraprese"));
System.out.println(request.getParameter("oggetto"));
System.out.println(msg.getMisureintraprese());
System.out.println(msg.getOggetto());
System.out.println("NON HO PRESO I DATI DAI CAMPI TEXT DI MSGINCREP.JSP");
boolean inserito = queryManager.inserisciMsgIncrep(msg);
if(!inserito){
System.out.println("RECORD NON INSERITO");
System.out.println(inserito);
}
else {
System.out.println("RECORD INSERITO con successo!!!");
}
/*
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
// get access to file that is uploaded from client
Part p1 = request.getPart("oggetto");
InputStream is = p1.getInputStream();
// read filename which is sent as a part
Part p2 = request.getPart("nomeFile");
Scanner s = new Scanner(p2.getInputStream());
String filename = s.nextLine(); // read filename from stream
// get filename to use on the server
String outputfile = this.getServletContext().getRealPath(filename); // get path on the server
FileOutputStream os = new FileOutputStream(outputfile);
// write bytes taken from uploaded file to target file
int ch = is.read();
while (ch != -1) {
os.write(ch);
ch = is.read();
}
os.close();
out.println("<h3>File uploaded successfully!</h3>");
} catch (Exception ex) {
out.println("Exception -->" + ex.getMessage());
} finally {
out.close();
}
*/
} // end of doPost()
try {
request.getRequestDispatcher(url).forward(request, response);
} catch (ServletException | IOException ex) {
ex.printStackTrace();
}
}
}