/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Java;
import Java.ORM.Account;
import Java.ORM.DatabaseORM;
import static Java.ORM.DatabaseORM.entityManagerFactoryString;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Mitch
*/
@WebServlet(name = "ServletAddProduct", urlPatterns = {"/ServletAddProduct"})
public class ServletAddProduct extends HttpServlet {
private final int daysBeforeShoppingcartExpires = 365;
private EntityManager em = Persistence.createEntityManagerFactory(entityManagerFactoryString).createEntityManager();
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String photo = (String)request.getSession().getAttribute("photo");
RequestDispatcher rd = request.getRequestDispatcher("/addproduct.jsp?photo=" + photo);
String curUser = (String)request.getSession().getAttribute("username");
if (curUser != null) {
PrintWriter out = response.getWriter();
String type = (String)request.getParameter("type");
String product = (String)request.getParameter("product");
Integer amount = readInt(request, "amount");
if (amount == null) {
rd.forward(request, response);
return;
}
Integer x = readInt(request, "x");
if (x == null) {
rd.forward(request, response);
return;
}
Integer y = readInt(request, "y");
if (y == null) {
rd.forward(request, response);
return;
}
Integer width = readInt(request, "width");
if(width == null) {
rd.forward(request, response);
return;
}
Integer height = readInt(request, "height");
if (height == null) {
rd.forward(request, response);
return;
}
Shoppingcart cart = DatabaseORM.getShoppingcart(em, curUser);
if (cart == null) {
Account account = DatabaseORM.findAccount(em, curUser);
cart = new Shoppingcart(account);
}
try {
if(photo != null && product != null && type != null) {
int photonr = Integer.valueOf(photo);
if("sepia".equalsIgnoreCase(type)) {
type = "sepia";
}else if("black-white".equalsIgnoreCase(type) || "zwart-wit".equalsIgnoreCase(type)) {
type = "zwart-wit";
}else{
type = "kleur";
}
cart.add(photonr, product, type, amount, x, y, width, height);
}else{
setMessage(request, "Er is een interne fout opgetreden.", "red");
rd.forward(request, response);
return;
}
} catch (IllegalArgumentException iae) {
setMessage(request, "Er is een interne fout opgetreden.", "red");
rd.forward(request, response);
return;
} catch (DoublePhotoException ex) {
setMessage(request, ex.getMessage(), "red");
rd.forward(request, response);
return;
}
DatabaseORM.insertShoppingcart(em, cart);
setMessage(request, "Het product is succesvol toegevoegd aan uw winkelwagen.", "green");
rd.forward(request, response);
} else {
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
private Integer readInt(HttpServletRequest request, String paramName) {
String integer = request.getParameter(paramName);
Integer x = null;
if (integer != null)
x = Integer.valueOf(integer);
return x;
}
private void setMessage(HttpServletRequest request, String message, String color) {
request.setAttribute("addProductMessage", message);
request.setAttribute("color", color);
}
// <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 {
processRequest(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 {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}