Package com.sertaogames.mundoj.servlet

Source Code of com.sertaogames.mundoj.servlet.CadastrarCompraServlet

package com.sertaogames.mundoj.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sertaogames.mundoj.model.ItemCompra;
import com.sertaogames.mundoj.model.Produto;
import com.sertaogames.mundoj.model.Compra;

public class CadastrarCompraServlet extends BasicServlet{

  private static final long serialVersionUID = 1L;

  @Override
  protected void execute(HttpServletRequest req, HttpServletResponse resp) {

    Compra compra = new Compra();

    Gson gson = new Gson();
    List<Integer> ids =  gson.fromJson(req.getParameter("produtos"), new TypeToken<List<Integer>>(){}.getType());

    for (Integer id : ids) {
      Produto p = jdo.findById(Produto.class, id);

      compra.setTotal(compra.getTotal().add(p.getPreco()));

      ItemCompra itemCompra = new ItemCompra();
      itemCompra.setProduto(p);
      compra.getItens().add(itemCompra);
    }

    try {
     
      compra.saveJSON();
      jdo.save(compra);
     
      req.setAttribute("compra", compra);
      getServletContext().getRequestDispatcher("/detalhe-compra.jsp").forward(req, resp);
    } catch (Exception e) {
      e.printStackTrace();
    }


  }

}
TOP

Related Classes of com.sertaogames.mundoj.servlet.CadastrarCompraServlet

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.