/*
* Auction Servlet
*
*/
package servlets;
import com.oreilly.servlet.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
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.Auction;
import models.Category;
import models.User;
/**
*
* @author mattia
*/
public class AdminAuctionController 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 = "auction";
response.setContentType("text/html;charset=UTF-8");
String servletPath = request.getServletPath();
PrintWriter p = response.getWriter();
if(servletPath.equals("/admin/auctions")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
List<Auction> auctions = Auction.getAll(false);
request.setAttribute("auctions", auctions);
}
}
else {
if(servletPath.equals("/admin/auctions/search")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
String mode = request.getParameter("searchMode");
List<Auction> auctions;
if(mode.equals("name")) {
auctions = Auction.getByProductName(request.getParameter("searchName"));
request.setAttribute("auctions", auctions);
}
else if(mode.equals("id")) {
auctions = Auction.getByUserId(new Integer(request.getParameter("searchId")));
request.setAttribute("auctions", auctions);
}
else if(mode.equals("price")) {
Float min = new Float(request.getParameter("pSearchMin"));
Float max = new Float(request.getParameter("pSearchMax"));
auctions = Auction.getByPrice(min, max);
request.setAttribute("auctions", auctions);
}
else if(mode.equals("date")) {
java.sql.Date max, min;
if(!request.getParameter("dSearchMin").isEmpty() && !request.getParameter("dSearchMax").isEmpty()) {
String minDate[] = request.getParameter("dSearchMin").split("/");
String maxDate[] = request.getParameter("dSearchMax").split("/");
min = new java.sql.Date(new Integer(minDate[2]) - 1900,new Integer(minDate[1]) - 1,new Integer(minDate[0]));
max = new java.sql.Date(new Integer(maxDate[2]) - 1900,new Integer(maxDate[1]) - 1,new Integer(maxDate[0]));
}
else {
min = new java.sql.Date(0, 0, 0);
max = new java.sql.Date(8099, 11, 31);
}
auctions = Auction.getByDate(min, max);
request.setAttribute("auctions", auctions);
}
}
}
if(servletPath.equals("/admin/auctions/new")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
page += "_new";
List<Category> categories = Category.getAll();
List<User> users = User.getAll();
request.setAttribute("categories", categories);
request.setAttribute("users", users);
}
}
else if(servletPath.equals("/admin/auctions/edit")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
page += "_edit";
String[] tokens = request.getRequestURI().split("/");
Auction auction = Auction.getById(new Integer(tokens[tokens.length - 1]));
request.setAttribute("auction", auction);
}
}
else if(servletPath.equals("/admin/auctions/delete")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
String[] tokens = request.getRequestURI().split("/");
Auction.delete(new Integer(tokens[tokens.length - 1]));
List<Auction> auctions = Auction.getAll(false);
request.setAttribute("auctions", auctions);
}
}
}
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();
PrintWriter p = response.getWriter();
if(servletPath.equals("/admin/auctions/save")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
Auction auction = new Auction();
MultipartRequest multi = new MultipartRequest(request, "/home/buzz/NetBeansProjects/bweb/web/images/");
auction.setProduct(multi.getParameter("newProduct"));
auction.setCatId(new Integer(multi.getParameter("newCat")));
File f = multi.getFile("newImage");
String fileName = multi.getFilesystemName("newImage");
auction.setPhoto(fileName);
auction.setStartingPrice(new Float (multi.getParameter("newStartingPrice")));
auction.setLastPrice(new Float (multi.getParameter("newStartingPrice")));
auction.setMinPrice( new Float (multi.getParameter("newMinPrice")));
Integer duration,i = new Integer(multi.getParameter("newDuration"));
if(i == 1) duration = 4;
else if(i == 2) duration = 6;
else duration = 10;
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, duration );
Long e = (cal.getTimeInMillis());
java.sql.Date expire = new java.sql.Date(e);
auction.setExpire(expire);
auction.setShipping(multi.getParameter("newShip"));
auction.setUserId(new Integer(multi.getParameter("newCat")));
auction.setLastPrice(null);
auction.setLastUserId(null);
auction.setDescription(multi.getParameter("newDesc"));
auction.save();
}
}
else if(servletPath.equals("/admin/auctions/update")) {
if(session.getAttribute("admin") != null && session.getAttribute("admin").equals("true")) {
Auction auction = new Auction();
MultipartRequest multi = new MultipartRequest(request, "/home/buzz/NetBeansProjects/bweb/web/images/");
auction.setId(new Integer(multi.getParameter("editId")));
auction.setProduct(multi.getParameter("editProduct"));
auction.setCatId(new Integer(multi.getParameter("editCat")));
String fileName = multi.getFilesystemName("editImage");
File f = null;
if(fileName == null || fileName.equals("")) {
fileName = multi.getParameter("oldImage");
}
else {
f = multi.getFile("editImage");
}
auction.setPhoto(fileName);
//auction.setStartingPrice(new Float (multi.getParameter("editStartingPrice")));
//auction.setMinPrice( new Float (multi.getParameter("editMinPrice")));
auction.setShipping(multi.getParameter("editShip"));
auction.setUserId(new Integer(multi.getParameter("editCat")));
auction.setDescription(multi.getParameter("editDesc"));
auction.update();
}
}
response.sendRedirect(request.getContextPath() + "/admin/auctions");
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}