/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package servlets;
import com.oreilly.servlet.MultipartRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import models.Feedback;
import models.User;
/**
*
* @author mattia
*/
public class AdminFeedbackController extends HttpServlet {
// <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 {
HttpSession session = request.getSession();
String page = "feedback";
response.setContentType("text/html;charset=UTF-8");
String servletPath = request.getServletPath();
if(servletPath.equals("/admin/feedbacks")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
List<Feedback> feedbacks = Feedback.getAll();
request.setAttribute("feedbacks", feedbacks);
}
}
else {
if(servletPath.equals("/admin/feedbacks/search")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
String mode = request.getParameter("searchMode");
List<Feedback> feedbacks;
if(mode.equals("fromid")) {
feedbacks = Feedback.getByFromId(new Integer(request.getParameter("searchFrom")));
request.setAttribute("feedbacks", feedbacks);
}
else if(mode.equals("toid")) {
feedbacks = Feedback.getByToId(new Integer(request.getParameter("searchTo")));
request.setAttribute("feedbacks", feedbacks);
}
}
}
else if(servletPath.equals("/admin/feedbacks/new")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
page += "_new";
List<User> users = User.getAll();
request.setAttribute("users",users);
}
}
else if(servletPath.equals("/admin/feedbacks/edit")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
page += "_edit";
String[] tokens = request.getRequestURI().split("/");
Feedback feedback = Feedback.getById(new Integer(tokens[tokens.length - 1]));
List<User> users = User.getAll();
request.setAttribute("feedback", feedback);
request.setAttribute("users", users);
}
}
else if(servletPath.equals("/admin/feedbacks/delete")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
String[] tokens = request.getRequestURI().split("/");
Feedback.delete(new Integer(tokens[tokens.length - 1]));
List<Feedback> feedbacks = Feedback.getAll();
request.setAttribute("feedbacks", feedbacks);
}
}
}
getServletContext().getRequestDispatcher("/templates/admin/header.jsp").include(request, response);
getServletContext().getRequestDispatcher("/templates/admin/" + page + ".jsp").include(request, response);
getServletContext().getRequestDispatcher("/templates/admin/right.jsp").include(request, response);
getServletContext().getRequestDispatcher("/templates/admin/footer.jsp").include(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 {
HttpSession session = request.getSession();
String servletPath = request.getServletPath();
String comment = new String();
if(servletPath.equals("/admin/feedbacks/save")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
Feedback feedback = new Feedback();
MultipartRequest multi = new MultipartRequest(request, "/home/buzz/NetBeansProjects/bweb/web/images/");
feedback.setFromId(new Integer(multi.getParameter("newFromUser")));
feedback.setToId(new Integer(multi.getParameter("newToUser")));
String mode = multi.getParameter("commMode");
if(mode.equals("text")) {
feedback.setText(multi.getParameter("newTextComm"));
}
else if(mode.equals("file")) {
File f = multi.getFile("newFileComm");
if( f != null ) {
InputStream in = new FileInputStream(f);
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
comment += line;
}
} catch (IOException e) {
} finally {
if (in != null) in.close();
}
}
feedback.setText(comment);
}
feedback.setScore(new Integer(multi.getParameter("newScore")));
feedback.save();
}
}
else if(servletPath.equals("/admin/feedbacks/update")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
Feedback feedback = new Feedback();
MultipartRequest multi = new MultipartRequest(request, "/home/buzz/NetBeansProjects/bweb/web/images/");
feedback.setId(new Integer(multi.getParameter("editId")));
feedback.setFromId(new Integer(multi.getParameter("editFromUser")));
feedback.setToId(new Integer(multi.getParameter("editToUser")));
String mode = multi.getParameter("commMode");
if(mode.equals("text")) {
feedback.setText(multi.getParameter("editTextComm"));
}
else if(mode.equals("file")) {
File f = multi.getFile("editFileComm");
if( f != null ) {
InputStream in = new FileInputStream(f);
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
comment += line;
}
} catch (IOException e) {
} finally {
if (in != null) in.close();
}
}
feedback.setText(comment);
}
feedback.setScore(new Integer(multi.getParameter("editScore")));
feedback.update();
}
}
response.sendRedirect(request.getContextPath() + "/admin/feedbacks");
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}