Package servlets

Source Code of servlets.AuctionController

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package servlets;

import com.oreilly.servlet.MultipartRequest;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
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 AuctionController 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 {

        String page = "auction";

        response.setContentType("text/html;charset=UTF-8");

        String servletPath = request.getServletPath();

        if(servletPath.equals("/auctions")){

            List<Auction> auctions = new ArrayList<Auction>();

            auctions = Auction.getAll(false);
            request.setAttribute("auctions", auctions);
        }
       
         else {

             if(servletPath.equals("/auctions/show")) {

            page += "_show";

            String[] tokens = request.getRequestURI().split("/");
            Auction auction = Auction.getById(new Integer(tokens[tokens.length - 1]));

    request.setAttribute("auction", auction);
            
         }
            if(servletPath.equals("/auctions/new")) {

            page += "_new";

            List<Category> categories = Category.getAll();
            List<User> users = User.getAll();
               request.setAttribute("categories", categories);
               request.setAttribute("users", users);
            
         }

    if(servletPath.equals("/auctions/search")) {
    

      String p = request.getParameter("productname");
      if(p == null) p = "";

      Float mp = new Float(0.0);
      if(request.getParameter("minprice") != "")
        mp = new Float(request.getParameter("minprice"));

      Float map = Float.MAX_VALUE;
      if(request.getParameter("maxprice") != "")
        map = new Float(request.getParameter("maxprice"));

      Date de = new Date(2100 - 1900, 0, 0);
      if(request.getParameter("expire") != "") {
        String[] d = request.getParameter("expire").split("/");
        de = new Date(new Integer(d[2]) - 1900, new Integer(d[1]) - 1, new Integer(d[0]));
      }

      List<Auction> auctions = Auction.getBy(p, mp, map, de);
      request.setAttribute("auctions", auctions);
    }

         }
        getServletContext().getRequestDispatcher("/templates/header.jsp").include(request, response);
  getServletContext().getRequestDispatcher("/templates/right.jsp").include(request, response);
        getServletContext().getRequestDispatcher("/templates/" + page + ".jsp").include(request, response);
  getServletContext().getRequestDispatcher("/templates/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 {

        String servletPath = request.getServletPath();
        HttpSession session = request.getSession();
        if(servletPath.equals("/auctions/bid")) {

            User u = (User) session.getAttribute("user");
            String bid = request.getParameter("bid");
            Integer id = new Integer(request.getParameter("id"));
            Auction a = Auction.getById(id);
            a.setLastPrice(new Float(bid));
            a.setLastUserId(u.getId());
            a.update();

            response.sendRedirect(request.getContextPath());
       
        }
        else if(servletPath.equals("/auctions/save"))
        {
     
                            User u = (User) session.getAttribute("user");
                            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(u.getId());
                                auction.setLastPrice(null);
                                auction.setLastUserId(null);
                                auction.setDescription(multi.getParameter("newDesc"));

                                auction.save();
                                response.sendRedirect(request.getContextPath());
          }
}

}
TOP

Related Classes of servlets.AuctionController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.